From a95a9c97813b3a3a68b73a030c2f0101e32cb762 Mon Sep 17 00:00:00 2001 From: Homalozoa X <21300069+homalozoa@users.noreply.github.com> Date: Fri, 1 Sep 2023 20:53:57 +0800 Subject: [PATCH] feat: add arg `CROSS_ARCH` for cross-compilation (#147) Signed-off-by: homalozoa --- CMakeLists.txt | 17 ++++++++++++----- README.md | 9 +++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f7a14002..c52c2276 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,13 @@ find_package(ament_cmake REQUIRED) # find_package( REQUIRED) set(RUST_TARGET_DIR ${CMAKE_BINARY_DIR}/target) +if(DEFINED CROSS_ARCH) + set(RUST_INSTALL_DIR ${RUST_TARGET_DIR}/${CROSS_ARCH}) + set(BUILD_CMD cargo zigbuild --release --manifest-path "${CMAKE_SOURCE_DIR}/zenoh-bridge-dds/Cargo.toml" --target-dir "${RUST_TARGET_DIR}" --target ${CROSS_ARCH}) +else() + set(RUST_INSTALL_DIR ${RUST_TARGET_DIR}) + set(BUILD_CMD cargo build --release --manifest-path "${CMAKE_SOURCE_DIR}/zenoh-bridge-dds/Cargo.toml" --target-dir "${RUST_TARGET_DIR}") +endif() FILE(GLOB RUST_SRC_FILES ${CMAKE_SOURCE_DIR}/zenoh-bridge-dds/src/*.rs @@ -35,8 +42,8 @@ FILE(GLOB RUST_SRC_FILES add_custom_command( OUTPUT - ${RUST_TARGET_DIR}/release/zenoh-bridge-dds - COMMAND cargo build --release --manifest-path "${CMAKE_SOURCE_DIR}/zenoh-bridge-dds/Cargo.toml" --target-dir "${RUST_TARGET_DIR}" + ${RUST_INSTALL_DIR}/release/zenoh-bridge-dds + COMMAND ${BUILD_CMD} DEPENDS ${RUST_SRC_FILES} ) @@ -44,7 +51,7 @@ add_custom_command( add_custom_target( build_crate ALL DEPENDS - ${RUST_TARGET_DIR}/release/zenoh-bridge-dds + ${RUST_INSTALL_DIR}/release/zenoh-bridge-dds ) macro(INSTALL_ZENOH src_path dst_path) @@ -56,8 +63,8 @@ macro(INSTALL_ZENOH src_path dst_path) ) endmacro(INSTALL_ZENOH) -INSTALL_ZENOH(${RUST_TARGET_DIR}/release/zenoh-bridge-dds lib/${PROJECT_NAME}) +INSTALL_ZENOH(${RUST_INSTALL_DIR}/release/zenoh-bridge-dds lib/${PROJECT_NAME}) -INSTALL_ZENOH(${RUST_TARGET_DIR}/release/zenoh-bridge-dds bin) +INSTALL_ZENOH(${RUST_INSTALL_DIR}/release/zenoh-bridge-dds bin) ament_package() diff --git a/README.md b/README.md index e2a5a112..79b4f2bc 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,15 @@ colcon build --packages-select zenoh_bridge_dds --cmake-args -DCMAKE_BUILD_TYPE= ``` The `rosdep` command will automatically install *Rust* and *clang* as build dependencies. +If you want to cross-compile the package on x86 device for any target, you can use the following command: +```bash +rosdep install --from-paths . --ignore-src -r -y +colcon build --packages-select zenoh_bridge_dds --cmake-args -DCMAKE_BUILD_TYPE=Release --cmake-args -DCROSS_ARCH= +``` +where `` is the target architecture (e.g. `aarch64-unknown-linux-gnu`). The architechture list can be found [here](https://doc.rust-lang.org/nightly/rustc/platform-support.html). + +The cross-compilation uses `zig` as a linker. You can install it with instructions in [here](https://ziglang.org/download/). Also, the `zigbuild` package is required to be installed on the target device. You can install it with instructions in [here](https://github.com/rust-cross/cargo-zigbuild#installation). + ## Docker image The **`zenoh-bridge-dds`** standalone executable is also available as a [Docker images](https://hub.docker.com/r/eclipse/zenoh-bridge-dds/tags?page=1&ordering=last_updated) for both amd64 and arm64. To get it, do: - `docker pull eclipse/zenoh-bridge-dds:latest` for the latest release