Skip to content

Commit 077e1be

Browse files
mralephCommit Queue
authored andcommitted
[gn] Fix precompile_tools=True (attempt 2)
Turns out that dart compile exe --depfile produces depfile which does not work with ninja because it uses absolute path to the output file instead of using relative path. So we can't use it directly and need to fix depfile. TEST=manually Change-Id: I1aadfd5079cc38d392933f9afe333f63407bb295 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/435680 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Slava Egorov <[email protected]>
1 parent ab004f3 commit 077e1be

File tree

2 files changed

+126
-7
lines changed

2 files changed

+126
-7
lines changed

build/gn_dart_compile_exe.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/usr/bin/env python3
2+
# Copyright 2014 The Chromium Authors. All rights reserved.
3+
# Use of this source code is governed by a BSD-style license that can be
4+
# found in the LICENSE file.
5+
"""Helper script for GN to run `dart compile exe` and produce a depfile.
6+
7+
Run with:
8+
python3 gn_dart_compile_exe.py \
9+
--dart-binary <path to dart binary> \
10+
--entry-point <path to dart entry point> \
11+
--output <path to resulting executable> \
12+
--sdk-hash <SDK hash> \
13+
--packages <path to package config file> \
14+
--depfile <path to depfile to write>
15+
16+
This is workaround for `dart compile exe` not supporting --depfile option
17+
in the current version of prebuilt SDK. Once we roll in a new version
18+
of checked in SDK we can remove this helper.
19+
"""
20+
21+
import argparse
22+
import os
23+
import sys
24+
import subprocess
25+
from tempfile import TemporaryDirectory
26+
27+
28+
def parse_args(argv):
29+
parser = argparse.ArgumentParser()
30+
parser.add_argument("--dart-sdk",
31+
required=True,
32+
help="Path to the prebuilt Dart SDK")
33+
parser.add_argument("--sdk-hash", required=True, help="SDK hash")
34+
parser.add_argument("--entry-point",
35+
required=True,
36+
help="Dart entry point to precompile")
37+
parser.add_argument("--output",
38+
required=True,
39+
help="Path to resulting executable ")
40+
parser.add_argument("--packages",
41+
required=True,
42+
help="Path to package config file")
43+
parser.add_argument("--depfile",
44+
required=True,
45+
help="Path to depfile to write")
46+
return parser.parse_args(argv)
47+
48+
49+
# Run a command, swallowing the output unless there is an error.
50+
def run_command(command):
51+
try:
52+
subprocess.check_output(command, stderr=subprocess.STDOUT)
53+
return True
54+
except subprocess.CalledProcessError as e:
55+
print("Command failed: " + " ".join(command) + "\n" + "output: " +
56+
_decode(e.output))
57+
return False
58+
except OSError as e:
59+
print("Command failed: " + " ".join(command) + "\n" + "output: " +
60+
_decode(e.strerror))
61+
return False
62+
63+
64+
def _decode(bytes):
65+
return bytes.decode("utf-8")
66+
67+
68+
def main(argv):
69+
args = parse_args(argv[1:])
70+
71+
# Unless the path is absolute, this script is designed to run binaries
72+
# produced by the current build, which is the current working directory when
73+
# this script is run.
74+
prebuilt_sdk = os.path.abspath(args.dart_sdk)
75+
76+
dart_binary = os.path.join(prebuilt_sdk, "bin", "dart")
77+
if not os.path.isfile(dart_binary):
78+
print("Binary not found: " + dart_binary)
79+
return 1
80+
81+
# Compile the executable.
82+
ok = run_command([
83+
dart_binary,
84+
"compile",
85+
"exe",
86+
"--packages",
87+
args.packages,
88+
f"-Dsdk_hash={args.sdk_hash}",
89+
"--depfile",
90+
args.depfile,
91+
"-o",
92+
args.output,
93+
args.entry_point,
94+
])
95+
if not ok:
96+
return 1
97+
98+
# Fix generated depfile to refer to the relative output file name
99+
# instead referring to it using absolute path. ninja does not support
100+
# that.
101+
with open(args.depfile, "r") as f:
102+
content = f.read()
103+
deps = content.split(": ", 1)[1]
104+
with open(args.depfile, "w") as f:
105+
f.write(args.output)
106+
f.write(": ")
107+
f.write(deps)
108+
return 0
109+
110+
111+
if __name__ == "__main__":
112+
sys.exit(main(sys.argv))

utils/BUILD.gn

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import("../sdk_args.gni")
88
_dart_root = get_path_info("..", "abspath")
99

1010
template("aot_compile_using_prebuilt_sdk") {
11-
prebuilt_dart_action(target_name) {
11+
action(target_name) {
1212
forward_variables_from(invoker,
1313
[
1414
"deps",
@@ -17,24 +17,31 @@ template("aot_compile_using_prebuilt_sdk") {
1717
"visibility",
1818
])
1919

20+
script = "$_dart_root/build/gn_dart_compile_exe.py"
21+
2022
inputs = [
2123
invoker.entry_point,
2224
invoker.package_config,
2325
]
26+
2427
outputs = [ invoker.output ]
28+
2529
depfile = invoker.output + ".d"
2630

31+
# TODO(vegorov): support RBE by using rewrapper script.
2732
args = [
28-
"compile",
29-
"exe",
33+
"--dart-sdk",
34+
rebase_path("$_dart_root/tools/sdks/dart-sdk", root_build_dir),
35+
"--sdk-hash",
36+
"$sdk_hash",
37+
"--entry-point",
38+
rebase_path(invoker.entry_point, root_build_dir),
39+
"--output",
40+
rebase_path(invoker.output, root_build_dir),
3041
"--packages",
3142
rebase_path(invoker.package_config, root_build_dir),
32-
"-Dsdk_hash=$sdk_hash",
3343
"--depfile",
3444
rebase_path(depfile, root_build_dir),
35-
"-o",
36-
rebase_path(invoker.output, root_build_dir),
37-
rebase_path(invoker.entry_point, root_build_dir),
3845
]
3946
}
4047
}

0 commit comments

Comments
 (0)