From 53ba408a2f5502313792e46e5730a056e233bcab Mon Sep 17 00:00:00 2001 From: felixxia-oai Date: Mon, 7 Sep 2026 17:22:10 +0000 Subject: [PATCH] Fix jemalloc tools and compiler flags for Bazel musl builds (#43533) The musl CLI allocator needs declared native tools and consistent header flags. Configure `tikv-jemalloc-sys` to use Bazel's Make toolchain and LLVM's `llvm-nm` and `llvm-ranlib`, and patch its build script to honor `MAKE`. Preserve `CFLAGS` and `CXXFLAGS` when generating jemalloc header dependencies so dependency generation uses the same compiler flags as compilation. GitOrigin-RevId: 4022b587ce0c8bce9331c541adbdf29c1d245558 --- MODULE.bazel | 21 ++++++++++ patches/BUILD.bazel | 2 + .../tikv-jemalloc-sys_dependency_flags.patch | 38 +++++++++++++++++++ patches/tikv-jemalloc-sys_make_env.patch | 13 +++++++ 4 files changed, 74 insertions(+) create mode 100644 patches/tikv-jemalloc-sys_dependency_flags.patch create mode 100644 patches/tikv-jemalloc-sys_make_env.patch diff --git a/MODULE.bazel b/MODULE.bazel index bdb15bf876..b39e1a8581 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -613,6 +613,27 @@ crate.annotation( crate = "opusic-sys", ) +# The musl CLI's allocator needs declared native tools and consistent header flags. +crate.annotation( + build_script_env = { + "MAKE": "$${pwd}/$(MAKE)", + "NM": "$${pwd}/$(execpath @llvm//tools:llvm-nm)", + "RANLIB": "$${pwd}/$(execpath @llvm//tools:llvm-ranlib)", + }, + build_script_toolchains = ["@rules_foreign_cc//toolchains:current_make_toolchain"], + build_script_tools = [ + "@llvm//tools:llvm-nm", + "@llvm//tools:llvm-ranlib", + "@rules_foreign_cc//toolchains:current_make_toolchain", + ], + crate = "tikv-jemalloc-sys", + patch_args = ["-p1"], + patches = [ + "//patches:tikv-jemalloc-sys_make_env.patch", + "//patches:tikv-jemalloc-sys_dependency_flags.patch", + ], +) + inject_repo(crate, "rules_foreign_cc") crate.annotation( diff --git a/patches/BUILD.bazel b/patches/BUILD.bazel index d906c20ed2..2afcf05d1e 100644 --- a/patches/BUILD.bazel +++ b/patches/BUILD.bazel @@ -16,6 +16,8 @@ exports_files([ "rules_rs_zlib_snapshot_urls.patch", "rusty_v8_prebuilt_out_dir.patch", "ring_windows_msvc_include_dirs.patch", + "tikv-jemalloc-sys_make_env.patch", + "tikv-jemalloc-sys_dependency_flags.patch", "v8_bazel_rules.patch", "v8_module_deps.patch", "v8_source_portability.patch", diff --git a/patches/tikv-jemalloc-sys_dependency_flags.patch b/patches/tikv-jemalloc-sys_dependency_flags.patch new file mode 100644 index 0000000000..9c9d715718 --- /dev/null +++ b/patches/tikv-jemalloc-sys_dependency_flags.patch @@ -0,0 +1,38 @@ +diff --git a/jemalloc/Makefile.in b/jemalloc/Makefile.in +--- a/jemalloc/Makefile.in ++++ b/jemalloc/Makefile.in +@@ -508,7 +508,7 @@ + @mkdir -p $(@D) + $(CC) $(CFLAGS) -c $(CPPFLAGS) $(CTARGET) $< + ifdef CC_MM +- @$(CC) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $< ++ @$(CC) $(CFLAGS) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $< + endif + + $(C_SYMS): %.sym: +@@ -532,7 +532,7 @@ + @mkdir -p $(@D) + $(CXX) $(CXXFLAGS) -c $(CPPFLAGS) $(CTARGET) $< + ifdef CC_MM +- @$(CXX) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $< ++ @$(CXX) $(CXXFLAGS) -MM $(CPPFLAGS) -MT $@ -o $(@:%.$(O)=%.d) $< + endif + + ifneq ($(SOREV),$(SO)) +@@ -589,14 +589,14 @@ + @mkdir -p $(@D) + $(CC) $(CFLAGS) -c $(CPPFLAGS) -DJEMALLOC_STRESS_TEST -I$(srcroot)test/include -I$(objroot)test/include $(CTARGET) $< + ifdef CC_MM +- @$(CC) -MM $(CPPFLAGS) -DJEMALLOC_STRESS_TEST -I$(srcroot)test/include -I$(objroot)test/include -MT $@ -o $(@:%.$(O)=%.d) $< ++ @$(CC) $(CFLAGS) -MM $(CPPFLAGS) -DJEMALLOC_STRESS_TEST -I$(srcroot)test/include -I$(objroot)test/include -MT $@ -o $(@:%.$(O)=%.d) $< + endif + + $(objroot)test/stress/pa/%.$(O): $(srcroot)test/stress/pa/%.cpp + @mkdir -p $(@D) + $(CXX) $(CXXFLAGS) -c $(CPPFLAGS) -I$(srcroot)test/include -I$(objroot)test/include $(CTARGET) $< + ifdef CC_MM +- @$(CXX) -MM $(CPPFLAGS) -I$(srcroot)test/include -I$(objroot)test/include -MT $@ -o $(@:%.$(O)=%.d) $< ++ @$(CXX) $(CXXFLAGS) -MM $(CPPFLAGS) -I$(srcroot)test/include -I$(objroot)test/include -MT $@ -o $(@:%.$(O)=%.d) $< + endif + + build_lib_shared: $(DSOS) diff --git a/patches/tikv-jemalloc-sys_make_env.patch b/patches/tikv-jemalloc-sys_make_env.patch new file mode 100644 index 0000000000..305022b3af --- /dev/null +++ b/patches/tikv-jemalloc-sys_make_env.patch @@ -0,0 +1,13 @@ +diff --git a/build.rs b/build.rs +--- a/build.rs ++++ b/build.rs +@@ -331,7 +331,8 @@ + run_and_log(&mut cmd, &build_dir.join("config.log")); + + // Make: +- let make = make_cmd(&host); ++ let make = read_and_watch_env("MAKE").unwrap_or_else(|_| make_cmd(&host).to_owned()); ++ let make = make.as_str(); + run(&mut make_command(make, &build_dir, &num_jobs)); + + // Skip watching this environment variables to avoid rebuild in CI.