Skip to content

Commit b4ccad4

Browse files
committed
Change Rust integration to emit a single static library instead of compiling multiple crates to static libs and trying to link all of them into the final binary
This also replaces linking SpiderMonkey's Rust static library with including the required crates into our own library, achieving an end result of having a single static Rust library to link.
1 parent 9511921 commit b4ccad4

File tree

25 files changed

+22320
-632
lines changed

25 files changed

+22320
-632
lines changed

CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ else()
8989
endif()
9090
endif()
9191

92-
target_link_libraries(starling.wasm PRIVATE host_api extension_api builtins spidermonkey rust-url)
92+
target_link_libraries(starling.wasm PRIVATE host_api extension_api builtins spidermonkey rust_staticlib rust-glue)
9393

9494
# build a compilation cache of ICs
9595
if(WEVAL)
@@ -112,9 +112,8 @@ endif()
112112

113113
set(RUNTIME_FILE "starling.wasm")
114114
set(ADAPTER_FILE "preview1-adapter.wasm")
115-
configure_file("componentize.sh" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" @ONLY)
115+
configure_file("componentize.sh.in" "${CMAKE_CURRENT_BINARY_DIR}/componentize.sh" @ONLY)
116116
configure_file(${ADAPTER} "${CMAKE_CURRENT_BINARY_DIR}/${ADAPTER_FILE}" COPYONLY)
117-
configure_file(spin.toml spin.toml COPYONLY)
118117

119118
function(componentize OUTPUT)
120119
set(options)

cmake/add_builtin.cmake

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ function(add_builtin)
9797
endfunction()
9898

9999

100-
function(add_rust_builtin)
100+
function(add_rust_builtin name path)
101101
# TODO: restore additional config args
102-
list(GET ARGN 0 LIB_NAME)
102+
set(LIB_NAME ${name})
103+
set(LIB_PATH ${path})
103104
set(DEFAULT_ENABLE ON)
104105
set(LIB_TARGET_NAME ${LIB_NAME})
105106
string(REPLACE "-" "_" LIB_NAME ${LIB_NAME})
@@ -123,7 +124,7 @@ function(add_rust_builtin)
123124

124125
message(STATUS "Adding builtin ${DESCRIPTION}")
125126

126-
target_link_libraries(builtins PRIVATE ${LIB_TARGET_NAME} rust-glue)
127+
add_rust_lib(${name} ${path})
127128
add_dependencies(${LIB_TARGET_NAME} rust-bindings)
128129

129130
file(APPEND $CACHE{INSTALL_BUILTINS} "RS_DEF(${LIB_NAME}_install)\n")

cmake/build-crates.cmake

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
corrosion_import_crate(MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/Cargo.toml NO_LINKER_OVERRIDE)
1+
set(RUST_STATICLIB_RS "${CMAKE_CURRENT_BINARY_DIR}/rust-staticlib.rs" CACHE INTERNAL "Path to the Rust staticlibs bundler source file" FORCE)
2+
set(RUST_STATICLIB_TOML "${CMAKE_CURRENT_BINARY_DIR}/Cargo.toml" CACHE INTERNAL "Path to the Rust staticlibs bundler Cargo.toml file" FORCE)
3+
configure_file("runtime/crates/staticlib-template/rust-staticlib.rs.in" "${RUST_STATICLIB_RS}" COPYONLY)
4+
5+
# Add the debugmozjs feature for debug builds.
6+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
7+
set(DEBUGMOZJS_FEATURE "\"debugmozjs\"")
8+
endif()
9+
configure_file("runtime/crates/staticlib-template/Cargo.toml.in" "${RUST_STATICLIB_TOML}")
210

311
corrosion_import_crate(
412
MANIFEST_PATH ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/Cargo.toml
5-
NO_LINKER_OVERRIDE
6-
CRATE_TYPES "staticlib"
7-
IMPORTED_CRATES crates_list
13+
CRATES "generate-bindings"
814
)
9-
10-
#list(REMOVE_ITEM crates_list "generate-bindings")
11-
foreach (crate IN LISTS crates_list)
12-
if (crate STREQUAL "generate-bindings")
13-
continue()
14-
endif ()
15-
add_dependencies("cargo-prebuild_${crate}" cargo-build_generate-bindings)
16-
endforeach ()
17-
18-
message(STATUS "Imported crates: ${crates_list}")
19-
20-
add_library(rust-glue STATIC ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/jsapi-rs/cpp/jsglue.cpp)
21-
target_include_directories(rust-glue PRIVATE ${SM_INCLUDE_DIR})
22-
add_dependencies(cargo-prebuild_generate-bindings rust-glue)
23-
24-
corrosion_set_env_vars(generate-bindings
25-
LIBCLANG_PATH=/opt/homebrew/opt/llvm/lib
15+
corrosion_set_env_vars(generate_bindings
2616
SYSROOT=${WASI_SDK_PREFIX}/share/wasi-sysroot
2717
CXXFLAGS="${CMAKE_CXX_FLAGS}"
2818
BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}
2919
SM_HEADERS=${SM_INCLUDE_DIR}
3020
RUST_LOG=bindgen
3121
)
3222

33-
set_property(TARGET rust-url PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url/)
23+
corrosion_import_crate(
24+
MANIFEST_PATH ${RUST_STATICLIB_TOML}
25+
CRATES "rust-staticlib"
26+
NO_LINKER_OVERRIDE
27+
)
28+
29+
add_dependencies("cargo-prebuild_rust_staticlib" cargo-build_generate_bindings)
30+
31+
add_library(rust-glue ${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/jsapi-rs/cpp/jsglue.cpp)
32+
target_include_directories(rust-glue PRIVATE ${SM_INCLUDE_DIR})
33+
add_dependencies(rust_staticlib rust-glue)
34+
35+
function(add_rust_lib name path)
36+
add_library(${name} INTERFACE)
37+
file(APPEND $CACHE{RUST_STATICLIB_TOML} "${name} = { path = \"${path}\" }\n")
38+
string(REPLACE "-" "_" name ${name})
39+
file(APPEND $CACHE{RUST_STATICLIB_RS} "pub use ${name};\n")
40+
endfunction()
41+
42+
add_rust_lib("rust-url" "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url")
43+
set_property(TARGET rust-url PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/crates/rust-url")

cmake/builtins.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ add_builtin(
9696
INCLUDE_DIRS
9797
runtime)
9898

99-
add_rust_builtin(test-builtin)
99+
add_rust_builtin(test-builtin "${CMAKE_CURRENT_SOURCE_DIR}/runtime/crates/test-builtin")

cmake/spidermonkey.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ file(GLOB SM_OBJS ${SM_SOURCE_DIR}/lib/*.o)
3939
add_library(spidermonkey STATIC)
4040
target_sources(spidermonkey PRIVATE ${SM_OBJS} ${CMAKE_CURRENT_BINARY_DIR}/null.cpp)
4141
set_property(TARGET spidermonkey PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${SM_INCLUDE_DIR})
42-
target_link_libraries(spidermonkey PUBLIC ${SM_SOURCE_DIR}/lib/libjs_static.a ${SM_SOURCE_DIR}/lib/libjsrust.a)
42+
target_link_libraries(spidermonkey PUBLIC ${SM_SOURCE_DIR}/lib/libjs_static.a)
4343

4444
add_compile_definitions("MOZ_JS_STREAMS")
File renamed without changes.

crates/rust-url/Cargo.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ license = "Apache-2.0 WITH LLVM-exception"
55
edition = "2021"
66

77
[lib]
8-
crate-type = ["staticlib"]
98

109
[dependencies]
1110
url = "2.5.2"
12-
13-
[profile.release]
14-
lto = true
15-
panic = 'abort'
16-
17-
[profile.dev]
18-
lto = true
19-
panic = 'abort'

runtime/crates/Cargo.lock

Lines changed: 50 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/crates/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@ repository = "https://github.com/bytecodealliance/starlingmonkey"
44
license = "MPL-2.0"
55
authors = ["The StarlingMonkey project contributors", "The Servo Project Developers"]
66
edition = "2021"
7+
resolver = "2"
78

89
[workspace]
910
members = [
1011
"jsapi-rs",
1112
"spidermonkey-rs",
1213
"starlingmonkey-rs",
1314
"generate-bindings",
14-
"test-builtin",
1515
]
1616
resolver = "2"
1717

1818
[workspace.dependencies]
1919
jsapi-rs = { path = "jsapi-rs" }
2020
starlingmonkey-rs = { path = "starlingmonkey-rs" }
2121
spidermonkey-rs = { path = "spidermonkey-rs" }
22+
2223
bindgen = { version = "0.69", default-features = false, features = [
2324
"runtime",
2425
"prettyplease",
2526
"experimental",
2627
] }
28+
libc = "0.2"
2729

2830
[profile.release]
2931
lto = true

runtime/crates/generate-bindings/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@ crate-type = ["staticlib"]
1010

1111
[build-dependencies]
1212
bindgen.workspace = true
13-
14-
[features]
15-
debugmozjs = []
16-
jitspew = []
17-
profilemozjs = []
18-
streams = []

0 commit comments

Comments
 (0)