Skip to content

Commit 600dc4d

Browse files
authored
Add closure example for wasm_cc_binary (#941)
* Stub out addition of closure_externs_file option in wasm_cc_binary * WIP * args are passed, externs cannot be found * try location * update tests
1 parent d7d8fef commit 600dc4d

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

bazel/test_external/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ wasm_cc_binary(
1313
BASE_LINKOPTS = [
1414
"--bind", # Enable embind
1515
"-sMODULARIZE",
16+
"--pre-js",
17+
"hello-embind-interface.js",
1618
]
1719

1820
RELEASE_OPTS = [
1921
"--closure=1", # Run the closure compiler
22+
# Tell closure about the externs file, so as not to minify our JS public API.
23+
"--closure-args=--externs=$(location hello-embind-externs.js)"
2024
]
2125

2226
DEBUG_OPTS = [
@@ -36,6 +40,11 @@ config_setting(
3640
cc_binary(
3741
name = "hello-embind",
3842
srcs = ["hello-embind.cc"],
43+
features = ["emcc_debug_link"],
44+
additional_linker_inputs = [
45+
"hello-embind-externs.js",
46+
"hello-embind-interface.js",
47+
],
3948
linkopts = select({
4049
":debug_opts": BASE_LINKOPTS + DEBUG_OPTS,
4150
":release_opts": BASE_LINKOPTS + RELEASE_OPTS,
@@ -50,3 +59,4 @@ wasm_cc_binary(
5059
name = "hello-embind-wasm",
5160
cc_target = ":hello-embind",
5261
)
62+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// This file prevents customJSFunctionToTestClosure from being minified by the Closure compiler.
2+
Module.customJSFunctionToTestClosure = function() {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Module.customJSFunctionToTestClosure = function(firstParam, secondParam) {
2+
console.log("This function adds two numbers to get", firstParam + secondParam);
3+
}

test/test_bazel.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ bazel build //hello-world:hello-world-wasm-simd
2727
cd test_external
2828
bazel build //:hello-world-wasm
2929
bazel build //:hello-embind-wasm --compilation_mode dbg # debug
30+
3031
# Test use of the closure compiler
3132
bazel build //:hello-embind-wasm --compilation_mode opt # release
33+
# This function should not be minified if the externs file is loaded correctly.
34+
grep "customJSFunctionToTestClosure" bazel-bin/hello-embind-wasm/hello-embind.js

0 commit comments

Comments
 (0)