|
| 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)) |
0 commit comments