chore: cross-platform jemalloc.h generation and Bazel 9 support (#3)

- Replace jemalloc.h genrule with cross-platform cc_binary (jemalloc_hdr_gen)
- Migrate all rules_cc imports from defs.bzl compat shim to granular paths
- Add sh_binary targets for shell script genrules
- Pin MODULE.bazel deps to minimum versions required by Bazel 8
- Add bazel_lib and rules_shell dependencies
- Rename jemalloc_core to lib/jemalloc for conventional output naming
This commit is contained in:
UebelAndre 2026-03-11 22:27:00 -07:00 committed by Connor McEntee
parent db4f5f7461
commit 11ab2d4f38
18 changed files with 222 additions and 3418 deletions

View file

@ -1,7 +1,10 @@
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//tools:autoconf.bzl", "configure_header", "define_macro_if", "define_macro_with")
load("//tools:version.bzl", "parse_version")
load(":jemalloc_hdr.bzl", "jemalloc_hdr")
version = parse_version(module_version())
@ -48,27 +51,36 @@ expand_template(
template = "jemalloc_macros.h.in",
)
sh_binary(
name = "jemalloc_mangle_sh",
srcs = ["jemalloc_mangle.sh"],
)
# Note this scripts are not hermetic.
genrule(
name = "jemalloc_mangle",
srcs = [
"jemalloc_mangle.sh",
"//include/jemalloc/internal:public_symbols",
],
outs = ["jemalloc_mangle.h"],
cmd = "$(location jemalloc_mangle.sh) $(location //include/jemalloc/internal:public_symbols) je_ > $@",
cmd = "$(execpath :jemalloc_mangle_sh) $(execpath //include/jemalloc/internal:public_symbols) je_ > $@",
cmd_bat = "$(execpath :jemalloc_mangle_sh) $(execpath //include/jemalloc/internal:public_symbols) je_ > $@",
tags = ["manual"],
tools = [":jemalloc_mangle_sh"],
visibility = ["//visibility:public"],
)
# Note this scripts are not hermetic.
genrule(
name = "jemalloc_mangle_jet",
srcs = [
"jemalloc_mangle.sh",
"//include/jemalloc/internal:public_symbols",
],
outs = ["jemalloc_mangle_jet.h"],
cmd = "$(location jemalloc_mangle.sh) $(location //include/jemalloc/internal:public_symbols) jet_ > $@",
cmd = "$(execpath :jemalloc_mangle_sh) $(execpath //include/jemalloc/internal:public_symbols) jet_ > $@",
cmd_bat = "$(execpath :jemalloc_mangle_sh) $(execpath //include/jemalloc/internal:public_symbols) jet_ > $@",
tags = ["manual"],
tools = [":jemalloc_mangle_sh"],
visibility = ["//visibility:public"],
)
@ -92,16 +104,22 @@ expand_template(
template = "jemalloc_protos.h.in",
)
sh_binary(
name = "jemalloc_rename_sh",
srcs = ["jemalloc_rename.sh"],
)
# Invoke jemalloc_rename.sh to generate jemalloc_rename.h
genrule(
name = "jemalloc_rename",
srcs = [
"jemalloc_rename.sh",
"//include/jemalloc/internal:public_symbols",
],
outs = ["jemalloc_rename.h"],
cmd = "$(location jemalloc_rename.sh) $(location //include/jemalloc/internal:public_symbols) > $@",
cmd = "$(execpath :jemalloc_rename_sh) $(execpath //include/jemalloc/internal:public_symbols) > $@",
cmd_bat = "$(execpath :jemalloc_rename_sh) $(execpath //include/jemalloc/internal:public_symbols) > $@",
tags = ["manual"],
tools = [":jemalloc_rename_sh"],
visibility = ["//visibility:public"],
)
@ -112,9 +130,17 @@ expand_template(
template = "jemalloc_typedefs.h.in",
)
# jemalloc.sh generates the public jemalloc.h header file. Equivalent logic is replicated to handle pathing.
# Not fully hermetic but assumes no more about the host system than the Makefile does.
genrule(
cc_binary(
name = "jemalloc_hdr_gen",
srcs = ["jemalloc_hdr_gen.cc"],
cxxopts = select({
"@rules_cc//cc/compiler:msvc-cl": ["/std:c++11"],
"//conditions:default": ["-std=c++11"],
}),
visibility = ["//visibility:private"],
)
jemalloc_hdr(
name = "jemalloc",
srcs = [
":jemalloc_defs",
@ -124,23 +150,7 @@ genrule(
":jemalloc_typedefs",
":jemalloc_mangle",
],
outs = ["jemalloc.h"],
cmd = """
echo '#ifndef JEMALLOC_H_' > $@
echo '#define JEMALLOC_H_' >> $@
echo '#ifdef __cplusplus' >> $@
echo 'extern "C" {' >> $@
echo '#endif' >> $@
echo '' >> $@
for f in $(SRCS); do
grep -v 'Generated from .* by configure' $$f | sed 's/ $$//g' >> $@
echo '' >> $@
done
echo '#ifdef __cplusplus' >> $@
echo '}' >> $@
echo '#endif' >> $@
echo '#endif /* JEMALLOC_H_ */' >> $@
""",
out = "jemalloc.h",
tags = ["manual"],
visibility = ["//:__subpackages__"],
)

View file

@ -1,6 +1,7 @@
load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template")
load("@bazel_lib//lib:expand_template.bzl", "expand_template")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("//tools:autoconf.bzl", "configure_header", "define_macro", "define_macro_if", "define_macro_if_any", "define_macro_if_with", "define_macro_with", "undefine_macro")
load("//tools:version.bzl", "parse_version")
load(":private_namespace.bzl", "private_namespace")
@ -603,7 +604,7 @@ WRAP_SYMBOLS = select({
private_namespace(
name = "private_namespace",
out = "private_namespace.h",
input = "//:jemalloc_core",
input = "//:lib/jemalloc",
public_symbols = PUBLIC_SYMBOLS,
tags = ["manual"],
wrap_symbols = WRAP_SYMBOLS,
@ -613,33 +614,45 @@ private_namespace(
name = "private_namespace_jet",
out = "private_namespace_jet.h",
enable_jet = True,
input = "//:jemalloc_core",
input = "//:lib/jemalloc",
prefix = "jet_",
public_symbols = PUBLIC_SYMBOLS,
tags = ["manual"],
wrap_symbols = WRAP_SYMBOLS,
)
sh_binary(
name = "public_namespace_sh",
srcs = ["public_namespace.sh"],
)
genrule(
name = "public_namespace",
srcs = [
"public_namespace.sh",
":public_symbols",
],
outs = ["public_namespace.h"],
cmd = "$(location public_namespace.sh) $(location :public_symbols) > $@",
cmd = "$(execpath :public_namespace_sh) $(execpath :public_symbols) > $@",
cmd_bat = "$(execpath :public_namespace_sh) $(execpath :public_symbols) > $@",
tools = [":public_namespace_sh"],
tags = ["manual"],
visibility = ["//visibility:public"],
)
sh_binary(
name = "public_unnamespace_sh",
srcs = ["public_unnamespace.sh"],
)
genrule(
name = "public_unnamespace",
srcs = [
"public_unnamespace.sh",
":public_symbols",
],
outs = ["public_unnamespace.h"],
cmd = "$(location public_unnamespace.sh) $(location :public_symbols) > $@",
cmd = "$(execpath :public_unnamespace_sh) $(execpath :public_symbols) > $@",
cmd_bat = "$(execpath :public_unnamespace_sh) $(execpath :public_symbols) > $@",
tools = [":public_unnamespace_sh"],
tags = ["manual"],
visibility = ["//visibility:public"],
)

View file

@ -1,6 +1,5 @@
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc:defs.bzl", "CcToolchainConfigInfo")
load("@rules_cc//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE", "find_cc_toolchain", "use_cc_toolchain")
load("@rules_cc//cc:find_cc_toolchain.bzl", "find_cc_toolchain", "use_cc_toolchain")
def _private_namespace_impl(ctx):
cc_toolchain = find_cc_toolchain(ctx)
@ -63,7 +62,7 @@ def _private_namespace_impl(ctx):
return [DefaultInfo(files = depset([output]))]
def _transition_impl(settings, attr):
def _transition_impl(_settings, attr):
return {
# Only C sources are used for symbol extraction
"//settings/flags:enable_cxx": False,

View file

@ -0,0 +1,39 @@
"""jemalloc_hdr"""
def _jemalloc_hdr_impl(ctx):
out = ctx.outputs.out
args = ctx.actions.args()
args.add(out)
args.add_all(ctx.files.srcs)
ctx.actions.run(
mnemonic = "JemallocHdrGen",
inputs = ctx.files.srcs,
outputs = [out],
executable = ctx.executable._generator,
arguments = [args],
)
return [DefaultInfo(files = depset([out]))]
jemalloc_hdr = rule(
doc = "A rule to generate `jemalloc.h` headers.",
implementation = _jemalloc_hdr_impl,
attrs = {
"out": attr.output(
doc = "The output header",
mandatory = True,
),
"srcs": attr.label_list(
doc = "The source files to include",
allow_files = True,
mandatory = True,
),
"_generator": attr.label(
cfg = "exec",
executable = True,
default = Label("//include/jemalloc:jemalloc_hdr_gen"),
),
},
)

View file

@ -0,0 +1,66 @@
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
static bool should_exclude(const std::string& line) {
static const std::regex pattern("Generated from .* by configure");
return std::regex_search(line, pattern);
}
static std::string strip_trailing_spaces(std::string line) {
while (!line.empty() && line.back() == ' ') {
line.pop_back();
}
return line;
}
static bool append_filtered(const std::string& src_path, std::ofstream& out) {
std::ifstream in(src_path);
if (!in) {
std::cerr << "Error: could not open " << src_path << "\n";
return false;
}
std::string line;
while (std::getline(in, line)) {
if (!should_exclude(line)) {
out << strip_trailing_spaces(line) << "\n";
}
}
out << "\n";
return true;
}
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <output> <src...>\n";
return 1;
}
const std::string out_path = argv[1];
std::ofstream out(out_path);
if (!out) {
std::cerr << "Error: could not open output file " << out_path << "\n";
return 1;
}
out << "#ifndef JEMALLOC_H_\n";
out << "#define JEMALLOC_H_\n";
out << "#ifdef __cplusplus\n";
out << "extern \"C\" {\n";
out << "#endif\n";
out << "\n";
for (int i = 2; i < argc; ++i) {
if (!append_filtered(argv[i], out)) {
return 1;
}
}
out << "#ifdef __cplusplus\n";
out << "}\n";
out << "#endif\n";
out << "#endif /* JEMALLOC_H_ */\n";
return 0;
}