Skip to content

Commit 5840c71

Browse files
committed
Convert to module. Allows external Rust apps
1 parent a261620 commit 5840c71

File tree

10 files changed

+68
-42
lines changed

10 files changed

+68
-42
lines changed

CMakeLists.txt

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
cmake_minimum_required(VERSION 3.13.1)
44

5-
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
6-
project(hello_world)
7-
8-
target_sources(app PRIVATE src/main.c rust_obj.obj)
9-
105
# Use a clang_target known to clang so headers will be processed correctly with
116
# bindgen. rust_target may point to a custom json target.
127
if(${ARCH} STREQUAL "posix" OR ${ARCH} STREQUAL "x86")
@@ -42,8 +37,13 @@ add_custom_command(OUTPUT ${zephyr_bindgen}
4237
)
4338

4439
set(rust_src_dir ${CMAKE_CURRENT_SOURCE_DIR}/rust-app)
45-
set(rust_build_dir ${CMAKE_CURRENT_BINARY_DIR}/rust-app)
46-
set(rust_staticlib ${rust_build_dir}/${rust_target}/release/librust_app.a)
40+
set(rust_sysroot ${CMAKE_CURRENT_BINARY_DIR}/sysroot)
41+
set(rust_sysroot_build ${CMAKE_CURRENT_BINARY_DIR}/sysroot-build)
42+
set(rust_app_build ${CMAKE_CURRENT_BINARY_DIR}/app)
43+
set(rust_staticlib ${rust_app_build}/${rust_target}/release/librust_app.a)
44+
if(NOT DEFINED CARGO_MANIFEST)
45+
set(CARGO_MANIFEST ${CMAKE_SOURCE_DIR}/Cargo.toml)
46+
endif()
4747

4848
zephyr_get_include_directories_for_lang_as_string( C includes)
4949
zephyr_get_compile_definitions_for_lang_as_string( C definitions)
@@ -56,7 +56,7 @@ include(ExternalProject)
5656

