From 4c016f8d9248b036e2fc86be4be56004b09b5d4f Mon Sep 17 00:00:00 2001 From: John Moon Date: Wed, 30 Nov 2022 17:44:33 -0800 Subject: [PATCH] build: Add --out_dir argument to build_with_bazel.py Currently, there's no way for a user to specify a custom output directory when using build_with_bazel.py. Add a command line argument to allow such usage (similar to the OUT_DIR environment variable from build.sh). Change-Id: I44f720c85409fc8ec82a9f4a96d428fab80a7547 Signed-off-by: John Moon --- build_with_bazel.py | 53 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/build_with_bazel.py b/build_with_bazel.py index 40bc80ac6af5..777c1a3f23e3 100755 --- a/build_with_bazel.py +++ b/build_with_bazel.py @@ -23,7 +23,7 @@ DEFAULT_ABL_EXTENSIONS_SRC = "../bootable/bootloader/edk2/abl_extensions.bzl" class BazelBuilder: """Helper class for building with Bazel""" - def __init__(self, target_list, skip_list, user_opts): + def __init__(self, target_list, skip_list, out_dir, user_opts): self.workspace = os.path.realpath( os.path.join(os.path.dirname(os.path.realpath(__file__)), "..") ) @@ -36,6 +36,7 @@ class BazelBuilder: ) self.target_list = target_list self.skip_list = skip_list + self.out_dir = out_dir self.user_opts = user_opts self.process_list = [] self.setup_extensions() @@ -183,7 +184,12 @@ class BazelBuilder: return toolchain def bazel( - self, bazel_subcommand, targets, extra_options=None, us_cross_toolchain=None + self, + bazel_subcommand, + targets, + out_subdir="dist", + extra_options=None, + us_cross_toolchain=None, ): """Execute a bazel command""" cmdline = [self.bazel_bin, bazel_subcommand] @@ -192,6 +198,9 @@ class BazelBuilder: if us_cross_toolchain: cmdline.extend(self.get_cross_cli_opts(us_cross_toolchain)) cmdline.extend(targets) + if self.out_dir and bazel_subcommand == "run": + cmdline.extend(["--", "--dist_dir", os.path.join(self.out_dir, out_subdir)]) + cmdline_str = " ".join(cmdline) try: logging.info('Running "%s"', cmdline_str) @@ -206,16 +215,20 @@ class BazelBuilder: self.process_list.remove(build_proc) - def build_targets(self, targets, user_opts=None, us_cross_toolchain=None): + def build_targets( + self, targets, out_subdir="dist", user_opts=None, us_cross_toolchain=None + ): """Run "bazel build" on all targets in parallel""" if not targets: logging.warning("no targets to build") - self.bazel("build", targets, user_opts, us_cross_toolchain) + self.bazel("build", targets, out_subdir, user_opts, us_cross_toolchain) - def run_targets(self, targets, user_opts=None, us_cross_toolchain=None): + def run_targets( + self, targets, out_subdir="dist", user_opts=None, us_cross_toolchain=None + ): """Run "bazel run" on all targets in serial (since bazel run cannot have multiple targets)""" for target in targets: - self.bazel("run", [target], user_opts, us_cross_toolchain) + self.bazel("run", [target], out_subdir, user_opts, us_cross_toolchain) def build(self): """Determine which targets to build, then build them""" @@ -237,12 +250,24 @@ class BazelBuilder: sys.exit(1) logging.info("Building %s targets...", CPU) - self.build_targets(cross_targets_to_build, self.user_opts, us_cross_toolchain) - self.run_targets(cross_targets_to_build, self.user_opts, us_cross_toolchain) + self.build_targets( + cross_targets_to_build, + user_opts=self.user_opts, + us_cross_toolchain=us_cross_toolchain, + ) + self.run_targets( + cross_targets_to_build, + user_opts=self.user_opts, + us_cross_toolchain=us_cross_toolchain, + ) logging.info("Building host targets...") - self.build_targets(host_targets_to_build, self.user_opts) - self.run_targets(host_targets_to_build, self.user_opts) + self.build_targets( + host_targets_to_build, out_subdir="host", user_opts=self.user_opts + ) + self.run_targets( + host_targets_to_build, out_subdir="host", user_opts=self.user_opts + ) def main(): @@ -266,6 +291,12 @@ def main(): default=[], help="Skip specific build rules (e.g. --skip abl will skip the //msm-kernel:__abl build)", ) + parser.add_argument( + "-o", + "--out_dir", + metavar="OUT_DIR", + help='Specify the output distribution directory (by default, "$PWD/out/msm-kernel--variant")', + ) parser.add_argument( "--log", metavar="LEVEL", @@ -283,7 +314,7 @@ def main(): args.skip.extend(DEFAULT_SKIP_LIST) - builder = BazelBuilder(args.target, args.skip, user_opts) + builder = BazelBuilder(args.target, args.skip, args.out_dir, user_opts) try: builder.build() except KeyboardInterrupt: