Skip to content

Commit ca3b661

Browse files
anforowiczAravind Vasudevan
authored andcommitted
Tweak build_crubit.py to add support for --install-to flag.
Bug: 1329611 Change-Id: I260c0279d93d7cc036183c821bb0936ee1160ada Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3967741 Reviewed-by: Collin Baker <collinbaker@google.com> Commit-Queue: Łukasz Anforowicz <lukasza@chromium.org> Cr-Commit-Position: refs/heads/main@{#1061723} NOKEYCHECK=True GitOrigin-RevId: 401dc9c3876d8c9633b3e7085adccc555ee9434a
1 parent e6b884d commit ca3b661

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,13 @@ Just run `tools/rust/build_rust.py` or `tools/rust/build_crubit.py`.
110110
### Deploying
111111

112112
`build_rust.py` by default copies the newly build executables/binaries into
113-
`//third_party/rust-toolchain`. Currently a manual step is needed to copy
114-
Crubit binaries (note that the `_impl` suffix has been dropped from the binary
115-
name during the copy - this is expected):
113+
`//third_party/rust-toolchain`.
116114

117-
```sh
118-
cp \
119-
third_party/crubit/bazel-bin/rs_bindings_from_cc/rs_bindings_from_cc_impl \
120-
third_party/rust-toolchain/rs_bindings_from_cc
115+
`build_crubit.py` will copy files into the directory specified in the
116+
(optional) `--install-to` cmdline parameter - for example:
117+
118+
```
119+
$ tools/rust/build_crubit.py --install-to=third_party/rust-toolchain/bin/
121120
```
122121

123122
### Testing

build_crubit.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import pipes
1919
import shutil
20+
import stat
2021
import string
2122
import subprocess
2223
import sys
@@ -50,7 +51,7 @@ def RunCommand(command, env=None, cwd=None, fail_hard=True):
5051

5152
def CheckoutCrubit(commit, dir):
5253
"""Checkout the Crubit repo at a certain git commit in dir. Any local
53-
modifications in dir will be lost."""
54+
modifications in dir will be lost."""
5455

5556
print('Checking out crubit repo %s into %s' % (commit, dir))
5657

@@ -121,6 +122,22 @@ def BuildCrubit(gcc_toolchain_path):
121122
RunCommand(args + extra_args, env=env, cwd=CRUBIT_SRC_DIR)
122123

123124

125+
def InstallCrubit(install_dir):
126+
assert os.path.isdir(install_dir)
127+
128+
print('Installing crubit binaries to %s' % install_dir)
129+
130+
BAZEL_BIN_DIR = os.path.join(CRUBIT_SRC_DIR, "bazel-bin")
131+
SOURCE_PATH = os.path.join(BAZEL_BIN_DIR, "rs_bindings_from_cc",
132+
"rs_bindings_from_cc_impl")
133+
TARGET_PATH = os.path.join(install_dir, "rs_bindings_from_cc")
134+
shutil.copyfile(SOURCE_PATH, TARGET_PATH)
135+
136+
# Change from r-xr-xr-x to rwxrwxr-x, so that future copies will work fine.
137+
os.chmod(TARGET_PATH,
138+
stat.S_IRWXU | stat.S_IRWXG | stat.S_IROTH | stat.S_IXOTH)
139+
140+
124141
def CleanBazel():
125142
RunCommand([BAZEL_EXE, "clean", "--expunge"], cwd=CRUBIT_SRC_DIR)
126143

@@ -129,13 +146,36 @@ def ShutdownBazel():
129146
RunCommand([BAZEL_EXE, "shutdown"], cwd=CRUBIT_SRC_DIR)
130147

131148

149+
def WritableDir(d):
150+
""" Utility function to use as `argparse` `type` to verify that the argument
151+
is a writeable dir (and resolve it as an absolute path). """
152+
153+
try:
154+
real_d = os.path.realpath(d)
155+
except Exception as e:
156+
raise ArgumentTypeError(f"realpath failed: {e}")
157+
if not os.path.isdir(real_d):
158+
raise ArgumentTypeError(f"Not a directory: {d}")
159+
if not os.access(real_d, os.W_OK):
160+
raise ArgumentTypeError(f"Cannot write to: {d}")
161+
return real_d
162+
163+
132164
def main():
133165
parser = argparse.ArgumentParser(
134166
description='Build and package Crubit tools')
135167
parser.add_argument('-v',
136168
'--verbose',
137169
action='count',
138170
help='run subcommands with verbosity')
171+
parser.add_argument(
172+
'--install-to',
173+
type=WritableDir,
174+
help='skip Crubit git checkout. Useful for trying local changes')
175+
parser.add_argument(
176+
'--skip-clean',
177+
action='store_true',
178+
help='skip cleanup. Useful for retrying/rebuilding local changes')
139179
parser.add_argument(
140180
'--skip-checkout',
141181
action='store_true',
@@ -151,8 +191,13 @@ def main():
151191
CheckoutCrubit(CRUBIT_REVISION, CRUBIT_SRC_DIR)
152192

153193
try:
154-
CleanBazel()
194+
if not args.skip_clean:
195+
CleanBazel()
196+
155197
BuildCrubit(args.gcc_toolchain)
198+
199+
if args.install_to:
200+
InstallCrubit(args.install_to)
156201
finally:
157202
ShutdownBazel()
158203

0 commit comments

Comments
 (0)