From 841dc1396eb1d54d61b66781768359ed00d5de6c Mon Sep 17 00:00:00 2001 From: Kuba Kaflik Date: Mon, 4 Sep 2023 15:14:40 +0200 Subject: [PATCH] Use GNU binutils on MacOS (#116) * GNU ar for MacOS * Fix ranlib for library packaging * add README * Update library_builder.py --- README.md | 11 +++++++++++ microros_utils/library_builder.py | 19 ++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1c45809..fd2842e 100755 --- a/README.md +++ b/README.md @@ -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 diff --git a/microros_utils/library_builder.py b/microros_utils/library_builder.py index 3fed134..766d8d3 100644 --- a/microros_utils/library_builder.py +++ b/microros_utils/library_builder.py @@ -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 @@ -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) @@ -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: @@ -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 ""