5757
ExternalProject_Add(
5858
rust_project
59-
PREFIX ${rust_build_dir}
59+
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
6060
SOURCE_DIR ${rust_src_dir}
6161
BUILD_IN_SOURCE 1
6262
BUILD_ALWAYS 1
@@ -67,19 +67,21 @@ ExternalProject_Add(
6767
"ZEPHYR_BINDGEN=${zephyr_bindgen}"
6868
"CONFIG_USERSPACE=${CONFIG_USERSPACE}"
6969
"TARGET_CFLAGS=${external_project_cflags} --target=${clang_target}"
70-
"SYSROOT=${rust_build_dir}/sysroot"
70+
"SYSROOT=${rust_sysroot}"
71+
"SYSROOT_BUILD=${rust_sysroot_build}"
72+
"APP_BUILD=${rust_app_build}"
7173
"RUST_TARGET_PATH=${rust_src_dir}"
72-
"CARGO_TARGET_DIR=${rust_build_dir}"
7374
"RUST_TARGET=${rust_target}"
75+
"CARGO_MANIFEST=${CARGO_MANIFEST}"
7476
./build.sh
7577
INSTALL_COMMAND ""
7678
BUILD_BYPRODUCTS ${rust_staticlib}
7779
)
7880

79-
add_library(rust_obj OBJECT syscall-thunk.c rust-smem.c)
80-
target_link_libraries(rust_obj zephyr_interface)
81+
add_library(rust_c OBJECT syscall-thunk.c rust-smem.c)
82+
target_link_libraries(rust_c zephyr_interface)
8183
# High level target that means all headers have been generated
82-
add_dependencies(rust_obj offsets_h)
84+
add_dependencies(rust_c offsets_h)
8385

8486
# Relocatable single object library containing all Rust code. This allows using
8587
# a linker script during this link to rename sections, putting data/bss in the
@@ -91,17 +93,21 @@ if(CONFIG_USERSPACE)
9193
endif()
9294
set(RUST_OBJ_CFLAGS ${CMAKE_C_FLAGS})
9395
separate_arguments(RUST_OBJ_CFLAGS)
96+
set(rust_obj ${CMAKE_CURRENT_BINARY_DIR}/rust_obj.obj)
9497
add_custom_command(
95-
OUTPUT rust_obj.obj
98+
OUTPUT ${rust_obj}
9699
COMMAND ${CMAKE_C_COMPILER} ${RUST_OBJ_CFLAGS}
97100
-nostdlib
98101
-static
99102
-no-pie
100103
-Wl,-r
101104
${rust_ldscript}
102105
-Wl,--whole-archive ${rust_staticlib}
103-
-Wl,--no-whole-archive $<TARGET_OBJECTS:rust_obj>
106+
-Wl,--no-whole-archive $<TARGET_OBJECTS:rust_c>
104107
-o rust_obj.obj
105-
DEPENDS ${rust_staticlib} $<TARGET_OBJECTS:rust_obj>
108+
DEPENDS ${rust_staticlib} $<TARGET_OBJECTS:rust_c>
106109
COMMAND_EXPAND_LISTS
107110
)
111+
add_custom_target(rust_final DEPENDS ${rust_obj})
112+
zephyr_library_import(rust ${rust_obj})
113+
add_dependencies(rust rust_final)

rust-app/Cargo.toml

Lines changed: 0 additions & 21 deletions
This file was deleted.

rust-app/build.sh

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
HOST=$(rustc -vV | grep host: | cut -d ' ' -f 2)
44
VERSION="+nightly-2019-05-22"
5-
cargo ${VERSION} -v build --target=${RUST_TARGET} --target-dir=${CARGO_TARGET_DIR}/sysroot-build --release --manifest-path=./sysroot/Cargo.toml -p std
5+
CARGO_ARGS="${VERSION} -v build --target=${RUST_TARGET} --release"
6+
cargo ${CARGO_ARGS} --target-dir=${SYSROOT_BUILD} --manifest-path=./sysroot/Cargo.toml -p std
67

78
SYSROOT_LIB="${SYSROOT}/lib/rustlib/${RUST_TARGET}/lib"
89
SYSROOT_LIB_HOST="${SYSROOT}/lib/rustlib/${HOST}/lib"
9-
mkdir -p ${SYSROOT_LIB} ${SYSROOT_LIB_HOST}
10-
cp -a ${CARGO_TARGET_DIR}/sysroot-build/${RUST_TARGET}/release/deps/* ${SYSROOT_LIB}
11-
cp -a ${CARGO_TARGET_DIR}/sysroot-build/release/deps/* ${SYSROOT_LIB_HOST}
10+
11+
copy_dir_if_changed() {
12+
mkdir -p $2
13+
if ! diff --brief --recursive $1 $2; then
14+
rm -r $2
15+
cp -a $1 $2
16+
fi
17+
}
18+
19+
copy_dir_if_changed ${SYSROOT_BUILD}/${RUST_TARGET}/release/deps ${SYSROOT_LIB}
20+
copy_dir_if_changed ${SYSROOT_BUILD}/release/deps ${SYSROOT_LIB_HOST}
1221

1322
export RUSTFLAGS="${RUSTFLAGS} --sysroot ${SYSROOT}"
14-
cargo ${VERSION} -v build --target=${RUST_TARGET} --target-dir=${CARGO_TARGET_DIR} --release
23+
cargo ${CARGO_ARGS} --target-dir=${APP_BUILD} --manifest-path=${CARGO_MANIFEST}

samples/rust-app/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.13.1)
4+
get_filename_component(ZEPHYR_RUST ${CMAKE_CURRENT_SOURCE_DIR}/../.. ABSOLUTE)
5+
list(APPEND ZEPHYR_MODULES ${ZEPHYR_RUST})
6+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
7+
project(hello_world)
8+
target_sources(app PRIVATE ./src/main.c)
File renamed without changes.

samples/rust-app/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "rust-app"
3+
version = "0.1.0"
4+
authors = ["Tyler Hall <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["staticlib"]
9+
10+
[dependencies]
11+
cstr = "0.1.7"
12+
# Fix these path dependencies
13+
zephyr-sys = { path = "../../rust-app/zephyr-sys" }
14+
zephyr = { path = "../../rust-app/zephyr", features = ["have_std"] }
15+
zephyr-macros = { path = "../../rust-app/zephyr-macros" }
16+
zephyr-logger = { path = "../../rust-app/zephyr-logger" }
17+
log = "0.4"
18+
19+
[profile.release]
20+
panic = "abort"
21+
lto = true
22+
debug = true
File renamed without changes.
File renamed without changes.
File renamed without changes.

zephyr/module.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build:
2+
cmake: .

0 commit comments

Comments
 (0)