17
17
import os
18
18
import pipes
19
19
import shutil
20
+ import stat
20
21
import string
21
22
import subprocess
22
23
import sys
@@ -50,7 +51,7 @@ def RunCommand(command, env=None, cwd=None, fail_hard=True):
50
51
51
52
def CheckoutCrubit (commit , dir ):
52
53
"""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."""
54
55
55
56
print ('Checking out crubit repo %s into %s' % (commit , dir ))
56
57
@@ -121,6 +122,22 @@ def BuildCrubit(gcc_toolchain_path):
121
122
RunCommand (args + extra_args , env = env , cwd = CRUBIT_SRC_DIR )
122
123
123
124
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
+
124
141
def CleanBazel ():
125
142
RunCommand ([BAZEL_EXE , "clean" , "--expunge" ], cwd = CRUBIT_SRC_DIR )
126
143
@@ -129,13 +146,36 @@ def ShutdownBazel():
129
146
RunCommand ([BAZEL_EXE , "shutdown" ], cwd = CRUBIT_SRC_DIR )
130
147
131
148
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
+
132
164
def main ():
133
165
parser = argparse .ArgumentParser (
134
166
description = 'Build and package Crubit tools' )
135
167
parser .add_argument ('-v' ,
136
168
'--verbose' ,
137
169
action = 'count' ,
138
170
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' )
139
179
parser .add_argument (
140
180
'--skip-checkout' ,
141
181
action = 'store_true' ,
@@ -151,8 +191,13 @@ def main():
151
191
CheckoutCrubit (CRUBIT_REVISION , CRUBIT_SRC_DIR )
152
192
153
193
try :
154
- CleanBazel ()
194
+ if not args .skip_clean :
195
+ CleanBazel ()
196
+
155
197
BuildCrubit (args .gcc_toolchain )
198
+
199
+ if args .install_to :
200
+ InstallCrubit (args .install_to )
156
201
finally :
157
202
ShutdownBazel ()
158
203
0 commit comments