• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1load("@rules_cc//cc:defs.bzl", "cc_binary")
2load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")
3
4BASE_LINKOPTS = [
5    #"-flto",  # https://github.com/emscripten-core/emsdk/issues/807
6    "--bind",  # Compiles the source code using the Embind bindings to connect C/C++ and JavaScript
7    "-s ALLOW_MEMORY_GROWTH=1",
8    "-s USE_PTHREADS=0",  # Disable pthreads
9    "-s MODULARIZE=1",
10    "-s EXPORT_NAME=WebGPUKitInit",
11    "-s DISABLE_EXCEPTION_CATCHING=1",  # Disable all exception catching
12    "-s NODEJS_CATCH_EXIT=0",  # We don't have a 'main' so disable exit() catching
13    "-s WASM=1",
14    "-s USE_WEBGPU=1",
15    "-s ASYNCIFY",
16]
17
18RELEASE_OPTS = [
19    "-s ASSERTIONS=0",  # Turn off assertions
20    "-O3",
21
22    # TODO(armansito): The closure compiler doesn't play well with Asyncify such that an
23    # async Embind binding seems to lose its return value if it's awaited on from JS. While it
24    # isn't strictly necessary, our example awaits on `drawWithSkia` and uses the result to the
25    # update the HTML with a status message. Hence, we keep this turned off.
26    "--closure 0",  # Run the closure compiler
27]
28
29DEBUG_OPTS = [
30    "-s ASSERTIONS=1",  # Turn on assertions
31    "--closure 0",  # Do not use closure
32
33    # Building without optimizations causes Chrome to hit a limit when loading the WASM module with
34    # the following error message:
35    #   RuntimeError: Aborted(CompileError: WebAssembly.instantiate():
36    #       Compiling function #6515:"blur_x_radius_3((anonymous namespace)::SkNx<8, ..." failed:
37    #           local count too large @+6422486)
38    #
39    # As a workaround for now, we tell emscripten to enable optimizations while retaining some debug
40    # information.
41    "-O2 -g",
42]
43
44config_setting(
45    name = "release_opts",
46    values = {"compilation_mode": "opt"},
47)
48
49config_setting(
50    name = "debug_opts",
51    values = {"compilation_mode": "dbg"},
52)
53
54cc_binary(
55    name = "hello-world",
56    srcs = ["bindings.cpp"],
57    linkopts = select({
58        ":debug_opts": BASE_LINKOPTS + DEBUG_OPTS,
59        ":release_opts": BASE_LINKOPTS + RELEASE_OPTS,
60        "//conditions:default": BASE_LINKOPTS + RELEASE_OPTS,
61    }),
62    # This target won't build successfully on its own because of missing emscripten
63    # headers etc. Therefore, we hide it from wildcards.
64    tags = ["manual"],
65    deps = ["//:skia_public"],
66)
67
68wasm_cc_binary(
69    name = "hello-world-wasm",
70    cc_target = ":hello-world",
71)
72