|
1 | | -corrosion_import_crate(MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/Cargo.toml NO_LINKER_OVERRIDE) |
2 | | -set_property(TARGET rust-url PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/) |
| 1 | +# To avoid conflicts caused by multiple definitions of the same symbols, |
| 2 | +# we need to build all Rust crates as part of a single staticlib. |
| 3 | +# This is done by synthesizing a Rust crate that depends on all the others, |
| 4 | +# and contains `pub use` statements for each of them. |
| 5 | +# |
| 6 | +# You might ask why we don't just do all of this in Rust itself, without CMake |
| 7 | +# involved at all. |
| 8 | +# A key reason is that this setup enables us to add Rust crates wherever we'd also |
| 9 | +# be able to add C++ libraries, and have them all be built together. |
| 10 | +# Notably, this way we can have host-API implementations that include Rust code, |
| 11 | +# while keeping the host-API implementation fully self-contained, without changes |
| 12 | +# outside its own folder. |
3 | 13 |
|
4 | | -corrosion_import_crate(MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-multipart/Cargo.toml NO_LINKER_OVERRIDE CRATES multipart FEATURES capi) |
5 | | -set_property(TARGET multipart PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-multipart/) |
| 14 | +set(RUST_STATICLIB_RS "${CMAKE_CURRENT_BINARY_DIR}/rust-staticlib.rs" CACHE INTERNAL "Path to the Rust staticlibs bundler source file" FORCE) |
| 15 | +set(RUST_STATICLIB_TOML "${CMAKE_CURRENT_BINARY_DIR}/Cargo.toml" CACHE INTERNAL "Path to the Rust staticlibs bundler Cargo.toml file" FORCE) |
| 16 | +set(RUST_STATICLIB_LOCK "${CMAKE_CURRENT_BINARY_DIR}/Cargo.lock" CACHE INTERNAL "Path to the Rust staticlibs bundler Cargo.toml file" FORCE) |
| 17 | + |
| 18 | +configure_file("runtime/crates/staticlib-template/rust-staticlib.rs.in" "${RUST_STATICLIB_RS}" COPYONLY) |
| 19 | +configure_file("runtime/crates/staticlib-template/Cargo.toml.in" "${RUST_STATICLIB_TOML}") |
| 20 | +configure_file("runtime/crates/staticlib-template/Cargo.lock" "${RUST_STATICLIB_LOCK}" COPYONLY) |
| 21 | + |
| 22 | +corrosion_import_crate( |
| 23 | + MANIFEST_PATH ${RUST_STATICLIB_TOML} |
| 24 | + CRATES "rust-staticlib" |
| 25 | + NO_LINKER_OVERRIDE |
| 26 | +) |
| 27 | + |
| 28 | +# Add a Rust library to the staticlib bundle. |
| 29 | +function(add_rust_lib name path) |
| 30 | + add_library(${name} INTERFACE) |
| 31 | + target_include_directories(${name} INTERFACE "${path}") |
| 32 | + file(APPEND $CACHE{RUST_STATICLIB_TOML} "${name} = { path = \"${path}\", features = [${ARGN}] }\n") |
| 33 | + string(REPLACE "-" "_" name ${name}) |
| 34 | + file(APPEND $CACHE{RUST_STATICLIB_RS} "pub use ${name};\n") |
| 35 | +endfunction() |
| 36 | + |
| 37 | +# These two crates are needed by SpiderMonkey: |
| 38 | +add_rust_lib(rust-encoding "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-encoding") |
| 39 | +add_rust_lib(rust-hooks "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-hooks") |
| 40 | + |
| 41 | +# The rust-hooks crate needs a supporting CPP file |
| 42 | +add_library(rust-hooks-wrappers STATIC "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-hooks/src/wrappers.cpp") |
| 43 | +target_link_libraries(rust-hooks-wrappers PRIVATE spidermonkey) |
| 44 | +add_library(rust-crates STATIC ${CMAKE_CURRENT_BINARY_DIR}/null.cpp) |
| 45 | +target_link_libraries(rust-crates PRIVATE rust_staticlib rust-hooks-wrappers) |
| 46 | + |
| 47 | +# Add crates as needed here: |
| 48 | +add_rust_lib(rust-url "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url") |
| 49 | +add_rust_lib(multipart "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-multipart" "\"capi\", \"simd\"") |
0 commit comments