Skip to content

Commit

Permalink
iox-#73 Support cross compile for aarch64
Browse files Browse the repository at this point in the history
  • Loading branch information
tony-slamtec committed Dec 19, 2023
1 parent fdd6495 commit e02a359
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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` library, which has a dependency `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.
Expand Down
43 changes: 33 additions & 10 deletions iceoryx-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
archive_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", archive_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);
Expand All @@ -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(format!("-DCMAKE_BUILD_TYPE=Release"));
cmake_args.push(format!("-DBUILD_SHARED_LIBS=OFF"));
cmake_args.push(format!("-DROUDI_ENVIRONMENT=ON"));
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()
{
Expand All @@ -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(format!("--build"));
cmake_args.push(format!("."));
cmake_args.push(format!("--target"));
cmake_args.push(format!("install"));

if !Command::new("cmake")
.current_dir(&component_build_dir)
.args(["--build", ".", "--target", "install"])
.args(&cmake_args)
.status()?
.success()
{
Expand Down Expand Up @@ -110,6 +132,7 @@ fn main() -> std::io::Result<()> {
extract_archive(&iceoryx_archive_dir, &iceoryx_source_dir, ICEORYX_VERSION)?;

make_and_install(
&iceoryx_archive_dir,
&iceoryx_source_dir,
&iceoryx_build_dir,
&iceoryx_install_dir,
Expand Down
Original file line number Diff line number Diff line change
@@ -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++)

0 comments on commit e02a359

Please sign in to comment.