Flutter Engine
The Flutter Engine
gn_dart_compile_exe.py
Go to the documentation of this file.
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
7Run 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
16This is workaround for `dart compile exe` not supporting --depfile option
17in the current version of prebuilt SDK. Once we roll in a new version
18of checked in SDK we can remove this helper.
19"""
20
21import argparse
22import os
23import sys
24import subprocess
25from tempfile import TemporaryDirectory
26
27
28def 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.
50def 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
64def _decode(bytes):
65 return bytes.decode("utf-8")
66
67
68def 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 dartaotruntime_binary = os.path.join(prebuilt_sdk, "bin", "dartaotruntime")
82 if not os.path.isfile(dartaotruntime_binary):
83 print("Binary not found: " + dartaotruntime_binary)
84 return 1
85
86 gen_kernel_snapshot = os.path.join(prebuilt_sdk, "bin", "snapshots",
87 "gen_kernel_aot.dart.snapshot")
88 if not os.path.isfile(gen_kernel_snapshot):
89 print("Binary not found: " + gen_kernel_snapshot)
90 return 1
91
92 platform_dill = os.path.join(prebuilt_sdk, "lib", "_internal",
93 "vm_platform_strong.dill")
94 if not os.path.isfile(platform_dill):
95 print("Binary not found: " + platform_dill)
96 return 1
97
98 # Compile the executable.
99 ok = run_command([
100 dart_binary,
101 "compile",
102 "exe",
103 "--packages",
104 args.packages,
105 f"-Dsdk_hash={args.sdk_hash}",
106 "-o",
107 args.output,
108 args.entry_point,
109 ])
110 if not ok:
111 return 1
112
113 # Collect dependencies by using gen_kernel.
114 with TemporaryDirectory() as tmpdir:
115 output_dill = os.path.join(tmpdir, "output.dill")
116 ok = run_command([
117 dartaotruntime_binary,
118 gen_kernel_snapshot,
119 "--platform",
120 platform_dill,
121 "--packages",
122 args.packages,
123 "--depfile",
124 args.depfile,
125 "-o",
126 output_dill,
127 args.entry_point,
128 ])
129 if not ok:
130 return 1
131
132 # Fix generated depfile to refer to the output file name instead
133 # of referring to the temporary dill file we have generated.
134 with open(args.depfile, "r") as f:
135 content = f.read()
136 (target_name, deps) = content.split(": ", 1)
137 if target_name != output_dill:
138 print(
139 "ERROR: Something is wrong with generated depfile: expected {output_dill} as target, but got {target_name}"
140 )
141 return 1
142 with open(args.depfile, "w") as f:
143 f.write(args.output)
144 f.write(": ")
145 f.write(deps)
146
147 return 0
148
149
150if __name__ == "__main__":
151 sys.exit(main(sys.argv))
Definition: main.py:1
def print(*args, **kwargs)
Definition: run_tests.py:49
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741