aboutsummaryrefslogtreecommitdiff
path: root/gn/standalone/wasm.gni
blob: b3a6c1ad5eebe5c16441392b78efc486deca058f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright (C) 2018 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("../wasm_vars.gni")

emsdk_dir = rebase_path("//buildtools/emsdk", "")
nodejs_dir = rebase_path("//buildtools/nodejs", "")

# This variable is used by the //gn/standalone/toolchain/BUILD.gn.
em_config = "EMSCRIPTEN_ROOT='$emsdk_dir/emscripten';"
em_config += "LLVM_ROOT='$emsdk_dir/llvm';"
em_config += "BINARYEN_ROOT='$emsdk_dir/llvm/binaryen';"
em_config += "EMSCRIPTEN_NATIVE_OPTIMIZER='$emsdk_dir/llvm/optimizer';"
em_config += "NODE_JS='$nodejs_dir/bin/node';"
em_config += "COMPILER_ENGINE=NODE_JS;"
em_config += "JS_ENGINES=[NODE_JS];"
em_config = "\"$em_config\""

# Defines a WASM library target.
# Args:
#  generate_js: when true generates a .wasm file and a .js file that wraps it
#      and provides the boilerplate to initialize the module.
#  generate_html: when true generates also an example .html file which contains
#      a minimal console to interact with the module (useful for testing).
template("wasm_lib") {
  assert(defined(invoker.name))

  # If the name is foo the target_name must be foo_wasm.
  assert(invoker.name + "_wasm" == target_name)
  _lib_name = invoker.name

  if (is_wasm) {
    _target_ldflags = [
      "-s",
      "WASM=1",
      "-s",
      "DISABLE_EXCEPTION_CATCHING=1",
      "-s",
      "NO_DYNAMIC_EXECUTION=1",
      "-s",
      "TOTAL_MEMORY=33554432",
      "-s",
      "ALLOW_MEMORY_GROWTH=1",
      "-s",
      "RESERVED_FUNCTION_POINTERS=32",
      "-s",
      "BINARYEN_METHOD='native-wasm'",
      "-s",
      "BINARYEN_TRAP_MODE='clamp'",
      "-s",
      "EXPORT_FUNCTION_TABLES=1",
      "-s",
      "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap', 'addFunction', 'FS']",

      # This forces the MEMFS filesystem library to always use typed arrays
      # instead of building strings/arrays when appending to a file. This allows
      # to deal with pseudo-files larger than 128 MB when calling trace_to_text.
      "-s",
      "MEMFS_APPEND_TO_TYPED_ARRAYS=1",

      # Reduces global namespace pollution.
      "-s",
      "MODULARIZE=1",

      # This is to prevent that two different wasm modules end up generating
      # JS that overrides the same global variable (var Module = ...)
      "-s",
      "EXPORT_NAME=${target_name}",
    ]
    if (is_debug) {
      _target_ldflags += [ "-g4" ]
    } else {
      _target_ldflags += [ "-O3" ]
    }

    if (defined(invoker.js_library)) {
      _target_ldflags += [
        "--js-library",
        invoker.js_library,
      ]
    }

    _vars_to_forward = [
      "cflags",
      "defines",
      "deps",
      "includes",
      "sources",
      "include_dirs",
      "public_configs",
      "testonly",
      "visibility",
    ]

    executable("${_lib_name}.js") {
      forward_variables_from(invoker, _vars_to_forward)
      ldflags = _target_ldflags
      output_extension = ""
    }

    # This is just a workaround to deal with the fact that GN doesn't allow
    # spcifying extra outputs for an executable() target. In reality the .wasm
    # file here is generated by the executable() target above, together with the
    # .js file. This dummy target is here to tell GN "there is a target that
    # outputs also the .wasm file", so we can depend on that in copy() targets.
    action("${_lib_name}.wasm") {
      inputs = []
      deps = [
        ":${_lib_name}.js",
      ]
      outputs = [
        "$root_out_dir/$_lib_name.wasm",
      ]
      if (is_debug) {
        outputs += [ "$root_out_dir/$_lib_name.wasm.map" ]
      }
      args = [ "--noop" ]
      script = "//gn/standalone/build_tool_wrapper.py"
    }

    copy("${_lib_name}.d.ts") {
      sources = [
        "//gn/standalone/wasm_typescript_declaration.d.ts",
      ]
      outputs = [
        "$root_out_dir/$_lib_name.d.ts",
      ]
    }
  } else {  # is_wasm
    not_needed(invoker, "*")
  }

  group(target_name) {
    deps = [
      ":${_lib_name}.d.ts($wasm_toolchain)",
      ":${_lib_name}.js($wasm_toolchain)",
      ":${_lib_name}.wasm($wasm_toolchain)",
    ]
  }
}  # template