| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
|
| 3 |
include(FBCMakeParseArgs) |
| 4 |
|
| 5 |
set( |
| 6 |
USE_CARGO_VENDOR AUTO CACHE STRING |
| 7 |
"Download Rust Crates from an internally vendored location" |
| 8 |
) |
| 9 |
set_property(CACHE USE_CARGO_VENDOR PROPERTY STRINGS AUTO ON OFF) |
| 10 |
|
| 11 |
set( |
| 12 |
GENERATE_CARGO_VENDOR_CONFIG AUTO CACHE STRING |
| 13 |
"Whether to generate Rust cargo vendor config or use existing" |
| 14 |
) |
| 15 |
set_property(CACHE GENERATE_CARGO_VENDOR_CONFIG PROPERTY STRINGS AUTO ON OFF) |
| 16 |
|
| 17 |
set(RUST_VENDORED_CRATES_DIR "$ENV{RUST_VENDORED_CRATES_DIR}") |
| 18 |
|
| 19 |
if("${USE_CARGO_VENDOR}" STREQUAL "AUTO") |
| 20 |
if(EXISTS "${RUST_VENDORED_CRATES_DIR}") |
| 21 |
set(USE_CARGO_VENDOR ON) |
| 22 |
else() |
| 23 |
set(USE_CARGO_VENDOR OFF) |
| 24 |
endif() |
| 25 |
endif() |
| 26 |
|
| 27 |
if("${GENERATE_CARGO_VENDOR_CONFIG}" STREQUAL "AUTO") |
| 28 |
set(GENERATE_CARGO_VENDOR_CONFIG "${USE_CARGO_VENDOR}") |
| 29 |
endif() |
| 30 |
|
| 31 |
if(GENERATE_CARGO_VENDOR_CONFIG) |
| 32 |
if(NOT EXISTS "${RUST_VENDORED_CRATES_DIR}") |
| 33 |
message( |
| 34 |
FATAL "vendored rust crates not present: " |
| 35 |
"${RUST_VENDORED_CRATES_DIR}" |
| 36 |
) |
| 37 |
endif() |
| 38 |
|
| 39 |
set(RUST_CARGO_HOME "${CMAKE_BINARY_DIR}/_cargo_home") |
| 40 |
file(MAKE_DIRECTORY "${RUST_CARGO_HOME}") |
| 41 |
|
| 42 |
file( |
| 43 |
TO_NATIVE_PATH "${RUST_VENDORED_CRATES_DIR}" |
| 44 |
ESCAPED_RUST_VENDORED_CRATES_DIR |
| 45 |
) |
| 46 |
string( |
| 47 |
REPLACE "\\" "\\\\" |
| 48 |
ESCAPED_RUST_VENDORED_CRATES_DIR |
| 49 |
"${ESCAPED_RUST_VENDORED_CRATES_DIR}" |
| 50 |
) |
| 51 |
file( |
| 52 |
WRITE "${RUST_CARGO_HOME}/config" |
| 53 |
"[source.crates-io]\n" |
| 54 |
"replace-with = \"vendored-sources\"\n" |
| 55 |
"\n" |
| 56 |
"[source.vendored-sources]\n" |
| 57 |
"directory = \"${ESCAPED_RUST_VENDORED_CRATES_DIR}\"\n" |
| 58 |
) |
| 59 |
endif() |
| 60 |
|
| 61 |
find_program(CARGO_COMMAND cargo REQUIRED) |
| 62 |
|
| 63 |
# Cargo is a build system in itself, and thus will try to take advantage of all |
| 64 |
# the cores on the system. Unfortunately, this conflicts with Ninja, since it |
| 65 |
# also tries to utilize all the cores. This can lead to a system that is |
| 66 |
# completely overloaded with compile jobs to the point where nothing else can |
| 67 |
# be achieved on the system. |
| 68 |
# |
| 69 |
# Let's inform Ninja of this fact so it won't try to spawn other jobs while |
| 70 |
# Rust being compiled. |
| 71 |
set_property(GLOBAL APPEND PROPERTY JOB_POOLS rust_job_pool=1) |
| 72 |
|
| 73 |
# This function creates an interface library target based on the static library |
| 74 |
# built by Cargo. It will call Cargo to build a staticlib and generate a CMake |
| 75 |
# interface library with it. |
| 76 |
# |
| 77 |
# This function requires `find_package(Python COMPONENTS Interpreter)`. |
| 78 |
# |
| 79 |
# You need to set `lib:crate-type = ["staticlib"]` in your Cargo.toml to make |
| 80 |
# Cargo build static library. |
| 81 |
# |
| 82 |
# ```cmake |
| 83 |
# rust_static_library(<TARGET> [CRATE <CRATE_NAME>] [FEATURES <FEATURE_NAME>]) |
| 84 |
# ``` |
| 85 |
# |
| 86 |
# Parameters: |
| 87 |
# - TARGET: |
| 88 |
# Name of the target name. This function will create an interface library |
| 89 |
# target with this name. |
| 90 |
# - CRATE_NAME: |
| 91 |
# Name of the crate. This parameter is optional. If unspecified, it will |
| 92 |
# fallback to `${TARGET}`. |
| 93 |
# - FEATURE_NAME: |
| 94 |
# Name of the Rust feature to enable. |
| 95 |
# |
| 96 |
# This function creates two targets: |
| 97 |
# - "${TARGET}": an interface library target contains the static library built |
| 98 |
# from Cargo. |
| 99 |
# - "${TARGET}.cargo": an internal custom target that invokes Cargo. |
| 100 |
# |
| 101 |
# If you are going to use this static library from C/C++, you will need to |
| 102 |
# write header files for the library (or generate with cbindgen) and bind these |
| 103 |
# headers with the interface library. |
| 104 |
# |
| 105 |
function(rust_static_library TARGET) |
| 106 |
fb_cmake_parse_args(ARG "" "CRATE;FEATURES" "" "${ARGN}") |
| 107 |
|
| 108 |
if(DEFINED ARG_CRATE) |
| 109 |
set(crate_name "${ARG_CRATE}") |
| 110 |
else() |
| 111 |
set(crate_name "${TARGET}") |
| 112 |
endif() |
| 113 |
if(DEFINED ARG_FEATURES) |
| 114 |
set(features --features ${ARG_FEATURES}) |
| 115 |
else() |
| 116 |
set(features ) |
| 117 |
endif() |
| 118 |
|
| 119 |
set(cargo_target "${TARGET}.cargo") |
| 120 |
set(target_dir $<IF:$<CONFIG:Debug>,debug,release>) |
| 121 |
set(staticlib_name "${CMAKE_STATIC_LIBRARY_PREFIX}${crate_name}${CMAKE_STATIC_LIBRARY_SUFFIX}") |
| 122 |
set(rust_staticlib "${CMAKE_CURRENT_BINARY_DIR}/${target_dir}/${staticlib_name}") |
| 123 |
|
| 124 |
if(DEFINED ARG_FEATURES) |
| 125 |
set(cargo_flags build $<IF:$<CONFIG:Debug>,,--release> -p ${crate_name} --features ${ARG_FEATURES}) |
| 126 |
else() |
| 127 |
set(cargo_flags build $<IF:$<CONFIG:Debug>,,--release> -p ${crate_name}) |
| 128 |
endif() |
| 129 |
if(USE_CARGO_VENDOR) |
| 130 |
set(extra_cargo_env "CARGO_HOME=${RUST_CARGO_HOME}") |
| 131 |
set(cargo_flags ${cargo_flags}) |
| 132 |
endif() |
| 133 |
|
| 134 |
add_custom_target( |
| 135 |
${cargo_target} |
| 136 |
COMMAND |
| 137 |
"${CMAKE_COMMAND}" -E remove -f "${CMAKE_CURRENT_SOURCE_DIR}/Cargo.lock" |
| 138 |
COMMAND |
| 139 |
"${CMAKE_COMMAND}" -E env |
| 140 |
"CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" |
| 141 |
${extra_cargo_env} |
| 142 |
${CARGO_COMMAND} |
| 143 |
${cargo_flags} |
| 144 |
COMMENT "Building Rust crate '${crate_name}'..." |
| 145 |
JOB_POOL rust_job_pool |
| 146 |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 147 |
BYPRODUCTS |
| 148 |
"${CMAKE_CURRENT_BINARY_DIR}/debug/${staticlib_name}" |
| 149 |
"${CMAKE_CURRENT_BINARY_DIR}/release/${staticlib_name}" |
| 150 |
) |
| 151 |
|
| 152 |
add_library(${TARGET} INTERFACE) |
| 153 |
add_dependencies(${TARGET} ${cargo_target}) |
| 154 |
set_target_properties( |
| 155 |
${TARGET} |
| 156 |
PROPERTIES |
| 157 |
INTERFACE_STATICLIB_OUTPUT_PATH "${rust_staticlib}" |
| 158 |
INTERFACE_INSTALL_LIBNAME |
| 159 |
"${CMAKE_STATIC_LIBRARY_PREFIX}${crate_name}_rs${CMAKE_STATIC_LIBRARY_SUFFIX}" |
| 160 |
) |
| 161 |
target_link_libraries( |
| 162 |
${TARGET} |
| 163 |
INTERFACE "$<BUILD_INTERFACE:${rust_staticlib}>" |
| 164 |
) |
| 165 |
endfunction() |
| 166 |
|
| 167 |
# This function instructs CMake to define a target that will use `cargo build` |
| 168 |
# to build a bin crate referenced by the Cargo.toml file in the current source |
| 169 |
# directory. |
| 170 |
# It accepts a single `TARGET` parameter which will be passed as the package |
| 171 |
# name to `cargo build -p TARGET`. If binary has different name as package, |
| 172 |
# use optional flag BINARY_NAME to override it. |
| 173 |
# It also accepts a `FEATURES` parameter if you want to enable certain features |
| 174 |
# in your Rust binary. |
| 175 |
# The CMake target will be registered to build by default as part of the |
| 176 |
# ALL target. |
| 177 |
function(rust_executable TARGET) |
| 178 |
fb_cmake_parse_args(ARG "" "BINARY_NAME;FEATURES" "" "${ARGN}") |
| 179 |
|
| 180 |
set(crate_name "${TARGET}") |
| 181 |
set(cargo_target "${TARGET}.cargo") |
| 182 |
set(target_dir $<IF:$<CONFIG:Debug>,debug,release>) |
| 183 |
|
| 184 |
if(DEFINED ARG_BINARY_NAME) |
| 185 |
set(executable_name "${ARG_BINARY_NAME}${CMAKE_EXECUTABLE_SUFFIX}") |
| 186 |
else() |
| 187 |
set(executable_name "${crate_name}${CMAKE_EXECUTABLE_SUFFIX}") |
| 188 |
endif() |
| 189 |
if(DEFINED ARG_FEATURES) |
| 190 |
set(features --features ${ARG_FEATURES}) |
| 191 |
else() |
| 192 |
set(features ) |
| 193 |
endif() |
| 194 |
|
| 195 |
if(DEFINED ARG_FEATURES) |
| 196 |
set(cargo_flags build $<IF:$<CONFIG:Debug>,,--release> -p ${crate_name} --features ${ARG_FEATURES}) |
| 197 |
else() |
| 198 |
set(cargo_flags build $<IF:$<CONFIG:Debug>,,--release> -p ${crate_name}) |
| 199 |
endif() |
| 200 |
if(USE_CARGO_VENDOR) |
| 201 |
set(extra_cargo_env "CARGO_HOME=${RUST_CARGO_HOME}") |
| 202 |
set(cargo_flags ${cargo_flags}) |
| 203 |
endif() |
| 204 |
|
| 205 |
add_custom_target( |
| 206 |
${cargo_target} |
| 207 |
ALL |
| 208 |
COMMAND |
| 209 |
"${CMAKE_COMMAND}" -E remove -f "${CMAKE_CURRENT_SOURCE_DIR}/Cargo.lock" |
| 210 |
COMMAND |
| 211 |
"${CMAKE_COMMAND}" -E env |
| 212 |
"CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}" |
| 213 |
${extra_cargo_env} |
| 214 |
${CARGO_COMMAND} |
| 215 |
${cargo_flags} |
| 216 |
COMMENT "Building Rust executable '${crate_name}'..." |
| 217 |
JOB_POOL rust_job_pool |
| 218 |
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 219 |
BYPRODUCTS |
| 220 |
"${CMAKE_CURRENT_BINARY_DIR}/debug/${executable_name}" |
| 221 |
"${CMAKE_CURRENT_BINARY_DIR}/release/${executable_name}" |
| 222 |
) |
| 223 |
|
| 224 |
set_property(TARGET "${cargo_target}" |
| 225 |
PROPERTY EXECUTABLE "${CMAKE_CURRENT_BINARY_DIR}/${target_dir}/${executable_name}") |
| 226 |
endfunction() |
| 227 |
|
| 228 |
# This function can be used to install the executable generated by a prior |
| 229 |
# call to the `rust_executable` function. |
| 230 |
# It requires a `TARGET` parameter to identify the target to be installed, |
| 231 |
# and an optional `DESTINATION` parameter to specify the installation |
| 232 |
# directory. If DESTINATION is not specified then the `bin` directory |
| 233 |
# will be assumed. |
| 234 |
function(install_rust_executable TARGET) |
| 235 |
# Parse the arguments |
| 236 |
set(one_value_args DESTINATION) |
| 237 |
set(multi_value_args) |
| 238 |
fb_cmake_parse_args( |
| 239 |
ARG "" "${one_value_args}" "${multi_value_args}" "${ARGN}" |
| 240 |
) |
| 241 |
|
| 242 |
if(NOT DEFINED ARG_DESTINATION) |
| 243 |
set(ARG_DESTINATION bin) |
| 244 |
endif() |
| 245 |
|
| 246 |
get_target_property(foo "${TARGET}.cargo" EXECUTABLE) |
| 247 |
|
| 248 |
install( |
| 249 |
PROGRAMS "${foo}" |
| 250 |
DESTINATION "${ARG_DESTINATION}" |
| 251 |
) |
| 252 |
endfunction() |
| 253 |
|
| 254 |
# This function installs the interface target generated from the function |
| 255 |
# `rust_static_library`. Use this function if you want to export your Rust |
| 256 |
# target to external CMake targets. |
| 257 |
# |
| 258 |
# ```cmake |
| 259 |
# install_rust_static_library( |
| 260 |
# <TARGET> |
| 261 |
# INSTALL_DIR <INSTALL_DIR> |
| 262 |
# [EXPORT <EXPORT_NAME>] |
| 263 |
# ) |
| 264 |
# ``` |
| 265 |
# |
| 266 |
# Parameters: |
| 267 |
# - TARGET: Name of the Rust static library target. |
| 268 |
# - EXPORT_NAME: Name of the exported target. |
| 269 |
# - INSTALL_DIR: Path to the directory where this library will be installed. |
| 270 |
# |
| 271 |
function(install_rust_static_library TARGET) |
| 272 |
fb_cmake_parse_args(ARG "" "EXPORT;INSTALL_DIR" "" "${ARGN}") |
| 273 |
|
| 274 |
get_property( |
| 275 |
staticlib_output_path |
| 276 |
TARGET "${TARGET}" |
| 277 |
PROPERTY INTERFACE_STATICLIB_OUTPUT_PATH |
| 278 |
) |
| 279 |
get_property( |
| 280 |
staticlib_output_name |
| 281 |
TARGET "${TARGET}" |
| 282 |
PROPERTY INTERFACE_INSTALL_LIBNAME |
| 283 |
) |
| 284 |
|
| 285 |
if(NOT DEFINED staticlib_output_path) |
| 286 |
message(FATAL_ERROR "Not a rust_static_library target.") |
| 287 |
endif() |
| 288 |
|
| 289 |
if(NOT DEFINED ARG_INSTALL_DIR) |
| 290 |
message(FATAL_ERROR "Missing required argument.") |
| 291 |
endif() |
| 292 |
|
| 293 |
if(DEFINED ARG_EXPORT) |
| 294 |
set(install_export_args EXPORT "${ARG_EXPORT}") |
| 295 |
endif() |
| 296 |
|
| 297 |
set(install_interface_dir "${ARG_INSTALL_DIR}") |
| 298 |
if(NOT IS_ABSOLUTE "${install_interface_dir}") |
| 299 |
set(install_interface_dir "\${_IMPORT_PREFIX}/${install_interface_dir}") |
| 300 |
endif() |
| 301 |
|
| 302 |
target_link_libraries( |
| 303 |
${TARGET} INTERFACE |
| 304 |
"$<INSTALL_INTERFACE:${install_interface_dir}/${staticlib_output_name}>" |
| 305 |
) |
| 306 |
install( |
| 307 |
TARGETS ${TARGET} |
| 308 |
${install_export_args} |
| 309 |
LIBRARY DESTINATION ${ARG_INSTALL_DIR} |
| 310 |
) |
| 311 |
install( |
| 312 |
FILES ${staticlib_output_path} |
| 313 |
RENAME ${staticlib_output_name} |
| 314 |
DESTINATION ${ARG_INSTALL_DIR} |
| 315 |
) |
| 316 |
endfunction() |