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