2023-11-11 15:02:02 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import argparse
|
|
|
|
|
2024-05-19 01:51:20 +02:00
|
|
|
parser = argparse.ArgumentParser(description="Ninja Bazel builder.")
|
2023-11-11 15:02:02 +01:00
|
|
|
|
2024-05-19 01:51:20 +02:00
|
|
|
parser.add_argument("--ninja-file", type=str, help="The ninja file in use", default="build.ninja")
|
|
|
|
parser.add_argument("--verbose", action="store_true", help="Turn on verbose mode")
|
|
|
|
parser.add_argument(
|
|
|
|
"--integration-debug",
|
|
|
|
action="store_true",
|
|
|
|
help="Turn on extra debug output about the ninja-bazel integration",
|
|
|
|
)
|
2023-11-11 15:02:02 +01:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# This corresponds to BAZEL_INTEGRATION_DEBUG=1 from SCons command line
|
2024-03-13 20:22:25 +01:00
|
|
|
if args.integration_debug:
|
2023-11-11 15:02:02 +01:00
|
|
|
|
|
|
|
def print_debug(msg):
|
|
|
|
print("[BAZEL_INTEGRATION_DEBUG] " + msg)
|
|
|
|
else:
|
|
|
|
|
|
|
|
def print_debug(msg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2024-08-02 22:30:12 +02:00
|
|
|
# our ninja python module intercepts the command lines and
|
|
|
|
# prints out the targets everytime ninja is executed
|
|
|
|
ninja_command_line_targets = []
|
|
|
|
try:
|
|
|
|
ninja_last_cmd_file = ".ninja_last_command_line_targets.txt"
|
|
|
|
with open(ninja_last_cmd_file) as f:
|
|
|
|
ninja_command_line_targets = [target.strip() for target in f.readlines() if target.strip()]
|
|
|
|
except OSError as exc:
|
|
|
|
print(
|
|
|
|
f"Failed to open {ninja_last_cmd_file}, this is expected to be generated on ninja execution by the mongo-ninja-python module."
|
|
|
|
)
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
|
2023-11-11 15:02:02 +01:00
|
|
|
# Our ninja generation process generates all the build info related to
|
|
|
|
# the specific ninja file
|
|
|
|
ninja_build_info = dict()
|
|
|
|
try:
|
2024-05-19 01:51:20 +02:00
|
|
|
bazel_info_file = ".bazel_info_for_ninja.txt"
|
2023-11-11 15:02:02 +01:00
|
|
|
with open(bazel_info_file) as f:
|
|
|
|
ninja_build_info = json.load(f)
|
|
|
|
except OSError as exc:
|
|
|
|
print(
|
|
|
|
f"Failed to open {bazel_info_file}, this is expected to be generated by scons during ninja generation."
|
|
|
|
)
|
|
|
|
raise exc
|
|
|
|
|
|
|
|
# ninja will automatically create directories for any outputs, but in this case
|
|
|
|
# bazel will be creating a symlink for the bazel-out dir to its cache. We don't want
|
|
|
|
# ninja to interfere so delete the dir if it was not a link (made by bazel)
|
2023-12-13 18:08:58 +01:00
|
|
|
if sys.platform == "win32":
|
|
|
|
if os.path.exists("bazel-out"):
|
|
|
|
try:
|
|
|
|
os.readlink("bazel-out")
|
|
|
|
except OSError:
|
|
|
|
shutil.rmtree("bazel-out")
|
|
|
|
|
|
|
|
else:
|
|
|
|
if not os.path.islink("bazel-out"):
|
|
|
|
shutil.rmtree("bazel-out")
|
2023-11-11 15:02:02 +01:00
|
|
|
|
|
|
|
# now we are ready to build all bazel buildable files
|
2024-05-02 03:35:50 +02:00
|
|
|
targets_to_build = ["//src/..."]
|
2024-03-13 20:22:25 +01:00
|
|
|
if args.verbose:
|
|
|
|
extra_args = []
|
|
|
|
else:
|
2024-03-22 08:00:22 +01:00
|
|
|
extra_args = ["--output_filter=DONT_MATCH_ANYTHING"]
|
2024-05-19 01:51:20 +02:00
|
|
|
bazel_proc = subprocess.run(
|
|
|
|
ninja_build_info["bazel_cmd"] + extra_args + targets_to_build, check=True
|
|
|
|
)
|
2024-08-02 22:30:12 +02:00
|
|
|
if (
|
|
|
|
"compiledb" in ninja_command_line_targets
|
|
|
|
or "compile_commands.json" in ninja_command_line_targets
|
|
|
|
):
|
|
|
|
bazel_proc = subprocess.run(ninja_build_info["compiledb_cmd"], check=True)
|