-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathCMakeLists.txt
202 lines (181 loc) · 7.81 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.8.2)
add_subdirectory(uart-buffered)
add_subdirectory(mutex-pool)
# Use a clang_target known to clang so headers will be processed correctly with
# bindgen. rust_target may point to a custom json target.
if(${ARCH} STREQUAL "posix" OR ${ARCH} STREQUAL "x86")
set(rust_target i686-unknown-zephyr)
set(clang_target i686-unknown-linux-gnu)
elseif(${ARCH} STREQUAL "arm")
if(CONFIG_CPU_AARCH32_CORTEX_R OR CONFIG_CPU_CORTEX_R)
if(CONFIG_FPU)
set(rust_target thumbv7r-zephyr-eabihf)
set(clang_target thumbv7r-unknown-none-eabihf)
else()
set(rust_target thumbv7r-zephyr-eabi)
set(clang_target thumbv7r-unknown-none-eabi)
endif()
elseif(CONFIG_CPU_CORTEX_M4 OR CONFIG_CPU_CORTEX_M7)
if(CONFIG_FPU)
set(rust_target thumbv7em-zephyr-eabihf)
set(clang_target thumbv7em-unknown-none-eabihf)
else()
set(rust_target thumbv7em-zephyr-eabi)
set(clang_target thumbv7em-unknown-none-eabi)
endif()
elseif(CONFIG_ARMV8_M_MAINLINE)
if(CONFIG_FPU)
set(rust_target thumbv8m.main-zephyr-eabihf)
set(clang_target thumbv8m.main-unknown-none-eabihf)
else()
set(rust_target thumbv8m.main-zephyr-eabi)
set(clang_target thumbv8m.main-unknown-none-eabi)
endif()
else()
set(rust_target thumbv7m-zephyr-eabi)
set(clang_target thumbv7m-none-eabi)
endif()
elseif(${ARCH} STREQUAL "riscv")
set(rust_target "riscv")
if(CONFIG_64BIT)
string(CONCAT rust_target ${rust_target} "64")
else()
string(CONCAT rust_target ${rust_target} "32")
endif()
set(clang_target ${rust_target})
if (CONFIG_RISCV_ISA_RV32E)
string(CONCAT rust_target ${rust_target} "e")
else()
string(CONCAT rust_target ${rust_target} "i")
endif()
if (CONFIG_RISCV_ISA_EXT_M)
string(CONCAT rust_target ${rust_target} "m")
endif()
if (CONFIG_RISCV_ISA_EXT_A)
string(CONCAT rust_target ${rust_target} "a")
endif()
if(CONFIG_RISCV_ISA_EXT_C)
string(CONCAT rust_target ${rust_target} "c")
endif()
string(CONCAT rust_target ${rust_target} "-unknown-zephyr-elf")
else()
message(FATAL_ERROR "Arch ${ARCH} not supported")
endif()
message("Rust target: ${rust_target}")
message("Clang target: ${clang_target}")
set(all_syscalls ${ZEPHYR_BINARY_DIR}/include/generated/all_syscalls.h)
set(syscall_thunks ${ZEPHYR_BINARY_DIR}/include/generated/syscall_thunks.c)
set(syscalls_json ${ZEPHYR_BINARY_DIR}/misc/generated/syscalls.json)
add_custom_command(OUTPUT ${all_syscalls} ${syscall_thunks}
# Also, some files are written to include/generated/syscalls/
COMMAND
${PYTHON_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen_syscalls.py
--json-file ${syscalls_json} # Read this file
--thunks ${syscall_thunks}
--all-syscalls ${all_syscalls}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${syscalls_json}
${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen_syscalls.py
)
add_custom_target(syscall_thunks_target DEPENDS
${all_syscalls} ${syscall_thunks} ${SYSCALL_LIST_H_TARGET} kernel)
if(NOT ZEPHYR_BINDGEN)
set(zephyr_bindgen_src_dir ${CMAKE_CURRENT_SOURCE_DIR}/zephyr-bindgen)
set(zephyr_bindgen_build_dir ${CMAKE_CURRENT_BINARY_DIR}/zephyr-bindgen)
set(ZEPHYR_BINDGEN ${zephyr_bindgen_build_dir}/release/zephyr-bindgen)
add_custom_target(zephyr_bindgen_target DEPENDS ${ZEPHYR_BINDGEN})
set(zephyr_bindgen_target zephyr_bindgen_target)
add_custom_command(OUTPUT ${ZEPHYR_BINDGEN}
WORKING_DIRECTORY ${zephyr_bindgen_src_dir}
DEPENDS syscall_thunks_target
COMMAND
cargo -v build --release --target-dir=${zephyr_bindgen_build_dir}
)
endif()
set(rust_src_dir ${CMAKE_CURRENT_SOURCE_DIR}/rust)
set(rust_sysroot ${CMAKE_CURRENT_BINARY_DIR}/sysroot)
set(rust_sysroot_build ${CMAKE_CURRENT_BINARY_DIR}/sysroot-build)
set(rust_generated_project ${CMAKE_CURRENT_BINARY_DIR}/rust-app)
set(rust_app_build ${CMAKE_CURRENT_BINARY_DIR}/app)
set(rust_staticlib ${rust_app_build}/${rust_target}/release/librust_app.a)
if(NOT DEFINED CARGO_SOURCE_DIR)
set(CARGO_SOURCE_DIR ${CMAKE_SOURCE_DIR})
endif()
add_custom_command(OUTPUT ${rust_generated_project}/Cargo.toml
COMMAND ${rust_src_dir}/genproject.sh ${CARGO_SOURCE_DIR} ${rust_generated_project}
DEPENDS ${rust_src_dir}/genproject.sh
)
add_custom_target(rust_generated_project DEPENDS ${rust_generated_project}/Cargo.toml)
# external_project_cflags comes from the example: zephyr/samples/application_development/external_lib/CMakeLists.txt
zephyr_get_include_directories_for_lang_as_string( C includes)
zephyr_get_system_include_directories_for_lang_as_string(C system_includes)
zephyr_get_compile_definitions_for_lang_as_string( C definitions)
# `options` is not included because many of the flags are not supported by clang
# `-imacros ${AUTOCONF_H}` is needed and would have been in `options`
#zephyr_get_compile_options_for_lang_as_string( C options)
set(external_project_cflags
"${includes} ${definitions} ${system_includes} -imacros ${AUTOCONF_H}"
)
# Add the Cargo project only if CONFIG_RUST because this will alawys invoke Cargo.
if (CONFIG_RUST)
include(ExternalProject)
ExternalProject_Add(
rust_project
PREFIX ${CMAKE_CURRENT_BINARY_DIR}
SOURCE_DIR ${rust_src_dir}
BUILD_IN_SOURCE 1
BUILD_ALWAYS 1
DEPENDS syscall_thunks_target ${zephyr_bindgen_target} rust_generated_project
CONFIGURE_COMMAND ""
BUILD_COMMAND
env
"ZEPHYR_BINDGEN=${ZEPHYR_BINDGEN}"
"ZEPHYR_KERNEL_VERSION_NUM=${KERNEL_VERSION_NUMBER}"
"CONFIG_USERSPACE=${CONFIG_USERSPACE}"
"CONFIG_RUST_ALLOC_POOL=${CONFIG_RUST_ALLOC_POOL}"
"CONFIG_RUST_MUTEX_POOL=${CONFIG_RUST_MUTEX_POOL}"
"CONFIG_POSIX_CLOCK=${CONFIG_POSIX_CLOCK}"
"TARGET_CFLAGS=${external_project_cflags} --target=${clang_target}"
"SYSROOT=${rust_sysroot}"
"SYSROOT_BUILD=${rust_sysroot_build}"
"APP_BUILD=${rust_app_build}"
"RUST_TARGET=${rust_target}"
"RUST_TARGET_SPEC=${rust_src_dir}/targets/${rust_target}.json"
"CARGO_MANIFEST=${rust_generated_project}/Cargo.toml"
./build.sh
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${rust_staticlib}
)
# A regular Zephyr C library for our C code
zephyr_library_named(rust_c)
set(thunk_sources syscall-thunk-any.c)
if(CONFIG_USERSPACE)
set(thunk_sources ${thunk_sources} syscall-thunk-kernel.c syscall-thunk-user.c)
endif()
target_sources(rust_c PRIVATE ${thunk_sources} rust-smem.c abort.c)
if(DEFINED syscall_thunk_cflags)
set_source_files_properties(${thunk_sources} PROPERTIES COMPILE_FLAGS "${syscall_thunk_cflags}")
endif()
add_dependencies(rust_c syscall_thunks_target)
# Compatibility for Zephyr 1.14
if (NOT COMMAND zephyr_library_import)
# Add the imported library 'library_name', located at 'library_path' to the
# global list of Zephyr CMake libraries.
function(zephyr_library_import library_name library_path)
add_library(${library_name} STATIC IMPORTED GLOBAL)
set_target_properties(${library_name}
PROPERTIES IMPORTED_LOCATION
${library_path}
)
zephyr_append_cmake_library(${library_name})
endfunction()
endif()
zephyr_library_import(rust ${rust_staticlib})
if(CONFIG_USERSPACE)
set_property(TARGET zephyr_property_target
APPEND PROPERTY COMPILE_OPTIONS
"-l" librust_app.a "rust_std_partition")
endif()
endif()