Primiano Tucci | 4bdc4c4 | 2018-05-10 15:52:33 +0100 | [diff] [blame] | 1 | # 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 Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 15 | import("../wasm_vars.gni") |
| 16 | |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 17 | # Used by //gn/standalone/toolchain/BUILD.gn . |
| 18 | em_config = rebase_path(".emscripten", "") |
| 19 | if (is_mac_host) { |
| 20 | emsdk_dir = rebase_path("//buildtools/mac/emsdk", "") |
| 21 | } else { |
| 22 | emsdk_dir = rebase_path("//buildtools/linux64/emsdk", "") |
| 23 | } |
Primiano Tucci | 4bdc4c4 | 2018-05-10 15:52:33 +0100 | [diff] [blame] | 24 | |
| 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). |
| 31 | template("wasm_lib") { |
Hector Dearman | e845074 | 2018-08-31 14:36:51 +0100 | [diff] [blame] | 32 | 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 Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 37 | if (is_wasm) { |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 38 | _exports = "['ccall', 'callMain', 'addFunction', 'FS']" |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 39 | _target_ldflags = [ |
| 40 | "-s", |
| 41 | "WASM=1", |
| 42 | "-s", |
Hector Dearman | 5f9da79 | 2023-03-27 20:12:49 +0100 | [diff] [blame] | 43 | "ENVIRONMENT=web,worker", |
| 44 | "-s", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 45 | "DISABLE_EXCEPTION_CATCHING=1", |
| 46 | "-s", |
Hector Dearman | 5ebc8f9 | 2018-06-13 13:47:04 +0100 | [diff] [blame] | 47 | "NO_DYNAMIC_EXECUTION=1", |
| 48 | "-s", |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 49 | "INITIAL_MEMORY=33554432", |
Primiano Tucci | 76545c1 | 2018-10-03 00:24:57 +0100 | [diff] [blame] | 50 | "-s", |
| 51 | "ALLOW_MEMORY_GROWTH=1", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 52 | "-s", |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 53 | "ALLOW_TABLE_GROWTH=1", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 54 | "-s", |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 55 | "WASM_ASYNC_COMPILATION=0", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 56 | "-s", |
Primiano Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 57 | "EXTRA_EXPORTED_RUNTIME_METHODS=" + _exports, |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 58 | |
| 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 Dearman | a9545e5 | 2022-05-17 12:23:25 +0100 | [diff] [blame] | 61 | # to deal with pseudo-files larger than 128 MB when calling traceconv. |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 62 | "-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 Tucci | 7e9865c | 2021-01-10 23:55:15 +0100 | [diff] [blame] | 73 | |
| 74 | "-lworkerfs.js", # For FS.filesystems.WORKERFS |
Primiano Tucci | 4bdc4c4 | 2018-05-10 15:52:33 +0100 | [diff] [blame] | 75 | ] |
Hector Dearman | a824ba9 | 2018-09-19 17:51:25 +0100 | [diff] [blame] | 76 | if (is_debug) { |
Primiano Tucci | 4ed03f2 | 2021-03-15 14:26:07 +0000 | [diff] [blame] | 77 | _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 Dearman | a824ba9 | 2018-09-19 17:51:25 +0100 | [diff] [blame] | 87 | } else { |
Primiano Tucci | 4ed03f2 | 2021-03-15 14:26:07 +0000 | [diff] [blame] | 88 | _target_ldflags += [ |
| 89 | "-s", |
| 90 | "ASSERTIONS=1", |
| 91 | "-g2", # Required for getting C++ symbol names. |
| 92 | "-O3", |
| 93 | ] |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 94 | } |
| 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 Tucci | 2925e9d | 2020-01-27 10:15:58 +0000 | [diff] [blame] | 128 | deps = [ ":${_lib_name}.js" ] |
| 129 | outputs = [ "$root_out_dir/$_lib_name.wasm" ] |
Hector Dearman | a824ba9 | 2018-09-19 17:51:25 +0100 | [diff] [blame] | 130 | if (is_debug) { |
| 131 | outputs += [ "$root_out_dir/$_lib_name.wasm.map" ] |
| 132 | } |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 133 | args = [ "--noop" ] |
| 134 | script = "//gn/standalone/build_tool_wrapper.py" |
| 135 | } |
Hector Dearman | 21fa916 | 2018-06-22 14:50:29 +0100 | [diff] [blame] | 136 | |
| 137 | copy("${_lib_name}.d.ts") { |
Primiano Tucci | 2925e9d | 2020-01-27 10:15:58 +0000 | [diff] [blame] | 138 | sources = [ "//gn/standalone/wasm_typescript_declaration.d.ts" ] |
| 139 | outputs = [ "$root_out_dir/$_lib_name.d.ts" ] |
Hector Dearman | 21fa916 | 2018-06-22 14:50:29 +0100 | [diff] [blame] | 140 | } |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 141 | } else { # is_wasm |
| 142 | not_needed(invoker, "*") |
Primiano Tucci | 4bdc4c4 | 2018-05-10 15:52:33 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Primiano Tucci | 4bdc4c4 | 2018-05-10 15:52:33 +0100 | [diff] [blame] | 145 | group(target_name) { |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 146 | deps = [ |
Hector Dearman | 21fa916 | 2018-06-22 14:50:29 +0100 | [diff] [blame] | 147 | ":${_lib_name}.d.ts($wasm_toolchain)", |
Primiano Tucci | 3faad74 | 2018-05-16 19:30:48 +0100 | [diff] [blame] | 148 | ":${_lib_name}.js($wasm_toolchain)", |
| 149 | ":${_lib_name}.wasm($wasm_toolchain)", |
| 150 | ] |
| 151 | } |
Primiano Tucci | 4bdc4c4 | 2018-05-10 15:52:33 +0100 | [diff] [blame] | 152 | } # template |