Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 1 | #!/usr/bin/env vpython3 |
| 2 | |
| 3 | # Copyright 2021 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # This is a wrapper script which runs a Cargo build.rs build script |
| 8 | # executable in a Cargo-like environment. Build scripts can do arbitrary |
| 9 | # things and we can't support everything. Moreover, we do not WANT |
| 10 | # to support everything because that means the build is not deterministic. |
| 11 | # Code review processes must be applied to ensure that the build script |
| 12 | # depends upon only these inputs: |
| 13 | # |
| 14 | # * The environment variables set by Cargo here: |
| 15 | # https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts |
| 16 | # * Output from rustc commands, e.g. to figure out the Rust version. |
| 17 | # |
| 18 | # Similarly, the only allowable output from such a build script |
| 19 | # is currently: |
| 20 | # |
| 21 | # * Generated .rs files |
| 22 | # * cargo:rustc-cfg output. |
| 23 | # |
| 24 | # That's it. We don't even support the other standard cargo:rustc- |
| 25 | # output messages. |
| 26 | |
| 27 | import os |
| 28 | import sys |
| 29 | |
Adrian Taylor | 1f172cd | 2021-11-04 18:35:44 | [diff] [blame] | 30 | # Set up path to be able to import build_utils |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 31 | sys.path.append( |
| 32 | os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir, |
Adrian Taylor | 1f172cd | 2021-11-04 18:35:44 | [diff] [blame] | 33 | os.pardir, 'build', 'android', 'gyp')) |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 34 | from util import build_utils |
| 35 | |
| 36 | import argparse |
| 37 | import io |
| 38 | import subprocess |
| 39 | import re |
| 40 | import platform |
| 41 | |
| 42 | RUSTC_VERSION_LINE = re.compile(r"(\w+): (.*)") |
| 43 | |
| 44 | |
| 45 | def rustc_name(): |
| 46 | if platform.system() == 'Windows': |
| 47 | return "rustc.exe" |
| 48 | else: |
| 49 | return "rustc" |
| 50 | |
| 51 | |
| 52 | def host_triple(rustc_path): |
| 53 | """ Works out the host rustc target. """ |
| 54 | args = [rustc_path, "-vV"] |
| 55 | known_vars = dict() |
| 56 | proc = subprocess.Popen(args, stdout=subprocess.PIPE) |
| 57 | for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"): |
| 58 | m = RUSTC_VERSION_LINE.match(line.rstrip()) |
| 59 | if m: |
| 60 | known_vars[m.group(1)] = m.group(2) |
| 61 | return known_vars["host"] |
| 62 | |
| 63 | |
| 64 | RUSTC_CFG_LINE = re.compile("cargo:rustc-cfg=(.*)") |
| 65 | |
| 66 | parser = argparse.ArgumentParser(description='Run Rust build script.') |
| 67 | parser.add_argument('--build-script', required=True, help='build script to run') |
| 68 | parser.add_argument('--output', |
| 69 | required=True, |
| 70 | help='where to write output rustc flags') |
| 71 | parser.add_argument('--target', help='rust target triple') |
Adrian Taylor | e9e15d1 | 2021-11-18 19:36:21 | [diff] [blame] | 72 | parser.add_argument('--features', help='features', nargs='+') |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 73 | parser.add_argument('--rust-prefix', required=True, help='rust path prefix') |
| 74 | parser.add_argument('--out-dir', required=True, help='target out dir') |
Adrian Taylor | b238a97 | 2021-12-01 15:02:23 | [diff] [blame^] | 75 | parser.add_argument('--src-dir', required=True, help='target source dir') |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 76 | |
| 77 | args = parser.parse_args() |
| 78 | |
| 79 | rustc_path = os.path.join(args.rust_prefix, rustc_name()) |
| 80 | env = os.environ.copy() |
| 81 | env.clear() # try to avoid build scripts depending on other things |
Adrian Taylor | b238a97 | 2021-12-01 15:02:23 | [diff] [blame^] | 82 | env["RUSTC"] = os.path.abspath(rustc_path) |
| 83 | env["OUT_DIR"] = os.path.abspath(args.out_dir) |
| 84 | env["CARGO_MANIFEST_DIR"] = os.path.abspath(args.src_dir) |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 85 | env["HOST"] = host_triple(rustc_path) |
| 86 | if args.target is None: |
| 87 | env["TARGET"] = env["HOST"] |
| 88 | else: |
| 89 | env["TARGET"] = args.target |
Adrian Taylor | e9e15d1 | 2021-11-18 19:36:21 | [diff] [blame] | 90 | if args.features: |
| 91 | for f in args.features: |
| 92 | feature_name = f.upper().replace("-", "_") |
| 93 | env["CARGO_FEATURE_%s" % feature_name] = "1" |
| 94 | |
| 95 | # In the future we should, set all the variables listed here: |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 96 | # https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts |
| 97 | |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 98 | # In the future, we could consider isolating this build script |
| 99 | # into a chroot jail or similar on some platforms, but ultimately |
| 100 | # we are always going to be reliant on code review to ensure the |
| 101 | # build script is deterministic and trustworthy, so this would |
| 102 | # really just be a backup to humans. |
Adrian Taylor | a63952d5 | 2021-11-30 18:49:22 | [diff] [blame] | 103 | proc = subprocess.run([os.path.abspath(args.build_script)], |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 104 | env=env, |
Adrian Taylor | b238a97 | 2021-12-01 15:02:23 | [diff] [blame^] | 105 | cwd=args.src_dir, |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 106 | text=True, |
| 107 | capture_output=True) |
| 108 | |
Adrian Taylor | b238a97 | 2021-12-01 15:02:23 | [diff] [blame^] | 109 | if proc.stderr.rstrip(): |
| 110 | print(proc.stderr.rstrip(), file=sys.stderr) |
| 111 | proc.check_returncode() |
| 112 | |
Adrian Taylor | 5711e213 | 2021-11-09 15:09:09 | [diff] [blame] | 113 | flags = "" |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 114 | for line in proc.stdout.split("\n"): |
| 115 | m = RUSTC_CFG_LINE.match(line.rstrip()) |
| 116 | if m: |
Adrian Taylor | 5711e213 | 2021-11-09 15:09:09 | [diff] [blame] | 117 | flags = "%s--cfg\n%s\n" % (flags, m.group(1)) |
Adrian Taylor | 792467f2 | 2021-10-29 20:18:30 | [diff] [blame] | 118 | |
Adrian Taylor | 5711e213 | 2021-11-09 15:09:09 | [diff] [blame] | 119 | # AtomicOutput will ensure we only write to the file on disk if what we give to |
| 120 | # write() is different than what's currently on disk. |
| 121 | with build_utils.AtomicOutput(args.output) as output: |
| 122 | output.write(flags.encode("utf-8")) |