[dart2wasm] Add building binaryen as part of our GN build

We use binaryen from googlesource mirror with commits that have already
been rolled into g3.

Since binaryen doesn't come with GN files (it uses cmake), we add
our own in third_party/binaryen/BUILD.gn.

TEST=ci

Change-Id: Iee5c37c9d391ee41dc57250d7a33177cbe9ed2aa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/270640
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
This commit is contained in:
Martin Kustermann 2022-11-18 10:58:54 +00:00 committed by Commit Queue
parent 91509ba7c5
commit 0fdeaee8f6
5 changed files with 196 additions and 0 deletions

View file

@ -125,6 +125,13 @@ group("dart2wasm") {
]
}
group("dart2wasm_benchmark") {
deps = [
":dart2wasm_platform",
"third_party/binaryen:wasm-opt",
]
}
group("dartanalyzer") {
deps = [ "utils/dartanalyzer" ]
}

5
DEPS
View file

@ -99,6 +99,7 @@ vars = {
"boolean_selector_rev": "5082b3debf97421c071be957063763d98412b2e2",
"boringssl_gen_rev": "ced85ef0a00bbca77ce5a91261a5f2ae61b1e62f",
"boringssl_rev": "87f316d7748268eb56f2dc147bd593254ae93198",
"binaryen_rev" : "ec53f4b2d5b0d52ae703c5b696ecf052ad5fffbb",
"browser-compat-data_tag": "ac8cae697014da1ff7124fba33b0b4245cc6cd1b", # v1.0.22
"browser_launcher_rev": "5fa0bd6cddc33785f43c920576fc03dcee1c3caa",
"characters_rev": "4ffccb88a98c026f3351671479c85a886451dc33",
@ -276,6 +277,10 @@ deps = {
"https://boringssl.googlesource.com/boringssl.git" +
"@" + Var("boringssl_rev"),
Var("dart_root") + "/third_party/binaryen/src" :
Var("chromium_git") + "/external/github.com/WebAssembly/binaryen.git" +
"@" + Var("binaryen_rev"),
Var("dart_root") + "/third_party/gsutil": {
"packages": [{
"package": "infra/3pp/tools/gsutil",

112
third_party/binaryen/BUILD.gn vendored Normal file
View file

@ -0,0 +1,112 @@
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
import("../../sdk_args.gni")
action("generate_needed_files") {
script = "generate_needed_files.py"
inputs = [
"src/src/passes/wasm-intrinsics.wat",
"src/src/passes/WasmIntrinsics.cpp.in",
"src/CMakeLists.txt",
"src/config.h.in",
]
args = [
rebase_path("src/src/passes/wasm-intrinsics.wat", root_build_dir),
rebase_path("src/src/passes/WasmIntrinsics.cpp.in", root_build_dir),
rebase_path("$target_gen_dir/WasmIntrinsics.cpp", root_build_dir),
rebase_path("src/CMakeLists.txt", root_build_dir),
rebase_path("src/config.h.in", root_build_dir),
rebase_path("$target_gen_dir/config.h", root_build_dir),
]
outputs = [
"$target_gen_dir/WasmIntrinsics.cpp",
"$target_gen_dir/config.h",
]
}
source_set("binaryen_sources") {
src_dirs = [
rebase_path("src/src/ir", root_build_dir),
rebase_path("src/src/asmjs", root_build_dir),
rebase_path("src/src/cfg", root_build_dir),
rebase_path("src/src/passes", root_build_dir),
rebase_path("src/src/support", root_build_dir),
rebase_path("src/src/wasm", root_build_dir),
rebase_path("src/src/tools/fuzzing", root_build_dir),
rebase_path("src/src/emscripten-optimizer", root_build_dir),
]
# We avoid listing all sources manually, this means when updating
# binaryen in DEPS (or manually editing) one has to ensure to
# re-run GN in DEPS to ensure new dependencies are picked up.
# (this lowers the maintainaince burden of GN build support for binaryen)
sources = exec_script(rebase_path("list_sources.py", root_build_dir),
src_dirs,
"list lines",
[ "$default_git_folder/logs/HEAD" ])
include_dirs = [ "src/src" ]
# Ensure WasmIntrinsics.cpp/config.h files are generated.
deps = [ ":generate_needed_files" ]
# Ensure generated WasmIntrinsics.cpp file is included in build.
sources += [ "$target_gen_dir/WasmIntrinsics.cpp" ]
# Ensure generated config.h file is include path.
include_dirs += [ "$target_gen_dir" ]
}
template("wasm_tool") {
executable(target_name) {
sources = [ string_join("",
[
"src/src/tools/",
target_name,
".cpp",
]) ]
include_dirs = [ "src/src" ]
deps = [ ":binaryen_sources" ]
forward_variables_from(invoker, "*")
}
}
wasm_tool("wasm-opt") {
}
wasm_tool("wasm-shell") {
}
wasm_tool("wasm-metadce") {
}
wasm_tool("wasm-emscripten-finalize") {
}
wasm_tool("wasm-as") {
}
wasm_tool("wasm-dis") {
}
wasm_tool("wasm-ctor-eval") {
}
wasm_tool("wasm-reduce") {
}
wasm_tool("wasm-fuzz-types") {
}
wasm_tool("wasm2js") {
}
group("binaryen_tools") {
deps = [
":wasm-as",
":wasm-ctor-eval",
":wasm-dis",
":wasm-emscripten-finalize",
":wasm-fuzz-types",
":wasm-metadce",
":wasm-opt",
":wasm-reduce",
":wasm-shell",
":wasm2js",
]
}

View file

@ -0,0 +1,54 @@
#!/usr/bin/env python3
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
import os
import re
import subprocess
import sys
def main(args):
# Build Wasm WasmIntrinsics.cpp.
wasm_intrinsics_wat = args[0]
wasm_intrinsics_cpp_in = args[1]
wasm_intrinsics_cpp_out = args[2]
with open(wasm_intrinsics_wat, 'rb') as fd:
wat = fd.read()
wat_hex = ','.join([hex(byte) for byte in wat])
with open(wasm_intrinsics_cpp_in) as fd:
template = fd.read()
template = template.replace('@WASM_INTRINSICS_SIZE@', '%s' % len(wat))
template = template.replace('@WASM_INTRINSICS_EMBED@', wat_hex)
with open(wasm_intrinsics_cpp_out, 'w') as fd:
fd.write(template)
# Build config.h
cmake_in = args[3]
config_h_in = args[4]
config_out = args[5]
with open(cmake_in) as fd:
cmake = fd.read()
with open(config_h_in) as fd:
template = fd.read()
match = re.search(
r'project\(binaryen LANGUAGES C CXX VERSION (?P<version>[0-9]+)\)',
cmake)
version = match['version']
git_args = ['git', 'describe', '--tags', '--match', 'version_*']
try:
output = subprocess.check_output(git_args,
cwd=os.path.dirname(cmake_in),
text=True)
version = '%s (%s)' % (version, output.strip())
except:
pass
template = template.replace('#cmakedefine', '#define')
template = template.replace('${PROJECT_VERSION}', version)
with open(config_out, 'w') as fd:
fd.write(template)
if __name__ == '__main__':
main(sys.argv[1:])

18
third_party/binaryen/list_sources.py vendored Normal file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env python3
# Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
import os
import sys
def main(args):
for dirname in args:
for file in os.listdir(dirname):
if file.endswith('.cpp') or file.endswith('.h'):
print(os.path.join(dirname, file))
if __name__ == '__main__':
main(sys.argv[1:])