Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GNU binutils on MacOS #116

Merged
merged 5 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ The community is encouraged to open pull request with custom use cases.
```bash
apt install -y git cmake python3-pip
```

### Platform specific requirements

#### MacOS

XCode command line tools are distributed with toolchain that is not fully compatible with micro-ROS build process.
To fix this, install GNU [binutils](https://www.gnu.org/software/binutils/) using [Homebrew](https://brew.sh/):

```bash
brew install binutils
```

## How to add to your project

Expand Down
20 changes: 16 additions & 4 deletions microros_utils/library_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from .utils import run_cmd
from .repositories import Repository, Sources

pablogs9 marked this conversation as resolved.
Show resolved Hide resolved
class CMakeToolchain:
def __init__(self, path, cc, cxx, ar, cflags, cxxflags):
cmake_toolchain = """include(CMakeForceCompiler)
Expand Down Expand Up @@ -55,7 +54,7 @@ def __init__(self, library_folder, packages_folder, distro, python_env):
self.env = {}

def run(self, meta, toolchain, user_meta = ""):
if os.path.exists(self.library_path):
if os.path.exists(self.library):
print("micro-ROS already built")
return

Expand Down Expand Up @@ -183,6 +182,7 @@ def build_mcu_environment(self, meta_file, toolchain_file, user_meta = ""):
sys.exit(1)

def package_mcu_library(self):
binutils_path = self.resolve_binutils_path()
aux_folder = self.build_folder + "/aux"

shutil.rmtree(aux_folder, ignore_errors=True)
Expand All @@ -194,12 +194,12 @@ def package_mcu_library(self):
if f.endswith('.a'):
os.makedirs(aux_folder + "/naming", exist_ok=True)
os.chdir(aux_folder + "/naming")
os.system("ar x {}".format(root + "/" + f))
os.system("{}ar x {}".format(binutils_path, root + "/" + f))
for obj in [x for x in os.listdir() if x.endswith('obj')]:
os.rename(obj, '../' + f.split('.')[0] + "__" + obj)

os.chdir(aux_folder)
command = "ar rc libmicroros.a $(ls *.o *.obj 2> /dev/null); rm *.o *.obj 2> /dev/null; ranlib libmicroros.a"
command = "{binutils}ar rc libmicroros.a $(ls *.o *.obj 2> /dev/null); rm *.o *.obj 2> /dev/null; {binutils}ranlib libmicroros.a".format(binutils=binutils_path)
result = run_cmd(command)

if 0 != result.returncode:
Expand All @@ -221,3 +221,15 @@ def package_mcu_library(self):
if os.path.exists(repeated_path):
shutil.copytree(repeated_path, folder_path, copy_function=shutil.move, dirs_exist_ok=True)
shutil.rmtree(repeated_path)

def resolve_binutils_path(self):
if sys.platform == "darwin":
homebrew_binutils_path = "/opt/homebrew/opt/binutils/bin/"
if os.path.exists(homebrew_binutils_path):
return homebrew_binutils_path

print("ERROR: GNU binutils not found. ({}) Please install binutils with homebrew: brew install binutils"
.format(homebrew_binutils_path))
sys.exit(1)

return ""