Skip to content

Commit b4b16fa

Browse files
ryanberryhillMongoDB Bot
authored andcommitted
SERVER-95382 Introduce python module to find the mongo toolchain (#31229)
GitOrigin-RevId: cd137fb
1 parent 37db508 commit b4b16fa

File tree

11 files changed

+381
-988
lines changed

11 files changed

+381
-988
lines changed

bazel/mongo_script_rules.bzl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""Common mongo-specific bazel build rules intended to be used for buildscripts.
2+
"""
3+
4+
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
5+
6+
MONGO_TOOLCHAIN_V4_PATH = "/opt/mongodbtoolchain/v4"
7+
MONGO_TOOLCHAIN_V5_PATH = "external/mongo_toolchain_v5/v5"
8+
9+
def _py_cxx_wrapper(*, python_path, toolchain_path, python_interpreter, main_py):
10+
return "\n".join([
11+
"export PYTHONPATH={}".format(python_path),
12+
"export MONGO_TOOLCHAIN_PATH={}".format(toolchain_path),
13+
"{} {}".format(python_interpreter, main_py),
14+
])
15+
16+
def _py_cxx_test_impl(ctx):
17+
python = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime
18+
19+
python_path = []
20+
for dep in ctx.attr.deps:
21+
for path in dep[PyInfo].imports.to_list():
22+
if path not in python_path:
23+
python_path.append(
24+
ctx.expand_make_variables(
25+
"python_library_imports",
26+
"$${RUNFILES_DIR}/" + path,
27+
ctx.var,
28+
),
29+
)
30+
python_path_str = ctx.configuration.host_path_separator.join(python_path)
31+
32+
cc_toolchain = find_cpp_toolchain(ctx)
33+
runfiles = ctx.runfiles(
34+
files = (
35+
ctx.files.srcs +
36+
ctx.files.data +
37+
ctx.files.deps +
38+
ctx.files.main +
39+
cc_toolchain.all_files.to_list()
40+
),
41+
)
42+
transitive_runfiles = []
43+
for runfiles_attr in (
44+
[ctx.attr.main],
45+
ctx.attr.srcs,
46+
ctx.attr.deps,
47+
ctx.attr.data,
48+
):
49+
for target in runfiles_attr:
50+
transitive_runfiles.append(target[DefaultInfo].default_runfiles)
51+
runfiles = runfiles.merge_all(transitive_runfiles)
52+
53+
main_py = ctx.attr.main.files.to_list()[0].path
54+
script = _py_cxx_wrapper(
55+
python_path = python_path_str,
56+
toolchain_path = ctx.attr.toolchain_path,
57+
python_interpreter = python.interpreter.path,
58+
main_py = main_py,
59+
)
60+
ctx.actions.write(
61+
output = ctx.outputs.executable,
62+
content = script,
63+
)
64+
65+
return DefaultInfo(files = depset([ctx.outputs.executable]), runfiles = runfiles)
66+
67+
py_cxx_test = rule(
68+
implementation = _py_cxx_test_impl,
69+
attrs = {
70+
"main": attr.label(allow_single_file = True, mandatory = True),
71+
"srcs": attr.label_list(allow_files = [".py"]),
72+
"deps": attr.label_list(),
73+
"data": attr.label_list(),
74+
"toolchain_path": attr.string(mandatory = True),
75+
},
76+
toolchains = ["@bazel_tools//tools/cpp:toolchain_type", "@bazel_tools//tools/python:toolchain_type"],
77+
executable = True,
78+
test = True,
79+
)
80+
81+
def mongo_toolchain_py_cxx_test(**kwargs):
82+
py_cxx_test(
83+
toolchain_path = select({
84+
"//bazel/config:mongo_toolchain_v5": MONGO_TOOLCHAIN_V5_PATH,
85+
"//conditions:default": MONGO_TOOLCHAIN_V4_PATH,
86+
}),
87+
target_compatible_with = ["@//bazel/platforms:use_mongo_toolchain"],
88+
**kwargs
89+
)

buildscripts/BUILD.bazel

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ py_binary(
6868
"click",
6969
group = "evergreen",
7070
),
71+
dependency(
72+
"typing-extensions",
73+
group = "core",
74+
),
7175
],
7276
)
7377

@@ -194,3 +198,31 @@ sh_binary(
194198
},
195199
visibility = ["//visibility:public"],
196200
)
201+
202+
py_library(
203+
name = "mongo_toolchain",
204+
srcs = [
205+
"mongo_toolchain.py",
206+
],
207+
visibility = ["//visibility:public"],
208+
deps = [
209+
dependency(
210+
"typer",
211+
group = "core",
212+
),
213+
],
214+
)
215+
216+
py_library(
217+
name = "clang_tidy_lib",
218+
srcs = [
219+
"apply_clang_tidy_fixes.py",
220+
"clang_tidy.py",
221+
"clang_tidy_vscode.py",
222+
],
223+
visibility = ["//visibility:public"],
224+
deps = [
225+
"mongo_toolchain",
226+
"simple_report",
227+
],
228+
)

buildscripts/clang_format.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
from buildscripts.linter import git, parallel
3434
from buildscripts.linter.filediff import gather_changed_files_for_lint
35+
from buildscripts.mongo_toolchain import get_mongo_toolchain
3536

3637
##############################################################################
3738
#
@@ -47,12 +48,14 @@
4748
# Name of clang-format as a binary
4849
CLANG_FORMAT_PROGNAME = "clang-format"
4950

51+
TOOLCHAIN_VERSION = "v4"
52+
5053
CLANG_FORMAT_HTTP_DARWIN_CACHE = (
5154
"http://mongodbtoolchain.build.10gen.cc/toolchain/osx/clang-format-12.0.1"
5255
)
5356

54-
# TODO: Move clang format to the v4 toolchain
55-
CLANG_FORMAT_TOOLCHAIN_PATH = "/opt/mongodbtoolchain/v4/bin/clang-format"
57+
toolchain = get_mongo_toolchain(version=TOOLCHAIN_VERSION)
58+
CLANG_FORMAT_TOOLCHAIN_PATH = toolchain.get_tool_path(CLANG_FORMAT_PROGNAME)
5659

5760

5861
##############################################################################

buildscripts/clang_tidy.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
from typing import Any, Dict, List, Optional, Tuple
1919

2020
import yaml
21+
22+
# Get relative imports to work when the package is not installed on the PYTHONPATH.
23+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
24+
2125
from clang_tidy_vscode import CHECKS_SO
26+
from mongo_toolchain import get_mongo_toolchain
2227
from simple_report import make_report, put_report, try_combine_reports
2328

2429
checks_so = ""
@@ -163,7 +168,8 @@ def __dedup_errors(clang_tidy_errors_threads: List[str]) -> str:
163168

164169

165170
def _run_tidy(args, parser_defaults):
166-
clang_tidy_binary = f"/opt/mongodbtoolchain/{args.clang_tidy_toolchain}/bin/clang-tidy"
171+
toolchain = get_mongo_toolchain(version=args.clang_tidy_toolchain)
172+
clang_tidy_binary = toolchain.get_tool_path("clang-tidy")
167173

168174
if os.path.exists(args.check_module):
169175
mongo_tidy_check_module = args.check_module
@@ -366,8 +372,7 @@ def main():
366372
default=False,
367373
help="if this is a test evaluating clang tidy itself.",
368374
)
369-
# TODO: Is there someway to get this without hardcoding this much
370-
parser.add_argument("-y", "--clang-tidy-toolchain", type=str, default="v4")
375+
parser.add_argument("-y", "--clang-tidy-toolchain", type=str, default=None)
371376
parser.add_argument("-f", "--clang-tidy-cfg", type=str, default=config_file)
372377
args = parser.parse_args()
373378

buildscripts/clang_tidy_vscode.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
import subprocess
2929
import sys
3030

31+
# Get relative imports to work when the package is not installed on the PYTHONPATH.
32+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
33+
34+
from mongo_toolchain import get_mongo_toolchain
35+
3136
CHECKS_SO = [
3237
"build/install/lib/libmongo_tidy_checks.so",
3338
]
@@ -38,7 +43,8 @@
3843

3944

4045
def main():
41-
clang_tidy_args = ["/opt/mongodbtoolchain/v4/bin/clang-tidy"]
46+
toolchain = get_mongo_toolchain()
47+
clang_tidy_args = [toolchain.get_tool_path("clang-tidy")]
4248
for check_lib in CHECKS_SO:
4349
if os.path.isfile(check_lib):
4450
clang_tidy_args += [f"-load={check_lib}"]

0 commit comments

Comments
 (0)