@@ -49,12 +49,28 @@ function(_rust_map_target)
49
49
endif ()
50
50
endfunction ()
51
51
52
+ function (get_include_dirs target dirs )
53
+ get_target_property (include_dirs ${target} INTERFACE_INCLUDE_DIRECTORIES )
54
+ if (include_dirs )
55
+ set (${dirs} ${include_dirs} PARENT_SCOPE )
56
+ else ()
57
+ set (${dirs} "" PARENT_SCOPE )
58
+ endif ()
59
+ endfunction ()
60
+
52
61
function (rust_cargo_application )
53
62
# For now, hard-code the Zephyr crate directly here. Once we have
54
63
# more than one crate, these should be added by the modules
55
64
# themselves.
56
65
set (LIB_RUST_CRATES zephyr zephyr-build )
57
66
67
+ get_include_dirs (zephyr_interface include_dirs )
68
+
69
+ get_target_property (include_dirs, zephyr_interface INTERFACE_INCLUDE_DIRECTORIES )
70
+ get_property (include_defines TARGET zephyr_interface PROPERTY INTERFACE_COMPILE_DEFINITIONS )
71
+ message (STATUS "Includes: ${include_dirs} " )
72
+ message (STATUS "Defines: ${include_defines} " )
73
+
58
74
_rust_map_target ()
59
75
message (STATUS "Building Rust llvm target ${RUST_TARGET} " )
60
76
@@ -68,6 +84,10 @@ function(rust_cargo_application)
68
84
set (RUST_LIBRARY "${CARGO_TARGET_DIR} /${RUST_TARGET} /${RUST_BUILD_TYPE} /librustapp.a" )
69
85
set (SAMPLE_CARGO_CONFIG "${CMAKE_CURRENT_BINARY_DIR} /rust/sample-cargo-config.toml" )
70
86
87
+ # The generated C binding wrappers. These are bindgen-generated wrappers for the inline functions
88
+ # within Zephyr.
89
+ set (WRAPPER_FILE "${CMAKE_CURRENT_BINARY_DIR} /rust/wrapper.c" )
90
+
71
91
# To get cmake to always invoke Cargo requires a bit of a trick. We make the output of the
72
92
# command a file that never gets created. This will cause cmake to always rerun cargo. We
73
93
# add the actual library as a BYPRODUCTS list of this command, otherwise, the first time the
@@ -109,6 +129,9 @@ target-dir = \"${CARGO_TARGET_DIR}\"
109
129
BUILD_DIR = \" ${CMAKE_CURRENT_BINARY_DIR} \"
110
130
DOTCONFIG = \" ${DOTCONFIG} \"
111
131
ZEPHYR_DTS = \" ${ZEPHYR_DTS} \"
132
+ INCLUDE_DIRS = \" ${include_dirs} \"
133
+ INCLUDE_DEFINES = \" ${include_defines} \"
134
+ WRAPPER_FILE = \" ${WRAPPER_FILE} \"
112
135
113
136
[patch.crates-io]
114
137
${config_paths}
@@ -117,12 +140,15 @@ ${config_paths}
117
140
# The library is built by invoking Cargo.
118
141
add_custom_command (
119
142
OUTPUT ${DUMMY_FILE}
120
- BYPRODUCTS ${RUST_LIBRARY}
143
+ BYPRODUCTS ${RUST_LIBRARY} ${WRAPPER_FILE}
121
144
COMMAND
122
145
${CMAKE_EXECUTABLE}
123
146
env BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}
124
147
DOTCONFIG=${DOTCONFIG}
125
148
ZEPHYR_DTS=${ZEPHYR_DTS}
149
+ INCLUDE_DIRS= "${include_dirs} "
150
+ INCLUDE_DEFINES= "${include_defines} "
151
+ WRAPPER_FilE= "${WRAPPER_FILE} "
126
152
cargo build
127
153
# TODO: release flag if release build
128
154
# --release
@@ -151,5 +177,9 @@ ${config_paths}
151
177
152
178
# Presumably, Rust applications will have no C source files, but cmake will require them.
153
179
# Add an empty file so that this will build. The main will come from the rust library.
154
- target_sources (app PRIVATE ${ZEPHYR_BASE} /lib/rust/main.c )
180
+ target_sources (app PRIVATE ${ZEPHYR_BASE} /lib/rust/main.c ${WRAPPER_FILE} )
181
+ set_source_files_properties (
182
+ ${WRAPPER_FILE}
183
+ COMPILE_FLAGS "-I${ZEPHYR_BASE} /lib/rust/zephyr-sys"
184
+ )
155
185
endfunction ()
0 commit comments