2024-10-10 19:59:18 +02:00
|
|
|
import io
|
2024-04-30 07:36:06 +02:00
|
|
|
import os
|
|
|
|
import subprocess
|
2024-10-10 19:59:18 +02:00
|
|
|
import sys
|
2024-04-30 07:36:06 +02:00
|
|
|
|
|
|
|
# assume we are always running from project root to find buildscripts
|
2024-05-17 18:01:09 +02:00
|
|
|
sys.path.append(".")
|
2024-04-30 07:36:06 +02:00
|
|
|
|
|
|
|
from buildscripts.install_bazel import install_bazel
|
|
|
|
|
|
|
|
bazel_bin_dir = os.path.expanduser("~/.local/bin")
|
|
|
|
if not os.path.exists(bazel_bin_dir):
|
|
|
|
os.makedirs(bazel_bin_dir)
|
|
|
|
|
|
|
|
|
|
|
|
fake_out = io.StringIO()
|
|
|
|
orig_stdout = sys.stdout
|
|
|
|
orig_stderr = sys.stderr
|
|
|
|
sys.stdout = fake_out
|
|
|
|
sys.stderr = fake_out
|
|
|
|
bazel_executable = install_bazel(bazel_bin_dir)
|
|
|
|
sys.stdout = orig_stdout
|
|
|
|
sys.stderr = orig_stderr
|
|
|
|
|
2024-09-04 00:12:49 +02:00
|
|
|
|
2024-10-23 21:36:26 +02:00
|
|
|
subprocess.run([bazel_executable, "--output_user_root=/data/bazel_cache", "clean", "--expunge"])
|
|
|
|
|
2024-09-04 00:12:49 +02:00
|
|
|
cmd = (
|
|
|
|
[
|
|
|
|
sys.executable,
|
|
|
|
"./buildscripts/scons.py",
|
|
|
|
]
|
|
|
|
+ sys.argv
|
2024-10-23 21:36:26 +02:00
|
|
|
+ [
|
|
|
|
"BAZEL_INTEGRATION_DEBUG=1",
|
|
|
|
"\\$BUILD_ROOT/scons/\\$VARIANT_DIR/sconf_temp",
|
|
|
|
]
|
2024-09-04 00:12:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Run a lightwieght scons build to generate the bazel command.
|
|
|
|
bazel_cmd_args = None
|
|
|
|
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
|
|
for line in proc.stdout.splitlines():
|
|
|
|
if "BAZEL_COMMAND:" in line:
|
|
|
|
# The script is intended to be have output placed into a bash variable, so we should
|
|
|
|
# only ever print the bazel build command
|
|
|
|
bazel_cmd_args = line.split("BAZEL_COMMAND:")[-1].strip().split()[2:-1]
|
|
|
|
break
|
|
|
|
|
|
|
|
|
2024-04-30 07:36:06 +02:00
|
|
|
# coverity requires a single target which has dependencies on all
|
|
|
|
# the cc_library and cc_binaries in our build. There is not a good way from
|
|
|
|
# within the build to get all those targets, so we will generate the list via query
|
|
|
|
# https://sig-product-docs.synopsys.com/bundle/coverity-docs/page/coverity-analysis/topics/building_with_bazel.html#build_with_bazel
|
2024-05-17 18:01:09 +02:00
|
|
|
proc = subprocess.run(
|
|
|
|
[
|
|
|
|
bazel_executable,
|
|
|
|
"aquery",
|
2024-09-04 00:12:49 +02:00
|
|
|
]
|
|
|
|
+ bazel_cmd_args
|
|
|
|
+ [
|
2024-05-17 18:01:09 +02:00
|
|
|
"--config=local",
|
|
|
|
'mnemonic("CppCompile|LinkCompile", //src/mongo/...)',
|
|
|
|
],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE,
|
|
|
|
text=True,
|
|
|
|
)
|
2024-04-30 07:36:06 +02:00
|
|
|
|
2024-08-08 17:23:26 +02:00
|
|
|
targets = set()
|
2024-04-30 07:36:06 +02:00
|
|
|
for line in proc.stdout.splitlines():
|
|
|
|
if line.startswith(" Target: "):
|
2024-08-08 17:23:26 +02:00
|
|
|
targets.add(line.split()[-1])
|
2024-04-30 07:36:06 +02:00
|
|
|
|
|
|
|
|
2024-10-23 21:36:26 +02:00
|
|
|
enterprise_coverity_dir = os.path.join("src", "mongo", "db", "modules", "enterprise", "coverity")
|
|
|
|
os.makedirs(enterprise_coverity_dir, exist_ok=True)
|
|
|
|
with open(os.path.join(enterprise_coverity_dir, "BUILD.bazel"), "w") as buildfile:
|
2024-04-30 07:36:06 +02:00
|
|
|
buildfile.write("""\
|
|
|
|
load("@rules_coverity//coverity:defs.bzl", "cov_gen_script")
|
|
|
|
cov_gen_script(
|
2024-10-23 21:36:26 +02:00
|
|
|
name="enterprise_coverity_build",
|
|
|
|
testonly=True,
|
|
|
|
tags=["coverity"],
|
2024-04-30 07:36:06 +02:00
|
|
|
deps=[
|
|
|
|
""")
|
|
|
|
for target in targets:
|
2024-05-17 18:01:09 +02:00
|
|
|
buildfile.write(
|
|
|
|
"""\
|
2024-04-30 07:36:06 +02:00
|
|
|
"%s",
|
2024-05-17 18:01:09 +02:00
|
|
|
"""
|
|
|
|
% target
|
|
|
|
)
|
2024-04-30 07:36:06 +02:00
|
|
|
|
|
|
|
buildfile.write("""\
|
|
|
|
],
|
|
|
|
)
|
|
|
|
""")
|
|
|
|
|
2024-09-04 00:12:49 +02:00
|
|
|
print(
|
|
|
|
" ".join(
|
2024-10-23 21:36:26 +02:00
|
|
|
[bazel_executable, "--output_user_root=/data/bazel_cache", "build"]
|
|
|
|
+ bazel_cmd_args
|
|
|
|
+ ["//src/mongo/db/modules/enterprise/coverity:enterprise_coverity_build"]
|
2024-09-04 00:12:49 +02:00
|
|
|
)
|
2024-05-17 18:01:09 +02:00
|
|
|
)
|