Skip to content

Commit 86986de

Browse files
committed
Use crates_vendor-provided wasmtime-c-api-impl
Always uses prefixed wasmtime-c-api-impl, otherwise the prefixed implementation bitrots. Prefixes the headers in the repo rule to allow for globbing, which is not possible in a genrule. The crates_vendor-provided wasmtime-c-api-impl provided build allows us to upgrade wasmtime solely by changing the version number in the Cargo.toml file (and the corresponding repo in bazel/repositories.bzl for the C headers). Signed-off-by: Matt Leon <mattleon@google.com>
1 parent 90881fe commit 86986de

5 files changed

Lines changed: 60 additions & 96 deletions

File tree

BUILD

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -187,45 +187,6 @@ cc_library(
187187
],
188188
)
189189

190-
cc_library(
191-
name = "wasmtime_lib",
192-
srcs = [
193-
"src/common/types.h",
194-
"src/wasmtime/types.h",
195-
"src/wasmtime/wasmtime.cc",
196-
],
197-
hdrs = ["include/proxy-wasm/wasmtime.h"],
198-
copts = [
199-
"-DWASM_API_EXTERN=",
200-
],
201-
defines = [
202-
"PROXY_WASM_HAS_RUNTIME_WASMTIME",
203-
"PROXY_WASM_HOST_ENGINE_WASMTIME",
204-
],
205-
# See: https://bytecodealliance.github.io/wasmtime/c-api/
206-
linkopts = select({
207-
"@platforms//os:macos": [],
208-
"@platforms//os:windows": [
209-
"ws2_32.lib",
210-
"advapi32.lib",
211-
"userenv.lib",
212-
"ntdll.lib",
213-
"shell32.lib",
214-
"ole32.lib",
215-
"bcrypt.lib",
216-
],
217-
"//conditions:default": [
218-
"-ldl",
219-
"-lm",
220-
"-lpthread",
221-
],
222-
}),
223-
deps = [
224-
":wasm_vm_headers",
225-
"@com_github_bytecodealliance_wasmtime//:wasmtime_lib",
226-
],
227-
)
228-
229190
genrule(
230191
name = "prefixed_wasmtime_sources",
231192
srcs = [
@@ -239,15 +200,14 @@ genrule(
239200
cmd = """
240201
for file in $(SRCS); do
241202
sed -e 's/wasm_/wasmtime_wasm_/g' \
242-
-e 's/include\\/wasm.h/include\\/prefixed_wasm.h/g' \
243203
-e 's/wasmtime\\/types.h/wasmtime\\/prefixed_types.h/g' \
244204
$$file >$(@D)/$$(dirname $$file)/prefixed_$$(basename $$file)
245205
done
246206
""",
247207
)
248208

249209
cc_library(
250-
name = "prefixed_wasmtime_lib",
210+
name = "wasmtime_lib",
251211
srcs = [
252212
"src/common/types.h",
253213
"src/wasmtime/prefixed_types.h",
@@ -281,7 +241,7 @@ cc_library(
281241
}),
282242
deps = [
283243
":wasm_vm_headers",
284-
"@com_github_bytecodealliance_wasmtime//:prefixed_wasmtime_lib",
244+
"@com_github_bytecodealliance_wasmtime//:wasmtime_lib",
285245
],
286246
)
287247

@@ -299,6 +259,5 @@ cc_library(
299259
[":wasmedge_lib"],
300260
) + proxy_wasm_select_engine_wasmtime(
301261
[":wasmtime_lib"],
302-
[":prefixed_wasmtime_lib"],
303262
),
304263
)

bazel/cargo/wasmtime/Cargo.toml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ rust-version = "1.78.0"
2222
path = "fake_lib.rs"
2323

2424
[dependencies]
25-
env_logger = "0.10"
26-
anyhow = "1.0"
27-
once_cell = "1.12"
28-
log = {version = "0.4.8", default-features = false}
29-
tracing = "0.1.26"
30-
wasmtime = {version = "24.0.0", default-features = false, features = ['cranelift', 'runtime', 'gc', 'std']}
31-
wasmtime-c-api-macros = {git = "https://github.com/bytecodealliance/wasmtime", tag = "v24.0.0"}
25+
# Fixes testdata build error due to missing crate_features = ["std"]
26+
log = {version = "0.4.29", default-features = false, features = ['std']}
27+
wasmtime-c-api-impl = {version = "24.0.0", default-features = false, features = ['cranelift', 'wasi', 'wat']}

bazel/external/wasmtime.BUILD

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,66 @@ package(default_visibility = ["//visibility:public"])
2121

2222
cc_library(
2323
name = "wasmtime_lib",
24-
hdrs = [
25-
"crates/c-api/include/wasm.h",
26-
],
27-
deps = [
28-
":rust_c_api",
24+
srcs = [
25+
":prefixed_wasmtime_c_api_lib",
2926
],
27+
hdrs = glob(["crates/c-api/include/**"]) + [":wasmtime_conf.h"],
28+
includes = ["crates/c-api/include/"],
3029
)
3130

32-
genrule(
33-
name = "prefixed_wasmtime_c_api_headers",
31+
# Wrap wasmtime-c-api-impl in a rust_static_library so it can be used as a cc_library.
32+
rust_static_library(
33+
name = "wasmtime_lib_staticlib",
3434
srcs = [
35-
"crates/c-api/include/wasm.h",
35+
"lib.rs",
3636
],
37-
outs = [
38-
"crates/c-api/include/prefixed_wasm.h",
37+
edition = "2021",
38+
deps = [
39+
"@proxy_wasm_cpp_host//bazel/cargo/wasmtime/remote:wasmtime-c-api-impl",
3940
],
40-
cmd = """
41-
sed -e 's/\\ wasm_/\\ wasmtime_wasm_/g' \
42-
-e 's/\\*wasm_/\\*wasmtime_wasm_/g' \
43-
-e 's/(wasm_/(wasmtime_wasm_/g' \
44-
$(<) >$@
45-
""",
4641
)
4742

4843
genrule(
4944
name = "prefixed_wasmtime_c_api_lib",
5045
srcs = [
51-
":rust_c_api",
46+
":wasmtime_lib_staticlib",
5247
],
5348
outs = [
5449
"prefixed_wasmtime_c_api.a",
5550
],
5651
cmd = """
5752
for symbol in $$(nm -P $(<) 2>/dev/null | grep -E ^_?wasm_ | cut -d" " -f1); do
58-
echo $$symbol | sed -r 's/^(_?)(wasm_[a-z_]+)$$/\\1\\2 \\1wasmtime_\\2/' >>prefixed
53+
echo $$symbol | perl -p -e 's!^(_?)(wasm_[a-z_0-9.:-]+)$$!\\1\\2 \\1wasmtime_\\2!' >>prefixed
5954
done
6055
# This should be OBJCOPY, but bazel-zig-cc doesn't define it.
6156
objcopy --redefine-syms=prefixed $(<) $@
6257
""",
6358
toolchains = ["@bazel_tools//tools/cpp:current_cc_toolchain"],
64-
)
65-
66-
cc_library(
67-
name = "prefixed_wasmtime_lib",
68-
srcs = [
69-
":prefixed_wasmtime_c_api_lib",
59+
tools = [
60+
"@llvm_toolchain_llvm//:objcopy",
7061
],
71-
hdrs = [
72-
":prefixed_wasmtime_c_api_headers",
73-
],
74-
linkstatic = 1,
7562
)
7663

77-
rust_static_library(
78-
name = "rust_c_api",
79-
srcs = glob(["crates/c-api/src/**/*.rs"]),
80-
crate_features = ["cranelift"],
81-
crate_root = "crates/c-api/src/lib.rs",
82-
edition = "2021",
83-
proc_macro_deps = [
84-
"@proxy_wasm_cpp_host//bazel/cargo/wasmtime/remote:wasmtime-c-api-macros",
85-
],
86-
deps = [
87-
"@proxy_wasm_cpp_host//bazel/cargo/wasmtime/remote:anyhow",
88-
"@proxy_wasm_cpp_host//bazel/cargo/wasmtime/remote:env_logger",
89-
"@proxy_wasm_cpp_host//bazel/cargo/wasmtime/remote:once_cell",
90-
# buildifier: leave-alone
91-
"@proxy_wasm_cpp_host//bazel/cargo/wasmtime/remote:wasmtime",
92-
],
64+
# This must match the features defined in `bazel/cargo/wasmtime/Cargo.toml` for
65+
# the C/C++ API to expose the right set of methods.
66+
features = [
67+
"cranelift",
68+
"wat",
69+
"wasi",
70+
]
71+
72+
# Wasmtime C-api headers use cmakedefines to generate the config file.
73+
# This does the same as CMake's configure_file, but using the crate features array above.
74+
genrule(
75+
name = "wasmtime_conf.h",
76+
srcs = ["crates/c-api/include/wasmtime/conf.h.in"],
77+
outs = ["crates/c-api/include/wasmtime/conf.h"],
78+
cmd = """
79+
cat < $< > $$TMPDIR/working_file
80+
for enabled_feature in $$(echo "{}"); do
81+
perl -pi -e "s/#cmakedefine WASMTIME_FEATURE_$$enabled_feature/#define WASMTIME_FEATURE_$$enabled_feature 1/" $$TMPDIR/working_file
82+
done
83+
perl -pi -e 's?#cmakedefine (.*)?// \\1 is not defined.?' $$TMPDIR/working_file
84+
cp $$TMPDIR/working_file $@
85+
""".format(" ".join([f.upper() for f in features])),
9386
)

bazel/repositories.bzl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,20 @@ def proxy_wasm_cpp_host_repositories():
384384
sha256 = "2ccb49bb3bfa4d86907ad4c80d1147aef6156c7b6e3f7f14ed02a39de9761155",
385385
strip_prefix = "wasmtime-24.0.0",
386386
url = "https://github.com/bytecodealliance/wasmtime/archive/v24.0.0.tar.gz",
387+
# Prefix wasm_c_api functions for coexistence with other runtimes.
388+
patch_cmds = ["""
389+
find ./crates/c-api -type f -exec sed -i.bak \
390+
-e 's/\\ wasm_/\\ wasmtime_wasm_/g' \
391+
-e 's/\\*wasm_/\\*wasmtime_wasm_/g' \
392+
-e 's/^wasm_/wasmtime_wasm_/g' \
393+
-e 's/<wasm_/<wasmtime_wasm_/g' \
394+
-e 's/\\.wasm_/\\.wasmtime_wasm_/g' \
395+
-e 's/\\&wasm_/\\&wasmtime_wasm_/g' \
396+
-e 's/\\[wasm_/\\[wasmtime_wasm_/g' \
397+
-e 's/wasmtime_config_wasm_/wasmtime_config_wasmtime_wasm_/g' \
398+
-e 's/(wasm_/(wasmtime_wasm_/g' {} \\;
399+
cat >lib.rs <<EOF
400+
pub use wasmtime_c_api;
401+
EOF
402+
"""],
387403
)

bazel/select.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ def proxy_wasm_select_engine_wamr(xs):
3434
"//conditions:default": [],
3535
})
3636

37-
def proxy_wasm_select_engine_wasmtime(xs, xp):
37+
def proxy_wasm_select_engine_wasmtime(xs):
3838
return select({
3939
"@proxy_wasm_cpp_host//bazel:engine_wasmtime": xs,
40-
"@proxy_wasm_cpp_host//bazel:multiengine": xp,
40+
"@proxy_wasm_cpp_host//bazel:multiengine": xs,
4141
"//conditions:default": [],
4242
})
4343

0 commit comments

Comments
 (0)