blob: 3b09531b3e85da7af8ad32a93ee750a62916381f [file] [log] [blame]
Primiano Tucci4bdc4c42018-05-10 15:52:33 +01001# Copyright (C) 2018 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Primiano Tucci02c11762019-08-30 00:57:59 +020015import("../wasm_vars.gni")
16
Primiano Tucci7e9865c2021-01-10 23:55:15 +010017# Used by //gn/standalone/toolchain/BUILD.gn .
18em_config = rebase_path(".emscripten", "")
19if (is_mac_host) {
20 emsdk_dir = rebase_path("//buildtools/mac/emsdk", "")
21} else {
22 emsdk_dir = rebase_path("//buildtools/linux64/emsdk", "")
23}
Primiano Tucci4bdc4c42018-05-10 15:52:33 +010024
25# Defines a WASM library target.
26# Args:
27# generate_js: when true generates a .wasm file and a .js file that wraps it
28# and provides the boilerplate to initialize the module.
29# generate_html: when true generates also an example .html file which contains
30# a minimal console to interact with the module (useful for testing).
31template("wasm_lib") {
Hector Dearmane8450742018-08-31 14:36:51 +010032 assert(defined(invoker.name))
33
34 # If the name is foo the target_name must be foo_wasm.
35 assert(invoker.name + "_wasm" == target_name)
36 _lib_name = invoker.name
Primiano Tucci3faad742018-05-16 19:30:48 +010037 if (is_wasm) {
Primiano Tucci7e9865c2021-01-10 23:55:15 +010038 _exports = "['ccall', 'callMain', 'addFunction', 'FS']"
Primiano Tucci3faad742018-05-16 19:30:48 +010039 _target_ldflags = [
40 "-s",
41 "WASM=1",
42 "-s",
Hector Dearman5f9da792023-03-27 20:12:49 +010043 "ENVIRONMENT=web,worker",
44 "-s",
Primiano Tucci3faad742018-05-16 19:30:48 +010045 "DISABLE_EXCEPTION_CATCHING=1",
46 "-s",
Hector Dearman5ebc8f92018-06-13 13:47:04 +010047 "NO_DYNAMIC_EXECUTION=1",
48 "-s",
Primiano Tucci7e9865c2021-01-10 23:55:15 +010049 "INITIAL_MEMORY=33554432",
Primiano Tucci76545c12018-10-03 00:24:57 +010050 "-s",
51 "ALLOW_MEMORY_GROWTH=1",
Primiano Tucci3faad742018-05-16 19:30:48 +010052 "-s",
Primiano Tucci7e9865c2021-01-10 23:55:15 +010053 "ALLOW_TABLE_GROWTH=1",
Primiano Tucci3faad742018-05-16 19:30:48 +010054 "-s",
Primiano Tucci7e9865c2021-01-10 23:55:15 +010055 "WASM_ASYNC_COMPILATION=0",
Primiano Tucci3faad742018-05-16 19:30:48 +010056 "-s",
Primiano Tucci7e9865c2021-01-10 23:55:15 +010057 "EXTRA_EXPORTED_RUNTIME_METHODS=" + _exports,
Primiano Tucci1c752c12018-10-23 09:27:19 +010058
59 # This forces the MEMFS filesystem library to always use typed arrays
60 # instead of building strings/arrays when appending to a file. This allows
Hector Dearmana9545e52022-05-17 12:23:25 +010061 # to deal with pseudo-files larger than 128 MB when calling traceconv.
Primiano Tucci1c752c12018-10-23 09:27:19 +010062 "-s",
63 "MEMFS_APPEND_TO_TYPED_ARRAYS=1",
64
65 # Reduces global namespace pollution.
66 "-s",
67 "MODULARIZE=1",
68
69 # This is to prevent that two different wasm modules end up generating
70 # JS that overrides the same global variable (var Module = ...)
71 "-s",
72 "EXPORT_NAME=${target_name}",
Primiano Tucci7e9865c2021-01-10 23:55:15 +010073
74 "-lworkerfs.js", # For FS.filesystems.WORKERFS
Primiano Tucci4bdc4c42018-05-10 15:52:33 +010075 ]
Hector Dearmana824ba92018-09-19 17:51:25 +010076 if (is_debug) {
Primiano Tucci4ed03f22021-03-15 14:26:07 +000077 _target_ldflags += [
78 "-s",
79 "ASSERTIONS=2",
80 "-s",
81 "SAFE_HEAP=1",
82 "-s",
83 "STACK_OVERFLOW_CHECK=1",
84 "-g4",
85 "-O0",
86 ]
Hector Dearmana824ba92018-09-19 17:51:25 +010087 } else {
Primiano Tucci4ed03f22021-03-15 14:26:07 +000088 _target_ldflags += [
89 "-s",
90 "ASSERTIONS=1",
91 "-g2", # Required for getting C++ symbol names.
92 "-O3",
93 ]
Primiano Tucci3faad742018-05-16 19:30:48 +010094 }
95
96 if (defined(invoker.js_library)) {
97 _target_ldflags += [
98 "--js-library",
99 invoker.js_library,
100 ]
101 }
102
103 _vars_to_forward = [
104 "cflags",
105 "defines",
106 "deps",
107 "includes",
108 "sources",
109 "include_dirs",
110 "public_configs",
111 "testonly",
112 "visibility",
113 ]
114
115 executable("${_lib_name}.js") {
116 forward_variables_from(invoker, _vars_to_forward)
117 ldflags = _target_ldflags
118 output_extension = ""
119 }
120
121 # This is just a workaround to deal with the fact that GN doesn't allow
122 # spcifying extra outputs for an executable() target. In reality the .wasm
123 # file here is generated by the executable() target above, together with the
124 # .js file. This dummy target is here to tell GN "there is a target that
125 # outputs also the .wasm file", so we can depend on that in copy() targets.
126 action("${_lib_name}.wasm") {
127 inputs = []
Primiano Tucci2925e9d2020-01-27 10:15:58 +0000128 deps = [ ":${_lib_name}.js" ]
129 outputs = [ "$root_out_dir/$_lib_name.wasm" ]
Hector Dearmana824ba92018-09-19 17:51:25 +0100130 if (is_debug) {
131 outputs += [ "$root_out_dir/$_lib_name.wasm.map" ]
132 }
Primiano Tucci3faad742018-05-16 19:30:48 +0100133 args = [ "--noop" ]
134 script = "//gn/standalone/build_tool_wrapper.py"
135 }
Hector Dearman21fa9162018-06-22 14:50:29 +0100136
137 copy("${_lib_name}.d.ts") {
Primiano Tucci2925e9d2020-01-27 10:15:58 +0000138 sources = [ "//gn/standalone/wasm_typescript_declaration.d.ts" ]
139 outputs = [ "$root_out_dir/$_lib_name.d.ts" ]
Hector Dearman21fa9162018-06-22 14:50:29 +0100140 }
Primiano Tucci3faad742018-05-16 19:30:48 +0100141 } else { # is_wasm
142 not_needed(invoker, "*")
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100143 }
144
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100145 group(target_name) {
Primiano Tucci3faad742018-05-16 19:30:48 +0100146 deps = [
Hector Dearman21fa9162018-06-22 14:50:29 +0100147 ":${_lib_name}.d.ts($wasm_toolchain)",
Primiano Tucci3faad742018-05-16 19:30:48 +0100148 ":${_lib_name}.js($wasm_toolchain)",
149 ":${_lib_name}.wasm($wasm_toolchain)",
150 ]
151 }
Primiano Tucci4bdc4c42018-05-10 15:52:33 +0100152} # template