1# Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
2# Licensed under the Apache License, Version 2.0 (the "License");
3# you may not use this file except in compliance with the License.
4# You may obtain a copy of the License at
5#
6#     http://www.apache.org/licenses/LICENSE-2.0
7#
8# Unless required by applicable law or agreed to in writing, software
9# distributed under the License is distributed on an "AS IS" BASIS,
10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
14import("./wasm_vars.gni")
15
16declare_args() {
17  use_local_emsdk = false
18}
19if (!use_local_emsdk) {
20  em_config = rebase_path("//tools/emsdk/.emscripten", "")
21  emsdk_dir = rebase_path("//tools/emsdk/upstream", "")
22}
23template("wasm_lib") {
24  _exports = "['ccall', 'callMain', 'addFunction', 'FS']"
25  print(invoker.name)
26  assert(defined(invoker.name))
27
28  # If the name is trace_streamer_builtin the target_name must be trace_streamer_builtin_wasm.
29  assert(invoker.name + "_wasm" == target_name)
30  _target_ldflags = [
31    "-s",
32    "DISABLE_EXCEPTION_CATCHING=1",
33    "-s",
34    "WASM=1",
35    "-s",
36    "NO_DYNAMIC_EXECUTION=1",
37    "-s",
38    "ALLOW_MEMORY_GROWTH=1",
39    "-s",
40    "INITIAL_MEMORY=33554432",
41    "-s",
42    "ALLOW_TABLE_GROWTH=1",
43    "-s",
44    "MEMFS_APPEND_TO_TYPED_ARRAYS=1",
45    "-s",
46    "EXPORTED_RUNTIME_METHODS=" + _exports,
47    "-s",
48    "EXPORT_NAME=${target_name}",
49    "-s",
50    "MODULARIZE=1",
51    "-lworkerfs.js",  # For FS.filesystems.WORKERFS
52    "-s",
53    "ASSERTIONS=1",
54  ]
55  if (wasm_use_thread) {
56    _target_ldflags += [
57      "-s",
58      "USE_PTHREADS=1",
59      "-s",
60      "PTHREAD_POOL_SIZE=6",
61      "-s",
62      "-pthread",
63    ]
64  } else {
65    _target_ldflags += [
66      "-s",
67      "WASM_ASYNC_COMPILATION=0",
68    ]
69  }
70
71  _lib_name = invoker.name
72  if (is_debug) {
73    _target_ldflags += [
74      "-s",
75      "ASSERTIONS=2",
76      "-s",
77      "STACK_OVERFLOW_CHECK=1",
78      "-s",
79      "SAFE_HEAP=1",
80      "-g4",
81      "-O3",
82    ]
83  } else {
84    _target_ldflags += [
85      # "-g2",  # Required for getting C++ symbol names.
86      "-O3",
87      #  "-s",
88      #  "ASSERTIONS=1",
89    ]
90  }
91
92  _vars_to_forward = [ "deps" ]
93
94  executable("${_lib_name}.js") {
95    ldflags = _target_ldflags
96    output_extension = ""
97    forward_variables_from(invoker, _vars_to_forward)
98  }
99}
100