@@ -21,73 +21,66 @@ package(default_visibility = ["//visibility:public"])
2121
2222cc_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
4843genrule(
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)
0 commit comments