From adbdc7546dfa89b3a415404ec751cd5876cc7fc4 Mon Sep 17 00:00:00 2001 From: Tony Huang Date: Tue, 5 Dec 2023 17:25:42 +0800 Subject: [PATCH] iox-#73 Support cross compile for aarch64 --- .cargo/config.toml | 3 ++ README.md | 20 +++++++- iceoryx-sys/build.rs | 51 ++++++++++++++----- .../toolchain.aarch64-unknown-linux-gnu.cmake | 4 ++ 4 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 iceoryx-sys/iceoryx-cpp/toolchain.aarch64-unknown-linux-gnu.cmake diff --git a/.cargo/config.toml b/.cargo/config.toml index f8ed142..e462a96 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,3 +1,6 @@ [env] RUST_TEST_THREADS = "1" # prevent running multiple RouDiEnvironments in parallel with `cargo test` NEXTEST_TEST_THREADS = "1" # prevent running multiple RouDiEnvironments in parallel with `cargo nextest run` + +[target.aarch64-unknown-linux-gnu] +linker = "aarch64-linux-gnu-gcc" diff --git a/README.md b/README.md index 789da0a..d7c6101 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ Safe Rust bindings for [Eclipse iceoryx](https://github.com/eclipse-iceoryx/iceo - [How to start RouDi](#how-to-start-roudi) - [Run the simple publisher and subscriber example](#run-the-simple-publisher-and-subscriber-example) 3. [How to write a simple application](#how-to-write-a-simple-application) -4. [Limitations](#limitations) +4. [Cross-compiling](#cross-compiling) +5. [Limitations](#limitations) ## About @@ -262,6 +263,23 @@ Please have a look at the [examples](https://github.com/eclipse-iceoryx/iceoryx- in the repository. It contains additional examples to show how uninitialized samples can be loaned and how the `wait_for_samples` method of the `SampleReceiver` can be used to get notified on new samples. +## Cross-Compiling + +`iceoryx-sys` uses `cmake` to build `iceoryx`, which has a dependency to `libacl` on Linux. As a result, +to link to a cross-compiled version of the native `iceoryx`, it's necessary to set several environment variables +to link against the prebuilt libacl. + +To tell the linker lookup libraries from your sysroot, please set the following environment variables, and invoke the build script: + +```bash +# let's take the target aarch64-unknown-linux-gnu for instance +SYSROOT=/path/to/your/cross/compile/sysroot +export LDFLAGS="--sysroot $SYSROOT" +export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUSTFLAGS="-C link-arg=--sysroot=$SYSROOT" + +cargo build --target aarch64-unknown-linux-gnu --all-targets +``` + ## Limitations Currently, only a subset of Eclipse iceoryx v2.0 is supported and some features are missing. diff --git a/iceoryx-sys/build.rs b/iceoryx-sys/build.rs index 9da9d9d..0a2f2a1 100644 --- a/iceoryx-sys/build.rs +++ b/iceoryx-sys/build.rs @@ -9,10 +9,25 @@ use std::process::Command; const ICEORYX_VERSION: &str = "v2.0.3"; -fn make_and_install(source_dir: &str, build_dir: &str, install_dir: &str) -> std::io::Result<()> { +fn make_and_install( + cpp_dir: &str, + source_dir: &str, + build_dir: &str, + install_dir: &str, +) -> std::io::Result<()> { let cmake_install_prefix = format!("-DCMAKE_INSTALL_PREFIX={}", install_dir); let cmake_prefix_path = format!("-DCMAKE_PREFIX_PATH={}", install_dir); + let target = env::var("TARGET").expect("Target"); + let host = env::var("HOST").expect("Host"); + + let extra_cmake_args = if target == host { + vec![] + } else { + let toolchain_cmake_file = format!("{}/toolchain.{}.cmake", cpp_dir, target); + vec![format!("-DCMAKE_TOOLCHAIN_FILE={}", toolchain_cmake_file)] + }; + for iceoryx_component in ["iceoryx_hoofs", "iceoryx_posh"] { let component_source_dir = format!("{}/{}", source_dir, iceoryx_component); let component_build_dir = format!("{}/{}", build_dir, iceoryx_component); @@ -28,16 +43,17 @@ fn make_and_install(source_dir: &str, build_dir: &str, install_dir: &str) -> std )); } + let mut cmake_args = extra_cmake_args.clone(); + cmake_args.push("-DCMAKE_BUILD_TYPE=Release".into()); + cmake_args.push("-DBUILD_SHARED_LIBS=OFF".into()); + cmake_args.push("-DROUDI_ENVIRONMENT=ON".into()); + cmake_args.push(cmake_prefix_path.clone()); + cmake_args.push(cmake_install_prefix.clone()); + cmake_args.push(component_source_dir.clone()); + if !Command::new("cmake") .current_dir(&component_build_dir) - .args([ - "-DCMAKE_BUILD_TYPE=Release", - "-DBUILD_SHARED_LIBS=OFF", - "-DROUDI_ENVIRONMENT=ON", - &cmake_prefix_path, - &cmake_install_prefix, - &component_source_dir, - ]) + .args(&cmake_args) .status()? .success() { @@ -47,9 +63,15 @@ fn make_and_install(source_dir: &str, build_dir: &str, install_dir: &str) -> std )); } + let mut cmake_args = Vec::new(); + cmake_args.push("--build"); + cmake_args.push("."); + cmake_args.push("--target"); + cmake_args.push("install"); + if !Command::new("cmake") .current_dir(&component_build_dir) - .args(["--build", ".", "--target", "install"]) + .args(&cmake_args) .status()? .success() { @@ -63,7 +85,7 @@ fn make_and_install(source_dir: &str, build_dir: &str, install_dir: &str) -> std Ok(()) } -fn extract_archive(archive_dir: &str, source_dir: &str, version: &str) -> std::io::Result<()> { +fn extract_archive(cpp_dir: &str, source_dir: &str, version: &str) -> std::io::Result<()> { if !Command::new("mkdir") .args(["-p", source_dir]) .status()? @@ -78,7 +100,7 @@ fn extract_archive(archive_dir: &str, source_dir: &str, version: &str) -> std::i if !Command::new("tar") .args([ "-xf", - &format!("{}/{}.tar.gz", archive_dir, version), + &format!("{}/{}.tar.gz", cpp_dir, version), "-C", source_dir, "--strip-components=1", @@ -102,14 +124,15 @@ fn main() -> std::io::Result<()> { let out_dir = env::var("OUT_DIR").expect("Target output directory"); let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Cargo manifest directory"); - let iceoryx_archive_dir = format!("{}/{}", manifest_dir, "iceoryx-cpp"); + let iceoryx_cpp_dir = format!("{}/{}", manifest_dir, "iceoryx-cpp"); let iceoryx_source_dir = format!("{}/{}/", out_dir, "iceoryx-cpp"); let iceoryx_build_dir = format!("{}/{}", out_dir, "iceoryx-build"); let iceoryx_install_dir = format!("{}/{}", out_dir, "iceoryx-install"); - extract_archive(&iceoryx_archive_dir, &iceoryx_source_dir, ICEORYX_VERSION)?; + extract_archive(&iceoryx_cpp_dir, &iceoryx_source_dir, ICEORYX_VERSION)?; make_and_install( + &iceoryx_cpp_dir, &iceoryx_source_dir, &iceoryx_build_dir, &iceoryx_install_dir, diff --git a/iceoryx-sys/iceoryx-cpp/toolchain.aarch64-unknown-linux-gnu.cmake b/iceoryx-sys/iceoryx-cpp/toolchain.aarch64-unknown-linux-gnu.cmake new file mode 100644 index 0000000..a96243c --- /dev/null +++ b/iceoryx-sys/iceoryx-cpp/toolchain.aarch64-unknown-linux-gnu.cmake @@ -0,0 +1,4 @@ +set(CMAKE_SYSTEM_NAME Linux) + +set(CMAKE_C_COMPILER aarch64-linux-gnu-gcc) +set(CMAKE_CXX_COMPILER aarch64-linux-gnu-g++)