2024-04-30 07:36:06 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import io
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
# 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",
|
|
|
|
"--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
|
|
|
|
|
|
|
coverity_dir = os.path.dirname(__file__)
|
|
|
|
analysis_dir = os.path.join(coverity_dir, "analysis")
|
|
|
|
os.makedirs(analysis_dir, exist_ok=True)
|
|
|
|
|
2024-05-17 18:01:09 +02:00
|
|
|
with open(os.path.join(coverity_dir, "analysis", "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(
|
|
|
|
name="coverity_build",
|
|
|
|
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-05-17 18:01:09 +02:00
|
|
|
cmd = (
|
|
|
|
[
|
|
|
|
sys.executable,
|
|
|
|
"./buildscripts/scons.py",
|
|
|
|
]
|
|
|
|
+ sys.argv
|
|
|
|
+ ["BAZEL_INTEGRATION_DEBUG=1", "\\$BUILD_ROOT/scons/\\$VARIANT_DIR/sconf_temp"]
|
|
|
|
)
|
2024-04-30 07:36:06 +02:00
|
|
|
|
|
|
|
# Run a lightwieght scons build to generate the bazel command.
|
|
|
|
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
|
|
for line in proc.stdout.splitlines():
|
|
|
|
if "BAZEL_COMMAND:" in line:
|
2024-05-17 18:01:09 +02:00
|
|
|
# The script is intended to be have output placed into a bash variable, so we should
|
2024-04-30 07:36:06 +02:00
|
|
|
# only ever print the bazel build command
|
2024-05-17 18:01:09 +02:00
|
|
|
cmd = line.split("BAZEL_COMMAND:")[-1].strip().split()[:-1] + [
|
|
|
|
"//bazel/coverity/analysis:coverity_build"
|
|
|
|
]
|
|
|
|
print(" ".join(cmd))
|