SERVER-120217 Move python rules to using py_binary rather than using the toolchain (#48508)

GitOrigin-RevId: c2e5ada619cf3acc6ea6849540b9c8239df76de7
This commit is contained in:
Andrew Bradshaw 2026-02-26 13:57:27 -08:00 committed by MongoDB Bot
parent a874bc8eb4
commit 3703480801
14 changed files with 82 additions and 90 deletions

View File

@ -100,29 +100,27 @@ render_template(
name = "clang_tidy_config",
srcs = [
".clang-tidy.in",
"//buildscripts:clang_tidy_config_gen.py",
],
cmd = [
"$(location //buildscripts:clang_tidy_config_gen.py)",
"--input=$(location .clang-tidy.in)",
"--output=$(location .clang-tidy)",
],
output = ".clang-tidy",
python_file = "//buildscripts:clang_tidy_config_gen.py",
)
render_template(
name = "clang_tidy_config_strict",
srcs = [
".clang-tidy.in",
"//buildscripts:clang_tidy_config_gen.py",
],
cmd = [
"$(location //buildscripts:clang_tidy_config_gen.py)",
"--input=$(location .clang-tidy.in)",
"--output=$(location .clang-tidy.strict)",
"--warnings-as-errors",
],
output = ".clang-tidy.strict",
python_file = "//buildscripts:clang_tidy_config_gen.py",
)
genrule(

View File

@ -82,10 +82,11 @@ config_setting(
],
)
# Expose script for external usage through bazel.
exports_files([
"generate_config_header.py",
])
py_binary(
name = "generate_config_header",
srcs = ["generate_config_header.py"],
main = "generate_config_header.py",
)
# --------------------------------------
# Compiler types

View File

@ -61,9 +61,6 @@ def generate_config_header_impl(ctx):
"cpp_defines": " ".join(ctx.attr.cpp_defines),
}
python = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime
generator_script = ctx.attr.generator_script.files.to_list()[0].path
additional_inputs = []
additional_inputs_depsets = []
for additional_input in ctx.attr.additional_inputs:
@ -74,18 +71,15 @@ def generate_config_header_impl(ctx):
additional_inputs.append(file.path)
ctx.actions.run(
executable = python.interpreter.path,
executable = ctx.executable.generator_script,
outputs = [ctx.outputs.output, ctx.outputs.logfile],
mnemonic = "ConfigHeaderGen",
inputs = depset(transitive = [
cc_toolchain.all_files,
python.files,
ctx.attr.generator_script.files,
ctx.attr.template.files,
ctx.attr.checks.files,
] + additional_inputs_depsets),
arguments = [
generator_script, # bazel/config/mongo_config_header.py
"--output-path",
ctx.outputs.output.path,
"--template-path",
@ -148,8 +142,9 @@ generate_config_header_rule = rule(
),
"generator_script": attr.label(
doc = "The python generator script to use.",
default = "//bazel/config:generate_config_header.py",
allow_single_file = True,
default = "//bazel/config:generate_config_header",
executable = True,
cfg = "exec",
),
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
"_sdkroot": attr.label(default = "//bazel/config:sdkroot"),

View File

@ -1,26 +1,22 @@
load("//bazel:utils.bzl", "write_target")
load("@rules_python//python:defs.bzl", "py_binary")
def render_template_impl(ctx):
python = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime
python_libs = [py_dep[PyInfo].transitive_sources for py_dep in ctx.attr.python_libs]
python_path = []
for py_dep in ctx.attr.python_libs:
for path in py_dep[PyInfo].imports.to_list():
if path not in python_path:
python_path.append(ctx.expand_make_variables("python_library_imports", "$(BINDIR)/external/" + path, ctx.var))
expanded_args = [
ctx.expand_make_variables("render_template_expand", ctx.expand_location(arg, ctx.attr.srcs), ctx.var)
for arg in ctx.attr.cmd
]
# Add runfiles package dir to PYTHONPATH so scripts can import python_libs (e.g. gen_helper).
runfiles_package_dir = ctx.executable.python_binary.path + ".runfiles/" + ctx.workspace_name + "/" + ctx.label.package
env = {"PYTHONPATH": runfiles_package_dir}
ctx.actions.run(
executable = python.interpreter.path,
executable = ctx.executable.python_binary,
outputs = [ctx.outputs.output],
inputs = depset(transitive = [python.files, depset([arg.files.to_list()[0] for arg in ctx.attr.srcs])] + python_libs),
inputs = depset(transitive = [depset([arg.files.to_list()[0] for arg in ctx.attr.srcs])]),
arguments = expanded_args,
env = {"PYTHONPATH": ctx.configuration.host_path_separator.join(python_path)},
env = env,
mnemonic = "TemplateRenderer",
)
@ -40,43 +36,49 @@ render_template_rule = rule(
"cmd": attr.string_list(
doc = "The command line arguments to pass to python",
),
"python_libs": attr.label_list(
providers = [PyInfo],
default = [],
"python_binary": attr.label(
executable = True,
cfg = "exec",
),
},
toolchains = ["@bazel_tools//tools/python:toolchain_type"],
output_to_genfiles = True,
)
def render_template(name, tags = [], **kwargs):
def render_template(name, srcs, cmd, output, python_file, python_libs = [], tags = [], **kwargs):
py_binary(
name = name + "_python",
srcs = [python_file],
main = python_file,
tags = tags + ["gen_source"],
deps = python_libs,
)
render_template_rule(
name = name,
srcs = srcs,
cmd = cmd,
output = output,
python_binary = name + "_python",
tags = tags + ["gen_source"],
**kwargs
)
def render_templates_impl(ctx):
python = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime
python_libs = [py_dep[PyInfo].transitive_sources for py_dep in ctx.attr.python_libs]
python_path = []
for py_dep in ctx.attr.python_libs:
for path in py_dep[PyInfo].imports.to_list():
if path not in python_path:
python_path.append(ctx.expand_make_variables("python_library_imports", "$(BINDIR)/external/" + path, ctx.var))
expanded_args = [
ctx.expand_make_variables("render_template_expand", ctx.expand_location(arg, ctx.attr.srcs), ctx.var)
for arg in ctx.attr.cmd
]
# Add runfiles package dir to PYTHONPATH so scripts can import python_libs (e.g. gen_helper).
runfiles_package_dir = ctx.executable.python_binary.path + ".runfiles/" + ctx.workspace_name + "/" + ctx.label.package
env = {"PYTHONPATH": runfiles_package_dir}
ctx.actions.run(
executable = python.interpreter.path,
executable = ctx.executable.python_binary,
outputs = ctx.outputs.outputs,
inputs = depset(transitive = [python.files, depset([arg.files.to_list()[0] for arg in ctx.attr.srcs])] + python_libs),
inputs = depset(transitive = [depset([arg.files.to_list()[0] for arg in ctx.attr.srcs])]),
arguments = expanded_args,
env = {"PYTHONPATH": ctx.configuration.host_path_separator.join(python_path)},
env = env,
mnemonic = "TemplateRenderer",
)
@ -96,18 +98,29 @@ render_templates_rule = rule(
"cmd": attr.string_list(
doc = "The command line arguments to pass to python",
),
"python_libs": attr.label_list(
providers = [PyInfo],
default = [],
"python_binary": attr.label(
executable = True,
cfg = "exec",
),
},
toolchains = ["@bazel_tools//tools/python:toolchain_type"],
output_to_genfiles = True,
)
def render_templates(name, tags = [], **kwargs):
def render_templates(name, srcs, cmd, outputs, python_file, python_libs = [], tags = [], **kwargs):
py_binary(
name = name + "_python",
srcs = [python_file],
main = python_file,
tags = tags + ["gen_source"],
deps = python_libs,
)
render_templates_rule(
name = name,
srcs = srcs,
cmd = cmd,
outputs = outputs,
python_binary = name + "_python",
tags = tags + ["gen_source"],
**kwargs
)

View File

@ -19,15 +19,14 @@ render_template(
srcs = [
"error_codes.tpl.cpp",
"error_codes.yml",
"generate_error_codes.py",
],
cmd = [
"$(location generate_error_codes.py)",
"$(location error_codes.yml)",
"$(location error_codes.tpl.cpp)",
"$(location error_codes.cpp)",
],
output = "error_codes.cpp",
python_file = "generate_error_codes.py",
python_libs = [
dependency(
"ct3",
@ -45,15 +44,14 @@ render_template(
srcs = [
"error_codes.tpl.h",
"error_codes.yml",
"generate_error_codes.py",
],
cmd = [
"$(location generate_error_codes.py)",
"$(location error_codes.yml)",
"$(location error_codes.tpl.h)",
"$(location error_codes.h)",
],
output = "error_codes.h",
python_file = "generate_error_codes.py",
python_libs = [
dependency(
"ct3",

View File

@ -52,11 +52,9 @@ render_template(
name = "feature_flag_test_tpl",
srcs = [
"feature_flag_test.idl.tpl",
"//buildscripts:cheetah_source_generator.py",
"//src/mongo/util/version:releases.yml",
],
cmd = [
"$(location //buildscripts:cheetah_source_generator.py)",
"-o",
"$(location feature_flag_test.idl)",
"$(location feature_flag_test.idl.tpl)",
@ -64,6 +62,7 @@ render_template(
"$(MONGO_VERSION)",
],
output = "feature_flag_test.idl",
python_file = "//buildscripts:cheetah_source_generator.py",
python_libs = [
dependency(
"ct3",

View File

@ -361,15 +361,14 @@ render_template(
srcs = [
"builtin_roles.tpl.cpp",
"builtin_roles.yml",
"builtin_roles_gen.py",
],
cmd = [
"$(location builtin_roles_gen.py)",
"$(location builtin_roles.yml)",
"$(location builtin_roles.tpl.cpp)",
"$(location builtin_roles.cpp)",
],
output = "builtin_roles.cpp",
python_file = "builtin_roles_gen.py",
python_libs = [
dependency(
"ct3",

View File

@ -149,15 +149,14 @@ pkg_name = "//" + package_name() + "/"
name = extension_name + "_conf",
srcs = [
"configurations.yml",
":gen_conf.py",
],
cmd = [
"$(location :gen_conf.py)",
"$(location " + extension_name + ".conf)",
"$(location configurations.yml)",
extension_name,
],
output = extension_name + ".conf",
python_file = ":gen_conf.py",
python_libs = [
dependency(
"pyyaml",

View File

@ -41,18 +41,15 @@ render_template(
srcs = [
"stop_words_{}.txt".format(lang)
for lang in STOP_WORD_LANGUAGES
] + [
"generate_stop_words.py",
],
cmd = [
"$(location generate_stop_words.py)",
] + [
"$(location stop_words_{}.txt)".format(lang)
for lang in STOP_WORD_LANGUAGES
] + [
"$(location stop_words_list.cpp)",
],
output = "stop_words_list.cpp",
python_file = "generate_stop_words.py",
)
mongo_cc_library(

View File

@ -13,46 +13,43 @@ exports_files(
render_template(
name = "codepoints_casefold_gen",
srcs = [
"gen_casefold_map.py",
"gen_helper.py",
"//src/third_party/unicode-8.0.0:CaseFolding.txt",
],
cmd = [
"$(location gen_casefold_map.py)",
"$(location //src/third_party/unicode-8.0.0:CaseFolding.txt)",
"$(location codepoints_casefold.cpp)",
],
output = "codepoints_casefold.cpp",
python_file = "gen_casefold_map.py",
python_libs = [":gen_helper"],
)
render_template(
name = "codepoints_delimiter_list_gen",
srcs = [
"gen_delimiter_list.py",
"gen_helper.py",
"//src/third_party/unicode-8.0.0:PropList.txt",
],
cmd = [
"$(location gen_delimiter_list.py)",
"$(location //src/third_party/unicode-8.0.0:PropList.txt)",
"$(location codepoints_delimiter_list.cpp)",
],
output = "codepoints_delimiter_list.cpp",
python_file = "gen_delimiter_list.py",
python_libs = [":gen_helper"],
)
render_template(
name = "codepoints_diacritic_list_gen",
srcs = [
"gen_diacritic_list.py",
"gen_helper.py",
"//src/third_party/unicode-8.0.0:PropList.txt",
],
cmd = [
"$(location gen_diacritic_list.py)",
"$(location //src/third_party/unicode-8.0.0:PropList.txt)",
"$(location codepoints_diacritic_list.cpp)",
],
output = "codepoints_diacritic_list.cpp",
python_file = "gen_diacritic_list.py",
python_libs = [":gen_helper"],
)
mongo_cc_library(
@ -83,6 +80,11 @@ mongo_cc_unit_test(
],
)
py_library(
name = "gen_helper",
srcs = ["gen_helper.py"],
)
py_binary(
name = "gen_diacritic_map",
srcs = ["gen_diacritic_map.py"],

View File

@ -33,14 +33,13 @@ render_template(
name = "mongohelpers_js_cpp",
srcs = [
"mongohelpers.js",
"//buildscripts:jstoh.py",
],
cmd = [
"$(location //buildscripts:jstoh.py)",
"$(location mongohelpers_js.cpp)",
"$(location mongohelpers.js)",
],
output = "mongohelpers_js.cpp",
python_file = "//buildscripts:jstoh.py",
)
mongo_js_library(

View File

@ -235,15 +235,14 @@ render_template(
srcs = [
"error_codes.tpl.js",
"//src/mongo/base:error_codes.yml",
"//src/mongo/base:generate_error_codes.py",
],
cmd = [
"$(location //src/mongo/base:generate_error_codes.py)",
"$(location //src/mongo/base:error_codes.yml)",
"$(location error_codes.tpl.js)",
"$(location error_codes.js)",
],
output = "error_codes.js",
python_file = "//src/mongo/base:generate_error_codes.py",
python_libs = [
dependency(
"ct3",
@ -294,17 +293,15 @@ MONGOJS_CPP_JSFILES = [
# into the native binary via bytecode upon startup.
render_template(
name = "mongojs_cpp",
srcs = [
"//buildscripts:jstoh.py",
] + MONGOJS_CPP_JSFILES,
srcs = MONGOJS_CPP_JSFILES,
cmd = [
"$(location //buildscripts:jstoh.py)",
"$(location mongojs.cpp)",
] + [
"$(location {})".format(file)
for file in MONGOJS_CPP_JSFILES
],
output = "mongojs.cpp",
python_file = "//buildscripts:jstoh.py",
)
MONGO_SERVER_CPP_JS_FILES = [
@ -323,17 +320,15 @@ MONGO_SERVER_CPP_JS_FILES = [
render_template(
name = "mongo_server_cpp",
srcs = [
"//buildscripts:jstoh.py",
] + MONGO_SERVER_CPP_JS_FILES,
srcs = MONGO_SERVER_CPP_JS_FILES,
cmd = [
"$(location //buildscripts:jstoh.py)",
"$(location mongo-server.cpp)",
] + [
"$(location {})".format(file)
for file in MONGO_SERVER_CPP_JS_FILES
],
output = "mongo-server.cpp",
python_file = "//buildscripts:jstoh.py",
)
mongo_cc_library(

View File

@ -206,14 +206,11 @@ mongo_cc_library(
render_template(
name = "icu_init_cpp",
srcs = [
"generate_icu_init_cpp.py",
] + select({
srcs = select({
"@platforms//cpu:s390x": ["//src/third_party/icu4c-57.1/source:mongo_sources/icudt57b.dat"],
"//conditions:default": ["//src/third_party/icu4c-57.1/source:mongo_sources/icudt57l.dat"],
}),
cmd = [
"$(location generate_icu_init_cpp.py)",
"-o",
"$(location icu_init.cpp)",
"-i",
@ -222,6 +219,7 @@ render_template(
"//conditions:default": ["$(location //src/third_party/icu4c-57.1/source:mongo_sources/icudt57l.dat)"],
}),
output = "icu_init.cpp",
python_file = "generate_icu_init_cpp.py",
)
# When using ICU from third_party, icu_init.cpp will load a subset of

View File

@ -8,10 +8,8 @@ render_template(
srcs = [
"releases.tpl.h",
"releases.yml",
"//buildscripts:cheetah_source_generator.py",
],
cmd = [
"$(location //buildscripts:cheetah_source_generator.py)",
"-o",
"$(location releases.h)",
"$(location releases.tpl.h)",
@ -19,6 +17,7 @@ render_template(
"$(MONGO_VERSION)",
],
output = "releases.h",
python_file = "//buildscripts:cheetah_source_generator.py",
python_libs = [
dependency(
"ct3",