From bca8bdf288b0d47101373cf81dbbd63880d1c4a6 Mon Sep 17 00:00:00 2001 From: Alexandros Theodoridis Date: Mon, 18 May 2026 09:35:37 +0000 Subject: [PATCH 1/7] Remove unused dead code --- .../wrappers/hipcc_wrapper | 56 ------------------- 1 file changed, 56 deletions(-) diff --git a/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper b/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper index 8bdc2b45..0cc7d292 100755 --- a/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper +++ b/cc/impls/linux_x86_64_linux_x86_64_rocm/wrappers/hipcc_wrapper @@ -100,62 +100,6 @@ def GetOptionValue(argv, option): return sum(vars(args)[option], []) -def GetHostCompilerOptions(argv, filter_hermetic_llvm=False, filter_sysroot=False): - """Collect include paths and sysroot options from argv. - - This function extracts -isystem, -iquote, --sysroot, and -g flags from - the argument list, with optional filtering for GPU compilation. - - Args: - argv: A list of strings containing command-line arguments. - filter_hermetic_llvm: If True, filter out hermetic LLVM headers. - Used for GPU compilation where ROCm provides its own LLVM headers. - filter_sysroot: If True, filter out --sysroot flag. - Used for GPU compilation to avoid system headers. - - Returns: - A list of options ready to be passed to hipcc or clang. - - Notes: - - Filters hermetic LLVM headers for GPU (they conflict with ROCm's) - - Filters sysroot headers for GPU (incompatible with ROCm clang) - - Used by InvokeHipcc to build GPU compiler options - """ - parser = ArgumentParser() - parser.add_argument("-isystem", nargs="*", action="append") - parser.add_argument("-iquote", nargs="*", action="append") - parser.add_argument("--sysroot", nargs=1) - parser.add_argument("-g", nargs="*", action="append") - parser.add_argument("-no-canonical-prefixes", action="store_true") - - args, _ = parser.parse_known_args(argv) - - opts = [] - - if args.isystem: - for p in sum(args.isystem, []): - # Skip hermetic LLVM headers for GPU compilation - they conflict with ROCm's - # ROCm provides its own LLVM headers via --rocm-path - if filter_hermetic_llvm and "/llvm" in p and "/clang/" in p: - continue - # Skip sysroot headers for GPU compilation - they have non-device-compatible operator new - # ROCm provides device-compatible standard library headers via cuda_wrappers - if filter_sysroot and "/sysroot_" in p: - continue - opts.extend(["-isystem", p]) - if args.iquote: - for p in sum(args.iquote, []): - opts.extend(["-iquote", p]) - if args.g: - opts.append("-g" + "".join(sum(args.g, []))) - if args.no_canonical_prefixes: - opts.append("-no-canonical-prefixes") - if args.sysroot and not filter_sysroot: - opts.extend(["--sysroot", args.sysroot[0]]) - - return opts - - def GetHipccOptions(argv): """Collect ROCm/hipcc-specific compiler options from argv. From eb8010bc3c58dd11c00cab28a1ff473cec223b8b Mon Sep 17 00:00:00 2001 From: Alexandros Theodoridis Date: Mon, 18 May 2026 09:51:27 +0000 Subject: [PATCH 2/7] Support hermetic sysroot for rocm --- cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD index e89089b5..12f0ea24 100644 --- a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD +++ b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD @@ -115,11 +115,9 @@ cc_toolchain_import( "@sysroot_linux_x86_64//:sys_incs", "@llvm_linux_x86_64//:libclang_rt", ] + select({ - # Local sysroot: don't include hermetic libs (use system libs via --sysroot=/) "//cc/sysroots:local_sysroot_enabled": [], - # Hermetic sysroot: use hermetic libs "//conditions:default": [ - "@sysroot_linux_x86_64//:std_libs", + ":std_libs", "@sysroot_linux_x86_64//:sys_libs", ], }), @@ -281,3 +279,20 @@ cc_toolchain( toolchain_config = ":config", toolchain_identifier = "toolchain_linux_x86_64_linux_x86_64_rocm_id", ) + +# Aliases to select between libc++ and libstdc++ +alias( + name = "std_incs", + actual = select({ + "//common:is_stdlib_libcxx": "@llvm_linux_x86_64//:std_incs", + "//conditions:default": "@sysroot_linux_x86_64//:std_incs", + }), +) + +alias( + name = "std_libs", + actual = select({ + "//common:is_stdlib_libcxx": "@llvm_linux_x86_64//:std_libs", + "//conditions:default": "@sysroot_linux_x86_64//:std_libs", + }), +) From c6a5ba26dd63f4889122194d8d1ce809609cd0af Mon Sep 17 00:00:00 2001 From: Alexandros Theodoridis Date: Mon, 18 May 2026 12:00:28 +0000 Subject: [PATCH 3/7] Remove local sysroot --- MODULE.bazel | 6 - WORKSPACE | 7 -- cc/features/BUILD | 25 ----- cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD | 43 ++------ cc/sysroots/BUILD | 16 --- cc/sysroots/defs.bzl.tpl | 17 --- cc/sysroots/local_sysroot_configure.bzl | 103 ------------------ extensions/local_sysroot_configure.bzl | 16 --- 8 files changed, 8 insertions(+), 225 deletions(-) delete mode 100644 cc/sysroots/defs.bzl.tpl delete mode 100644 cc/sysroots/local_sysroot_configure.bzl delete mode 100644 extensions/local_sysroot_configure.bzl diff --git a/MODULE.bazel b/MODULE.bazel index e1de7e91..b4c9dea2 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -172,10 +172,4 @@ use_repo( "xz", ) -############################################################## -# Local sysroot configuration - -local_sysroot = use_extension("//extensions:local_sysroot_configure.bzl", "local_sysroot_ext") -use_repo(local_sysroot, "local_sysroot_config") - register_toolchains("//cc/...") diff --git a/WORKSPACE b/WORKSPACE index cea8f4e1..56d8b9d9 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -244,10 +244,3 @@ sycl_configure(name = "local_config_sycl") load("//gpu/rocm:hipcc_configure.bzl", "hipcc_configure") hipcc_configure(name = "config_rocm_hipcc") - -############################################################## -# Local sysroot configuration - -load("//cc/sysroots:local_sysroot_configure.bzl", "local_sysroot_configure") - -local_sysroot_configure(name = "local_sysroot_config") diff --git a/cc/features/BUILD b/cc/features/BUILD index 93eb6a02..665b4828 100644 --- a/cc/features/BUILD +++ b/cc/features/BUILD @@ -89,31 +89,6 @@ cc_toolchain_sysroot_feature( target = "x86_64-linux-gnu", ) -# Local sysroot feature - use system root instead of hermetic sysroot -# Generic for all Linux architectures -cc_feature( - name = "local_sysroot_linux", - enabled = True, - compiler_flags = ["--sysroot=/"], - linker_flags = ["--sysroot=/"], - env_sets = { - "HOST_SYSROOT": "/", - }, -) - -# When using local sysroot, we need to explicitly link default system libraries -# since we don't use hermetic_libraries feature -cc_feature( - name = "local_sysroot_default_libs", - enabled = True, - linker_flags = [ - "-lstdc++", - "-lm", - "-lpthread", - "-ldl", - ], -) - cc_toolchain_sysroot_feature( name = "sysroot_linux_aarch64", enabled = True, diff --git a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD index 12f0ea24..730916ff 100644 --- a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD +++ b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD @@ -1,5 +1,3 @@ -load("@local_sysroot_config//:defs.bzl", "local_sysroot_info") - # Copyright 2026 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -85,12 +83,7 @@ cc_toolchain_import( alias( name = "startup_libs", - actual = select({ - # Local sysroot: startup libs via --sysroot flag - "//cc/sysroots:local_sysroot_enabled": ":empty_startup_libs", - # Hermetic sysroot: use hermetic startup libs - "//conditions:default": "@sysroot_linux_x86_64//:startup_libs", - }), + actual = "@sysroot_linux_x86_64//:startup_libs", visibility = ["//visibility:public"], ) @@ -113,14 +106,9 @@ cc_toolchain_import( # Hermetic LLVM headers "@llvm_linux_x86_64//:compiler_incs", "@sysroot_linux_x86_64//:sys_incs", - "@llvm_linux_x86_64//:libclang_rt", - ] + select({ - "//cc/sysroots:local_sysroot_enabled": [], - "//conditions:default": [ - ":std_libs", - "@sysroot_linux_x86_64//:sys_libs", - ], - }), + ":std_libs", + "@sysroot_linux_x86_64//:sys_libs", + ], visibility = ["//visibility:public"], ) @@ -193,15 +181,8 @@ filegroup( # Features # ============================================================================= -FEATURES_ESSENTIAL = select({ - # Local sysroot: DO NOT use hermetic_libraries (no -nostdinc/-nostdinc++) - # We want to use system headers and libraries - "//cc/sysroots:local_sysroot_enabled": [ - "//cc/features:local_sysroot_default_libs", - ], - # Hermetic sysroot: use hermetic_libraries feature - "//conditions:default": ["//third_party/rules_cc_toolchain/features:hermetic_libraries"], -}) + [ +FEATURES_ESSENTIAL = [ + "//third_party/rules_cc_toolchain/features:hermetic_libraries", # Library imports ":imports_feature", ":startup_libs_feature", @@ -216,10 +197,7 @@ FEATURES_ESSENTIAL = select({ "//third_party/rules_cc_toolchain/features:warnings", "//third_party/rules_cc_toolchain/features:reproducible", "//third_party/rules_cc_toolchain/features:use_lld", -] + select({ - "//cc/sysroots:local_sysroot_enabled": ["//cc/features:local_sysroot_linux"], - "//conditions:default": ["//cc/features:sysroot_linux_x86_64"], -}) + [ + "//cc/features:sysroot_linux_x86_64", # PIC / PIE flags "//third_party/rules_cc_toolchain/features:supports_pic", "//third_party/rules_cc_toolchain/features:position_independent_code", @@ -252,12 +230,7 @@ cc_toolchain_config( c_compiler = "@llvm_linux_x86_64//:clang", cc_compiler = "@llvm_linux_x86_64//:clang++", compiler_features = FEATURES_ESSENTIAL, - cxx_builtin_include_directories = select({ - # Local sysroot: use dynamically detected system include paths - "//cc/sysroots:local_sysroot_enabled": local_sysroot_info.include_directories, - # Hermetic sysroot: no system directories - "//conditions:default": [], - }), + cxx_builtin_include_directories = [], linker = "@llvm_linux_x86_64//:ld", strip_tool = "@llvm_linux_x86_64//:strip", target_cpu = "x86_64", diff --git a/cc/sysroots/BUILD b/cc/sysroots/BUILD index 019c58b1..76a04159 100644 --- a/cc/sysroots/BUILD +++ b/cc/sysroots/BUILD @@ -5,22 +5,6 @@ load(":collect_cc_libraries.bzl", "collect_cc_libraries") load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") -# Export template file for local_sysroot_configure repository rule -exports_files(["defs.bzl.tpl"]) - -# Flag to control whether to use local (system) sysroot instead of hermetic -bool_flag( - name = "use_local_sysroot", - build_setting_default = False, - visibility = ["//visibility:public"], -) - -config_setting( - name = "local_sysroot_enabled", - flag_values = {":use_local_sysroot": "True"}, - visibility = ["//visibility:public"], -) - alias( name = "openmp", actual = select({ diff --git a/cc/sysroots/defs.bzl.tpl b/cc/sysroots/defs.bzl.tpl deleted file mode 100644 index 9dd8c5e1..00000000 --- a/cc/sysroots/defs.bzl.tpl +++ /dev/null @@ -1,17 +0,0 @@ -# Auto-generated by local_sysroot_configure -# System compiler and sysroot information detected at configure time - -local_sysroot_info = struct( - # Path to the system compiler used for detection - compiler = %{compiler}, - - # Compiler version string - compiler_version = %{compiler_version}, - - # C++ builtin include directories for cxx_builtin_include_directories - # Includes both absolute paths (/usr/include/...) and sysroot-relative paths (/include/...) - include_directories = %{include_directories}, -) - -# Deprecated: Use local_sysroot_info.include_directories instead -SYSTEM_INCLUDE_PATHS = local_sysroot_info.include_directories diff --git a/cc/sysroots/local_sysroot_configure.bzl b/cc/sysroots/local_sysroot_configure.bzl deleted file mode 100644 index f0b291b5..00000000 --- a/cc/sysroots/local_sysroot_configure.bzl +++ /dev/null @@ -1,103 +0,0 @@ -"""Repository rule to detect system compiler include paths for local sysroot.""" - -load( - "//gpu:compiler_common_tools.bzl", - "get_cxx_inc_directories", - "to_list_of_strings", -) - -def _find_system_compiler(repository_ctx): - """Find the system C++ compiler. - - Tries to find a system compiler in order: cc, c++, gcc, g++, clang, clang++ - - Args: - repository_ctx: The repository context. - - Returns: - Path to the system compiler, or None if not found. - """ - # Try common compiler names in order of preference - for compiler in ["cc", "c++", "gcc", "g++", "clang", "clang++"]: - result = repository_ctx.which(compiler) - if result: - return str(result) - return None - -def _local_sysroot_configure_impl(repository_ctx): - """Implementation of local_sysroot_configure repository rule. - - Detects system compiler and include paths, generates a .bzl file with the info. - """ - # Find system compiler - cc = _find_system_compiler(repository_ctx) - if not cc: - fail("Could not find system compiler (tried: cc, c++, gcc, g++, clang, clang++)") - - # Get C++ include directories using existing detection logic - # Pass None for tf_sys_root since we want system paths, not custom sysroot - include_dirs = get_cxx_inc_directories(repository_ctx, cc, None) - - # Add sysroot-relative paths for --sysroot=/ - # When using --sysroot=/, paths like /include/c++/9 are relative to the sysroot - # So we need both /usr/include/c++/9 (absolute) and /include/c++/9 (sysroot-relative) - all_include_dirs = [] - for inc in include_dirs: - all_include_dirs.append(inc) - # Add sysroot-relative version: /usr/... -> /... - if inc.startswith("/usr/"): - relative = inc.replace("/usr/", "/", 1) - if relative not in all_include_dirs: - all_include_dirs.append(relative) - - # Get compiler version - version_result = repository_ctx.execute([cc, "--version"]) - compiler_version = version_result.stdout.split("\n")[0] if version_result.return_code == 0 else "unknown" - - # Generate defs.bzl from template - repository_ctx.template( - "defs.bzl", - repository_ctx.attr._defs_template, - substitutions = { - "%{compiler}": repr(cc), - "%{compiler_version}": repr(compiler_version), - "%{include_directories}": str(all_include_dirs), - }, - executable = False, - ) - - # Create a minimal BUILD file - repository_ctx.file( - "BUILD", - content = """# Auto-generated by local_sysroot_configure - -package(default_visibility = ["//visibility:public"]) -""", - executable = False, - ) - -local_sysroot_configure = repository_rule( - implementation = _local_sysroot_configure_impl, - local = True, - configure = True, - attrs = { - "_defs_template": attr.label( - default = Label("//cc/sysroots:defs.bzl.tpl"), - allow_single_file = True, - ), - }, - doc = """ -Detects system compiler include paths for use with local sysroot. - -This repository rule finds the system C++ compiler and queries it for -default include directories. These paths are used in cxx_builtin_include_directories -when --sysroot=/ is used for local (non-hermetic) sysroot compilation. - -The rule tries to find a compiler in this order: cc, c++, gcc, g++, clang, clang++ -It then runs the compiler with -E -xc++ to detect include paths, similar to -how Bazel's cc_configure works. - -The detected paths are written to local_sysroot_paths.bzl and can be loaded -in BUILD files. -""", -) diff --git a/extensions/local_sysroot_configure.bzl b/extensions/local_sysroot_configure.bzl deleted file mode 100644 index 08eb1a28..00000000 --- a/extensions/local_sysroot_configure.bzl +++ /dev/null @@ -1,16 +0,0 @@ -"""Extension for local_sysroot_configure repository rule (bzlmod support).""" - -load("//cc/sysroots:local_sysroot_configure.bzl", "local_sysroot_configure") - -def _local_sysroot_ext_impl(module_ctx): - """Extension implementation for local_sysroot_configure. - - Args: - module_ctx: The module extension context. - """ - # Create the local_sysroot_config repository - local_sysroot_configure(name = "local_sysroot_config") - -local_sysroot_ext = module_extension( - implementation = _local_sysroot_ext_impl, -) From ab965cb3e19ed71dae502d3947521046836ca9f6 Mon Sep 17 00:00:00 2001 From: Alexandros Theodoridis Date: Wed, 20 May 2026 09:28:30 +0000 Subject: [PATCH 4/7] Add static linking support for libstdc++ in ROCm toolchain - Add LIBSTDCXX_FLAGS constant for static libstdc++ linking - Update ROCm toolchain to support both libc++ and libstdc++ static linking - This fixes hermetic builds where -nodefaultlibs requires explicit library linking - Resolves shutdown crashes caused by mixing hermetic headers with system runtime libs --- cc/config/constants.bzl | 12 ++++- cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD | 49 +++++++++++++++++-- test.sh | 9 ++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100755 test.sh diff --git a/cc/config/constants.bzl b/cc/config/constants.bzl index f55acaa2..dea8a366 100644 --- a/cc/config/constants.bzl +++ b/cc/config/constants.bzl @@ -13,9 +13,19 @@ # limitations under the License. # ============================================================================== -# +# Flags for statically linking libc++ LIBCXX_FLAGS = [ "-lc++", "-lc++abi", "-lunwind", +] + +# Flags for statically linking libstdc++ +# When using -nodefaultlibs with hermetic builds, we need to explicitly +# link the C++ standard library. This list ensures all necessary libraries +# are statically linked. +# Note: libgcc, libm, libc are already provided by cc_toolchain_import from sysroot +# We only need to explicitly request static linking for libstdc++ +LIBSTDCXX_FLAGS = [ + "-lstdc++", ] \ No newline at end of file diff --git a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD index 730916ff..aa201f2a 100644 --- a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD +++ b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD @@ -19,10 +19,11 @@ load("@config_rocm_hipcc//rocm:build_defs.bzl", "hipcc_config") load("@rules_cc//cc:defs.bzl", "cc_toolchain") +load("//cc/config:constants.bzl", "LIBCXX_FLAGS", "LIBSTDCXX_FLAGS") load("//cc/rocm/features:rocm_hipcc_feature.bzl", "rocm_hipcc_feature") load("//third_party/rules_cc_toolchain:toolchain_config.bzl", "cc_toolchain_config") load("//third_party/rules_cc_toolchain/features:cc_toolchain_import.bzl", "cc_toolchain_import") -load("//third_party/rules_cc_toolchain/features:features.bzl", "cc_toolchain_import_feature") +load("//third_party/rules_cc_toolchain/features:features.bzl", "cc_feature", "cc_toolchain_import_feature") package( default_visibility = [ @@ -89,7 +90,7 @@ alias( cc_toolchain_import_feature( name = "startup_libs_feature", - enabled = True, + enabled = False, toolchain_import = ":startup_libs", ) @@ -112,6 +113,49 @@ cc_toolchain_import( visibility = ["//visibility:public"], ) +config_setting( + name = "use_static_libcxx", + flag_values = { + "//common:stdlib": "libc++", + "//common:static_libcxx": "True", + }, +) + +config_setting( + name = "use_static_libstdcxx", + flag_values = { + "//common:stdlib": "libstdc++", + "//common:static_libcxx": "True", + }, +) + +# Feature to force static linking of libstdc++ +# Use compiler's built-in flags for static linking +cc_feature( + name = "static_link_libstdcxx", + enabled = select({ + ":use_static_libstdcxx": True, + "//conditions:default": False, + }), + linker_flags = [ + "-static-libstdc++", + "-static-libgcc", + ], +) + +# Feature to force static linking of libc++ +# For libc++, we need to use -Wl,-Bstatic since there's no -static-libc++ flag +cc_feature( + name = "static_link_libcxx", + enabled = select({ + ":use_static_libcxx": True, + "//conditions:default": False, + }), + linker_flags = [ + "-static-libc++", + ], +) + cc_toolchain_import_feature( name = "imports_feature", enabled = True, @@ -182,7 +226,6 @@ filegroup( # ============================================================================= FEATURES_ESSENTIAL = [ - "//third_party/rules_cc_toolchain/features:hermetic_libraries", # Library imports ":imports_feature", ":startup_libs_feature", diff --git a/test.sh b/test.sh new file mode 100755 index 00000000..ae613238 --- /dev/null +++ b/test.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +bazel test \ + --config=rocm \ + --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ + --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ + --config=bzlmod \ + --define=using_rocm=true \ + //cc/tests/gpu/rocm:all From 4c90b4421a3c85f261a71b61f2daaf1d0dd27e5f Mon Sep 17 00:00:00 2001 From: Alexandros Theodoridis Date: Wed, 20 May 2026 11:22:00 +0000 Subject: [PATCH 5/7] Revert local sysroot support --- MODULE.bazel | 6 ++ WORKSPACE | 7 ++ cc/config/constants.bzl | 10 +-- cc/features/BUILD | 25 ++++++ cc/sysroots/BUILD | 16 ++++ cc/sysroots/defs.bzl.tpl | 17 ++++ cc/sysroots/local_sysroot_configure.bzl | 103 ++++++++++++++++++++++++ extensions/local_sysroot_configure.bzl | 16 ++++ test.sh | 9 --- 9 files changed, 192 insertions(+), 17 deletions(-) create mode 100644 cc/sysroots/defs.bzl.tpl create mode 100644 cc/sysroots/local_sysroot_configure.bzl create mode 100644 extensions/local_sysroot_configure.bzl delete mode 100755 test.sh diff --git a/MODULE.bazel b/MODULE.bazel index b4c9dea2..e1de7e91 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -172,4 +172,10 @@ use_repo( "xz", ) +############################################################## +# Local sysroot configuration + +local_sysroot = use_extension("//extensions:local_sysroot_configure.bzl", "local_sysroot_ext") +use_repo(local_sysroot, "local_sysroot_config") + register_toolchains("//cc/...") diff --git a/WORKSPACE b/WORKSPACE index 56d8b9d9..cea8f4e1 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -244,3 +244,10 @@ sycl_configure(name = "local_config_sycl") load("//gpu/rocm:hipcc_configure.bzl", "hipcc_configure") hipcc_configure(name = "config_rocm_hipcc") + +############################################################## +# Local sysroot configuration + +load("//cc/sysroots:local_sysroot_configure.bzl", "local_sysroot_configure") + +local_sysroot_configure(name = "local_sysroot_config") diff --git a/cc/config/constants.bzl b/cc/config/constants.bzl index dea8a366..871616ad 100644 --- a/cc/config/constants.bzl +++ b/cc/config/constants.bzl @@ -13,19 +13,13 @@ # limitations under the License. # ============================================================================== -# Flags for statically linking libc++ +# LIBCXX_FLAGS = [ "-lc++", "-lc++abi", "-lunwind", ] -# Flags for statically linking libstdc++ -# When using -nodefaultlibs with hermetic builds, we need to explicitly -# link the C++ standard library. This list ensures all necessary libraries -# are statically linked. -# Note: libgcc, libm, libc are already provided by cc_toolchain_import from sysroot -# We only need to explicitly request static linking for libstdc++ LIBSTDCXX_FLAGS = [ "-lstdc++", -] \ No newline at end of file +] diff --git a/cc/features/BUILD b/cc/features/BUILD index 665b4828..93eb6a02 100644 --- a/cc/features/BUILD +++ b/cc/features/BUILD @@ -89,6 +89,31 @@ cc_toolchain_sysroot_feature( target = "x86_64-linux-gnu", ) +# Local sysroot feature - use system root instead of hermetic sysroot +# Generic for all Linux architectures +cc_feature( + name = "local_sysroot_linux", + enabled = True, + compiler_flags = ["--sysroot=/"], + linker_flags = ["--sysroot=/"], + env_sets = { + "HOST_SYSROOT": "/", + }, +) + +# When using local sysroot, we need to explicitly link default system libraries +# since we don't use hermetic_libraries feature +cc_feature( + name = "local_sysroot_default_libs", + enabled = True, + linker_flags = [ + "-lstdc++", + "-lm", + "-lpthread", + "-ldl", + ], +) + cc_toolchain_sysroot_feature( name = "sysroot_linux_aarch64", enabled = True, diff --git a/cc/sysroots/BUILD b/cc/sysroots/BUILD index 76a04159..019c58b1 100644 --- a/cc/sysroots/BUILD +++ b/cc/sysroots/BUILD @@ -5,6 +5,22 @@ load(":collect_cc_libraries.bzl", "collect_cc_libraries") load("@bazel_skylib//rules:common_settings.bzl", "bool_flag") +# Export template file for local_sysroot_configure repository rule +exports_files(["defs.bzl.tpl"]) + +# Flag to control whether to use local (system) sysroot instead of hermetic +bool_flag( + name = "use_local_sysroot", + build_setting_default = False, + visibility = ["//visibility:public"], +) + +config_setting( + name = "local_sysroot_enabled", + flag_values = {":use_local_sysroot": "True"}, + visibility = ["//visibility:public"], +) + alias( name = "openmp", actual = select({ diff --git a/cc/sysroots/defs.bzl.tpl b/cc/sysroots/defs.bzl.tpl new file mode 100644 index 00000000..9dd8c5e1 --- /dev/null +++ b/cc/sysroots/defs.bzl.tpl @@ -0,0 +1,17 @@ +# Auto-generated by local_sysroot_configure +# System compiler and sysroot information detected at configure time + +local_sysroot_info = struct( + # Path to the system compiler used for detection + compiler = %{compiler}, + + # Compiler version string + compiler_version = %{compiler_version}, + + # C++ builtin include directories for cxx_builtin_include_directories + # Includes both absolute paths (/usr/include/...) and sysroot-relative paths (/include/...) + include_directories = %{include_directories}, +) + +# Deprecated: Use local_sysroot_info.include_directories instead +SYSTEM_INCLUDE_PATHS = local_sysroot_info.include_directories diff --git a/cc/sysroots/local_sysroot_configure.bzl b/cc/sysroots/local_sysroot_configure.bzl new file mode 100644 index 00000000..f0b291b5 --- /dev/null +++ b/cc/sysroots/local_sysroot_configure.bzl @@ -0,0 +1,103 @@ +"""Repository rule to detect system compiler include paths for local sysroot.""" + +load( + "//gpu:compiler_common_tools.bzl", + "get_cxx_inc_directories", + "to_list_of_strings", +) + +def _find_system_compiler(repository_ctx): + """Find the system C++ compiler. + + Tries to find a system compiler in order: cc, c++, gcc, g++, clang, clang++ + + Args: + repository_ctx: The repository context. + + Returns: + Path to the system compiler, or None if not found. + """ + # Try common compiler names in order of preference + for compiler in ["cc", "c++", "gcc", "g++", "clang", "clang++"]: + result = repository_ctx.which(compiler) + if result: + return str(result) + return None + +def _local_sysroot_configure_impl(repository_ctx): + """Implementation of local_sysroot_configure repository rule. + + Detects system compiler and include paths, generates a .bzl file with the info. + """ + # Find system compiler + cc = _find_system_compiler(repository_ctx) + if not cc: + fail("Could not find system compiler (tried: cc, c++, gcc, g++, clang, clang++)") + + # Get C++ include directories using existing detection logic + # Pass None for tf_sys_root since we want system paths, not custom sysroot + include_dirs = get_cxx_inc_directories(repository_ctx, cc, None) + + # Add sysroot-relative paths for --sysroot=/ + # When using --sysroot=/, paths like /include/c++/9 are relative to the sysroot + # So we need both /usr/include/c++/9 (absolute) and /include/c++/9 (sysroot-relative) + all_include_dirs = [] + for inc in include_dirs: + all_include_dirs.append(inc) + # Add sysroot-relative version: /usr/... -> /... + if inc.startswith("/usr/"): + relative = inc.replace("/usr/", "/", 1) + if relative not in all_include_dirs: + all_include_dirs.append(relative) + + # Get compiler version + version_result = repository_ctx.execute([cc, "--version"]) + compiler_version = version_result.stdout.split("\n")[0] if version_result.return_code == 0 else "unknown" + + # Generate defs.bzl from template + repository_ctx.template( + "defs.bzl", + repository_ctx.attr._defs_template, + substitutions = { + "%{compiler}": repr(cc), + "%{compiler_version}": repr(compiler_version), + "%{include_directories}": str(all_include_dirs), + }, + executable = False, + ) + + # Create a minimal BUILD file + repository_ctx.file( + "BUILD", + content = """# Auto-generated by local_sysroot_configure + +package(default_visibility = ["//visibility:public"]) +""", + executable = False, + ) + +local_sysroot_configure = repository_rule( + implementation = _local_sysroot_configure_impl, + local = True, + configure = True, + attrs = { + "_defs_template": attr.label( + default = Label("//cc/sysroots:defs.bzl.tpl"), + allow_single_file = True, + ), + }, + doc = """ +Detects system compiler include paths for use with local sysroot. + +This repository rule finds the system C++ compiler and queries it for +default include directories. These paths are used in cxx_builtin_include_directories +when --sysroot=/ is used for local (non-hermetic) sysroot compilation. + +The rule tries to find a compiler in this order: cc, c++, gcc, g++, clang, clang++ +It then runs the compiler with -E -xc++ to detect include paths, similar to +how Bazel's cc_configure works. + +The detected paths are written to local_sysroot_paths.bzl and can be loaded +in BUILD files. +""", +) diff --git a/extensions/local_sysroot_configure.bzl b/extensions/local_sysroot_configure.bzl new file mode 100644 index 00000000..08eb1a28 --- /dev/null +++ b/extensions/local_sysroot_configure.bzl @@ -0,0 +1,16 @@ +"""Extension for local_sysroot_configure repository rule (bzlmod support).""" + +load("//cc/sysroots:local_sysroot_configure.bzl", "local_sysroot_configure") + +def _local_sysroot_ext_impl(module_ctx): + """Extension implementation for local_sysroot_configure. + + Args: + module_ctx: The module extension context. + """ + # Create the local_sysroot_config repository + local_sysroot_configure(name = "local_sysroot_config") + +local_sysroot_ext = module_extension( + implementation = _local_sysroot_ext_impl, +) diff --git a/test.sh b/test.sh deleted file mode 100755 index ae613238..00000000 --- a/test.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -bazel test \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --config=bzlmod \ - --define=using_rocm=true \ - //cc/tests/gpu/rocm:all From 1a8660e8c1abbd687b1d8881fd0fcb6becafce30 Mon Sep 17 00:00:00 2001 From: Alexandros Theodoridis Date: Wed, 20 May 2026 12:25:30 +0000 Subject: [PATCH 6/7] Remove localsysroot tests for rocm --- .../rocm_linux_x86_64_linux_x86_64_build.yaml | 41 ------------------- 1 file changed, 41 deletions(-) diff --git a/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml b/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml index 1f9b3500..98d6bfd6 100644 --- a/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml +++ b/.github/workflows/rocm_linux_x86_64_linux_x86_64_build.yaml @@ -90,44 +90,3 @@ jobs: --define=using_rocm=true \ //cc/tests/cpu:all - - name: Test ROCm GPU code compilation (local sysroot) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --define=using_rocm=true \ - //cc/tests/gpu/rocm:all - - - name: Test host (CPU) code compilation (local sysroot) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --define=using_rocm=true \ - //cc/tests/cpu:all - - - name: Test ROCm GPU code compilation (local sysroot + bzlmod) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --config=bzlmod \ - --define=using_rocm=true \ - //cc/tests/gpu/rocm:all - - - name: Test host (CPU) code compilation (local sysroot + bzlmod) - run: | - bazel test --color=yes \ - --@rules_ml_toolchain//cc/sysroots:use_local_sysroot=True \ - --config=rocm \ - --repo_env=TF_ROCM_AMDGPU_TARGETS="gfx942" \ - --repo_env=ROCM_DISTRO_VERSION="rocm_7.12.0_gfx94X" \ - --config=bzlmod \ - --define=using_rocm=true \ - //cc/tests/cpu:all From c39d07aba9bf9b95b13bcb7a2c5ad2f0d41a8236 Mon Sep 17 00:00:00 2001 From: Alexandros Theodoridis Date: Thu, 21 May 2026 06:28:24 +0000 Subject: [PATCH 7/7] Address review comments --- .bazelrc | 1 + cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD | 39 ++++++++++++------- common/BUILD | 12 ++++++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/.bazelrc b/.bazelrc index a5c71cc8..7c659722 100644 --- a/.bazelrc +++ b/.bazelrc @@ -266,5 +266,6 @@ common:rocm_common --copt=-Wno-gnu-offsetof-extensions common:rocm --config=rocm_common common:rocm --dynamic_mode=off common:rocm --@rules_ml_toolchain//common:enable_hermetic_cc=True +common:rocm --@rules_ml_toolchain//common:enable_hermetic_libraries=True try-import %workspace%/.bazelrc.local diff --git a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD index aa201f2a..06146733 100644 --- a/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD +++ b/cc/impls/linux_x86_64_linux_x86_64_rocm/BUILD @@ -90,7 +90,10 @@ alias( cc_toolchain_import_feature( name = "startup_libs_feature", - enabled = False, + enabled = select({ + "//common:is_hermetic_libraries_enabled": True, + "//conditions:default": False, + }), toolchain_import = ":startup_libs", ) @@ -114,7 +117,7 @@ cc_toolchain_import( ) config_setting( - name = "use_static_libcxx", + name = "is_static_libcxx", flag_values = { "//common:stdlib": "libc++", "//common:static_libcxx": "True", @@ -122,7 +125,7 @@ config_setting( ) config_setting( - name = "use_static_libstdcxx", + name = "is_static_libstdcxx", flag_values = { "//common:stdlib": "libstdc++", "//common:static_libcxx": "True", @@ -134,7 +137,7 @@ config_setting( cc_feature( name = "static_link_libstdcxx", enabled = select({ - ":use_static_libstdcxx": True, + ":is_static_libstdcxx": True, "//conditions:default": False, }), linker_flags = [ @@ -143,25 +146,29 @@ cc_feature( ], ) -# Feature to force static linking of libc++ -# For libc++, we need to use -Wl,-Bstatic since there's no -static-libc++ flag +cc_toolchain_import_feature( + name = "imports_feature", + enabled = True, + toolchain_import = ":imports", +) + +# Conditional hermetic libraries feature +# Disabled by default for XLA (to avoid -nodefaultlibs issues with ROCm) +# Can be enabled via --@rules_ml_toolchain//common:enable_hermetic_libraries=True cc_feature( - name = "static_link_libcxx", + name = "hermetic_libraries_conditional", + cc_flags = ["-nostdinc++"], + compiler_flags = ["-nostdinc"], enabled = select({ - ":use_static_libcxx": True, + "//common:is_hermetic_libraries_enabled": True, "//conditions:default": False, }), linker_flags = [ - "-static-libc++", + "-nostdlib", + "-nodefaultlibs", ], ) -cc_toolchain_import_feature( - name = "imports_feature", - enabled = True, - toolchain_import = ":imports", -) - # buildifier: leave-alone filegroup( name = "all", @@ -226,6 +233,8 @@ filegroup( # ============================================================================= FEATURES_ESSENTIAL = [ + # Conditional hermetic libraries (disabled by default for XLA) + ":hermetic_libraries_conditional", # Library imports ":imports_feature", ":startup_libs_feature", diff --git a/common/BUILD b/common/BUILD index cb1df122..412b3da7 100644 --- a/common/BUILD +++ b/common/BUILD @@ -108,6 +108,18 @@ config_setting( }, ) +bool_flag( + name = "enable_hermetic_libraries", + build_setting_default = False, +) + +config_setting( + name = "is_hermetic_libraries_enabled", + flag_values = { + ":enable_hermetic_libraries": "True", + }, +) + string_flag( name = "stdlib", build_setting_default = "libstdc++",