16021
|
1 |
## Usage
|
|
2 |
|
|
3 |
### Automatically import crate targets with `corrosion_import_crate`
|
|
4 |
|
|
5 |
In order to integrate a Rust crate into CMake, you first need to import Rust crates from
|
|
6 |
a [package] or [workspace]. Corrosion provides `corrosion_import_crate()` to automatically import
|
|
7 |
crates defined in a Cargo.toml Manifest file:
|
|
8 |
|
|
9 |
{{#include ../../cmake/Corrosion.cmake:corrosion-import-crate}}
|
|
10 |
|
|
11 |
Corrosion will use `cargo metadata` to add a cmake target for each crate defined in the Manifest file
|
|
12 |
and add the necessary rules to build the targets.
|
|
13 |
For Rust executables an [`IMPORTED`] executable target is created with the same name as defined in the `[[bin]]`
|
|
14 |
section of the Manifest corresponding to this target.
|
|
15 |
If no such name was defined the target name defaults to the Rust package name.
|
|
16 |
For Rust library targets an [`INTERFACE`] library target is created with the same name as defined in the `[lib]`
|
|
17 |
section of the Manifest. This `INTERFACE` library links an internal corrosion target, which is either a
|
|
18 |
`SHARED` or `STATIC` `IMPORTED` library, depending on the Rust crate type (`cdylib` vs `staticlib`).
|
|
19 |
|
|
20 |
The created library targets can be linked into other CMake targets by simply using [target_link_libraries].
|
|
21 |
|
|
22 |
Corrosion will by default copy the produced Rust artifacts into `${CMAKE_CURRENT_BINARY_DIR}`. The target location
|
|
23 |
can be changed by setting the CMake `OUTPUT_DIRECTORY` target properties on the imported Rust targets.
|
|
24 |
See the [OUTPUT_DIRECTORY](#cmake-output_directory-target-properties-and-imported_location) section for more details.
|
|
25 |
|
|
26 |
Many of the options available for `corrosion_import_crate` can also be individually set per
|
|
27 |
target, see [Per Target options](#per-target-options) for details.
|
|
28 |
|
|
29 |
[package]: https://doc.rust-lang.org/book/ch07-01-packages-and-crates.html
|
|
30 |
[workspace]: https://doc.rust-lang.org/cargo/reference/workspaces.html
|
|
31 |
[`IMPORTED`]: https://cmake.org/cmake/help/latest/prop_tgt/IMPORTED.html
|
|
32 |
[`INTERFACE`]: https://cmake.org/cmake/help/latest/command/add_library.html#interface-libraries
|
|
33 |
[target_link_libraries]: https://cmake.org/cmake/help/latest/command/target_link_libraries.html
|
|
34 |
|
16038
|
35 |
### Experimental: Install crate and headers with `corrosion_install`
|
|
36 |
|
|
37 |
The default CMake [install commands] do not work correctly with the targets exported from `corrosion_import_crate()`.
|
|
38 |
Corrosion provides `corrosion_install` to automatically install relevant files:
|
|
39 |
|
|
40 |
{{#include ../../cmake/Corrosion.cmake:corrosion-install}}
|
|
41 |
|
|
42 |
The example below shows how to import a rust library and make it available for install through CMake.
|
|
43 |
|
|
44 |
```cmake
|
|
45 |
include(FetchContent)
|
|
46 |
|
|
47 |
FetchContent_Declare(
|
|
48 |
Corrosion
|
|
49 |
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
|
|
50 |
GIT_TAG v0.5 # Optionally specify a commit hash, version tag or branch here
|
|
51 |
)
|
|
52 |
# Set any global configuration variables such as `Rust_TOOLCHAIN` before this line!
|
|
53 |
FetchContent_MakeAvailable(Corrosion)
|
|
54 |
|
|
55 |
# Import targets defined in a package or workspace manifest `Cargo.toml` file
|
|
56 |
corrosion_import_crate(MANIFEST_PATH rust-lib/Cargo.toml)
|
|
57 |
|
|
58 |
# Add a manually written header file which will be exported
|
|
59 |
# Requires CMake >=3.23
|
|
60 |
target_sources(rust-lib INTERFACE
|
|
61 |
FILE_SET HEADERS
|
|
62 |
BASE_DIRS include
|
|
63 |
FILES
|
|
64 |
include/rust-lib/rust-lib.h
|
|
65 |
)
|
|
66 |
|
|
67 |
# OR for CMake <= 3.23
|
|
68 |
target_include_directories(is_odd INTERFACE
|
|
69 |
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
|
70 |
$<INSTALL_INTERFACE:include>
|
|
71 |
)
|
|
72 |
target_sources(is_odd
|
|
73 |
INTERFACE
|
|
74 |
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/rust-lib/rust-lib.h>
|
|
75 |
$<INSTALL_INTERFACE:include/rust-lib/rust-lib.h>
|
|
76 |
)
|
|
77 |
|
|
78 |
# Rust libraries must be installed using `corrosion_install`.
|
|
79 |
corrosion_install(TARGETS rust-lib EXPORT RustLibTargets)
|
|
80 |
|
|
81 |
# Installs the main target
|
|
82 |
install(
|
|
83 |
EXPORT RustLibTargets
|
|
84 |
NAMESPACE RustLib::
|
|
85 |
DESTINATION lib/cmake/RustLib
|
|
86 |
)
|
|
87 |
|
|
88 |
# Necessary for packaging helper commands
|
|
89 |
include(CMakePackageConfigHelpers)
|
|
90 |
# Create a file for checking version compatibility
|
|
91 |
# Optional
|
|
92 |
write_basic_package_version_file(
|
|
93 |
"${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake"
|
|
94 |
VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
|
|
95 |
COMPATIBILITY AnyNewerVersion
|
|
96 |
)
|
|
97 |
|
|
98 |
# Configures the main config file that cmake loads
|
|
99 |
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
|
|
100 |
"${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake"
|
|
101 |
INSTALL_DESTINATION lib/cmake/RustLib
|
|
102 |
NO_SET_AND_CHECK_MACRO
|
|
103 |
NO_CHECK_REQUIRED_COMPONENTS_MACRO
|
|
104 |
)
|
|
105 |
# Config.cmake.in contains
|
|
106 |
# @PACKAGE_INIT@
|
|
107 |
#
|
|
108 |
# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargetsCorrosion.cmake)
|
|
109 |
# include(${CMAKE_CURRENT_LIST_DIR}/RustLibTargets.cmake)
|
|
110 |
|
|
111 |
# Install all generated files
|
|
112 |
install(FILES
|
|
113 |
${CMAKE_CURRENT_BINARY_DIR}/RustLibConfigVersion.cmake
|
|
114 |
${CMAKE_CURRENT_BINARY_DIR}/RustLibConfig.cmake
|
|
115 |
${CMAKE_CURRENT_BINARY_DIR}/corrosion/RustLibTargetsCorrosion.cmake
|
|
116 |
DESTINATION lib/cmake/RustLib
|
|
117 |
)
|
|
118 |
```
|
|
119 |
|
|
120 |
[install commands]: https://cmake.org/cmake/help/latest/command/install.html
|
|
121 |
|
16021
|
122 |
### Per Target options
|
|
123 |
|
|
124 |
Some configuration options can be specified individually for each target. You can set them via the
|
|
125 |
`corrosion_set_xxx()` functions specified below:
|
|
126 |
|
|
127 |
- `corrosion_set_env_vars(<target_name> <key1=value1> [... <keyN=valueN>])`: Define environment variables
|
|
128 |
that should be set during the invocation of `cargo build` for the specified target. Please note that
|
|
129 |
the environment variable will only be set for direct builds of the target via cmake, and not for any
|
|
130 |
build where cargo built the crate in question as a dependency for another target.
|
|
131 |
The environment variables may contain generator expressions.
|
|
132 |
- `corrosion_add_target_rustflags(<target_name> <rustflag> [... <rustflagN>])`: When building the target,
|
|
133 |
the `RUSTFLAGS` environment variable will contain the flags added via this function. Please note that any
|
|
134 |
dependencies (built by cargo) will also see these flags. See also: `corrosion_add_target_local_rustflags`.
|
|
135 |
- `corrosion_add_target_local_rustflags(target_name rustc_flag [more_flags ...])`: Support setting
|
|
136 |
rustflags for only the main target (crate) and none of its dependencies.
|
|
137 |
This is useful in cases where you only need rustflags on the main-crate, but need to set different
|
|
138 |
flags for different targets. Without "local" Rustflags this would require rebuilds of the
|
|
139 |
dependencies when switching targets.
|
|
140 |
- `corrosion_set_hostbuild(<target_name>)`: The target should be compiled for the Host target and ignore any
|
|
141 |
cross-compile configuration.
|
|
142 |
- `corrosion_set_features(<target_name> [ALL_FEATURES <Bool>] [NO_DEFAULT_FEATURES] [FEATURES <feature1> ... ])`:
|
|
143 |
For a given target, enable specific features via `FEATURES`, toggle `ALL_FEATURES` on or off or disable all features
|
|
144 |
via `NO_DEFAULT_FEATURES`. For more information on features, please see also the
|
|
145 |
[cargo reference](https://doc.rust-lang.org/cargo/reference/features.html).
|
|
146 |
- `corrosion_set_cargo_flags(<target_name> <flag1> ...])`:
|
|
147 |
For a given target, add options and flags at the end of `cargo build` invocation. This will be appended after any
|
|
148 |
arguments passed through the `FLAGS` during the crate import.
|
|
149 |
- `corrosion_set_linker(target_name linker)`: Use `linker` to link the target.
|
|
150 |
Please note that this only has an effect for targets where the final linker invocation is done
|
|
151 |
by cargo, i.e. targets where foreign code is linked into rust code and not the other way around.
|
|
152 |
Please also note that if you are cross-compiling and specify a linker such as `clang`, you are
|
|
153 |
responsible for also adding a rustflag which adds the necessary `--target=` argument for the
|
|
154 |
linker.
|
|
155 |
|
|
156 |
|
|
157 |
### Global Corrosion Options
|
16038
|
158 |
|
|
159 |
#### Selecting the Rust toolchain and target triple
|
|
160 |
|
|
161 |
The following variables are evaluated automatically in most cases. In typical cases you
|
16021
|
162 |
shouldn't need to alter any of these. If you do want to specify them manually, make sure to set
|
|
163 |
them **before** `find_package(Corrosion REQUIRED)`.
|
|
164 |
|
|
165 |
- `Rust_TOOLCHAIN:STRING` - Specify a named rustup toolchain to use. Changes to this variable
|
|
166 |
resets all other options. Default: If the first-found `rustc` is a `rustup` proxy, then the default
|
|
167 |
rustup toolchain (see `rustup show`) is used. Otherwise, the variable is unset by default.
|
|
168 |
- `Rust_ROOT:STRING` - CMake provided. Path to a Rust toolchain to use. This is an alternative if
|
|
169 |
you want to select a specific Rust toolchain, but it's not managed by rustup. Default: Nothing
|
|
170 |
- `Rust_COMPILER:STRING` - Path to `rustc`, which should be used for compiling or for toolchain
|
|
171 |
detection (if it is a `rustup` proxy). Default: The `rustc` in the first-found toolchain, either
|
|
172 |
from `rustup`, or from a toolchain available in the user's `PATH`.
|
|
173 |
- `Rust_RESOLVE_RUSTUP_TOOLCHAINS:BOOL` - If the found `rustc` is a `rustup` proxy, resolve a
|
|
174 |
concrete path to a specific toolchain managed by `rustup`, according to the `rustup` toolchain
|
|
175 |
selection rules and other options detailed here. If this option is turned off, the found `rustc`
|
|
176 |
will be used as-is to compile, even if it is a `rustup` proxy, which might increase compilation
|
|
177 |
time. Default: `ON` if the found `rustc` is a rustup proxy or a `rustup` managed toolchain was
|
|
178 |
requested, `OFF` otherwise. Forced `OFF` if `rustup` was not found.
|
|
179 |
- `Rust_CARGO:STRING` - Path to `cargo`. Default: the `cargo` installed next to `${Rust_COMPILER}`.
|
|
180 |
- `Rust_CARGO_TARGET:STRING` - The default target triple to build for. Alter for cross-compiling.
|
|
181 |
Default: On Visual Studio Generator, the matching triple for `CMAKE_VS_PLATFORM_NAME`. Otherwise,
|
|
182 |
the default target triple reported by `${Rust_COMPILER} --version --verbose`.
|
16038
|
183 |
|
|
184 |
#### Enable Convenience Options
|
|
185 |
|
|
186 |
The following options are off by default, but may increase convenience:
|
|
187 |
|
|
188 |
- `Rust_RUSTUP_INSTALL_MISSING_TARGET:BOOL`: Automatically install a missing target via `rustup` instead of failing.
|
16021
|
189 |
|
|
190 |
|
|
191 |
#### Developer/Maintainer Options
|
|
192 |
These options are not used in the course of normal Corrosion usage, but are used to configure how
|
|
193 |
Corrosion is built and installed. Only applies to Corrosion builds and subdirectory uses.
|
|
194 |
|
|
195 |
- `CORROSION_BUILD_TESTS:BOOL` - Build the Corrosion tests. Default: `Off` if Corrosion is a
|
|
196 |
subdirectory, `ON` if it is the top-level project
|
|
197 |
|
|
198 |
|
|
199 |
### Information provided by Corrosion
|
|
200 |
|
|
201 |
For your convenience, Corrosion sets a number of variables which contain information about the version of the rust
|
|
202 |
toolchain. You can use the CMake version comparison operators
|
|
203 |
(e.g. [`VERSION_GREATER_EQUAL`](https://cmake.org/cmake/help/latest/command/if.html#version-comparisons)) on the main
|
|
204 |
variable (e.g. `if(Rust_VERSION VERSION_GREATER_EQUAL "1.57.0")`), or you can inspect the major, minor and patch
|
|
205 |
versions individually.
|
|
206 |
- `Rust_VERSION<_MAJOR|_MINOR|_PATCH>` - The version of rustc.
|
|
207 |
- `Rust_CARGO_VERSION<_MAJOR|_MINOR|_PATCH>` - The cargo version.
|
|
208 |
- `Rust_LLVM_VERSION<_MAJOR|_MINOR|_PATCH>` - The LLVM version used by rustc.
|
|
209 |
- `Rust_IS_NIGHTLY` - 1 if a nightly toolchain is used, otherwise 0. Useful for selecting an unstable feature for a
|
|
210 |
crate, that is only available on nightly toolchains.
|
|
211 |
- Cache variables containing information based on the target triple for the selected target
|
|
212 |
as well as the default host target:
|
|
213 |
- `Rust_CARGO_TARGET_ARCH`, `Rust_CARGO_HOST_ARCH`: e.g. `x86_64` or `aarch64`
|
|
214 |
- `Rust_CARGO_TARGET_VENDOR`, `Rust_CARGO_HOST_VENDOR`: e.g. `apple`, `pc`, `unknown` etc.
|
|
215 |
- `Rust_CARGO_TARGET_OS`, `Rust_CARGO_HOST_OS`: e.g. `darwin`, `linux`, `windows`, `none`
|
|
216 |
- `Rust_CARGO_TARGET_ENV`, `Rust_CARGO_HOST_ENV`: e.g. `gnu`, `musl`
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
### Selecting a custom cargo profile
|
|
222 |
|
|
223 |
[Rust 1.57](https://blog.rust-lang.org/2021/12/02/Rust-1.57.0.html) stabilized the support for custom
|
|
224 |
[profiles](https://doc.rust-lang.org/cargo/reference/profiles.html). If you are using a sufficiently new rust toolchain,
|
|
225 |
you may select a custom profile by adding the optional argument `PROFILE <profile_name>` to
|
|
226 |
`corrosion_import_crate()`. If you do not specify a profile, or you use an older toolchain, corrosion will select
|
|
227 |
the standard `dev` profile if the CMake config is either `Debug` or unspecified. In all other cases the `release`
|
|
228 |
profile is chosen for cargo.
|
|
229 |
|
|
230 |
### Importing C-Style Libraries Written in Rust
|
|
231 |
Corrosion makes it completely trivial to import a crate into an existing CMake project. Consider
|
|
232 |
a project called [rust2cpp](test/rust2cpp/rust2cpp) with the following file structure:
|
|
233 |
```
|
|
234 |
rust2cpp/
|
|
235 |
rust/
|
|
236 |
src/
|
|
237 |
lib.rs
|
|
238 |
Cargo.lock
|
|
239 |
Cargo.toml
|
|
240 |
CMakeLists.txt
|
|
241 |
main.cpp
|
|
242 |
```
|
|
243 |
|
|
244 |
This project defines a simple Rust lib crate, like so, in [`rust2cpp/rust/Cargo.toml`](test/rust2cpp/rust2cpp/rust/Cargo.toml):
|
|
245 |
```toml
|
|
246 |
[package]
|
|
247 |
name = "rust-lib"
|
|
248 |
version = "0.1.0"
|
|
249 |
authors = ["Andrew Gaspar <andrew.gaspar@outlook.com>"]
|
|
250 |
license = "MIT"
|
|
251 |
edition = "2018"
|
|
252 |
|
|
253 |
[dependencies]
|
|
254 |
|
|
255 |
[lib]
|
|
256 |
crate-type=["staticlib"]
|
|
257 |
```
|
|
258 |
|
|
259 |
In addition to `"staticlib"`, you can also use `"cdylib"`. In fact, you can define both with a
|
|
260 |
single crate and switch between which is used using the standard
|
|
261 |
[`BUILD_SHARED_LIBS`](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html) variable.
|
|
262 |
|
|
263 |
This crate defines a simple crate called `rust-lib`. Importing this crate into your
|
|
264 |
[CMakeLists.txt](test/rust2cpp/CMakeLists.txt) is trivial:
|
|
265 |
```cmake
|
|
266 |
# Note: you must have already included Corrosion for `corrosion_import_crate` to be available. See # the `Installation` section above.
|
|
267 |
|
|
268 |
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)
|
|
269 |
```
|
|
270 |
|
|
271 |
Now that you've imported the crate into CMake, all of the executables, static libraries, and dynamic
|
|
272 |
libraries defined in the Rust can be directly referenced. So, merely define your C++ executable as
|
|
273 |
normal in CMake and add your crate's library using target_link_libraries:
|
|
274 |
```cmake
|
|
275 |
add_executable(cpp-exe main.cpp)
|
|
276 |
target_link_libraries(cpp-exe PUBLIC rust-lib)
|
|
277 |
```
|
|
278 |
|
|
279 |
That's it! You're now linking your Rust library to your C++ library.
|
|
280 |
|
|
281 |
#### Generate Bindings to Rust Library Automatically
|
|
282 |
|
|
283 |
Currently, you must manually declare bindings in your C or C++ program to the exported routines and
|
|
284 |
types in your Rust project. You can see boths sides of this in
|
|
285 |
[the Rust code](test/rust2cpp/rust2cpp/rust/src/lib.rs) and in [the C++ code](test/rust2cpp/rust2cpp/main.cpp).
|
|
286 |
|
|
287 |
Integration with [cbindgen](https://github.com/eqrion/cbindgen) is
|
|
288 |
planned for the future.
|
|
289 |
|
|
290 |
### Importing Libraries Written in C and C++ Into Rust
|
|
291 |
|
|
292 |
The rust targets can be imported with `corrosion_import_crate()` into CMake.
|
|
293 |
For targets where the linker should be invoked by Rust corrosion provides
|
|
294 |
`corrosion_link_libraries()` to link your C/C++ libraries with the Rust target.
|
|
295 |
For additional linker flags you may use `corrosion_add_target_local_rustflags()`
|
|
296 |
and pass linker arguments via the `-Clink-args` flag to rustc. These flags will
|
|
297 |
only be passed to the final rustc invocation and not affect any rust dependencies.
|
|
298 |
|
|
299 |
C bindings can be generated via [bindgen](https://github.com/rust-lang/rust-bindgen).
|
|
300 |
Corrosion does not offer any direct integration yet, but you can either generate the
|
|
301 |
bindings in the build-script of your crate, or generate the bindings as a CMake build step
|
|
302 |
(e.g. a custom target) and add a dependency from `cargo-prebuild_<rust_target>` to your
|
|
303 |
custom target for generating the bindings.
|
|
304 |
|
|
305 |
Example:
|
|
306 |
|
|
307 |
```cmake
|
|
308 |
# Import your Rust targets
|
|
309 |
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml)
|
|
310 |
# Link C/C++ libraries with your Rust target
|
|
311 |
corrosion_link_libraries(target_name c_library)
|
|
312 |
# Optionally explicitly define which linker to use.
|
|
313 |
corrosion_set_linker(target_name your_custom_linker)
|
|
314 |
# Optionally set linker arguments
|
|
315 |
corrosion_add_target_local_rustflags(target_name "-Clink-args=<linker arguments>")
|
|
316 |
# Optionally tell CMake that the rust crate depends on another target (e.g. a code generator)
|
|
317 |
add_dependencies(cargo-prebuild_<target_name> custom_bindings_target)
|
|
318 |
```
|
|
319 |
|
|
320 |
### Cross Compiling
|
|
321 |
Corrosion attempts to support cross-compiling as generally as possible, though not all
|
|
322 |
configurations are tested. Cross-compiling is explicitly supported in the following scenarios.
|
|
323 |
|
|
324 |
In all cases, you will need to install the standard library for the Rust target triple. When using
|
|
325 |
Rustup, you can use it to install the target standard library:
|
|
326 |
|
|
327 |
```bash
|
|
328 |
rustup target add <target-rust-triple>
|
|
329 |
```
|
|
330 |
|
|
331 |
If the target triple is automatically derived, Corrosion will print the target during configuration.
|
|
332 |
For example:
|
|
333 |
|
|
334 |
```
|
|
335 |
-- Rust Target: aarch64-linux-android
|
|
336 |
```
|
|
337 |
|
|
338 |
#### Windows-to-Windows
|
|
339 |
Corrosion supports cross-compiling between arbitrary Windows architectures using the Visual Studio
|
|
340 |
Generator. For example, to cross-compile for ARM64 from any platform, simply set the `-A`
|
|
341 |
architecture flag:
|
|
342 |
|
|
343 |
```bash
|
|
344 |
cmake -S. -Bbuild-arm64 -A ARM64
|
|
345 |
cmake --build build-arm64
|
|
346 |
```
|
|
347 |
|
|
348 |
Please note that for projects containing a build-script at least Rust 1.54 is required due to a bug
|
|
349 |
in previous cargo versions, which causes the build-script to incorrectly be built for the target
|
|
350 |
platform.
|
|
351 |
|
|
352 |
#### Linux-to-Linux
|
|
353 |
In order to cross-compile on Linux, you will need to install a cross-compiler. For example, on
|
|
354 |
Ubuntu, to cross compile for 64-bit Little-Endian PowerPC Little-Endian, install
|
|
355 |
`g++-powerpc64le-linux-gnu` from apt-get:
|
|
356 |
|
|
357 |
```bash
|
|
358 |
sudo apt install g++-powerpc64le-linux-gnu
|
|
359 |
```
|
|
360 |
|
|
361 |
Currently, Corrosion does not automatically determine the target triple while cross-compiling on
|
|
362 |
Linux, so you'll need to specify a matching `Rust_CARGO_TARGET`.
|
|
363 |
|
|
364 |
```bash
|
|
365 |
cmake -S. -Bbuild-ppc64le -DRust_CARGO_TARGET=powerpc64le-unknown-linux-gnu -DCMAKE_CXX_COMPILER=powerpc64le-linux-gnu-g++
|
|
366 |
cmake --build build-ppc64le
|
|
367 |
```
|
|
368 |
|
|
369 |
#### Android
|
|
370 |
|
|
371 |
Cross-compiling for Android is supported on all platforms with the Makefile and Ninja generators,
|
|
372 |
and the Rust target triple will automatically be selected. The CMake
|
|
373 |
[cross-compiling instructions for Android](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling-for-android)
|
|
374 |
apply here. For example, to build for ARM64:
|
|
375 |
|
|
376 |
```bash
|
|
377 |
cmake -S. -Bbuild-android-arm64 -GNinja -DCMAKE_SYSTEM_NAME=Android \
|
|
378 |
-DCMAKE_ANDROID_NDK=/path/to/android-ndk-rxxd -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a
|
|
379 |
```
|
|
380 |
|
|
381 |
**Important note:** The Android SDK ships with CMake 3.10 at newest, which Android Studio will
|
|
382 |
prefer over any CMake you've installed locally. CMake 3.10 is insufficient for using Corrosion,
|
16038
|
383 |
which requires a minimum of CMake 3.22. If you're using Android Studio to build your project,
|
16021
|
384 |
follow the instructions in the Android Studio documentation for
|
|
385 |
[using a specific version of CMake](https://developer.android.com/studio/projects/install-ndk#vanilla_cmake).
|
|
386 |
|
|
387 |
|
|
388 |
### CMake `OUTPUT_DIRECTORY` target properties and `IMPORTED_LOCATION`
|
|
389 |
|
16038
|
390 |
Corrosion respects the following `OUTPUT_DIRECTORY` target properties:
|
16021
|
391 |
- [ARCHIVE_OUTPUT_DIRECTORY](https://cmake.org/cmake/help/latest/prop_tgt/ARCHIVE_OUTPUT_DIRECTORY.html)
|
|
392 |
- [LIBRARY_OUTPUT_DIRECTORY](https://cmake.org/cmake/help/latest/prop_tgt/LIBRARY_OUTPUT_DIRECTORY.html)
|
|
393 |
- [RUNTIME_OUTPUT_DIRECTORY](https://cmake.org/cmake/help/latest/prop_tgt/RUNTIME_OUTPUT_DIRECTORY.html)
|
|
394 |
- [PDB_OUTPUT_DIRECTORY](https://cmake.org/cmake/help/latest/prop_tgt/PDB_OUTPUT_DIRECTORY.html)
|
|
395 |
|
|
396 |
If the target property is set (e.g. by defining the `CMAKE_XYZ_OUTPUT_DIRECTORY` variable before calling
|
|
397 |
`corrosion_import_crate()`), corrosion will copy the built rust artifacts to the location defined in the
|
|
398 |
target property.
|
|
399 |
Due to limitations in CMake these target properties are evaluated in a deferred manner, to
|
|
400 |
support the user setting the target properties after the call to `corrosion_import_crate()`.
|
|
401 |
This has the side effect that the `IMPORTED_LOCATION` property will be set late, and users should not
|
|
402 |
use `get_property` to read `IMPORTED_LOCATION` at configure time. Instead, generator expressions
|
|
403 |
should be used to get the location of the target artifact.
|
|
404 |
If `IMPORTED_LOCATION` is needed at configure time users may use `cmake_language(DEFER CALL ...)` to defer
|
|
405 |
evaluation to after the `IMPORTED_LOCATION` property is set.
|