Skip to content

Commit

Permalink
feat: add arg CROSS_ARCH for cross-compilation (eclipse-zenoh#147)
Browse files Browse the repository at this point in the history
Signed-off-by: homalozoa <[email protected]>
  • Loading branch information
homalozoa authored Sep 1, 2023
1 parent 7b066c6 commit a95a9c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
17 changes: 12 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ find_package(ament_cmake REQUIRED)
# find_package(<dependency> 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
Expand All @@ -35,16 +42,16 @@ 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}
)

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)
Expand All @@ -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()
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<target>
```
where `<target>` 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
Expand Down

0 comments on commit a95a9c9

Please sign in to comment.