16038
|
1 |
cmake_minimum_required(VERSION 3.22)
|
16021
|
2 |
|
|
3 |
list(APPEND CMAKE_MESSAGE_CONTEXT "Corrosion")
|
|
4 |
|
|
5 |
message(DEBUG "Using Corrosion ${Corrosion_VERSION} with CMake ${CMAKE_VERSION} "
|
|
6 |
"and the `${CMAKE_GENERATOR}` Generator"
|
|
7 |
)
|
|
8 |
|
|
9 |
get_cmake_property(COR_IS_MULTI_CONFIG GENERATOR_IS_MULTI_CONFIG)
|
|
10 |
set(COR_IS_MULTI_CONFIG "${COR_IS_MULTI_CONFIG}" CACHE BOOL "Do not change this" FORCE)
|
|
11 |
mark_as_advanced(FORCE COR_IS_MULTI_CONFIG)
|
|
12 |
|
16038
|
13 |
|
|
14 |
if(NOT COR_IS_MULTI_CONFIG AND DEFINED CMAKE_CONFIGURATION_TYPES)
|
16021
|
15 |
message(WARNING "The Generator is ${CMAKE_GENERATOR}, which is not a multi-config "
|
|
16 |
"Generator, but CMAKE_CONFIGURATION_TYPES is set. Please don't set "
|
|
17 |
"CMAKE_CONFIGURATION_TYPES unless you are using a multi-config Generator."
|
|
18 |
)
|
|
19 |
endif()
|
|
20 |
|
|
21 |
option(CORROSION_VERBOSE_OUTPUT "Enables verbose output from Corrosion and Cargo" OFF)
|
|
22 |
|
16038
|
23 |
if(DEFINED CORROSION_RESPECT_OUTPUT_DIRECTORY AND NOT CORROSION_RESPECT_OUTPUT_DIRECTORY)
|
|
24 |
message(WARNING "The option CORROSION_RESPECT_OUTPUT_DIRECTORY was removed."
|
|
25 |
" Corrosion now always attempts to respect the output directory.")
|
|
26 |
endif()
|
16021
|
27 |
|
|
28 |
option(
|
|
29 |
CORROSION_NO_WARN_PARSE_TARGET_TRIPLE_FAILED
|
|
30 |
"Surpresses a warning if the parsing the target triple failed."
|
|
31 |
OFF
|
|
32 |
)
|
|
33 |
|
|
34 |
find_package(Rust REQUIRED)
|
|
35 |
|
|
36 |
if(CMAKE_GENERATOR MATCHES "Visual Studio"
|
|
37 |
AND (NOT CMAKE_VS_PLATFORM_NAME STREQUAL CMAKE_VS_PLATFORM_NAME_DEFAULT)
|
|
38 |
AND Rust_VERSION VERSION_LESS "1.54")
|
|
39 |
message(FATAL_ERROR "Due to a cargo issue, cross-compiling with a Visual Studio generator and rust versions"
|
|
40 |
" before 1.54 is not supported. Rust build scripts would be linked with the cross-compiler linker, which"
|
|
41 |
" causes the build to fail. Please upgrade your Rust version to 1.54 or newer.")
|
|
42 |
endif()
|
|
43 |
|
16038
|
44 |
# message(STATUS "Using Corrosion as a subdirectory")
|
16021
|
45 |
|
|
46 |
get_property(
|
|
47 |
RUSTC_EXECUTABLE
|
|
48 |
TARGET Rust::Rustc PROPERTY IMPORTED_LOCATION
|
|
49 |
)
|
|
50 |
|
|
51 |
get_property(
|
|
52 |
CARGO_EXECUTABLE
|
|
53 |
TARGET Rust::Cargo PROPERTY IMPORTED_LOCATION
|
|
54 |
)
|
|
55 |
|
|
56 |
function(_corrosion_bin_target_suffix target_name out_var_suffix)
|
|
57 |
get_target_property(hostbuild "${target_name}" ${_CORR_PROP_HOST_BUILD})
|
|
58 |
if((hostbuild AND CMAKE_HOST_WIN32)
|
|
59 |
OR ((NOT hostbuild) AND (Rust_CARGO_TARGET_OS STREQUAL "windows")))
|
|
60 |
set(_suffix ".exe")
|
|
61 |
elseif(Rust_CARGO_TARGET_OS STREQUAL "vxworks")
|
|
62 |
set(_suffix ".vxe")
|
|
63 |
else()
|
|
64 |
set(_suffix "")
|
|
65 |
endif()
|
|
66 |
set(${out_var_suffix} "${_suffix}" PARENT_SCOPE)
|
|
67 |
endfunction()
|
|
68 |
|
|
69 |
# Do not call this function directly!
|
|
70 |
#
|
|
71 |
# This function should be called deferred to evaluate target properties late in the configure stage.
|
|
72 |
# IMPORTED_LOCATION does not support Generator expressions, so we must evaluate the output
|
|
73 |
# directory target property value at configure time. This function must be deferred to the end of
|
|
74 |
# the configure stage, so we can be sure that the output directory is not modified afterwards.
|
|
75 |
function(_corrosion_set_imported_location_deferred target_name base_property output_directory_property filename)
|
|
76 |
# The output directory property is expected to be set on the exposed target (without postfix),
|
|
77 |
# but we need to set the imported location on the actual library target with postfix.
|
|
78 |
if("${target_name}" MATCHES "^(.+)-(static|shared)$")
|
|
79 |
set(output_dir_prop_target_name "${CMAKE_MATCH_1}")
|
|
80 |
else()
|
|
81 |
set(output_dir_prop_target_name "${target_name}")
|
|
82 |
endif()
|
|
83 |
|
|
84 |
# Append .exe suffix for executable by-products if the target is windows or if it's a host
|
|
85 |
# build and the host is Windows.
|
|
86 |
get_target_property(target_type ${target_name} TYPE)
|
|
87 |
if(${target_type} STREQUAL "EXECUTABLE" AND (NOT "${filename}" MATCHES "\.pdb$"))
|
|
88 |
_corrosion_bin_target_suffix(${target_name} "suffix")
|
16038
|
89 |
string(APPEND filename "${suffix}")
|
16021
|
90 |
endif()
|
|
91 |
|
|
92 |
get_target_property(output_directory "${output_dir_prop_target_name}" "${output_directory_property}")
|
|
93 |
message(DEBUG "Output directory property (target ${output_dir_prop_target_name}): ${output_directory_property} dir: ${output_directory}")
|
|
94 |
|
|
95 |
foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
|
|
96 |
string(TOUPPER "${config_type}" config_type_upper)
|
|
97 |
get_target_property(output_dir_curr_config ${output_dir_prop_target_name}
|
|
98 |
"${output_directory_property}_${config_type_upper}"
|
|
99 |
)
|
|
100 |
if(output_dir_curr_config)
|
|
101 |
set(curr_out_dir "${output_dir_curr_config}")
|
|
102 |
elseif(output_directory)
|
|
103 |
set(curr_out_dir "${output_directory}")
|
|
104 |
else()
|
|
105 |
set(curr_out_dir "${CMAKE_CURRENT_BINARY_DIR}")
|
|
106 |
endif()
|
16038
|
107 |
string(REPLACE "\$<CONFIG>" "${config_type}" curr_out_dir "${curr_out_dir}")
|
16021
|
108 |
message(DEBUG "Setting ${base_property}_${config_type_upper} for target ${target_name}"
|
|
109 |
" to `${curr_out_dir}/${filename}`.")
|
16038
|
110 |
|
|
111 |
string(GENEX_STRIP "${curr_out_dir}" stripped_out_dir)
|
|
112 |
if(NOT ("${stripped_out_dir}" STREQUAL "${curr_out_dir}"))
|
|
113 |
message(FATAL_ERROR "${output_directory_property} for target ${output_dir_prop_target_name} "
|
|
114 |
"contained an unexpected Generator expression. Output dir: `${curr_out_dir}`"
|
|
115 |
"Note: Corrosion only supports the `\$<CONFIG>` generator expression for output directories.")
|
|
116 |
endif()
|
|
117 |
|
16021
|
118 |
# For Multiconfig we want to specify the correct location for each configuration
|
|
119 |
set_property(
|
|
120 |
TARGET ${target_name}
|
|
121 |
PROPERTY "${base_property}_${config_type_upper}"
|
|
122 |
"${curr_out_dir}/${filename}"
|
|
123 |
)
|
|
124 |
set(base_output_directory "${curr_out_dir}")
|
|
125 |
endforeach()
|
|
126 |
|
|
127 |
if(NOT COR_IS_MULTI_CONFIG)
|
|
128 |
if(output_directory)
|
|
129 |
set(base_output_directory "${output_directory}")
|
|
130 |
else()
|
|
131 |
set(base_output_directory "${CMAKE_CURRENT_BINARY_DIR}")
|
|
132 |
endif()
|
16038
|
133 |
string(REPLACE "\$<CONFIG>" "${CMAKE_BUILD_TYPE}" base_output_directory "${base_output_directory}")
|
|
134 |
string(GENEX_STRIP "${base_output_directory}" stripped_out_dir)
|
|
135 |
if(NOT ("${stripped_out_dir}" STREQUAL "${base_output_directory}"))
|
|
136 |
message(FATAL_ERROR "${output_dir_prop_target_name} for target ${output_dir_prop_target_name} "
|
|
137 |
"contained an unexpected Generator expression. Output dir: `${base_output_directory}`"
|
|
138 |
"Note: Corrosion only supports the `\$<CONFIG>` generator expression for output directories.")
|
|
139 |
endif()
|
16021
|
140 |
endif()
|
|
141 |
|
|
142 |
message(DEBUG "Setting ${base_property} for target ${target_name}"
|
|
143 |
" to `${base_output_directory}/${filename}`.")
|
|
144 |
|
|
145 |
# IMPORTED_LOCATION must be set regardless of possible overrides. In the multiconfig case,
|
|
146 |
# the last configuration "wins" (IMPORTED_LOCATION is not documented to have Genex support).
|
|
147 |
set_property(
|
|
148 |
TARGET ${target_name}
|
|
149 |
PROPERTY "${base_property}" "${base_output_directory}/${filename}"
|
|
150 |
)
|
|
151 |
endfunction()
|
|
152 |
|
|
153 |
# Set the imported location of a Rust target.
|
|
154 |
#
|
|
155 |
# Rust targets are built via custom targets / custom commands. The actual artifacts are exposed
|
|
156 |
# to CMake as imported libraries / executables that depend on the cargo_build command. For CMake
|
|
157 |
# to find the built artifact we need to set the IMPORTED location to the actual location on disk.
|
|
158 |
# Corrosion tries to copy the artifacts built by cargo to standard locations. The IMPORTED_LOCATION
|
|
159 |
# is set to point to the copy, and not the original from the cargo build directory.
|
|
160 |
#
|
|
161 |
# Parameters:
|
|
162 |
# - target_name: Name of the Rust target
|
|
163 |
# - base_property: Name of the base property - i.e. `IMPORTED_LOCATION` or `IMPORTED_IMPLIB`.
|
|
164 |
# - output_directory_property: Target property name that determines the standard location for the
|
|
165 |
# artifact.
|
|
166 |
# - filename of the artifact.
|
|
167 |
function(_corrosion_set_imported_location target_name base_property output_directory_property filename)
|
16038
|
168 |
cmake_language(EVAL CODE "
|
|
169 |
cmake_language(DEFER
|
|
170 |
CALL
|
|
171 |
_corrosion_set_imported_location_deferred
|
|
172 |
[[${target_name}]]
|
|
173 |
[[${base_property}]]
|
|
174 |
[[${output_directory_property}]]
|
|
175 |
[[${filename}]]
|
|
176 |
)
|
|
177 |
")
|
16021
|
178 |
endfunction()
|
|
179 |
|
16038
|
180 |
function(_corrosion_copy_byproduct_deferred target_name output_dir_prop_names cargo_build_dir file_names)
|
16021
|
181 |
if(ARGN)
|
|
182 |
message(FATAL_ERROR "Unexpected additional arguments")
|
|
183 |
endif()
|
|
184 |
|
16038
|
185 |
foreach(output_dir_prop_name ${output_dir_prop_names})
|
|
186 |
get_target_property(output_dir ${target_name} "${output_dir_prop_name}")
|
|
187 |
if(output_dir)
|
|
188 |
break()
|
|
189 |
endif()
|
|
190 |
endforeach()
|
16021
|
191 |
|
|
192 |
# A Genex expanding to the output directory depending on the configuration.
|
|
193 |
set(multiconfig_out_dir_genex "")
|
|
194 |
|
|
195 |
foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
|
|
196 |
string(TOUPPER "${config_type}" config_type_upper)
|
16038
|
197 |
foreach(output_dir_prop_name ${output_dir_prop_names})
|
|
198 |
get_target_property(output_dir_curr_config ${target_name} "${output_dir_prop_name}_${config_type_upper}")
|
|
199 |
if(output_dir_curr_config)
|
|
200 |
break()
|
|
201 |
endif()
|
|
202 |
endforeach()
|
16021
|
203 |
|
|
204 |
if(output_dir_curr_config)
|
|
205 |
set(curr_out_dir "${output_dir_curr_config}")
|
|
206 |
elseif(output_dir)
|
|
207 |
# Fallback to `output_dir` if specified
|
|
208 |
# Note: Multi-configuration generators append a per-configuration subdirectory to the
|
|
209 |
# specified directory unless a generator expression is used (from CMake documentation).
|
|
210 |
set(curr_out_dir "${output_dir}")
|
|
211 |
else()
|
|
212 |
# Fallback to the default directory. We do not append the configuration directory here
|
|
213 |
# and instead let CMake do this, since otherwise the resolving of dynamic library
|
|
214 |
# imported paths may fail.
|
|
215 |
set(curr_out_dir "${CMAKE_CURRENT_BINARY_DIR}")
|
|
216 |
endif()
|
|
217 |
set(multiconfig_out_dir_genex "${multiconfig_out_dir_genex}$<$<CONFIG:${config_type}>:${curr_out_dir}>")
|
|
218 |
endforeach()
|
|
219 |
|
|
220 |
if(COR_IS_MULTI_CONFIG)
|
|
221 |
set(output_dir "${multiconfig_out_dir_genex}")
|
|
222 |
else()
|
|
223 |
if(NOT output_dir)
|
|
224 |
# Fallback to default directory.
|
|
225 |
set(output_dir "${CMAKE_CURRENT_BINARY_DIR}")
|
|
226 |
endif()
|
|
227 |
endif()
|
|
228 |
|
|
229 |
# Append .exe suffix for executable by-products if the target is windows or if it's a host
|
|
230 |
# build and the host is Windows.
|
|
231 |
get_target_property(target_type "${target_name}" TYPE)
|
|
232 |
if (target_type STREQUAL "EXECUTABLE")
|
|
233 |
list(LENGTH file_names list_len)
|
|
234 |
if(NOT list_len EQUAL "1")
|
|
235 |
message(FATAL_ERROR
|
|
236 |
"Internal error: Exactly one filename should be passed for executable types.")
|
|
237 |
endif()
|
|
238 |
_corrosion_bin_target_suffix(${target_name} "suffix")
|
|
239 |
if(suffix AND (NOT "${file_names}" MATCHES "\.pdb$"))
|
|
240 |
# For executable targets we know / checked that only one file will be passed.
|
|
241 |
string(APPEND file_names "${suffix}")
|
|
242 |
endif()
|
|
243 |
endif()
|
16038
|
244 |
set(src_file_names "${file_names}")
|
|
245 |
if(Rust_CARGO_TARGET_ENV STREQUAL "gnullvm")
|
|
246 |
# Workaround for cargo not exposing implibs yet.
|
|
247 |
list(TRANSFORM src_file_names PREPEND "deps/" REGEX "\.dll\.a$")
|
|
248 |
endif()
|
|
249 |
list(TRANSFORM src_file_names PREPEND "${cargo_build_dir}/")
|
16021
|
250 |
list(TRANSFORM file_names PREPEND "${output_dir}/" OUTPUT_VARIABLE dst_file_names)
|
|
251 |
message(DEBUG "Adding command to copy byproducts `${file_names}` to ${dst_file_names}")
|
|
252 |
add_custom_command(TARGET _cargo-build_${target_name}
|
|
253 |
POST_BUILD
|
|
254 |
# output_dir may contain a Generator expression.
|
|
255 |
COMMAND ${CMAKE_COMMAND} -E make_directory "${output_dir}"
|
|
256 |
COMMAND
|
|
257 |
${CMAKE_COMMAND} -E copy_if_different
|
|
258 |
# tested to work with both multiple files and paths with spaces
|
|
259 |
${src_file_names}
|
|
260 |
"${output_dir}"
|
|
261 |
BYPRODUCTS ${dst_file_names}
|
|
262 |
COMMENT "Copying byproducts `${file_names}` to ${output_dir}"
|
|
263 |
VERBATIM
|
|
264 |
COMMAND_EXPAND_LISTS
|
|
265 |
)
|
|
266 |
endfunction()
|
|
267 |
|
16038
|
268 |
# Copy the artifacts generated by cargo to the appropriate destination.
|
|
269 |
#
|
|
270 |
# Parameters:
|
|
271 |
# - target_name: The name of the Rust target
|
|
272 |
# - output_dir_prop_names: The property name(s) controlling the destination (e.g.
|
|
273 |
# `LIBRARY_OUTPUT_DIRECTORY` or `PDB_OUTPUT_DIRECTORY;RUNTIME_OUTPUT_DIRECTORY`)
|
|
274 |
# - cargo_build_dir: the directory cargo build places it's output artifacts in.
|
|
275 |
# - filenames: the file names of any output artifacts as a list.
|
|
276 |
function(_corrosion_copy_byproducts target_name output_dir_prop_names cargo_build_dir file_names)
|
16021
|
277 |
cmake_language(EVAL CODE "
|
|
278 |
cmake_language(DEFER
|
|
279 |
CALL
|
|
280 |
_corrosion_copy_byproduct_deferred
|
|
281 |
[[${target_name}]]
|
16038
|
282 |
[[${output_dir_prop_names}]]
|
16021
|
283 |
[[${cargo_build_dir}]]
|
|
284 |
[[${file_names}]]
|
|
285 |
)
|
|
286 |
")
|
|
287 |
endfunction()
|
|
288 |
|
|
289 |
|
|
290 |
# Add targets for the static and/or shared libraries of the rust target.
|
|
291 |
# The generated byproduct names are returned via the `OUT_<type>_BYPRODUCTS` arguments.
|
|
292 |
function(_corrosion_add_library_target)
|
|
293 |
set(OPTIONS "")
|
|
294 |
set(ONE_VALUE_KEYWORDS
|
|
295 |
WORKSPACE_MANIFEST_PATH
|
|
296 |
TARGET_NAME
|
|
297 |
OUT_ARCHIVE_OUTPUT_BYPRODUCTS
|
|
298 |
OUT_SHARED_LIB_BYPRODUCTS
|
|
299 |
OUT_PDB_BYPRODUCT
|
|
300 |
)
|
|
301 |
set(MULTI_VALUE_KEYWORDS LIB_KINDS)
|
|
302 |
cmake_parse_arguments(PARSE_ARGV 0 CALT "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")
|
|
303 |
|
|
304 |
if(DEFINED CALT_UNPARSED_ARGUMENTS)
|
|
305 |
message(FATAL_ERROR "Internal error - unexpected arguments: ${CALT_UNPARSED_ARGUMENTS}")
|
|
306 |
elseif(DEFINED CALT_KEYWORDS_MISSING_VALUES)
|
|
307 |
message(FATAL_ERROR "Internal error - the following keywords had no associated value(s):"
|
|
308 |
"${CALT_KEYWORDS_MISSING_VALUES}")
|
|
309 |
endif()
|
|
310 |
list(TRANSFORM ONE_VALUE_KEYWORDS PREPEND CALT_ OUTPUT_VARIABLE required_arguments)
|
|
311 |
foreach(required_argument ${required_arguments} )
|
|
312 |
if(NOT DEFINED "${required_argument}")
|
|
313 |
message(FATAL_ERROR "Internal error: Missing required argument ${required_argument}."
|
|
314 |
"Complete argument list: ${ARGN}"
|
|
315 |
)
|
|
316 |
endif()
|
|
317 |
endforeach()
|
|
318 |
if("staticlib" IN_LIST CALT_LIB_KINDS)
|
|
319 |
set(has_staticlib TRUE)
|
|
320 |
endif()
|
|
321 |
if("cdylib" IN_LIST CALT_LIB_KINDS)
|
|
322 |
set(has_cdylib TRUE)
|
|
323 |
endif()
|
|
324 |
|
|
325 |
if(NOT (has_staticlib OR has_cdylib))
|
|
326 |
message(FATAL_ERROR "Unknown library type(s): ${CALT_LIB_KINDS}")
|
|
327 |
endif()
|
|
328 |
set(workspace_manifest_path "${CALT_WORKSPACE_MANIFEST_PATH}")
|
|
329 |
set(target_name "${CALT_TARGET_NAME}")
|
|
330 |
|
|
331 |
set(is_windows "")
|
|
332 |
set(is_windows_gnu "")
|
|
333 |
set(is_windows_msvc "")
|
|
334 |
set(is_macos "")
|
|
335 |
if(Rust_CARGO_TARGET_OS STREQUAL "windows")
|
|
336 |
set(is_windows TRUE)
|
|
337 |
if(Rust_CARGO_TARGET_ENV STREQUAL "msvc")
|
|
338 |
set(is_windows_msvc TRUE)
|
16038
|
339 |
elseif(Rust_CARGO_TARGET_ENV STREQUAL "gnu" OR Rust_CARGO_TARGET_ENV STREQUAL "gnullvm")
|
16021
|
340 |
set(is_windows_gnu TRUE)
|
|
341 |
endif()
|
|
342 |
elseif(Rust_CARGO_TARGET_OS STREQUAL "darwin")
|
|
343 |
set(is_macos TRUE)
|
|
344 |
endif()
|
|
345 |
|
|
346 |
# target file names
|
|
347 |
string(REPLACE "-" "_" lib_name "${target_name}")
|
|
348 |
|
|
349 |
if(is_windows_msvc)
|
|
350 |
set(static_lib_name "${lib_name}.lib")
|
|
351 |
else()
|
|
352 |
set(static_lib_name "lib${lib_name}.a")
|
|
353 |
endif()
|
|
354 |
|
|
355 |
if(is_windows)
|
|
356 |
set(dynamic_lib_name "${lib_name}.dll")
|
|
357 |
elseif(is_macos)
|
|
358 |
set(dynamic_lib_name "lib${lib_name}.dylib")
|
|
359 |
else()
|
|
360 |
set(dynamic_lib_name "lib${lib_name}.so")
|
|
361 |
endif()
|
|
362 |
|
|
363 |
if(is_windows_msvc)
|
|
364 |
set(implib_name "${lib_name}.dll.lib")
|
|
365 |
elseif(is_windows_gnu)
|
|
366 |
set(implib_name "lib${lib_name}.dll.a")
|
|
367 |
elseif(is_windows)
|
|
368 |
message(FATAL_ERROR "Unknown windows environment - Can't determine implib name")
|
|
369 |
endif()
|
|
370 |
|
|
371 |
|
|
372 |
set(pdb_name "${lib_name}.pdb")
|
|
373 |
|
|
374 |
set(archive_output_byproducts "")
|
|
375 |
if(has_staticlib)
|
|
376 |
list(APPEND archive_output_byproducts ${static_lib_name})
|
|
377 |
endif()
|
|
378 |
|
|
379 |
if(has_cdylib)
|
|
380 |
set("${CALT_OUT_SHARED_LIB_BYPRODUCTS}" "${dynamic_lib_name}" PARENT_SCOPE)
|
|
381 |
if(is_windows)
|
|
382 |
list(APPEND archive_output_byproducts ${implib_name})
|
|
383 |
endif()
|
|
384 |
if(is_windows_msvc)
|
|
385 |
set("${CALT_OUT_PDB_BYPRODUCT}" "${pdb_name}" PARENT_SCOPE)
|
|
386 |
endif()
|
|
387 |
endif()
|
|
388 |
set("${CALT_OUT_ARCHIVE_OUTPUT_BYPRODUCTS}" "${archive_output_byproducts}" PARENT_SCOPE)
|
|
389 |
|
|
390 |
if(has_staticlib)
|
|
391 |
add_library(${target_name}-static STATIC IMPORTED GLOBAL)
|
|
392 |
add_dependencies(${target_name}-static cargo-build_${target_name})
|
16038
|
393 |
set_target_properties(${target_name}-static PROPERTIES COR_FILE_NAME ${static_lib_name})
|
16021
|
394 |
|
|
395 |
_corrosion_set_imported_location("${target_name}-static" "IMPORTED_LOCATION"
|
|
396 |
"ARCHIVE_OUTPUT_DIRECTORY"
|
|
397 |
"${static_lib_name}")
|
|
398 |
|
|
399 |
# Todo: NO_STD target property?
|
|
400 |
if(NOT COR_NO_STD)
|
|
401 |
set_property(
|
|
402 |
TARGET ${target_name}-static
|
|
403 |
PROPERTY INTERFACE_LINK_LIBRARIES ${Rust_CARGO_TARGET_LINK_NATIVE_LIBS}
|
|
404 |
)
|
|
405 |
set_property(
|
|
406 |
TARGET ${target_name}-static
|
|
407 |
PROPERTY INTERFACE_LINK_OPTIONS ${Rust_CARGO_TARGET_LINK_OPTIONS}
|
|
408 |
)
|
|
409 |
if(is_macos)
|
|
410 |
set_property(TARGET ${target_name}-static
|
|
411 |
PROPERTY INTERFACE_LINK_DIRECTORIES "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
|
|
412 |
)
|
|
413 |
endif()
|
|
414 |
endif()
|
|
415 |
endif()
|
|
416 |
|
|
417 |
if(has_cdylib)
|
|
418 |
add_library(${target_name}-shared SHARED IMPORTED GLOBAL)
|
|
419 |
add_dependencies(${target_name}-shared cargo-build_${target_name})
|
16038
|
420 |
set_target_properties(${target_name}-shared PROPERTIES COR_FILE_NAME ${dynamic_lib_name})
|
16021
|
421 |
|
|
422 |
# Todo: (Not new issue): What about IMPORTED_SONAME and IMPORTED_NO_SYSTEM?
|
|
423 |
_corrosion_set_imported_location("${target_name}-shared" "IMPORTED_LOCATION"
|
|
424 |
"LIBRARY_OUTPUT_DIRECTORY"
|
|
425 |
"${dynamic_lib_name}"
|
|
426 |
)
|
|
427 |
# In the future we would probably prefer to let Rust set the soname for packages >= 1.0.
|
|
428 |
# This is tracked in issue #333.
|
|
429 |
set_target_properties(${target_name}-shared PROPERTIES IMPORTED_NO_SONAME TRUE)
|
|
430 |
|
|
431 |
if(is_windows)
|
|
432 |
_corrosion_set_imported_location("${target_name}-shared" "IMPORTED_IMPLIB"
|
|
433 |
"ARCHIVE_OUTPUT_DIRECTORY"
|
|
434 |
"${implib_name}"
|
|
435 |
)
|
16038
|
436 |
set_target_properties(${target_name}-shared PROPERTIES COR_IMPLIB_FILE_NAME ${implib_name})
|
16021
|
437 |
endif()
|
|
438 |
|
|
439 |
if(is_macos)
|
|
440 |
set_property(TARGET ${target_name}-shared
|
|
441 |
PROPERTY INTERFACE_LINK_DIRECTORIES "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
|
|
442 |
)
|
|
443 |
endif()
|
|
444 |
endif()
|
|
445 |
|
|
446 |
if(has_cdylib AND has_staticlib)
|
|
447 |
if(BUILD_SHARED_LIBS)
|
|
448 |
target_link_libraries(${target_name} INTERFACE ${target_name}-shared)
|
|
449 |
else()
|
|
450 |
target_link_libraries(${target_name} INTERFACE ${target_name}-static)
|
|
451 |
endif()
|
|
452 |
elseif(has_cdylib)
|
|
453 |
target_link_libraries(${target_name} INTERFACE ${target_name}-shared)
|
|
454 |
else()
|
|
455 |
target_link_libraries(${target_name} INTERFACE ${target_name}-static)
|
|
456 |
endif()
|
|
457 |
endfunction()
|
|
458 |
|
|
459 |
function(_corrosion_add_bin_target workspace_manifest_path bin_name out_bin_byproduct out_pdb_byproduct)
|
|
460 |
if(NOT bin_name)
|
|
461 |
message(FATAL_ERROR "No bin_name in _corrosion_add_bin_target for target ${target_name}")
|
|
462 |
endif()
|
|
463 |
|
|
464 |
string(REPLACE "-" "_" bin_name_underscore "${bin_name}")
|
|
465 |
|
|
466 |
set(pdb_name "${bin_name_underscore}.pdb")
|
|
467 |
|
|
468 |
if(Rust_CARGO_TARGET_ENV STREQUAL "msvc")
|
|
469 |
set(${out_pdb_byproduct} "${pdb_name}" PARENT_SCOPE)
|
|
470 |
endif()
|
|
471 |
|
16038
|
472 |
# Potential .exe suffix will be added later, also depending on possible hostbuild
|
|
473 |
# target property
|
16021
|
474 |
set(bin_filename "${bin_name}")
|
|
475 |
set(${out_bin_byproduct} "${bin_filename}" PARENT_SCOPE)
|
|
476 |
add_dependencies(${bin_name} cargo-build_${bin_name})
|
|
477 |
|
|
478 |
if(Rust_CARGO_TARGET_OS STREQUAL "darwin")
|
|
479 |
set_property(TARGET ${bin_name}
|
|
480 |
PROPERTY INTERFACE_LINK_DIRECTORIES "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib"
|
|
481 |
)
|
|
482 |
endif()
|
|
483 |
|
|
484 |
_corrosion_set_imported_location("${bin_name}" "IMPORTED_LOCATION"
|
|
485 |
"RUNTIME_OUTPUT_DIRECTORY"
|
|
486 |
"${bin_filename}"
|
|
487 |
)
|
|
488 |
|
|
489 |
endfunction()
|
|
490 |
|
|
491 |
|
16038
|
492 |
include(CorrosionGenerator)
|
16021
|
493 |
|
|
494 |
# Note: `cmake_language(GET_MESSAGE_LOG_LEVEL <output_variable>)` requires CMake 3.25,
|
|
495 |
# so we offer our own option to control verbosity of downstream commands (e.g. cargo build)
|
|
496 |
if (CORROSION_VERBOSE_OUTPUT)
|
|
497 |
set(_CORROSION_VERBOSE_OUTPUT_FLAG --verbose CACHE INTERNAL "")
|
|
498 |
else()
|
|
499 |
# We want to silence some less important commands by default.
|
|
500 |
set(_CORROSION_QUIET_OUTPUT_FLAG --quiet CACHE INTERNAL "")
|
|
501 |
endif()
|
|
502 |
|
|
503 |
set(_CORROSION_CARGO_VERSION ${Rust_CARGO_VERSION} CACHE INTERNAL "cargo version used by corrosion")
|
|
504 |
set(_CORROSION_RUST_CARGO_TARGET ${Rust_CARGO_TARGET} CACHE INTERNAL "target triple used by corrosion")
|
|
505 |
set(_CORROSION_RUST_CARGO_HOST_TARGET ${Rust_CARGO_HOST_TARGET} CACHE INTERNAL "host triple used by corrosion")
|
|
506 |
set(_CORROSION_RUSTC "${RUSTC_EXECUTABLE}" CACHE INTERNAL "Path to rustc used by corrosion")
|
|
507 |
set(_CORROSION_CARGO "${CARGO_EXECUTABLE}" CACHE INTERNAL "Path to cargo used by corrosion")
|
|
508 |
|
|
509 |
string(REPLACE "-" "_" _CORROSION_RUST_CARGO_TARGET_UNDERSCORE "${Rust_CARGO_TARGET}")
|
|
510 |
set(_CORROSION_RUST_CARGO_TARGET_UNDERSCORE "${_CORROSION_RUST_CARGO_TARGET_UNDERSCORE}" CACHE INTERNAL "lowercase target triple with underscores")
|
|
511 |
string(TOUPPER "${_CORROSION_RUST_CARGO_TARGET_UNDERSCORE}" _CORROSION_TARGET_TRIPLE_UPPER)
|
|
512 |
set(_CORROSION_RUST_CARGO_TARGET_UPPER
|
|
513 |
"${_CORROSION_TARGET_TRIPLE_UPPER}"
|
|
514 |
CACHE INTERNAL
|
|
515 |
"target triple in uppercase with underscore"
|
|
516 |
)
|
|
517 |
|
|
518 |
# We previously specified some Custom properties as part of our public API, however the chosen names prevented us from
|
|
519 |
# supporting CMake versions before 3.19. In order to both support older CMake versions and not break existing code
|
|
520 |
# immediately, we are using a different property name depending on the CMake version. However users avoid using
|
|
521 |
# any of the properties directly, as they are no longer part of the public API and are to be considered deprecated.
|
|
522 |
# Instead use the corrosion_set_... functions as documented in the Readme.
|
16038
|
523 |
set(_CORR_PROP_FEATURES CORROSION_FEATURES CACHE INTERNAL "")
|
|
524 |
set(_CORR_PROP_ALL_FEATURES CORROSION_ALL_FEATURES CACHE INTERNAL "")
|
|
525 |
set(_CORR_PROP_NO_DEFAULT_FEATURES CORROSION_NO_DEFAULT_FEATURES CACHE INTERNAL "")
|
|
526 |
set(_CORR_PROP_ENV_VARS CORROSION_ENVIRONMENT_VARIABLES CACHE INTERNAL "")
|
|
527 |
set(_CORR_PROP_HOST_BUILD CORROSION_USE_HOST_BUILD CACHE INTERNAL "")
|
16021
|
528 |
|
|
529 |
# Add custom command to build one target in a package (crate)
|
|
530 |
#
|
|
531 |
# A target may be either a specific bin
|
|
532 |
function(_add_cargo_build out_cargo_build_out_dir)
|
|
533 |
set(options NO_LINKER_OVERRIDE)
|
|
534 |
set(one_value_args PACKAGE TARGET MANIFEST_PATH WORKSPACE_MANIFEST_PATH)
|
|
535 |
set(multi_value_args BYPRODUCTS TARGET_KINDS)
|
|
536 |
cmake_parse_arguments(
|
|
537 |
ACB
|
|
538 |
"${options}"
|
|
539 |
"${one_value_args}"
|
|
540 |
"${multi_value_args}"
|
|
541 |
${ARGN}
|
|
542 |
)
|
|
543 |
|
|
544 |
if(DEFINED ACB_UNPARSED_ARGUMENTS)
|
|
545 |
message(FATAL_ERROR "Internal error - unexpected arguments: "
|
|
546 |
${ACB_UNPARSED_ARGUMENTS})
|
|
547 |
elseif(DEFINED ACB_KEYWORDS_MISSING_VALUES)
|
|
548 |
message(FATAL_ERROR "Internal error - missing values for the following arguments: "
|
|
549 |
${ACB_KEYWORDS_MISSING_VALUES})
|
|
550 |
endif()
|
|
551 |
|
|
552 |
set(package_name "${ACB_PACKAGE}")
|
|
553 |
set(target_name "${ACB_TARGET}")
|
|
554 |
set(path_to_toml "${ACB_MANIFEST_PATH}")
|
|
555 |
set(target_kinds "${ACB_TARGET_KINDS}")
|
|
556 |
set(workspace_manifest_path "${ACB_WORKSPACE_MANIFEST_PATH}")
|
16038
|
557 |
set(build_byproducts "${ACB_BYPRODUCTS}")
|
16021
|
558 |
|
16038
|
559 |
unset(cargo_rustc_crate_types)
|
16021
|
560 |
if(NOT target_kinds)
|
|
561 |
message(FATAL_ERROR "TARGET_KINDS not specified")
|
|
562 |
elseif("staticlib" IN_LIST target_kinds OR "cdylib" IN_LIST target_kinds)
|
|
563 |
set(cargo_rustc_filter "--lib")
|
16038
|
564 |
if("${Rust_VERSION}" VERSION_GREATER_EQUAL "1.64")
|
|
565 |
# https://doc.rust-lang.org/1.64.0/cargo/commands/cargo-rustc.html
|
|
566 |
# `--crate-type` is documented since Rust 1.64 for `cargo rustc`.
|
|
567 |
# We just unconditionally set it when available, to support overriding the crate type.
|
|
568 |
# Due to https://github.com/rust-lang/cargo/issues/14498 we can't use one argument and pass a
|
|
569 |
# comma seperated list. Instead we use multiple arguments.
|
|
570 |
set(cargo_rustc_crate_types "${target_kinds}")
|
|
571 |
list(TRANSFORM cargo_rustc_crate_types PREPEND "--crate-type=")
|
|
572 |
endif()
|
16021
|
573 |
elseif("bin" IN_LIST target_kinds)
|
|
574 |
set(cargo_rustc_filter "--bin=${target_name}")
|
|
575 |
else()
|
|
576 |
message(FATAL_ERROR "TARGET_KINDS contained unknown kind `${target_kind}`")
|
|
577 |
endif()
|
|
578 |
|
|
579 |
if (NOT IS_ABSOLUTE "${path_to_toml}")
|
|
580 |
set(path_to_toml "${CMAKE_SOURCE_DIR}/${path_to_toml}")
|
|
581 |
endif()
|
|
582 |
get_filename_component(workspace_toml_dir ${path_to_toml} DIRECTORY )
|
|
583 |
|
|
584 |
if (CMAKE_VS_PLATFORM_NAME)
|
|
585 |
set (build_dir "${CMAKE_VS_PLATFORM_NAME}/$<CONFIG>")
|
|
586 |
elseif(COR_IS_MULTI_CONFIG)
|
|
587 |
set (build_dir "$<CONFIG>")
|
|
588 |
else()
|
|
589 |
set (build_dir .)
|
|
590 |
endif()
|
|
591 |
|
|
592 |
# If a CMake sysroot is specified, forward it to the linker rustc invokes, too. CMAKE_SYSROOT is documented
|
|
593 |
# to be passed via --sysroot, so we assume that when it's set, the linker supports this option in that style.
|
|
594 |
if(CMAKE_CROSSCOMPILING AND CMAKE_SYSROOT)
|
|
595 |
set(corrosion_link_args "--sysroot=${CMAKE_SYSROOT}")
|
|
596 |
endif()
|
|
597 |
|
|
598 |
if(COR_ALL_FEATURES)
|
|
599 |
set(all_features_arg --all-features)
|
|
600 |
endif()
|
|
601 |
if(COR_NO_DEFAULT_FEATURES)
|
|
602 |
set(no_default_features_arg --no-default-features)
|
|
603 |
endif()
|
|
604 |
|
|
605 |
set(global_rustflags_target_property "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_RUSTFLAGS>>")
|
|
606 |
set(local_rustflags_target_property "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_LOCAL_RUSTFLAGS>>")
|
|
607 |
|
|
608 |
# todo: this probably should be TARGET_GENEX_EVAL
|
|
609 |
set(features_target_property "$<GENEX_EVAL:$<TARGET_PROPERTY:${target_name},${_CORR_PROP_FEATURES}>>")
|
|
610 |
set(features_genex "$<$<BOOL:${features_target_property}>:--features=$<JOIN:${features_target_property},$<COMMA>>>")
|
|
611 |
|
|
612 |
# target property overrides corrosion_import_crate argument
|
|
613 |
set(all_features_target_property "$<GENEX_EVAL:$<TARGET_PROPERTY:${target_name},${_CORR_PROP_ALL_FEATURES}>>")
|
|
614 |
set(all_features_arg "$<$<BOOL:${all_features_target_property}>:--all-features>")
|
|
615 |
|
|
616 |
set(no_default_features_target_property "$<GENEX_EVAL:$<TARGET_PROPERTY:${target_name},${_CORR_PROP_NO_DEFAULT_FEATURES}>>")
|
|
617 |
set(no_default_features_arg "$<$<BOOL:${no_default_features_target_property}>:--no-default-features>")
|
|
618 |
|
|
619 |
set(build_env_variable_genex "$<GENEX_EVAL:$<TARGET_PROPERTY:${target_name},${_CORR_PROP_ENV_VARS}>>")
|
|
620 |
set(hostbuild_override "$<BOOL:$<TARGET_PROPERTY:${target_name},${_CORR_PROP_HOST_BUILD}>>")
|
|
621 |
set(if_not_host_build_condition "$<NOT:${hostbuild_override}>")
|
|
622 |
|
|
623 |
set(corrosion_link_args "$<${if_not_host_build_condition}:${corrosion_link_args}>")
|
|
624 |
# We always set `--target`, so that cargo always places artifacts into a directory with the
|
|
625 |
# target triple.
|
|
626 |
set(cargo_target_option "--target=$<IF:${hostbuild_override},${_CORROSION_RUST_CARGO_HOST_TARGET},${_CORROSION_RUST_CARGO_TARGET}>")
|
|
627 |
|
|
628 |
# The target may be a filepath to custom target json file. For host targets we assume that they are built-in targets.
|
|
629 |
_corrosion_strip_target_triple(${_CORROSION_RUST_CARGO_TARGET} stripped_target_triple)
|
|
630 |
set(target_artifact_dir "$<IF:${hostbuild_override},${_CORROSION_RUST_CARGO_HOST_TARGET},${stripped_target_triple}>")
|
|
631 |
|
|
632 |
set(flags_genex "$<GENEX_EVAL:$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_CARGO_FLAGS>>")
|
|
633 |
|
|
634 |
set(explicit_linker_property "$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_LINKER>")
|
|
635 |
set(explicit_linker_defined "$<BOOL:${explicit_linker_property}>")
|
|
636 |
|
|
637 |
set(cargo_profile_target_property "$<TARGET_GENEX_EVAL:${target_name},$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_CARGO_PROFILE>>")
|
|
638 |
|
|
639 |
# Option to override the rustc/cargo binary to something other than the global default
|
|
640 |
set(rustc_override "$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_RUSTC>")
|
|
641 |
set(cargo_override "$<TARGET_PROPERTY:${target_name},INTERFACE_CORROSION_CARGO>")
|
|
642 |
set(rustc_bin "$<IF:$<BOOL:${rustc_override}>,${rustc_override},${_CORROSION_RUSTC}>")
|
|
643 |
set(cargo_bin "$<IF:$<BOOL:${cargo_override}>,${cargo_override},${_CORROSION_CARGO}>")
|
|
644 |
|
|
645 |
|
|
646 |
# Rust will add `-lSystem` as a flag for the linker on macOS. Adding the -L flag via RUSTFLAGS only fixes the
|
|
647 |
# problem partially - buildscripts still break, since they won't receive the RUSTFLAGS. This seems to only be a
|
|
648 |
# problem if we specify the linker ourselves (which we do, since this is necessary for e.g. linking C++ code).
|
|
649 |
# We can however set `LIBRARY_PATH`, which is propagated to the build-script-build properly.
|
|
650 |
if(NOT CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
|
651 |
# not needed anymore on macos 13 (and causes issues)
|
|
652 |
if(${CMAKE_SYSTEM_VERSION} VERSION_LESS 22)
|
|
653 |
set(cargo_library_path "LIBRARY_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib")
|
|
654 |
endif()
|
|
655 |
elseif(CMAKE_CROSSCOMPILING AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
|
|
656 |
if(${CMAKE_HOST_SYSTEM_VERSION} VERSION_LESS 22)
|
|
657 |
set(cargo_library_path "$<${hostbuild_override}:LIBRARY_PATH=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib>")
|
|
658 |
endif()
|
|
659 |
endif()
|
|
660 |
|
|
661 |
set(cargo_profile_set "$<BOOL:${cargo_profile_target_property}>")
|
|
662 |
# In the default case just specify --release or nothing to stay compatible with
|
|
663 |
# older rust versions.
|
|
664 |
set(default_profile_option "$<$<NOT:$<OR:$<CONFIG:Debug>,$<CONFIG:>>>:--release>")
|
|
665 |
# evaluates to either `--profile=<custom_profile>`, `--release` or nothing (for debug).
|
|
666 |
set(cargo_profile "$<IF:${cargo_profile_set},--profile=${cargo_profile_target_property},${default_profile_option}>")
|
|
667 |
|
|
668 |
# If the profile name is `dev` change the dir name to `debug`.
|
|
669 |
set(is_dev_profile "$<STREQUAL:${cargo_profile_target_property},dev>")
|
|
670 |
set(profile_dir_override "$<${is_dev_profile}:debug>")
|
|
671 |
set(profile_dir_is_overridden "$<BOOL:${profile_dir_override}>")
|
|
672 |
set(custom_profile_build_type_dir "$<IF:${profile_dir_is_overridden},${profile_dir_override},${cargo_profile_target_property}>")
|
|
673 |
|
|
674 |
set(default_build_type_dir "$<IF:$<OR:$<CONFIG:Debug>,$<CONFIG:>>,debug,release>")
|
|
675 |
set(build_type_dir "$<IF:${cargo_profile_set},${custom_profile_build_type_dir},${default_build_type_dir}>")
|
|
676 |
|
|
677 |
set(cargo_target_dir "${CMAKE_BINARY_DIR}/${build_dir}/cargo/build")
|
|
678 |
set(cargo_build_dir "${cargo_target_dir}/${target_artifact_dir}/${build_type_dir}")
|
|
679 |
set("${out_cargo_build_out_dir}" "${cargo_build_dir}" PARENT_SCOPE)
|
|
680 |
|
|
681 |
set(corrosion_cc_rs_flags)
|
|
682 |
|
|
683 |
if(CMAKE_C_COMPILER)
|
|
684 |
# This variable is read by cc-rs (often used in build scripts) to determine the c-compiler.
|
|
685 |
# It can still be overridden if the user sets the non underscore variant via the environment variables
|
|
686 |
# on the target.
|
|
687 |
list(APPEND corrosion_cc_rs_flags "CC_${_CORROSION_RUST_CARGO_TARGET_UNDERSCORE}=${CMAKE_C_COMPILER}")
|
|
688 |
endif()
|
|
689 |
if(CMAKE_CXX_COMPILER)
|
|
690 |
list(APPEND corrosion_cc_rs_flags "CXX_${_CORROSION_RUST_CARGO_TARGET_UNDERSCORE}=${CMAKE_CXX_COMPILER}")
|
|
691 |
endif()
|
|
692 |
# cc-rs doesn't seem to support `llvm-ar` (commandline syntax), wo we might as well just use
|
|
693 |
# the default AR.
|
|
694 |
if(CMAKE_AR AND NOT (Rust_CARGO_TARGET_ENV STREQUAL "msvc"))
|
|
695 |
list(APPEND corrosion_cc_rs_flags "AR_${_CORROSION_RUST_CARGO_TARGET_UNDERSCORE}=${CMAKE_AR}")
|
|
696 |
endif()
|
|
697 |
|
|
698 |
# Since we instruct cc-rs to use the compiler found by CMake, it is likely one that requires also
|
|
699 |
# specifying the target sysroot to use. CMake's generator makes sure to pass --sysroot with
|
|
700 |
# CMAKE_OSX_SYSROOT. Fortunately the compilers Apple ships also respect the SDKROOT environment
|
|
701 |
# variable, which we can set for use when cc-rs invokes the compiler.
|
|
702 |
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_SYSROOT)
|
|
703 |
list(APPEND corrosion_cc_rs_flags "SDKROOT=${CMAKE_OSX_SYSROOT}")
|
|
704 |
endif()
|
|
705 |
|
16038
|
706 |
# Ensure that cc-rs targets same Apple platform version as the CMake build
|
|
707 |
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_OSX_DEPLOYMENT_TARGET)
|
|
708 |
list(APPEND corrosion_cc_rs_flags "MACOSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET}")
|
|
709 |
endif()
|
|
710 |
|
16021
|
711 |
corrosion_add_target_local_rustflags("${target_name}" "$<$<BOOL:${corrosion_link_args}>:-Clink-args=${corrosion_link_args}>")
|
|
712 |
|
|
713 |
# todo: this should probably also be guarded by if_not_host_build_condition.
|
|
714 |
if(COR_NO_STD)
|
|
715 |
corrosion_add_target_local_rustflags("${target_name}" "-Cdefault-linker-libraries=no")
|
|
716 |
else()
|
|
717 |
corrosion_add_target_local_rustflags("${target_name}" "-Cdefault-linker-libraries=yes")
|
|
718 |
endif()
|
|
719 |
|
|
720 |
set(global_joined_rustflags "$<JOIN:${global_rustflags_target_property}, >")
|
|
721 |
set(global_rustflags_genex "$<$<BOOL:${global_rustflags_target_property}>:RUSTFLAGS=${global_joined_rustflags}>")
|
|
722 |
set(local_rustflags_delimiter "$<$<BOOL:${local_rustflags_target_property}>:-->")
|
|
723 |
set(local_rustflags_genex "$<$<BOOL:${local_rustflags_target_property}>:${local_rustflags_target_property}>")
|
|
724 |
|
|
725 |
set(deps_link_languages_prop "$<TARGET_PROPERTY:_cargo-build_${target_name},CARGO_DEPS_LINKER_LANGUAGES>")
|
|
726 |
set(deps_link_languages "$<TARGET_GENEX_EVAL:_cargo-build_${target_name},${deps_link_languages_prop}>")
|
|
727 |
set(target_uses_cxx "$<IN_LIST:CXX,${deps_link_languages}>")
|
|
728 |
unset(default_linker)
|
|
729 |
# With the MSVC ABI rustc only supports directly invoking the linker - Invoking cl as the linker driver is not supported.
|
|
730 |
if(NOT (Rust_CARGO_TARGET_ENV STREQUAL "msvc" OR COR_NO_LINKER_OVERRIDE))
|
|
731 |
set(default_linker "$<IF:$<BOOL:${target_uses_cxx}>,${CMAKE_CXX_COMPILER},${CMAKE_C_COMPILER}>")
|
|
732 |
endif()
|
|
733 |
# Used to set a linker for a specific target-triple.
|
|
734 |
set(cargo_target_linker_var "CARGO_TARGET_${_CORROSION_RUST_CARGO_TARGET_UPPER}_LINKER")
|
|
735 |
set(linker "$<IF:${explicit_linker_defined},${explicit_linker_property},${default_linker}>")
|
|
736 |
set(cargo_target_linker $<$<BOOL:${linker}>:${cargo_target_linker_var}=${linker}>)
|
|
737 |
|
|
738 |
if(Rust_CROSSCOMPILING AND (CMAKE_C_COMPILER_TARGET OR CMAKE_CXX_COMPILER_TARGET))
|
|
739 |
set(linker_target_triple "$<IF:$<BOOL:${target_uses_cxx}>,${CMAKE_CXX_COMPILER_TARGET},${CMAKE_C_COMPILER_TARGET}>")
|
|
740 |
set(rustflag_linker_arg "-Clink-args=--target=${linker_target_triple}")
|
|
741 |
set(rustflag_linker_arg "$<${if_not_host_build_condition}:${rustflag_linker_arg}>")
|
|
742 |
# Skip adding the linker argument, if the linker is explicitly set, since the
|
|
743 |
# explicit_linker_property will not be set when this function runs.
|
|
744 |
# Passing this rustflag is necessary for clang.
|
|
745 |
corrosion_add_target_local_rustflags("${target_name}" "$<$<NOT:${explicit_linker_defined}>:${rustflag_linker_arg}>")
|
|
746 |
endif()
|
|
747 |
|
16038
|
748 |
message(DEBUG "TARGET ${target_name} produces byproducts ${build_byproducts}")
|
16021
|
749 |
|
|
750 |
add_custom_target(
|
|
751 |
_cargo-build_${target_name}
|
|
752 |
# Build crate
|
|
753 |
COMMAND
|
|
754 |
${CMAKE_COMMAND} -E env
|
|
755 |
"${build_env_variable_genex}"
|
|
756 |
"${global_rustflags_genex}"
|
|
757 |
"${cargo_target_linker}"
|
|
758 |
"${corrosion_cc_rs_flags}"
|
|
759 |
"${cargo_library_path}"
|
|
760 |
"CORROSION_BUILD_DIR=${CMAKE_CURRENT_BINARY_DIR}"
|
|
761 |
"CARGO_BUILD_RUSTC=${rustc_bin}"
|
|
762 |
"${cargo_bin}"
|
|
763 |
rustc
|
|
764 |
${cargo_rustc_filter}
|
|
765 |
${cargo_target_option}
|
|
766 |
${_CORROSION_VERBOSE_OUTPUT_FLAG}
|
|
767 |
${all_features_arg}
|
|
768 |
${no_default_features_arg}
|
|
769 |
${features_genex}
|
|
770 |
--package ${package_name}
|
16038
|
771 |
${cargo_rustc_crate_types}
|
16021
|
772 |
--manifest-path "${path_to_toml}"
|
|
773 |
--target-dir "${cargo_target_dir}"
|
|
774 |
${cargo_profile}
|
|
775 |
${flags_genex}
|
|
776 |
# Any arguments to cargo must be placed before this line
|
|
777 |
${local_rustflags_delimiter}
|
|
778 |
${local_rustflags_genex}
|
|
779 |
|
16038
|
780 |
# Note: `BYPRODUCTS` may not contain **target specific** generator expressions.
|
|
781 |
# This means we cannot use `${cargo_build_dir}`, since it currently uses `$<TARGET_PROPERTY>`
|
|
782 |
# to determine the correct target directory, depending on if the hostbuild target property is
|
|
783 |
# set or not.
|
|
784 |
# BYPRODUCTS "${cargo_build_dir}/${build_byproducts}"
|
16021
|
785 |
# The build is conducted in the directory of the Manifest, so that configuration files such as
|
|
786 |
# `.cargo/config.toml` or `toolchain.toml` are applied as expected.
|
|
787 |
WORKING_DIRECTORY "${workspace_toml_dir}"
|
|
788 |
USES_TERMINAL
|
|
789 |
COMMAND_EXPAND_LISTS
|
|
790 |
VERBATIM
|
|
791 |
)
|
|
792 |
|
|
793 |
# User exposed custom target, that depends on the internal target.
|
|
794 |
# Corrosion post build steps are added on the internal target, which
|
|
795 |
# ensures that they run before any user defined post build steps on this
|
|
796 |
# target.
|
|
797 |
add_custom_target(
|
|
798 |
cargo-build_${target_name}
|
|
799 |
ALL
|
|
800 |
)
|
|
801 |
add_dependencies(cargo-build_${target_name} _cargo-build_${target_name})
|
|
802 |
|
|
803 |
# Add custom target before actual build that user defined custom commands (e.g. code generators) can
|
|
804 |
# use as a hook to do something before the build. This mainly exists to not expose the `_cargo-build` targets.
|
|
805 |
add_custom_target(cargo-prebuild_${target_name})
|
|
806 |
add_dependencies(_cargo-build_${target_name} cargo-prebuild_${target_name})
|
|
807 |
if(NOT TARGET cargo-prebuild)
|
|
808 |
add_custom_target(cargo-prebuild)
|
|
809 |
endif()
|
|
810 |
add_dependencies(cargo-prebuild cargo-prebuild_${target_name})
|
|
811 |
|
|
812 |
add_custom_target(
|
|
813 |
cargo-clean_${target_name}
|
|
814 |
COMMAND
|
|
815 |
"${cargo_bin}" clean ${cargo_target_option}
|
|
816 |
-p ${package_name} --manifest-path ${path_to_toml}
|
|
817 |
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/${build_dir}
|
|
818 |
USES_TERMINAL
|
|
819 |
)
|
|
820 |
|
|
821 |
if (NOT TARGET cargo-clean)
|
|
822 |
add_custom_target(cargo-clean)
|
|
823 |
endif()
|
|
824 |
add_dependencies(cargo-clean cargo-clean_${target_name})
|
|
825 |
endfunction()
|
|
826 |
|
|
827 |
#[=======================================================================[.md:
|
|
828 |
ANCHOR: corrosion-import-crate
|
|
829 |
```cmake
|
|
830 |
corrosion_import_crate(
|
|
831 |
MANIFEST_PATH <path/to/cargo.toml>
|
|
832 |
[ALL_FEATURES]
|
|
833 |
[NO_DEFAULT_FEATURES]
|
|
834 |
[NO_STD]
|
|
835 |
[NO_LINKER_OVERRIDE]
|
|
836 |
[LOCKED]
|
|
837 |
[FROZEN]
|
|
838 |
[PROFILE <cargo-profile>]
|
|
839 |
[IMPORTED_CRATES <variable-name>]
|
|
840 |
[CRATE_TYPES <crate_type1> ... <crate_typeN>]
|
16038
|
841 |
[OVERRIDE_CRATE_TYPE <crate_name>=<crate_type1,crate_type2,...> ...]
|
16021
|
842 |
[CRATES <crate1> ... <crateN>]
|
|
843 |
[FEATURES <feature1> ... <featureN>]
|
|
844 |
[FLAGS <flag1> ... <flagN>]
|
|
845 |
)
|
|
846 |
```
|
|
847 |
* **MANIFEST_PATH**: Path to a [Cargo.toml Manifest] file.
|
|
848 |
* **ALL_FEATURES**: Equivalent to [--all-features] passed to cargo build
|
|
849 |
* **NO_DEFAULT_FEATURES**: Equivalent to [--no-default-features] passed to cargo build
|
|
850 |
* **NO_STD**: Disable linking of standard libraries (required for no_std crates).
|
|
851 |
* **NO_LINKER_OVERRIDE**: Will let Rust/Cargo determine which linker to use instead of corrosion (when linking is invoked by Rust)
|
|
852 |
* **LOCKED**: Pass [`--locked`] to cargo build and cargo metadata.
|
|
853 |
* **FROZEN**: Pass [`--frozen`] to cargo build and cargo metadata.
|
|
854 |
* **PROFILE**: Specify cargo build profile (`dev`/`release` or a [custom profile]; `bench` and `test` are not supported)
|
|
855 |
* **IMPORTED_CRATES**: Save the list of imported crates into the variable with the provided name in the current scope.
|
|
856 |
* **CRATE_TYPES**: Only import the specified crate types. Valid values: `staticlib`, `cdylib`, `bin`.
|
16038
|
857 |
* **OVERRIDE_CRATE_TYPE**: Override the crate-types of a cargo crate with the given comma-separated values.
|
|
858 |
Internally uses the `rustc` flag [`--crate-type`] to override the crate-type.
|
|
859 |
Valid values for the crate types are the library types `staticlib` and `cdylib`.
|
16021
|
860 |
* **CRATES**: Only import the specified crates from a workspace. Values: Crate names.
|
|
861 |
* **FEATURES**: Enable the specified features. Equivalent to [--features] passed to `cargo build`.
|
|
862 |
* **FLAGS**: Arbitrary flags to `cargo build`.
|
|
863 |
|
|
864 |
[custom profile]: https://doc.rust-lang.org/cargo/reference/profiles.html#custom-profiles
|
|
865 |
[--all-features]: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options
|
|
866 |
[--no-default-features]: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options
|
|
867 |
[--features]: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options
|
|
868 |
[`--locked`]: https://doc.rust-lang.org/cargo/commands/cargo.html#manifest-options
|
|
869 |
[`--frozen`]: https://doc.rust-lang.org/cargo/commands/cargo.html#manifest-options
|
16038
|
870 |
[`--crate-type`]: https://doc.rust-lang.org/rustc/command-line-arguments.html#--crate-type-a-list-of-types-of-crates-for-the-compiler-to-emit
|
16021
|
871 |
[Cargo.toml Manifest]: https://doc.rust-lang.org/cargo/appendix/glossary.html#manifest
|
|
872 |
|
|
873 |
ANCHOR_END: corrosion-import-crate
|
|
874 |
#]=======================================================================]
|
|
875 |
function(corrosion_import_crate)
|
|
876 |
set(OPTIONS ALL_FEATURES NO_DEFAULT_FEATURES NO_STD NO_LINKER_OVERRIDE LOCKED FROZEN)
|
|
877 |
set(ONE_VALUE_KEYWORDS MANIFEST_PATH PROFILE IMPORTED_CRATES)
|
16038
|
878 |
set(MULTI_VALUE_KEYWORDS CRATE_TYPES CRATES FEATURES FLAGS OVERRIDE_CRATE_TYPE)
|
16021
|
879 |
cmake_parse_arguments(COR "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}" ${ARGN})
|
|
880 |
list(APPEND CMAKE_MESSAGE_CONTEXT "corrosion_import_crate")
|
|
881 |
|
|
882 |
if(DEFINED COR_UNPARSED_ARGUMENTS)
|
|
883 |
message(AUTHOR_WARNING "Unexpected arguments: " ${COR_UNPARSED_ARGUMENTS}
|
|
884 |
"\nCorrosion will ignore these unexpected arguments."
|
|
885 |
)
|
|
886 |
endif()
|
|
887 |
if(DEFINED COR_KEYWORDS_MISSING_VALUES)
|
|
888 |
message(DEBUG "Note: the following keywords passed to corrosion_import_crate had no associated value(s): "
|
|
889 |
${COR_KEYWORDS_MISSING_VALUES}
|
|
890 |
)
|
|
891 |
endif()
|
|
892 |
if (NOT DEFINED COR_MANIFEST_PATH)
|
|
893 |
message(FATAL_ERROR "MANIFEST_PATH is a required keyword to corrosion_add_crate")
|
|
894 |
endif()
|
|
895 |
_corrosion_option_passthrough_helper(NO_LINKER_OVERRIDE COR no_linker_override)
|
|
896 |
_corrosion_option_passthrough_helper(LOCKED COR locked)
|
|
897 |
_corrosion_option_passthrough_helper(FROZEN COR frozen)
|
|
898 |
_corrosion_arg_passthrough_helper(CRATES COR crate_allowlist)
|
|
899 |
_corrosion_arg_passthrough_helper(CRATE_TYPES COR crate_types)
|
|
900 |
|
|
901 |
if(COR_PROFILE)
|
|
902 |
if(Rust_VERSION VERSION_LESS 1.57.0)
|
|
903 |
message(FATAL_ERROR "Selecting custom profiles via `PROFILE` requires at least rust 1.57.0, but you "
|
|
904 |
"have ${Rust_VERSION}."
|
|
905 |
)
|
|
906 |
# The profile name could be part of a Generator expression, so this won't catch all occurences.
|
|
907 |
# Since it is hard to add an error message for genex, we don't do that here.
|
|
908 |
elseif("${COR_PROFILE}" STREQUAL "test" OR "${COR_PROFILE}" STREQUAL "bench")
|
|
909 |
message(FATAL_ERROR "Corrosion does not support building Rust crates with the cargo profiles"
|
|
910 |
" `test` or `bench`. These profiles add a hash to the output artifact name that we"
|
|
911 |
" cannot predict. Please consider using a custom cargo profile which inherits from the"
|
|
912 |
" built-in profile instead."
|
|
913 |
)
|
|
914 |
endif()
|
|
915 |
endif()
|
|
916 |
|
16038
|
917 |
# intended to be used with foreach(... ZIP_LISTS ...), meaning
|
|
918 |
# that the crate_types at index i of `override_crate_type_types_list` are
|
|
919 |
# for the package_name at index i of `override_crate_type_package_name_list`.
|
|
920 |
# It would really be nice if CMake had structs or dicts.
|
|
921 |
unset(override_crate_type_package_name_list)
|
|
922 |
unset(override_crate_type_types_list)
|
|
923 |
unset(OVERRIDE_CRATE_TYPE_ARGS)
|
|
924 |
if(DEFINED COR_OVERRIDE_CRATE_TYPE)
|
|
925 |
string(JOIN " " usage_help
|
|
926 |
"Each argument to OVERRIDE_CRATE_TYPE must be of the form `<package_name>=<crate_type(s)>."
|
|
927 |
"The package_name must be a valid cargo package name and the crate_type must be "
|
|
928 |
"a comma-seperated list with valid values being `staticlib`, `cdylib` and `bin`"
|
|
929 |
)
|
|
930 |
foreach(entry IN LISTS COR_OVERRIDE_CRATE_TYPE)
|
|
931 |
string(REPLACE "=" ";" key_val_list ${entry})
|
|
932 |
list(LENGTH key_val_list key_val_list_len)
|
|
933 |
if(NOT key_val_list_len EQUAL "2")
|
|
934 |
message(FATAL_ERROR "Invalid argument: `${entry}` for parameter OVERRIDE_CRATE_TYPE!\n"
|
|
935 |
"${usage_help}"
|
|
936 |
)
|
|
937 |
endif()
|
|
938 |
list(GET key_val_list "0" package_name)
|
|
939 |
list(GET key_val_list "1" crate_types)
|
|
940 |
list(APPEND override_crate_type_package_name_list "${package_name}")
|
|
941 |
list(APPEND override_crate_type_types_list "${crate_types}")
|
|
942 |
endforeach()
|
|
943 |
list(LENGTH override_crate_type_package_name_list num_override_packages)
|
|
944 |
list(LENGTH override_crate_type_types_list num_override_packages2)
|
|
945 |
if("${Rust_VERSION}" VERSION_LESS "1.64")
|
|
946 |
message(WARNING "OVERRIDE_CRATE_TYPE requires at Rust 1.64 or newer. Ignoring the option")
|
|
947 |
elseif(NOT num_override_packages EQUAL num_override_packages2)
|
|
948 |
message(WARNING "Internal error while parsing OVERRIDE_CRATE_TYPE arguments.\n"
|
|
949 |
"Corrosion will ignore this argument and continue."
|
|
950 |
)
|
|
951 |
else()
|
|
952 |
# Pass by ref: we intentionally pass the list names here!
|
|
953 |
set(override_crate_types_arg "OVERRIDE_CRATE_TYPE_ARGS" "override_crate_type_package_name_list" "override_crate_type_types_list")
|
|
954 |
endif()
|
|
955 |
endif()
|
|
956 |
|
16021
|
957 |
if (NOT IS_ABSOLUTE "${COR_MANIFEST_PATH}")
|
|
958 |
set(COR_MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${COR_MANIFEST_PATH})
|
|
959 |
endif()
|
|
960 |
|
|
961 |
set(additional_cargo_flags ${COR_FLAGS})
|
|
962 |
|
|
963 |
if(COR_LOCKED AND NOT "--locked" IN_LIST additional_cargo_flags)
|
|
964 |
list(APPEND additional_cargo_flags "--locked")
|
|
965 |
endif()
|
|
966 |
if(COR_FROZEN AND NOT "--frozen" IN_LIST additional_cargo_flags)
|
|
967 |
list(APPEND additional_cargo_flags "--frozen")
|
|
968 |
endif()
|
|
969 |
|
|
970 |
set(imported_crates "")
|
|
971 |
|
16038
|
972 |
_generator_add_cargo_targets(
|
|
973 |
MANIFEST_PATH
|
|
974 |
"${COR_MANIFEST_PATH}"
|
|
975 |
IMPORTED_CRATES
|
|
976 |
imported_crates
|
|
977 |
${crate_allowlist}
|
|
978 |
${crate_types}
|
|
979 |
${no_linker_override}
|
|
980 |
${override_crate_types_arg}
|
|
981 |
)
|
16021
|
982 |
|
|
983 |
# Not target props yet:
|
|
984 |
# NO_STD
|
|
985 |
# NO_LINKER_OVERRIDE # We could simply zero INTERFACE_CORROSION_LINKER if this is set.
|
|
986 |
# LOCKED / FROZEN get merged into FLAGS after cargo metadata.
|
|
987 |
|
|
988 |
# Initialize the target properties with the arguments to corrosion_import_crate.
|
|
989 |
set_target_properties(
|
|
990 |
${imported_crates}
|
|
991 |
PROPERTIES
|
|
992 |
"${_CORR_PROP_ALL_FEATURES}" "${COR_ALL_FEATURES}"
|
|
993 |
"${_CORR_PROP_NO_DEFAULT_FEATURES}" "${COR_NO_DEFAULT_FEATURES}"
|
|
994 |
"${_CORR_PROP_FEATURES}" "${COR_FEATURES}"
|
|
995 |
INTERFACE_CORROSION_CARGO_PROFILE "${COR_PROFILE}"
|
|
996 |
INTERFACE_CORROSION_CARGO_FLAGS "${additional_cargo_flags}"
|
|
997 |
)
|
|
998 |
|
|
999 |
# _CORR_PROP_ENV_VARS
|
|
1000 |
if(DEFINED COR_IMPORTED_CRATES)
|
|
1001 |
set(${COR_IMPORTED_CRATES} ${imported_crates} PARENT_SCOPE)
|
|
1002 |
endif()
|
|
1003 |
endfunction()
|
|
1004 |
|
|
1005 |
function(corrosion_set_linker target_name linker)
|
|
1006 |
if(NOT linker)
|
|
1007 |
message(FATAL_ERROR "The linker passed to `corrosion_set_linker` may not be empty")
|
|
1008 |
elseif(NOT TARGET "${target_name}")
|
|
1009 |
message(FATAL_ERROR "The target `${target_name}` does not exist.")
|
|
1010 |
endif()
|
|
1011 |
if(MSVC)
|
|
1012 |
message(WARNING "Explicitly setting the linker with the MSVC toolchain is currently not supported and ignored")
|
|
1013 |
endif()
|
|
1014 |
|
|
1015 |
if(TARGET "${target_name}-static" AND NOT TARGET "${target_name}-shared")
|
|
1016 |
message(WARNING "The target ${target_name} builds a static library."
|
|
1017 |
"The linker is never invoked for a static library so specifying a linker has no effect."
|
|
1018 |
)
|
|
1019 |
endif()
|
|
1020 |
|
|
1021 |
set_property(
|
|
1022 |
TARGET ${target_name}
|
|
1023 |
PROPERTY INTERFACE_CORROSION_LINKER "${linker}"
|
|
1024 |
)
|
|
1025 |
endfunction()
|
|
1026 |
|
|
1027 |
function(corrosion_set_hostbuild target_name)
|
|
1028 |
# Configure the target to be compiled for the Host target and ignore any cross-compile configuration.
|
|
1029 |
set_property(
|
|
1030 |
TARGET ${target_name}
|
|
1031 |
PROPERTY ${_CORR_PROP_HOST_BUILD} 1
|
|
1032 |
)
|
|
1033 |
endfunction()
|
|
1034 |
|
|
1035 |
# Add flags for rustc (RUSTFLAGS) which affect the target and all of it's Rust dependencies
|
|
1036 |
#
|
|
1037 |
# Additional rustflags may be passed as optional parameters after rustflag.
|
|
1038 |
# Please note, that if you import multiple targets from a package or workspace, but set different
|
|
1039 |
# Rustflags via this function, the Rust dependencies will have to be rebuilt when changing targets.
|
|
1040 |
# Consider `corrosion_add_target_local_rustflags()` as an alternative to avoid this.
|
|
1041 |
function(corrosion_add_target_rustflags target_name rustflag)
|
|
1042 |
# Additional rustflags may be passed as optional parameters after rustflag.
|
|
1043 |
set_property(
|
|
1044 |
TARGET ${target_name}
|
|
1045 |
APPEND
|
|
1046 |
PROPERTY INTERFACE_CORROSION_RUSTFLAGS ${rustflag} ${ARGN}
|
|
1047 |
)
|
|
1048 |
endfunction()
|
|
1049 |
|
|
1050 |
# Add flags for rustc (RUSTFLAGS) which only affect the target, but none of it's (Rust) dependencies
|
|
1051 |
#
|
|
1052 |
# Additional rustflags may be passed as optional parameters after rustc_flag.
|
|
1053 |
function(corrosion_add_target_local_rustflags target_name rustc_flag)
|
|
1054 |
# Set Rustflags via `cargo rustc` which only affect the current crate, but not dependencies.
|
|
1055 |
set_property(
|
|
1056 |
TARGET ${target_name}
|
|
1057 |
APPEND
|
|
1058 |
PROPERTY INTERFACE_CORROSION_LOCAL_RUSTFLAGS ${rustc_flag} ${ARGN}
|
|
1059 |
)
|
|
1060 |
endfunction()
|
|
1061 |
|
|
1062 |
function(corrosion_set_env_vars target_name env_var)
|
|
1063 |
# Additional environment variables may be passed as optional parameters after env_var.
|
|
1064 |
set_property(
|
|
1065 |
TARGET ${target_name}
|
|
1066 |
APPEND
|
|
1067 |
PROPERTY ${_CORR_PROP_ENV_VARS} ${env_var} ${ARGN}
|
|
1068 |
)
|
|
1069 |
endfunction()
|
|
1070 |
|
|
1071 |
function(corrosion_set_cargo_flags target_name)
|
|
1072 |
# corrosion_set_cargo_flags(<target_name> [<flag1> ... ])
|
|
1073 |
|
|
1074 |
set_property(
|
|
1075 |
TARGET ${target_name}
|
|
1076 |
APPEND
|
|
1077 |
PROPERTY INTERFACE_CORROSION_CARGO_FLAGS ${ARGN}
|
|
1078 |
)
|
|
1079 |
endfunction()
|
|
1080 |
|
|
1081 |
function(corrosion_set_features target_name)
|
|
1082 |
# corrosion_set_features(<target_name> [ALL_FEATURES=Bool] [NO_DEFAULT_FEATURES] [FEATURES <feature1> ... ])
|
|
1083 |
set(options NO_DEFAULT_FEATURES)
|
|
1084 |
set(one_value_args ALL_FEATURES)
|
|
1085 |
set(multi_value_args FEATURES)
|
|
1086 |
cmake_parse_arguments(
|
|
1087 |
PARSE_ARGV 1
|
|
1088 |
SET
|
|
1089 |
"${options}"
|
|
1090 |
"${one_value_args}"
|
|
1091 |
"${multi_value_args}"
|
|
1092 |
)
|
|
1093 |
|
|
1094 |
if(DEFINED SET_ALL_FEATURES)
|
|
1095 |
set_property(
|
|
1096 |
TARGET ${target_name}
|
|
1097 |
PROPERTY ${_CORR_PROP_ALL_FEATURES} ${SET_ALL_FEATURES}
|
|
1098 |
)
|
|
1099 |
endif()
|
|
1100 |
if(SET_NO_DEFAULT_FEATURES)
|
|
1101 |
set_property(
|
|
1102 |
TARGET ${target_name}
|
|
1103 |
PROPERTY ${_CORR_PROP_NO_DEFAULT_FEATURES} 1
|
|
1104 |
)
|
|
1105 |
endif()
|
|
1106 |
if(SET_FEATURES)
|
|
1107 |
set_property(
|
|
1108 |
TARGET ${target_name}
|
|
1109 |
APPEND
|
|
1110 |
PROPERTY ${_CORR_PROP_FEATURES} ${SET_FEATURES}
|
|
1111 |
)
|
|
1112 |
endif()
|
|
1113 |
endfunction()
|
|
1114 |
|
|
1115 |
function(corrosion_link_libraries target_name)
|
|
1116 |
if(TARGET "${target_name}-static")
|
|
1117 |
message(DEBUG "The target ${target_name} builds a static Rust library."
|
|
1118 |
"Calling `target_link_libraries()` instead."
|
|
1119 |
)
|
|
1120 |
target_link_libraries("${target_name}-static" INTERFACE ${ARGN})
|
|
1121 |
if(NOT TARGET "${target_name}-shared")
|
|
1122 |
# Early return, since Rust won't invoke the linker for static libraries
|
|
1123 |
return()
|
|
1124 |
endif()
|
|
1125 |
endif()
|
|
1126 |
foreach(library ${ARGN})
|
|
1127 |
set_property(
|
|
1128 |
TARGET _cargo-build_${target_name}
|
|
1129 |
APPEND
|
|
1130 |
PROPERTY CARGO_DEPS_LINKER_LANGUAGES
|
|
1131 |
$<TARGET_PROPERTY:${library},LINKER_LANGUAGE>
|
|
1132 |
)
|
|
1133 |
|
16038
|
1134 |
if (TARGET "${library}")
|
|
1135 |
corrosion_add_target_local_rustflags(${target_name}
|
|
1136 |
"-L$<TARGET_LINKER_FILE_DIR:${library}>"
|
|
1137 |
"-l$<TARGET_LINKER_FILE_BASE_NAME:${library}>"
|
|
1138 |
)
|
|
1139 |
add_dependencies(_cargo-build_${target_name} ${library})
|
|
1140 |
elseif(IS_ABSOLUTE "${library}")
|
|
1141 |
# Linking via full path (See https://doc.rust-lang.org/rustc/command-line-arguments.html#linking-modifiers-verbatim)
|
|
1142 |
corrosion_add_target_local_rustflags(${target_name} "-Clink-arg=${library}")
|
|
1143 |
else()
|
|
1144 |
# We have to assume ${library} is a non-CMake library name
|
|
1145 |
corrosion_add_target_local_rustflags(${target_name} "-l${library}")
|
|
1146 |
endif()
|
16021
|
1147 |
endforeach()
|
|
1148 |
endfunction()
|
|
1149 |
|
16038
|
1150 |
#[=======================================================================[.md:
|
|
1151 |
ANCHOR: corrosion-install
|
|
1152 |
** EXPERIMENTAL **: This function is currently still considered experimental
|
|
1153 |
and is not officially released yet. Feedback and Suggestions are welcome.
|
|
1154 |
|
|
1155 |
```cmake
|
|
1156 |
corrosion_install(TARGETS <target1> ... <targetN> [EXPORT <export-name>]
|
|
1157 |
[[ARCHIVE|LIBRARY|RUNTIME|PUBLIC_HEADER]
|
|
1158 |
[DESTINATION <dir>]
|
|
1159 |
[PERMISSIONS <permissions...>]
|
|
1160 |
[CONFIGURATIONS [Debug|Release|<other-configuration>]]
|
|
1161 |
] [...])
|
|
1162 |
```
|
|
1163 |
* **TARGETS**: Target or targets to install.
|
|
1164 |
* **EXPORT**: Creates an export that can be installed with `install(EXPORT)`. <export-name> must be globally unique.
|
|
1165 |
Also creates a file at ${CMAKE_BINARY_DIR}/corrosion/<export-name>Corrosion.cmake that must be included in the installed config file.
|
|
1166 |
* **ARCHIVE**/**LIBRARY**/**RUNTIME**/PUBLIC_HEADER: Designates that the following settings only apply to that specific type of object.
|
|
1167 |
* **DESTINATION**: The subdirectory within the CMAKE_INSTALL_PREFIX that a specific object should be placed. Defaults to values from GNUInstallDirs.
|
|
1168 |
* **PERMISSIONS**: The permissions of files copied into the install prefix.
|
|
1169 |
|
|
1170 |
Any `PUBLIC` or `INTERFACE` [file sets] will be installed.
|
|
1171 |
|
|
1172 |
[file sets]: https://cmake.org/cmake/help/latest/command/target_sources.html#file-sets
|
|
1173 |
|
|
1174 |
ANCHOR_END: corrosion-install
|
|
1175 |
#]=======================================================================]
|
16021
|
1176 |
function(corrosion_install)
|
|
1177 |
# Default install dirs
|
|
1178 |
include(GNUInstallDirs)
|
|
1179 |
|
|
1180 |
# Parse arguments to corrosion_install
|
|
1181 |
list(GET ARGN 0 INSTALL_TYPE)
|
|
1182 |
list(REMOVE_AT ARGN 0)
|
|
1183 |
|
|
1184 |
# The different install types that are supported. Some targets may have more than one of these
|
|
1185 |
# types. For example, on Windows, a shared library will have both an ARCHIVE component and a
|
|
1186 |
# RUNTIME component.
|
|
1187 |
set(INSTALL_TARGET_TYPES ARCHIVE LIBRARY RUNTIME PRIVATE_HEADER PUBLIC_HEADER)
|
|
1188 |
|
|
1189 |
# Arguments to each install target type
|
|
1190 |
set(OPTIONS)
|
|
1191 |
set(ONE_VALUE_ARGS DESTINATION)
|
|
1192 |
set(MULTI_VALUE_ARGS PERMISSIONS CONFIGURATIONS)
|
|
1193 |
set(TARGET_ARGS ${OPTIONS} ${ONE_VALUE_ARGS} ${MULTI_VALUE_ARGS})
|
|
1194 |
|
|
1195 |
if (INSTALL_TYPE STREQUAL "TARGETS")
|
|
1196 |
# Extract targets
|
|
1197 |
set(INSTALL_TARGETS)
|
|
1198 |
list(LENGTH ARGN ARGN_LENGTH)
|
|
1199 |
set(DELIMITERS EXPORT ${INSTALL_TARGET_TYPES} ${TARGET_ARGS})
|
|
1200 |
while(ARGN_LENGTH)
|
|
1201 |
# If we hit another keyword, stop - we've found all the targets
|
|
1202 |
list(GET ARGN 0 FRONT)
|
|
1203 |
if (FRONT IN_LIST DELIMITERS)
|
|
1204 |
break()
|
|
1205 |
endif()
|
|
1206 |
|
|
1207 |
list(APPEND INSTALL_TARGETS ${FRONT})
|
|
1208 |
list(REMOVE_AT ARGN 0)
|
|
1209 |
|
|
1210 |
# Update ARGN_LENGTH
|
|
1211 |
list(LENGTH ARGN ARGN_LENGTH)
|
|
1212 |
endwhile()
|
|
1213 |
|
|
1214 |
# Check if there are any args left before proceeding
|
|
1215 |
list(LENGTH ARGN ARGN_LENGTH)
|
|
1216 |
if (ARGN_LENGTH)
|
|
1217 |
list(GET ARGN 0 FRONT)
|
|
1218 |
if (FRONT STREQUAL "EXPORT")
|
|
1219 |
list(REMOVE_AT ARGN 0) # Pop "EXPORT"
|
|
1220 |
|
|
1221 |
list(GET ARGN 0 EXPORT_NAME)
|
|
1222 |
list(REMOVE_AT ARGN 0) # Pop <export-name>
|
16038
|
1223 |
set(EXTRA_TARGETS_EXPORT_NAME ${EXPORT_NAME}Corrosion.cmake)
|
|
1224 |
set(EXPORT_NAME EXPORT ${EXPORT_NAME})
|
|
1225 |
set(EXPORT_FILE_PATH "${CMAKE_BINARY_DIR}/corrosion/${EXTRA_TARGETS_EXPORT_NAME}")
|
|
1226 |
# Remove first, since otherwise we will append to the file on every reconfigure.
|
|
1227 |
# Assumes that the corrosion_install will only be called once for a given EXPORT_NAME.
|
|
1228 |
file(REMOVE "${EXPORT_FILE_PATH}")
|
16021
|
1229 |
endif()
|
16038
|
1230 |
else()
|
|
1231 |
# Prevent variable set in user code from interfering
|
|
1232 |
set(EXPORT_NAME)
|
16021
|
1233 |
endif()
|
|
1234 |
|
|
1235 |
# Loop over all arguments and get options for each install target type
|
|
1236 |
list(LENGTH ARGN ARGN_LENGTH)
|
|
1237 |
while(ARGN_LENGTH)
|
|
1238 |
# Check if we're dealing with arguments for a specific install target type, or with
|
|
1239 |
# default options for all target types.
|
|
1240 |
list(GET ARGN 0 FRONT)
|
|
1241 |
if (FRONT IN_LIST INSTALL_TARGET_TYPES)
|
|
1242 |
set(INSTALL_TARGET_TYPE ${FRONT})
|
|
1243 |
list(REMOVE_AT ARGN 0)
|
|
1244 |
else()
|
|
1245 |
set(INSTALL_TARGET_TYPE DEFAULT)
|
|
1246 |
endif()
|
|
1247 |
|
|
1248 |
# Gather the arguments to this install type
|
|
1249 |
set(ARGS)
|
|
1250 |
while(ARGN_LENGTH)
|
|
1251 |
# If the next keyword is an install target type, then break - arguments have been
|
|
1252 |
# gathered.
|
|
1253 |
list(GET ARGN 0 FRONT)
|
|
1254 |
if (FRONT IN_LIST INSTALL_TARGET_TYPES)
|
|
1255 |
break()
|
|
1256 |
endif()
|
|
1257 |
|
|
1258 |
list(APPEND ARGS ${FRONT})
|
|
1259 |
list(REMOVE_AT ARGN 0)
|
|
1260 |
|
|
1261 |
list(LENGTH ARGN ARGN_LENGTH)
|
|
1262 |
endwhile()
|
|
1263 |
|
|
1264 |
# Parse the arguments and register the file install
|
|
1265 |
cmake_parse_arguments(
|
|
1266 |
COR "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGS})
|
|
1267 |
|
|
1268 |
if (COR_DESTINATION)
|
|
1269 |
set(COR_INSTALL_${INSTALL_TARGET_TYPE}_DESTINATION ${COR_DESTINATION})
|
|
1270 |
endif()
|
|
1271 |
|
|
1272 |
if (COR_PERMISSIONS)
|
|
1273 |
set(COR_INSTALL_${INSTALL_TARGET_TYPE}_PERMISSIONS ${COR_PERMISSIONS})
|
|
1274 |
endif()
|
|
1275 |
|
|
1276 |
if (COR_CONFIGURATIONS)
|
|
1277 |
set(COR_INSTALL_${INSTALL_TARGET_TYPE}_CONFIGURATIONS ${COR_CONFIGURATIONS})
|
|
1278 |
endif()
|
|
1279 |
|
|
1280 |
# Update ARG_LENGTH
|
|
1281 |
list(LENGTH ARGN ARGN_LENGTH)
|
|
1282 |
endwhile()
|
|
1283 |
|
|
1284 |
# Default permissions for all files
|
|
1285 |
set(DEFAULT_PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ WORLD_READ)
|
|
1286 |
|
|
1287 |
# Loop through each install target and register file installations
|
|
1288 |
foreach(INSTALL_TARGET ${INSTALL_TARGETS})
|
16038
|
1289 |
if(NOT TARGET ${INSTALL_TARGET})
|
|
1290 |
message(FATAL_ERROR "Install target ${INSTALL_TARGET} is not a valid target")
|
|
1291 |
endif()
|
16021
|
1292 |
# Don't both implementing target type differentiation using generator expressions since
|
|
1293 |
# TYPE cannot change after target creation
|
|
1294 |
get_property(
|
|
1295 |
TARGET_TYPE
|
|
1296 |
TARGET ${INSTALL_TARGET} PROPERTY TYPE
|
|
1297 |
)
|
|
1298 |
|
|
1299 |
# Install executable files first
|
|
1300 |
if (TARGET_TYPE STREQUAL "EXECUTABLE")
|
|
1301 |
if (DEFINED COR_INSTALL_RUNTIME_DESTINATION)
|
|
1302 |
set(DESTINATION ${COR_INSTALL_RUNTIME_DESTINATION})
|
|
1303 |
elseif (DEFINED COR_INSTALL_DEFAULT_DESTINATION)
|
|
1304 |
set(DESTINATION ${COR_INSTALL_DEFAULT_DESTINATION})
|
|
1305 |
else()
|
|
1306 |
set(DESTINATION ${CMAKE_INSTALL_BINDIR})
|
|
1307 |
endif()
|
|
1308 |
|
|
1309 |
if (DEFINED COR_INSTALL_RUNTIME_PERMISSIONS)
|
|
1310 |
set(PERMISSIONS ${COR_INSTALL_RUNTIME_PERMISSIONS})
|
|
1311 |
elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS)
|
|
1312 |
set(PERMISSIONS ${COR_INSTALL_DEFAULT_PERMISSIONS})
|
|
1313 |
else()
|
|
1314 |
set(
|
|
1315 |
PERMISSIONS
|
|
1316 |
${DEFAULT_PERMISSIONS} OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE)
|
|
1317 |
endif()
|
|
1318 |
|
|
1319 |
if (DEFINED COR_INSTALL_RUNTIME_CONFIGURATIONS)
|
|
1320 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_RUNTIME_CONFIGURATIONS})
|
|
1321 |
elseif (DEFINED COR_INSTALL_DEFAULT_CONFIGURATIONS)
|
|
1322 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_DEFAULT_CONFIGURATIONS})
|
|
1323 |
else()
|
|
1324 |
set(CONFIGURATIONS)
|
|
1325 |
endif()
|
|
1326 |
|
|
1327 |
install(
|
|
1328 |
FILES $<TARGET_FILE:${INSTALL_TARGET}>
|
|
1329 |
DESTINATION ${DESTINATION}
|
|
1330 |
PERMISSIONS ${PERMISSIONS}
|
|
1331 |
${CONFIGURATIONS}
|
|
1332 |
)
|
16038
|
1333 |
elseif(TARGET_TYPE STREQUAL "INTERFACE_LIBRARY")
|
|
1334 |
if(TARGET ${INSTALL_TARGET}-static)
|
|
1335 |
if (DEFINED COR_INSTALL_ARCHIVE_DESTINATION)
|
|
1336 |
set(DESTINATION ${COR_INSTALL_ARCHIVE_DESTINATION})
|
|
1337 |
elseif (DEFINED COR_INSTALL_DEFAULT_DESTINATION)
|
|
1338 |
set(DESTINATION ${COR_INSTALL_DEFAULT_DESTINATION})
|
|
1339 |
else()
|
|
1340 |
set(DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
|
1341 |
endif()
|
|
1342 |
|
|
1343 |
if (DEFINED COR_INSTALL_ARCHIVE_PERMISSIONS)
|
|
1344 |
set(PERMISSIONS ${COR_INSTALL_ARCHIVE_PERMISSIONS})
|
|
1345 |
elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS)
|
|
1346 |
set(PERMISSIONS ${COR_INSTALL_DEFAULT_PERMISSIONS})
|
|
1347 |
else()
|
|
1348 |
set(PERMISSIONS ${DEFAULT_PERMISSIONS})
|
|
1349 |
endif()
|
|
1350 |
|
|
1351 |
if (DEFINED COR_INSTALL_ARCHIVE_CONFIGURATIONS)
|
|
1352 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_ARCHIVE_CONFIGURATIONS})
|
|
1353 |
elseif (DEFINED COR_INSTALL_DEFAULT_CONFIGURATIONS)
|
|
1354 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_DEFAULT_CONFIGURATIONS})
|
|
1355 |
else()
|
|
1356 |
set(CONFIGURATIONS)
|
|
1357 |
endif()
|
|
1358 |
|
|
1359 |
install(
|
|
1360 |
FILES $<TARGET_PROPERTY:${INSTALL_TARGET}-static,IMPORTED_LOCATION>
|
|
1361 |
DESTINATION ${DESTINATION}
|
|
1362 |
PERMISSIONS ${PERMISSIONS}
|
|
1363 |
${CONFIGURATIONS}
|
|
1364 |
)
|
|
1365 |
|
|
1366 |
if(EXPORT_NAME)
|
|
1367 |
get_target_property(COR_FILE_NAME ${INSTALL_TARGET}-static COR_FILE_NAME)
|
|
1368 |
file(APPEND "${EXPORT_FILE_PATH}"
|
|
1369 |
"
|
|
1370 |
add_library(${INSTALL_TARGET}-static STATIC IMPORTED)
|
|
1371 |
set_target_properties(${INSTALL_TARGET}-static
|
|
1372 |
PROPERTIES
|
|
1373 |
IMPORTED_LOCATION \"\${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME}\"
|
|
1374 |
)
|
|
1375 |
"
|
|
1376 |
)
|
|
1377 |
endif()
|
|
1378 |
endif()
|
|
1379 |
|
|
1380 |
if(TARGET ${INSTALL_TARGET}-shared)
|
|
1381 |
if (DEFINED COR_INSTALL_LIBRARY_DESTINATION)
|
|
1382 |
set(DESTINATION ${COR_INSTALL_LIBRARY_DESTINATION})
|
|
1383 |
elseif (DEFINED COR_INSTALL_DEFAULT_DESTINATION)
|
|
1384 |
set(DESTINATION ${COR_INSTALL_DEFAULT_DESTINATION})
|
|
1385 |
else()
|
|
1386 |
set(DESTINATION ${CMAKE_INSTALL_LIBDIR})
|
|
1387 |
endif()
|
|
1388 |
|
|
1389 |
if (DEFINED COR_INSTALL_LIBRARY_PERMISSIONS)
|
|
1390 |
set(PERMISSIONS ${COR_INSTALL_LIBRARY_PERMISSIONS})
|
|
1391 |
elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS)
|
|
1392 |
set(PERMISSIONS ${COR_INSTALL_DEFAULT_PERMISSIONS})
|
|
1393 |
else()
|
|
1394 |
set(
|
|
1395 |
PERMISSIONS
|
|
1396 |
${DEFAULT_PERMISSIONS} OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
|
|
1397 |
)
|
|
1398 |
endif()
|
|
1399 |
|
|
1400 |
if (DEFINED COR_INSTALL_LIBRARY_CONFIGURATIONS)
|
|
1401 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_LIBRARY_CONFIGURATIONS})
|
|
1402 |
elseif (DEFINED COR_INSTALL_DEFAULT_CONFIGURATIONS)
|
|
1403 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_DEFAULT_CONFIGURATIONS})
|
|
1404 |
else()
|
|
1405 |
set(CONFIGURATIONS)
|
|
1406 |
endif()
|
|
1407 |
|
|
1408 |
install(
|
|
1409 |
IMPORTED_RUNTIME_ARTIFACTS ${INSTALL_TARGET}-shared
|
|
1410 |
PERMISSIONS ${PERMISSIONS}
|
|
1411 |
DESTINATION ${DESTINATION}
|
|
1412 |
${CONFIGURATIONS}
|
|
1413 |
)
|
|
1414 |
|
|
1415 |
if(EXPORT_NAME)
|
|
1416 |
get_target_property(COR_FILE_NAME ${INSTALL_TARGET}-shared COR_FILE_NAME)
|
|
1417 |
file(APPEND "${EXPORT_FILE_PATH}"
|
|
1418 |
"
|
|
1419 |
add_library(${INSTALL_TARGET}-shared SHARED IMPORTED)
|
|
1420 |
set_target_properties(${INSTALL_TARGET}-shared
|
|
1421 |
PROPERTIES
|
|
1422 |
IMPORTED_LOCATION \"\${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_FILE_NAME}\"
|
|
1423 |
)
|
|
1424 |
"
|
|
1425 |
)
|
|
1426 |
|
|
1427 |
get_target_property(COR_IMPLIB_FILE_NAME ${INSTALL_TARGET}-shared COR_IMPLIB_FILE_NAME)
|
|
1428 |
if (NOT COR_IMPLIB_FILE_NAME MATCHES .*-NOTFOUND)
|
|
1429 |
file(APPEND "${EXPORT_FILE_PATH}"
|
|
1430 |
"
|
|
1431 |
set_target_properties(${INSTALL_TARGET}-shared
|
|
1432 |
PROPERTIES
|
|
1433 |
IMPORTED_IMPLIB \"\${PACKAGE_PREFIX_DIR}/${DESTINATION}/${COR_IMPLIB_FILE_NAME}\"
|
|
1434 |
)"
|
|
1435 |
)
|
|
1436 |
endif()
|
|
1437 |
endif()
|
|
1438 |
endif()
|
|
1439 |
else()
|
|
1440 |
message(FATAL_ERROR "Unknown target type ${TARGET_TYPE} for install target ${INSTALL_TARGET}")
|
|
1441 |
endif()
|
|
1442 |
|
|
1443 |
# Executables can also have export tables, so they _might_ also need header files
|
|
1444 |
if (DEFINED COR_INSTALL_PUBLIC_HEADER_DESTINATION)
|
|
1445 |
set(DESTINATION ${COR_INSTALL_PUBLIC_HEADER_DESTINATION})
|
|
1446 |
elseif (DEFINED COR_INSTALL_DEFAULT_DESTINATION)
|
|
1447 |
set(DESTINATION ${COR_INSTALL_DEFAULT_DESTINATION})
|
|
1448 |
else()
|
|
1449 |
set(DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
|
1450 |
endif()
|
|
1451 |
|
|
1452 |
if (DEFINED COR_INSTALL_PUBLIC_HEADER_PERMISSIONS)
|
|
1453 |
set(PERMISSIONS ${COR_INSTALL_PUBLIC_HEADER_PERMISSIONS})
|
|
1454 |
elseif (DEFINED COR_INSTALL_DEFAULT_PERMISSIONS)
|
|
1455 |
set(PERMISSIONS ${COR_INSTALL_DEFAULT_PERMISSIONS})
|
|
1456 |
else()
|
|
1457 |
# Directories need OWNER_EXECUTE in order to be deletable by owner
|
|
1458 |
set(PERMISSIONS ${DEFAULT_PERMISSIONS} OWNER_EXECUTE)
|
|
1459 |
endif()
|
|
1460 |
|
|
1461 |
if (DEFINED COR_INSTALL_PUBLIC_HEADER_CONFIGURATIONS)
|
|
1462 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_PUBLIC_HEADER_CONFIGURATIONS})
|
|
1463 |
elseif (DEFINED COR_INSTALL_DEFAULT_CONFIGURATIONS)
|
|
1464 |
set(CONFIGURATIONS CONFIGURATIONS ${COR_INSTALL_DEFAULT_CONFIGURATIONS})
|
|
1465 |
else()
|
|
1466 |
set(CONFIGURATIONS)
|
|
1467 |
endif()
|
|
1468 |
|
|
1469 |
get_target_property(FILE_SET ${INSTALL_TARGET} INTERFACE_HEADER_SETS)
|
|
1470 |
if(NOT FILE_SET OR FILE_SET MATCHES .*-NOTFOUND)
|
|
1471 |
set(TARGET_HAS_FILE_SET FALSE)
|
|
1472 |
else()
|
|
1473 |
set(TARGET_HAS_FILE_SET TRUE)
|
|
1474 |
endif()
|
|
1475 |
|
|
1476 |
if(NOT TARGET_HAS_FILE_SET)
|
|
1477 |
if(EXPORT_NAME)
|
|
1478 |
# We still need to generate a EXPORT but we can't do that with install(DIRECTORY)
|
|
1479 |
install(TARGETS ${INSTALL_TARGET} ${EXPORT_NAME})
|
|
1480 |
endif()
|
|
1481 |
|
|
1482 |
set(PUBLIC_HEADER_PROPERTIES INCLUDE_DIRECTORIES PUBLIC_INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES)
|
|
1483 |
foreach(PUBLIC_HEADER_PROPERTY ${PUBLIC_HEADER_PROPERTIES})
|
|
1484 |
get_target_property(PUBLIC_HEADER ${INSTALL_TARGET} ${PUBLIC_HEADER_PROPERTY})
|
|
1485 |
|
|
1486 |
if(NOT PUBLIC_HEADER MATCHES .*-NOTFOUND)
|
|
1487 |
foreach(INCLUDE_DIRECTORY ${PUBLIC_HEADER})
|
|
1488 |
install(
|
|
1489 |
DIRECTORY ${INCLUDE_DIRECTORY}
|
|
1490 |
DESTINATION .
|
|
1491 |
FILE_PERMISSIONS ${PERMISSIONS}
|
|
1492 |
DIRECTORY_PERMISSIONS ${PERMISSIONS}
|
|
1493 |
${CONFIGURATIONS}
|
|
1494 |
)
|
|
1495 |
endforeach()
|
|
1496 |
endif()
|
|
1497 |
endforeach()
|
|
1498 |
else()
|
|
1499 |
install(
|
|
1500 |
TARGETS ${INSTALL_TARGET}
|
|
1501 |
${EXPORT_NAME}
|
|
1502 |
FILE_SET HEADERS
|
|
1503 |
DESTINATION ${DESTINATION}
|
|
1504 |
PERMISSIONS ${PERMISSIONS}
|
|
1505 |
${CONFIGURATIONS}
|
|
1506 |
)
|
16021
|
1507 |
endif()
|
|
1508 |
endforeach()
|
|
1509 |
|
|
1510 |
elseif(INSTALL_TYPE STREQUAL "EXPORT")
|
|
1511 |
message(FATAL_ERROR "install(EXPORT ...) not yet implemented")
|
16038
|
1512 |
else()
|
|
1513 |
message(FATAL_ERROR "Unknown arg: ${INSTALL_TYPE}")
|
16021
|
1514 |
endif()
|
|
1515 |
endfunction()
|
|
1516 |
|
|
1517 |
#[=======================================================================[.md:
|
|
1518 |
** EXPERIMENTAL **: This function is currently still considered experimental
|
|
1519 |
and is not officially released yet. Feedback and Suggestions are welcome.
|
|
1520 |
|
|
1521 |
ANCHOR: corrosion_add_cxxbridge
|
|
1522 |
|
|
1523 |
```cmake
|
|
1524 |
corrosion_add_cxxbridge(cxx_target
|
|
1525 |
CRATE <imported_target_name>
|
16038
|
1526 |
REGEN_TARGET <regen_target_name>
|
16021
|
1527 |
[FILES <file1.rs> <file2.rs>]
|
|
1528 |
)
|
|
1529 |
```
|
|
1530 |
|
|
1531 |
Adds build-rules to create C++ bindings using the [cxx] crate.
|
|
1532 |
|
|
1533 |
### Arguments:
|
|
1534 |
* `cxxtarget`: Name of the C++ library target for the bindings, which corrosion will create.
|
|
1535 |
* **FILES**: Input Rust source file containing #[cxx::bridge].
|
|
1536 |
* **CRATE**: Name of an imported Rust target. Note: Parameter may be renamed before release
|
16038
|
1537 |
* **REGEN_TARGET**: Name of a custom target that will regenerate the cxx bindings **without** recompiling. Note: Parameter may be renamed before release
|
16021
|
1538 |
|
|
1539 |
#### Currently missing arguments
|
|
1540 |
|
|
1541 |
The following arguments to cxxbridge **currently** have no way to be passed by the user:
|
|
1542 |
- `--cfg`
|
|
1543 |
- `--cxx-impl-annotations`
|
|
1544 |
- `--include`
|
|
1545 |
|
|
1546 |
The created rules approximately do the following:
|
|
1547 |
- Check which version of `cxx` the Rust crate specified by the `CRATE` argument depends on.
|
|
1548 |
- Check if the exact same version of `cxxbridge-cmd` is installed (available in `PATH`)
|
|
1549 |
- If not, create a rule to build the exact same version of `cxxbridge-cmd`.
|
|
1550 |
- Create rules to run `cxxbridge` and generate
|
|
1551 |
- The `rust/cxx.h` header
|
|
1552 |
- A header and source file for each of the files specified in `FILES`
|
|
1553 |
- The generated sources (and header include directories) are added to the `cxxtarget` CMake
|
|
1554 |
library target.
|
|
1555 |
|
|
1556 |
### Limitations
|
|
1557 |
|
|
1558 |
We currently require the `CRATE` argument to be a target imported by Corrosion, however,
|
|
1559 |
Corrosion does not import `rlib` only libraries. As a workaround users can add
|
|
1560 |
`staticlib` to their list of crate kinds. In the future this may be solved more properly,
|
|
1561 |
by either adding an option to also import Rlib targets (without build rules) or by
|
|
1562 |
adding a `MANIFEST_PATH` argument to this function, specifying where the crate is.
|
|
1563 |
|
|
1564 |
### Contributing
|
|
1565 |
|
|
1566 |
Specifically some more realistic test / demo projects and feedback about limitations would be
|
|
1567 |
welcome.
|
|
1568 |
|
|
1569 |
[cxx]: https://github.com/dtolnay/cxx
|
|
1570 |
|
|
1571 |
ANCHOR_END: corrosion_add_cxxbridge
|
|
1572 |
#]=======================================================================]
|
|
1573 |
function(corrosion_add_cxxbridge cxx_target)
|
|
1574 |
set(OPTIONS)
|
16038
|
1575 |
set(ONE_VALUE_KEYWORDS CRATE REGEN_TARGET)
|
16021
|
1576 |
set(MULTI_VALUE_KEYWORDS FILES)
|
|
1577 |
cmake_parse_arguments(PARSE_ARGV 1 _arg "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")
|
|
1578 |
|
|
1579 |
set(required_keywords CRATE FILES)
|
|
1580 |
foreach(keyword ${required_keywords})
|
|
1581 |
if(NOT DEFINED "_arg_${keyword}")
|
|
1582 |
message(FATAL_ERROR "Missing required parameter `${keyword}`.")
|
|
1583 |
elseif("${_arg_${keyword}}" STREQUAL "")
|
|
1584 |
message(FATAL_ERROR "Required parameter `${keyword}` may not be set to an empty string.")
|
|
1585 |
endif()
|
|
1586 |
endforeach()
|
|
1587 |
|
|
1588 |
get_target_property(manifest_path "${_arg_CRATE}" INTERFACE_COR_PACKAGE_MANIFEST_PATH)
|
|
1589 |
|
|
1590 |
if(NOT EXISTS "${manifest_path}")
|
|
1591 |
message(FATAL_ERROR "Internal error: No package manifest found at ${manifest_path}")
|
|
1592 |
endif()
|
|
1593 |
|
|
1594 |
get_filename_component(manifest_dir ${manifest_path} DIRECTORY)
|
|
1595 |
|
|
1596 |
execute_process(COMMAND ${CMAKE_COMMAND} -E env
|
|
1597 |
"CARGO_BUILD_RUSTC=${_CORROSION_RUSTC}"
|
|
1598 |
${_CORROSION_CARGO} tree -i cxx --depth=0
|
|
1599 |
WORKING_DIRECTORY "${manifest_dir}"
|
|
1600 |
RESULT_VARIABLE cxx_version_result
|
|
1601 |
OUTPUT_VARIABLE cxx_version_output
|
|
1602 |
)
|
|
1603 |
if(NOT "${cxx_version_result}" EQUAL "0")
|
|
1604 |
message(FATAL_ERROR "Crate ${_arg_CRATE} does not depend on cxx.")
|
|
1605 |
endif()
|
|
1606 |
if(cxx_version_output MATCHES "cxx v([0-9]+.[0-9]+.[0-9]+)")
|
|
1607 |
set(cxx_required_version "${CMAKE_MATCH_1}")
|
|
1608 |
else()
|
|
1609 |
message(FATAL_ERROR "Failed to parse cxx version from cargo tree output: `cxx_version_output`")
|
|
1610 |
endif()
|
|
1611 |
|
|
1612 |
# First check if a suitable version of cxxbridge is installed
|
|
1613 |
find_program(INSTALLED_CXXBRIDGE cxxbridge PATHS "$ENV{HOME}/.cargo/bin/")
|
|
1614 |
mark_as_advanced(INSTALLED_CXXBRIDGE)
|
|
1615 |
if(INSTALLED_CXXBRIDGE)
|
|
1616 |
execute_process(COMMAND ${INSTALLED_CXXBRIDGE} --version OUTPUT_VARIABLE cxxbridge_version_output)
|
|
1617 |
if(cxxbridge_version_output MATCHES "cxxbridge ([0-9]+.[0-9]+.[0-9]+)")
|
|
1618 |
set(cxxbridge_version "${CMAKE_MATCH_1}")
|
|
1619 |
else()
|
|
1620 |
set(cxxbridge_version "")
|
|
1621 |
endif()
|
|
1622 |
endif()
|
|
1623 |
|
|
1624 |
set(cxxbridge "")
|
|
1625 |
if(cxxbridge_version)
|
|
1626 |
if(cxxbridge_version VERSION_EQUAL cxx_required_version)
|
|
1627 |
set(cxxbridge "${INSTALLED_CXXBRIDGE}")
|
|
1628 |
if(NOT TARGET "cxxbridge_v${cxx_required_version}")
|
|
1629 |
# Add an empty target.
|
|
1630 |
add_custom_target("cxxbridge_v${cxx_required_version}"
|
|
1631 |
)
|
|
1632 |
endif()
|
|
1633 |
endif()
|
|
1634 |
endif()
|
|
1635 |
|
|
1636 |
# No suitable version of cxxbridge was installed, so use custom target to build correct version.
|
|
1637 |
if(NOT cxxbridge)
|
|
1638 |
if(NOT TARGET "cxxbridge_v${cxx_required_version}")
|
16038
|
1639 |
unset(executable_postfix)
|
|
1640 |
if(Rust_CARGO_HOST_OS STREQUAL "windows")
|
|
1641 |
set(executable_postfix ".exe")
|
|
1642 |
endif()
|
|
1643 |
add_custom_command(OUTPUT "${CMAKE_BINARY_DIR}/corrosion/cxxbridge_v${cxx_required_version}/bin/cxxbridge${executable_postfix}"
|
16021
|
1644 |
COMMAND
|
|
1645 |
${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/corrosion/cxxbridge_v${cxx_required_version}"
|
|
1646 |
COMMAND
|
|
1647 |
${CMAKE_COMMAND} -E env
|
|
1648 |
"CARGO_BUILD_RUSTC=${_CORROSION_RUSTC}"
|
|
1649 |
${_CORROSION_CARGO} install
|
|
1650 |
cxxbridge-cmd
|
|
1651 |
--version "${cxx_required_version}"
|
|
1652 |
--root "${CMAKE_BINARY_DIR}/corrosion/cxxbridge_v${cxx_required_version}"
|
|
1653 |
--quiet
|
|
1654 |
# todo: use --target-dir to potentially reuse artifacts
|
|
1655 |
COMMENT "Building cxxbridge (version ${cxx_required_version})"
|
|
1656 |
)
|
|
1657 |
add_custom_target("cxxbridge_v${cxx_required_version}"
|
16038
|
1658 |
DEPENDS "${CMAKE_BINARY_DIR}/corrosion/cxxbridge_v${cxx_required_version}/bin/cxxbridge${executable_postfix}"
|
16021
|
1659 |
)
|
|
1660 |
endif()
|
16038
|
1661 |
set(cxxbridge "${CMAKE_BINARY_DIR}/corrosion/cxxbridge_v${cxx_required_version}/bin/cxxbridge${executable_postfix}")
|
16021
|
1662 |
endif()
|
|
1663 |
|
|
1664 |
|
|
1665 |
# The generated folder structure will be of the following form
|
|
1666 |
#
|
|
1667 |
# CMAKE_CURRENT_BINARY_DIR
|
|
1668 |
# corrosion_generated
|
|
1669 |
# cxxbridge
|
|
1670 |
# <cxx_target>
|
|
1671 |
# include
|
|
1672 |
# <cxx_target>
|
|
1673 |
# <headers>
|
|
1674 |
# rust
|
|
1675 |
# cxx.h
|
|
1676 |
# src
|
|
1677 |
# <sourcefiles>
|
|
1678 |
# cbindgen
|
|
1679 |
# ...
|
|
1680 |
# other
|
|
1681 |
# ...
|
|
1682 |
|
|
1683 |
set(corrosion_generated_dir "${CMAKE_CURRENT_BINARY_DIR}/corrosion_generated")
|
|
1684 |
set(generated_dir "${corrosion_generated_dir}/cxxbridge/${cxx_target}")
|
|
1685 |
set(header_placement_dir "${generated_dir}/include/${cxx_target}")
|
|
1686 |
set(source_placement_dir "${generated_dir}/src")
|
|
1687 |
|
|
1688 |
add_library(${cxx_target} STATIC)
|
|
1689 |
target_include_directories(${cxx_target}
|
|
1690 |
PUBLIC
|
|
1691 |
$<BUILD_INTERFACE:${generated_dir}/include>
|
|
1692 |
$<INSTALL_INTERFACE:include>
|
|
1693 |
)
|
|
1694 |
|
|
1695 |
# cxx generated code is using c++11 features in headers, so propagate c++11 as minimal requirement
|
|
1696 |
target_compile_features(${cxx_target} PUBLIC cxx_std_11)
|
|
1697 |
|
|
1698 |
# Todo: target_link_libraries is only necessary for rust2c projects.
|
|
1699 |
# It is possible that checking if the rust crate is an executable is a sufficient check,
|
|
1700 |
# but some more thought may be needed here.
|
|
1701 |
# Maybe we should also let the user do this, since for c2rust, the user also has to call
|
|
1702 |
# corrosion_link_libraries() themselves.
|
|
1703 |
get_target_property(crate_target_type ${_arg_CRATE} TYPE)
|
|
1704 |
if (NOT crate_target_type STREQUAL "EXECUTABLE")
|
|
1705 |
target_link_libraries(${cxx_target} PRIVATE ${_arg_CRATE})
|
|
1706 |
endif()
|
|
1707 |
|
|
1708 |
file(MAKE_DIRECTORY "${generated_dir}/include/rust")
|
|
1709 |
add_custom_command(
|
|
1710 |
OUTPUT "${generated_dir}/include/rust/cxx.h"
|
|
1711 |
COMMAND
|
|
1712 |
${cxxbridge} --header --output "${generated_dir}/include/rust/cxx.h"
|
|
1713 |
DEPENDS "cxxbridge_v${cxx_required_version}"
|
|
1714 |
COMMENT "Generating rust/cxx.h header"
|
|
1715 |
)
|
|
1716 |
|
16038
|
1717 |
set(GENERATED_FILES "${generated_dir}/include/rust/cxx.h")
|
|
1718 |
|
16021
|
1719 |
foreach(filepath ${_arg_FILES})
|
|
1720 |
get_filename_component(filename ${filepath} NAME_WE)
|
|
1721 |
get_filename_component(directory ${filepath} DIRECTORY)
|
|
1722 |
set(directory_component "")
|
|
1723 |
if(directory)
|
|
1724 |
set(directory_component "${directory}/")
|
|
1725 |
endif()
|
|
1726 |
# todo: convert potentially absolute paths to relative paths..
|
|
1727 |
set(cxx_header ${directory_component}${filename}.h)
|
|
1728 |
set(cxx_source ${directory_component}${filename}.cpp)
|
|
1729 |
|
|
1730 |
# todo: not all projects may use the `src` directory.
|
|
1731 |
set(rust_source_path "${manifest_dir}/src/${filepath}")
|
|
1732 |
|
|
1733 |
file(MAKE_DIRECTORY "${header_placement_dir}/${directory}" "${source_placement_dir}/${directory}")
|
|
1734 |
|
|
1735 |
add_custom_command(
|
|
1736 |
OUTPUT
|
|
1737 |
"${header_placement_dir}/${cxx_header}"
|
|
1738 |
"${source_placement_dir}/${cxx_source}"
|
|
1739 |
COMMAND
|
|
1740 |
${cxxbridge} ${rust_source_path} --header --output "${header_placement_dir}/${cxx_header}"
|
|
1741 |
COMMAND
|
|
1742 |
${cxxbridge} ${rust_source_path}
|
|
1743 |
--output "${source_placement_dir}/${cxx_source}"
|
|
1744 |
--include "${cxx_target}/${cxx_header}"
|
|
1745 |
DEPENDS "cxxbridge_v${cxx_required_version}" "${rust_source_path}"
|
16038
|
1746 |
COMMENT "Generating cxx bindings for crate ${_arg_CRATE} and file src/${filepath}"
|
16021
|
1747 |
)
|
|
1748 |
|
16038
|
1749 |
list(APPEND GENERATED_FILES
|
|
1750 |
"${header_placement_dir}/${cxx_header}"
|
|
1751 |
"${source_placement_dir}/${cxx_source}")
|
16021
|
1752 |
endforeach()
|
16038
|
1753 |
target_sources(${cxx_target} PRIVATE ${GENERATED_FILES})
|
|
1754 |
|
|
1755 |
if(DEFINED _arg_REGEN_TARGET)
|
|
1756 |
add_custom_target(${_arg_REGEN_TARGET}
|
|
1757 |
DEPENDS ${GENERATED_FILES}
|
|
1758 |
COMMENT "Generated cxx bindings for crate ${_arg_CRATE}")
|
|
1759 |
endif()
|
|
1760 |
|
16021
|
1761 |
endfunction()
|
|
1762 |
|
|
1763 |
#[=======================================================================[.md:
|
|
1764 |
ANCHOR: corrosion_cbindgen
|
|
1765 |
```cmake
|
|
1766 |
corrosion_cbindgen(
|
|
1767 |
TARGET <imported_target_name>
|
|
1768 |
HEADER_NAME <output_header_name>
|
16038
|
1769 |
[CARGO_PACKAGE <cargo_package_name>]
|
16021
|
1770 |
[MANIFEST_DIRECTORY <package_manifest_directory>]
|
|
1771 |
[CBINDGEN_VERSION <version>]
|
|
1772 |
[FLAGS <flag1> ... <flagN>]
|
|
1773 |
)
|
|
1774 |
```
|
|
1775 |
|
|
1776 |
A helper function which uses [cbindgen] to generate C/C++ bindings for a Rust crate.
|
|
1777 |
If `cbindgen` is not in `PATH` the helper function will automatically try to download
|
|
1778 |
`cbindgen` and place the built binary into `CMAKE_BINARY_DIR`. The binary is shared
|
|
1779 |
between multiple invocations of this function.
|
|
1780 |
|
|
1781 |
|
16038
|
1782 |
* **TARGET**: The name of an imported Rust library target, for which bindings should be generated.
|
16021
|
1783 |
If the target was not previously imported by Corrosion, because the crate only produces an
|
|
1784 |
`rlib`, you must additionally specify `MANIFEST_DIRECTORY`.
|
|
1785 |
|
|
1786 |
* **MANIFEST_DIRECTORY**: Directory of the package defining the library crate bindings should be generated for.
|
|
1787 |
If you want to avoid specifying `MANIFEST_DIRECTORY` you could add a `staticlib` target to your package
|
|
1788 |
manifest as a workaround to make corrosion import the crate.
|
|
1789 |
|
|
1790 |
* **HEADER_NAME**: The name of the generated header file. This will be the name which you include in your C/C++ code
|
|
1791 |
(e.g. `#include "myproject/myheader.h" if you specify `HEADER_NAME "myproject/myheader.h"`.
|
|
1792 |
* **CBINDGEN_VERSION**: Version requirement for cbindgen. Exact semantics to be specified. Currently not implemented.
|
|
1793 |
* **FLAGS**: Arbitrary other flags for `cbindgen`. Run `cbindgen --help` to see the possible flags.
|
|
1794 |
|
|
1795 |
[cbindgen]: https://github.com/eqrion/cbindgen
|
|
1796 |
|
16038
|
1797 |
### Current limitations
|
|
1798 |
|
|
1799 |
- Cbindgens (optional) macro expansion feature internally actually builds the crate / runs the build script.
|
|
1800 |
For this to work as expected in all cases, we probably need to set all the same environment variables
|
|
1801 |
as when corrosion builds the crate. However the crate is a **library**, so we would need to figure out which
|
|
1802 |
target builds it - and if there are multiple, potentially generate bindings per-target?
|
|
1803 |
Alternatively we could add support of setting some environment variables on rlibs, and pulling that
|
|
1804 |
information in when building the actual corrosion targets
|
|
1805 |
Alternatively we could restrict corrosions support of this feature to actual imported staticlib/cdylib targets.
|
16021
|
1806 |
ANCHOR_END: corrosion_cbindgen
|
|
1807 |
#]=======================================================================]
|
|
1808 |
function(corrosion_experimental_cbindgen)
|
|
1809 |
set(OPTIONS "")
|
16038
|
1810 |
set(ONE_VALUE_KEYWORDS
|
|
1811 |
TARGET
|
|
1812 |
MANIFEST_DIRECTORY
|
|
1813 |
HEADER_NAME
|
|
1814 |
CBINDGEN_VERSION)
|
16021
|
1815 |
set(MULTI_VALUE_KEYWORDS "FLAGS")
|
|
1816 |
cmake_parse_arguments(PARSE_ARGV 0 CCN "${OPTIONS}" "${ONE_VALUE_KEYWORDS}" "${MULTI_VALUE_KEYWORDS}")
|
|
1817 |
|
|
1818 |
set(required_keywords TARGET HEADER_NAME)
|
|
1819 |
foreach(keyword ${required_keywords})
|
|
1820 |
if(NOT DEFINED "CCN_${keyword}")
|
|
1821 |
message(FATAL_ERROR "Missing required parameter `${keyword}`.")
|
|
1822 |
elseif("${CCN_${keyword}}" STREQUAL "")
|
|
1823 |
message(FATAL_ERROR "Required parameter `${keyword}` may not be set to an empty string.")
|
|
1824 |
endif()
|
|
1825 |
endforeach()
|
|
1826 |
set(rust_target "${CCN_TARGET}")
|
|
1827 |
unset(package_manifest_dir)
|
|
1828 |
|
|
1829 |
|
|
1830 |
set(hostbuild_override "$<BOOL:$<TARGET_PROPERTY:${rust_target},${_CORR_PROP_HOST_BUILD}>>")
|
|
1831 |
set(cbindgen_target_triple "$<IF:${hostbuild_override},${_CORROSION_RUST_CARGO_HOST_TARGET},${_CORROSION_RUST_CARGO_TARGET}>")
|
|
1832 |
|
|
1833 |
if(TARGET "${rust_target}")
|
|
1834 |
get_target_property(package_manifest_path "${rust_target}" INTERFACE_COR_PACKAGE_MANIFEST_PATH)
|
|
1835 |
if(NOT EXISTS "${package_manifest_path}")
|
|
1836 |
message(FATAL_ERROR "Internal error: No package manifest found at ${package_manifest_path}")
|
|
1837 |
endif()
|
|
1838 |
get_filename_component(package_manifest_dir "${package_manifest_path}" DIRECTORY)
|
|
1839 |
# todo: as an optimization we could cache the cargo metadata output (but --no-deps makes that slightly more complicated)
|
|
1840 |
else()
|
|
1841 |
if(NOT DEFINED CCN_MANIFEST_DIRECTORY)
|
|
1842 |
message(FATAL_ERROR
|
|
1843 |
"`${rust_target}` is not a target imported by corrosion and `MANIFEST_DIRECTORY` was not provided."
|
|
1844 |
)
|
|
1845 |
else()
|
|
1846 |
set(package_manifest_dir "${CCN_MANIFEST_DIRECTORY}")
|
|
1847 |
endif()
|
|
1848 |
endif()
|
|
1849 |
|
16038
|
1850 |
get_target_property(rust_cargo_package "${rust_target}" COR_CARGO_PACKAGE_NAME )
|
|
1851 |
if(NOT rust_cargo_package)
|
|
1852 |
message(FATAL_ERROR "Internal Error: Could not determine cargo package name for cbindgen. "
|
|
1853 |
)
|
16021
|
1854 |
endif()
|
|
1855 |
message(STATUS "Using package ${rust_cargo_package} as crate for cbindgen")
|
|
1856 |
|
|
1857 |
|
|
1858 |
set(output_header_name "${CCN_HEADER_NAME}")
|
|
1859 |
|
|
1860 |
find_program(installed_cbindgen cbindgen)
|
|
1861 |
|
|
1862 |
# Install the newest cbindgen version into our build tree.
|
|
1863 |
if(installed_cbindgen)
|
|
1864 |
set(cbindgen "${installed_cbindgen}")
|
|
1865 |
else()
|
|
1866 |
set(local_cbindgen_install_dir "${CMAKE_BINARY_DIR}/corrosion/cbindgen")
|
|
1867 |
unset(executable_postfix)
|
|
1868 |
if(Rust_CARGO_HOST_OS STREQUAL "windows")
|
|
1869 |
set(executable_postfix ".exe")
|
|
1870 |
endif()
|
|
1871 |
set(cbindgen "${local_cbindgen_install_dir}/bin/cbindgen${executable_postfix}")
|
|
1872 |
if(NOT TARGET "_corrosion_cbindgen")
|
|
1873 |
file(MAKE_DIRECTORY "${local_cbindgen_install_dir}")
|
|
1874 |
add_custom_command(OUTPUT "${cbindgen}"
|
|
1875 |
COMMAND ${CMAKE_COMMAND}
|
|
1876 |
-E env
|
|
1877 |
"CARGO_BUILD_RUSTC=${_CORROSION_RUSTC}"
|
|
1878 |
${_CORROSION_CARGO} install
|
|
1879 |
cbindgen
|
|
1880 |
--root "${local_cbindgen_install_dir}"
|
|
1881 |
${_CORROSION_QUIET_OUTPUT_FLAG}
|
|
1882 |
COMMENT "Building cbindgen"
|
|
1883 |
)
|
|
1884 |
add_custom_target("_corrosion_cbindgen"
|
|
1885 |
DEPENDS "${cbindgen}"
|
|
1886 |
)
|
|
1887 |
endif()
|
|
1888 |
endif()
|
|
1889 |
|
|
1890 |
set(corrosion_generated_dir "${CMAKE_CURRENT_BINARY_DIR}/corrosion_generated")
|
|
1891 |
set(generated_dir "${corrosion_generated_dir}/cbindgen/${rust_target}")
|
|
1892 |
set(header_placement_dir "${generated_dir}/include/")
|
|
1893 |
set(depfile_placement_dir "${generated_dir}/depfile")
|
|
1894 |
set(generated_depfile "${depfile_placement_dir}/${output_header_name}.d")
|
|
1895 |
set(generated_header "${header_placement_dir}/${output_header_name}")
|
|
1896 |
message(STATUS "rust target is ${rust_target}")
|
|
1897 |
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.23")
|
|
1898 |
target_sources(${rust_target}
|
|
1899 |
INTERFACE
|
|
1900 |
FILE_SET HEADERS
|
|
1901 |
BASE_DIRS "${header_placement_dir}"
|
|
1902 |
FILES "${header_placement_dir}/${output_header_name}"
|
|
1903 |
)
|
|
1904 |
else()
|
|
1905 |
# Note: not clear to me how install would best work before CMake 3.23
|
|
1906 |
target_include_directories(${rust_target}
|
|
1907 |
INTERFACE
|
|
1908 |
$<BUILD_INTERFACE:${header_placement_dir}>
|
|
1909 |
$<INSTALL_INTERFACE:include>
|
|
1910 |
)
|
|
1911 |
endif()
|
|
1912 |
|
|
1913 |
# This may be different from $header_placement_dir since the user specified HEADER_NAME may contain
|
|
1914 |
# relative directories.
|
|
1915 |
get_filename_component(generated_header_dir "${generated_header}" DIRECTORY)
|
|
1916 |
file(MAKE_DIRECTORY "${generated_header_dir}")
|
|
1917 |
|
|
1918 |
unset(depfile_cbindgen_arg)
|
|
1919 |
unset(depfile_cmake_arg)
|
|
1920 |
get_filename_component(generated_depfile_dir "${generated_depfile}" DIRECTORY)
|
|
1921 |
file(MAKE_DIRECTORY "${generated_depfile_dir}")
|
|
1922 |
set(depfile_cbindgen_arg "--depfile=${generated_depfile}")
|
|
1923 |
|
16038
|
1924 |
add_custom_command(
|
|
1925 |
OUTPUT
|
|
1926 |
"${generated_header}"
|
|
1927 |
COMMAND
|
|
1928 |
"${CMAKE_COMMAND}" -E env
|
|
1929 |
TARGET="${cbindgen_target_triple}"
|
|
1930 |
"${cbindgen}"
|
|
1931 |
--output "${generated_header}"
|
|
1932 |
--crate "${rust_cargo_package}"
|
|
1933 |
${depfile_cbindgen_arg}
|
|
1934 |
${CCN_FLAGS}
|
|
1935 |
COMMENT "Generate cbindgen bindings for package ${rust_cargo_package} and output header ${generated_header}"
|
|
1936 |
DEPFILE "${generated_depfile}"
|
|
1937 |
COMMAND_EXPAND_LISTS
|
|
1938 |
WORKING_DIRECTORY "${package_manifest_dir}"
|
|
1939 |
)
|
16021
|
1940 |
|
|
1941 |
if(NOT installed_cbindgen)
|
|
1942 |
add_custom_command(
|
|
1943 |
OUTPUT "${generated_header}"
|
|
1944 |
APPEND
|
|
1945 |
DEPENDS _corrosion_cbindgen
|
|
1946 |
)
|
|
1947 |
endif()
|
|
1948 |
|
|
1949 |
if(NOT TARGET "_corrosion_cbindgen_${rust_target}_bindings")
|
|
1950 |
add_custom_target(_corrosion_cbindgen_${rust_target}_bindings
|
|
1951 |
COMMENT "Generate cbindgen bindings for package ${rust_cargo_package}"
|
|
1952 |
)
|
|
1953 |
endif()
|
16038
|
1954 |
# Users might want to call cbindgen multiple times, e.g. to generate separate C++ and C header files.
|
|
1955 |
string(MAKE_C_IDENTIFIER "${output_header_name}" header_identifier )
|
|
1956 |
add_custom_target("_corrosion_cbindgen_${rust_target}_bindings_${header_identifier}"
|
|
1957 |
DEPENDS "${generated_header}"
|
|
1958 |
COMMENT "Generate ${generated_header} for ${rust_target}"
|
|
1959 |
)
|
16021
|
1960 |
add_dependencies("_corrosion_cbindgen_${rust_target}_bindings" "_corrosion_cbindgen_${rust_target}_bindings_${header_identifier}")
|
|
1961 |
add_dependencies(${rust_target} "_corrosion_cbindgen_${rust_target}_bindings")
|
|
1962 |
endfunction()
|
|
1963 |
|
|
1964 |
# Parse the version of a Rust package from it's package manifest (Cargo.toml)
|
|
1965 |
function(corrosion_parse_package_version package_manifest_path out_package_version)
|
|
1966 |
if(NOT EXISTS "${package_manifest_path}")
|
|
1967 |
message(FATAL_ERROR "Package manifest `${package_manifest_path}` does not exist.")
|
|
1968 |
endif()
|
|
1969 |
|
|
1970 |
file(READ "${package_manifest_path}" package_manifest)
|
|
1971 |
|
|
1972 |
# Find the package table. It may contain arrays, so match until \n\[, which should mark the next
|
|
1973 |
# table. Note: backslashes must be doubled to escape the backslash for the bracket. LF is single
|
|
1974 |
# backslash however. On windows the line also ends in \n, so matching against \n\[ is sufficient
|
|
1975 |
# to detect an opening bracket on a new line.
|
|
1976 |
set(package_table_regex "\\[package\\](.*)\n\\[")
|
|
1977 |
|
|
1978 |
string(REGEX MATCH "${package_table_regex}" _package_table "${package_manifest}")
|
|
1979 |
|
|
1980 |
if(CMAKE_MATCH_COUNT EQUAL "1")
|
|
1981 |
set(package_table "${CMAKE_MATCH_1}")
|
|
1982 |
else()
|
|
1983 |
message(DEBUG
|
|
1984 |
"Failed to find `[package]` table in package manifest `${package_manifest_path}`.\n"
|
|
1985 |
"Matches: ${CMAKE_MATCH_COUNT}\n"
|
|
1986 |
)
|
|
1987 |
set(${out_package_version}
|
|
1988 |
"NOTFOUND"
|
|
1989 |
PARENT_SCOPE
|
|
1990 |
)
|
|
1991 |
endif()
|
|
1992 |
# Match `version = "0.3.2"`, `"version" = "0.3.2" Contains one matching group for the version
|
|
1993 |
set(version_regex "[\r]?\n[\"']?version[\"']?[ \t]*=[ \t]*[\"']([0-9\.]+)[\"']")
|
|
1994 |
|
|
1995 |
string(REGEX MATCH "${version_regex}" _version "${package_table}")
|
|
1996 |
|
|
1997 |
if("${package_table}" MATCHES "${version_regex}")
|
|
1998 |
set(${out_package_version}
|
|
1999 |
"${CMAKE_MATCH_1}"
|
|
2000 |
PARENT_SCOPE
|
|
2001 |
)
|
|
2002 |
else()
|
|
2003 |
message(DEBUG "Failed to extract package version from manifest `${package_manifest_path}`.")
|
|
2004 |
set(${out_package_version}
|
|
2005 |
"NOTFOUND"
|
|
2006 |
PARENT_SCOPE
|
|
2007 |
)
|
|
2008 |
endif()
|
|
2009 |
endfunction()
|
|
2010 |
|
16038
|
2011 |
function(_corrosion_initialize_properties target_name)
|
|
2012 |
# Initialize the `<XYZ>_OUTPUT_DIRECTORY` properties based on `CMAKE_<XYZ>_OUTPUT_DIRECTORY`.
|
|
2013 |
foreach(output_var RUNTIME_OUTPUT_DIRECTORY ARCHIVE_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY PDB_OUTPUT_DIRECTORY)
|
|
2014 |
if (DEFINED "CMAKE_${output_var}")
|
|
2015 |
set_property(TARGET ${target_name} PROPERTY "${output_var}" "${CMAKE_${output_var}}")
|
|
2016 |
endif()
|
|
2017 |
|
|
2018 |
foreach(config_type ${CMAKE_CONFIGURATION_TYPES})
|
|
2019 |
string(TOUPPER "${config_type}" config_type_upper)
|
|
2020 |
if (DEFINED "CMAKE_${output_var}_${config_type_upper}")
|
|
2021 |
set_property(TARGET ${target_name} PROPERTY "${output_var}_${config_type_upper}" "${CMAKE_${output_var}_${config_type_upper}}")
|
|
2022 |
endif()
|
|
2023 |
endforeach()
|
|
2024 |
endforeach()
|
|
2025 |
endfunction()
|
|
2026 |
|
16021
|
2027 |
# Helper macro to pass through an optional `OPTION` argument parsed via `cmake_parse_arguments`
|
|
2028 |
# to another function that takes the same OPTION.
|
|
2029 |
# If the option was set, then the variable <var_name> will be set to the same option name again,
|
|
2030 |
# otherwise <var_name> will be unset.
|
|
2031 |
macro(_corrosion_option_passthrough_helper option_name prefix var_name)
|
|
2032 |
if(${${prefix}_${option_name}})
|
|
2033 |
set("${var_name}" "${option_name}")
|
|
2034 |
else()
|
|
2035 |
unset("${var_name}")
|
|
2036 |
endif()
|
|
2037 |
endmacro()
|
|
2038 |
|
|
2039 |
# Helper macro to pass through an optional argument with value(s), parsed via `cmake_parse_arguments`,
|
|
2040 |
# to another function that takes the same keyword + associated values.
|
|
2041 |
# If the argument was given, then the variable <var_name> will be a list of the argument name and the values,
|
|
2042 |
# which will be expanded, when calling the function (assuming no quotes).
|
|
2043 |
macro(_corrosion_arg_passthrough_helper arg_name prefix var_name)
|
|
2044 |
if(DEFINED "${prefix}_${arg_name}")
|
|
2045 |
set("${var_name}" "${arg_name}" "${${prefix}_${arg_name}}")
|
|
2046 |
else()
|
|
2047 |
unset("${var_name}")
|
|
2048 |
endif()
|
|
2049 |
endmacro()
|
|
2050 |
|
|
2051 |
list(POP_BACK CMAKE_MESSAGE_CONTEXT)
|
|
2052 |
|