Skip to content

Commit

Permalink
Use GNU binutils on MacOS (#116)
Browse files Browse the repository at this point in the history
* GNU ar for MacOS

* Fix ranlib for library packaging

* add README

* Update library_builder.py
  • Loading branch information
jkaflik committed Sep 4, 2023
1 parent fdeb1ea commit 841dc13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
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
19 changes: 16 additions & 3 deletions microros_utils/library_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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 +183,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 +195,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 +222,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 ""

0 comments on commit 841dc13

Please sign in to comment.