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
This commit is contained in:
Kevin Lubick
2022-02-17 13:16:18 -05:00
committed by GitHub
parent d7d8fef23e
commit 600dc4d912
4 changed files with 18 additions and 0 deletions

View File

@@ -13,10 +13,14 @@ wasm_cc_binary(
BASE_LINKOPTS = [
"--bind", # Enable embind
"-sMODULARIZE",
"--pre-js",
"hello-embind-interface.js",
]
RELEASE_OPTS = [
"--closure=1", # Run the closure compiler
# Tell closure about the externs file, so as not to minify our JS public API.
"--closure-args=--externs=$(location hello-embind-externs.js)"
]
DEBUG_OPTS = [
@@ -36,6 +40,11 @@ config_setting(
cc_binary(
name = "hello-embind",
srcs = ["hello-embind.cc"],
features = ["emcc_debug_link"],
additional_linker_inputs = [
"hello-embind-externs.js",
"hello-embind-interface.js",
],
linkopts = select({
":debug_opts": BASE_LINKOPTS + DEBUG_OPTS,
":release_opts": BASE_LINKOPTS + RELEASE_OPTS,
@@ -50,3 +59,4 @@ wasm_cc_binary(
name = "hello-embind-wasm",
cc_target = ":hello-embind",
)

View File

@@ -0,0 +1,2 @@
// This file prevents customJSFunctionToTestClosure from being minified by the Closure compiler.
Module.customJSFunctionToTestClosure = function() {}

View File

@@ -0,0 +1,3 @@
Module.customJSFunctionToTestClosure = function(firstParam, secondParam) {
console.log("This function adds two numbers to get", firstParam + secondParam);
}

View File

@@ -27,5 +27,8 @@ bazel build //hello-world:hello-world-wasm-simd
cd test_external
bazel build //:hello-world-wasm
bazel build //:hello-embind-wasm --compilation_mode dbg # debug
# Test use of the closure compiler
bazel build //:hello-embind-wasm --compilation_mode opt # release
# This function should not be minified if the externs file is loaded correctly.
grep "customJSFunctionToTestClosure" bazel-bin/hello-embind-wasm/hello-embind.js