xref: /aosp_15_r20/external/angle/build/rust/run_build_script.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1#!/usr/bin/env vpython3
2
3# Copyright 2021 The Chromium Authors
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
27import argparse
28import io
29import os
30import platform
31import re
32import subprocess
33import sys
34import tempfile
35
36# Set up path to be able to import action_helpers
37sys.path.append(
38    os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir,
39                 os.pardir, 'build'))
40import action_helpers
41
42
43RUSTC_VERSION_LINE = re.compile(r"(\w+): (.*)")
44
45
46def rustc_name():
47  if platform.system() == 'Windows':
48    return "rustc.exe"
49  else:
50    return "rustc"
51
52
53def host_triple(rustc_path):
54  """ Works out the host rustc target. """
55  args = [rustc_path, "-vV"]
56  known_vars = dict()
57  proc = subprocess.Popen(args, stdout=subprocess.PIPE)
58  for line in io.TextIOWrapper(proc.stdout, encoding="utf-8"):
59    m = RUSTC_VERSION_LINE.match(line.rstrip())
60    if m:
61      known_vars[m.group(1)] = m.group(2)
62  return known_vars["host"]
63
64
65# Before 1.77, the format was `cargo:rustc-cfg=`. As of 1.77 the format is now
66# `cargo::rustc-cfg=`.
67RUSTC_CFG_LINE = re.compile("cargo::?rustc-cfg=(.*)")
68
69
70def main():
71  parser = argparse.ArgumentParser(description='Run Rust build script.')
72  parser.add_argument('--build-script',
73                      required=True,
74                      help='build script to run')
75  parser.add_argument('--output',
76                      required=True,
77                      help='where to write output rustc flags')
78  parser.add_argument('--target', help='rust target triple')
79  parser.add_argument('--target-abi', help='rust target_abi')
80  parser.add_argument('--pointer-width', help='rust target pointer width')
81  parser.add_argument('--features', help='features', nargs='+')
82  parser.add_argument('--env', help='environment variable', nargs='+')
83  parser.add_argument('--rustflags',
84                      help=('path to a file of newline-separated command line '
85                            'flags for rustc'))
86  parser.add_argument('--rust-prefix', required=True, help='rust path prefix')
87  parser.add_argument('--generated-files', nargs='+', help='any generated file')
88  parser.add_argument('--out-dir', required=True, help='target out dir')
89  parser.add_argument('--src-dir', required=True, help='target source dir')
90
91  args = parser.parse_args()
92
93  rustc_path = os.path.join(args.rust_prefix, rustc_name())
94
95  # We give the build script an OUT_DIR of a temporary directory,
96  # and copy out only any files which gn directives say that it
97  # should generate. Mostly this is to ensure we can atomically
98  # create those files, but it also serves to avoid side-effects
99  # from the build script.
100  # In the future, we could consider isolating this build script
101  # into a chroot jail or similar on some platforms, but ultimately
102  # we are always going to be reliant on code review to ensure the
103  # build script is deterministic and trustworthy, so this would
104  # really just be a backup to humans.
105  with tempfile.TemporaryDirectory() as tempdir:
106    env = {}  # try to avoid build scripts depending on other things
107    env["RUSTC"] = os.path.abspath(rustc_path)
108    env["OUT_DIR"] = tempdir
109    env["CARGO_MANIFEST_DIR"] = os.path.abspath(args.src_dir)
110    env["HOST"] = host_triple(rustc_path)
111    env["CARGO_CFG_TARGET_POINTER_WIDTH"] = args.pointer_width
112    if args.target is None:
113      env["TARGET"] = env["HOST"]
114    else:
115      env["TARGET"] = args.target
116    target_components = env["TARGET"].split("-")
117    if len(target_components) == 2:
118      env["CARGO_CFG_TARGET_ARCH"] = target_components[0]
119      env["CARGO_CFG_TARGET_VENDOR"] = ''
120      env["CARGO_CFG_TARGET_OS"] = target_components[1]
121      env["CARGO_CFG_TARGET_ENV"] = ''
122    elif len(target_components) == 3:
123      env["CARGO_CFG_TARGET_ARCH"] = target_components[0]
124      env["CARGO_CFG_TARGET_VENDOR"] = target_components[1]
125      env["CARGO_CFG_TARGET_OS"] = target_components[2]
126      env["CARGO_CFG_TARGET_ENV"] = ''
127    elif len(target_components) == 4:
128      env["CARGO_CFG_TARGET_ARCH"] = target_components[0]
129      env["CARGO_CFG_TARGET_VENDOR"] = target_components[1]
130      env["CARGO_CFG_TARGET_OS"] = target_components[2]
131      env["CARGO_CFG_TARGET_ENV"] = target_components[3]
132    else:
133      print(f'Invalid TARGET {env["TARGET"]}')
134      sys.exit(1)
135    # See https://crbug.com/325543500 for background.
136    # Cargo sets CARGO_CFG_TARGET_OS to "android" even when targeting
137    # *-androideabi.
138    if env["CARGO_CFG_TARGET_OS"].startswith("android"):
139      env["CARGO_CFG_TARGET_OS"] = "android"
140    elif env["CARGO_CFG_TARGET_OS"] == "darwin":
141      env["CARGO_CFG_TARGET_OS"] = "macos"
142    env["CARGO_CFG_TARGET_ENDIAN"] = "little"
143    if env["CARGO_CFG_TARGET_OS"] == "windows":
144      env["CARGO_CFG_TARGET_FAMILY"] = "windows"
145    else:
146      env["CARGO_CFG_TARGET_FAMILY"] = "unix"
147    env["CARGO_CFG_TARGET_ABI"] = args.target_abi if args.target_abi else ""
148    if args.features:
149      for f in args.features:
150        feature_name = f.upper().replace("-", "_")
151        env["CARGO_FEATURE_%s" % feature_name] = "1"
152    if args.rustflags:
153      with open(args.rustflags) as flags:
154        env["CARGO_ENCODED_RUSTFLAGS"] = '\x1f'.join(flags.readlines())
155    if args.env:
156      for e in args.env:
157        (k, v) = e.split("=")
158        env[k] = v
159    # Pass through a couple which are useful for diagnostics
160    if os.environ.get("RUST_BACKTRACE"):
161      env["RUST_BACKTRACE"] = os.environ.get("RUST_BACKTRACE")
162    if os.environ.get("RUST_LOG"):
163      env["RUST_LOG"] = os.environ.get("RUST_LOG")
164
165    # In the future we should, set all the variables listed here:
166    # https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts
167
168    proc = subprocess.run([os.path.abspath(args.build_script)],
169                          env=env,
170                          cwd=args.src_dir,
171                          encoding='utf8',
172                          stdout=subprocess.PIPE,
173                          stderr=subprocess.PIPE)
174
175    if proc.stderr.rstrip():
176      print(proc.stderr.rstrip(), file=sys.stderr)
177    proc.check_returncode()
178
179    flags = ""
180    for line in proc.stdout.split("\n"):
181      m = RUSTC_CFG_LINE.match(line.rstrip())
182      if m:
183        flags = "%s--cfg\n%s\n" % (flags, m.group(1))
184
185    # AtomicOutput will ensure we only write to the file on disk if what we
186    # give to write() is different than what's currently on disk.
187    with action_helpers.atomic_output(args.output) as output:
188      output.write(flags.encode("utf-8"))
189
190    # Copy any generated code out of the temporary directory,
191    # atomically.
192    if args.generated_files:
193      for generated_file in args.generated_files:
194        in_path = os.path.join(tempdir, generated_file)
195        out_path = os.path.join(args.out_dir, generated_file)
196        out_dir = os.path.dirname(out_path)
197        if not os.path.exists(out_dir):
198          os.makedirs(out_dir)
199        with open(in_path, 'rb') as input:
200          with action_helpers.atomic_output(out_path) as output:
201            content = input.read()
202            output.write(content)
203
204
205if __name__ == '__main__':
206  sys.exit(main())
207