Bladeren bron

Add LLVM_SYMBOLIZER_PATH to the standard cc_binary environment. (#2291)

LLVM_SYMBOLIZER_PATH is required if `llvm-symbolizer` isn't in the developer's PATH. This sets it via bazel instead of having a developer handle it. I noticed this because the apt install of clang doesn't put llvm-symbolizer in the PATH.

I'd like to make this the default without putting it everywhere, but I don't see a way to do this intrinsically through [the toolchain](https://bazel.build/docs/cc-toolchain-config-reference), and the [rules_cc/defs.bzl](https://github.com/bazelbuild/rules_cc/blob/main/cc/defs.bzl) remains a thin wrapper around the native cc_binary.

Since I'm adding another env, it seems undesirable to have the macos asan workaround separate. As a consequence, this merges it in. Note bazel doesn't support merging a dict and a select, so it's also necessary to have the two env vars at least mildly aware there's something up (and this could get worse if we end up having more selects).
Jon Ross-Perkins 3 jaren geleden
bovenliggende
commit
c451a5004d

+ 36 - 2
bazel/cc_toolchains/BUILD

@@ -2,5 +2,39 @@
 # Exceptions. See /LICENSE for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-# Empty `BUILD` file as this package just provides repository rule inputs to
-# configure a Clang toolchain.
+load("@bazel_skylib//lib:selects.bzl", "selects")
+
+# For use by rules.bzl.
+# Matches when asan is enabled on a macOS platform.
+selects.config_setting_group(
+    name = "macos_asan",
+    match_all = [":is_macos", ":macos_asan_build_modes"],
+)
+
+# For use by rules.bzl.
+# Matches macOS platforms.
+config_setting(
+    name = "is_macos",
+    constraint_values = ["@platforms//os:osx"],
+)
+
+# For use by rules.bzl.
+# Matches build modes where asan is enabled.
+selects.config_setting_group(
+    name = "macos_asan_build_modes",
+    match_any = [":dbg", ":fastbuild"],
+)
+
+# For use by rules.bzl.
+# Matches dbg.
+config_setting(
+    name = "dbg",
+    values = {"compilation_mode": "dbg"},
+)
+
+# For use by rules.bzl.
+# Matches fastbuild.
+config_setting(
+    name = "fastbuild",
+    values = {"compilation_mode": "fastbuild"},
+)

+ 10 - 9
bazel/cc_toolchains/clang_configuration.bzl

@@ -69,7 +69,7 @@ def _detect_system_clang(repository_ctx):
     if "clang" not in version_output:
         fail("Searching for clang or CC (%s), and found (%s), which is not a Clang compiler" % (cc, cc_path))
     clang_version, clang_version_for_cache = _clang_version(version_output)
-    return (cc_path, clang_version, clang_version_for_cache)
+    return (cc_path.realpath, clang_version, clang_version_for_cache)
 
 def _compute_clang_resource_dir(repository_ctx, clang):
     """Runs the `clang` binary to get its resource dir."""
@@ -165,16 +165,16 @@ def _configure_clang_toolchain_impl(repository_ctx):
     (clang, clang_version, clang_version_for_cache) = _detect_system_clang(
         repository_ctx,
     )
-    clang = clang.realpath.dirname.get_child("clang++")
+    clang_cpp = clang.dirname.get_child("clang++")
 
     # Compute the various directories used by Clang.
-    resource_dir = _compute_clang_resource_dir(repository_ctx, clang)
+    resource_dir = _compute_clang_resource_dir(repository_ctx, clang_cpp)
     sysroot_dir = None
     if repository_ctx.os.name.lower().startswith("mac os"):
         sysroot_dir = _compute_mac_os_sysroot(repository_ctx)
     include_dirs = _compute_clang_cpp_include_search_paths(
         repository_ctx,
-        clang,
+        clang_cpp,
         sysroot_dir,
     )
 
@@ -182,10 +182,10 @@ def _configure_clang_toolchain_impl(repository_ctx):
     # First look for llvm-ar adjacent to clang, so that if found,
     # it is most likely to match the same version as clang.
     # Otherwise, try PATH.
-    arpath = clang.dirname.get_child("llvm-ar")
-    if not arpath.exists:
-        arpath = repository_ctx.which("llvm-ar")
-        if not arpath:
+    ar_path = clang.dirname.get_child("llvm-ar")
+    if not ar_path.exists:
+        ar_path = repository_ctx.which("llvm-ar")
+        if not ar_path:
             fail("`llvm-ar` not found in PATH or adjacent to clang")
 
     # By default Windows uses '\' in its paths. These will be
@@ -199,7 +199,8 @@ def _configure_clang_toolchain_impl(repository_ctx):
         "clang_detected_variables.bzl",
         repository_ctx.attr._clang_detected_variables_template,
         substitutions = {
-            "{LLVM_BINDIR}": str(arpath.dirname),
+            "{LLVM_BINDIR}": str(ar_path.dirname),
+            "{LLVM_SYMBOLIZER}": str(ar_path.dirname.get_child("llvm-symbolizer")),
             "{CLANG_BINDIR}": str(clang.dirname),
             "{CLANG_VERSION}": str(clang_version),
             "{CLANG_VERSION_FOR_CACHE}": clang_version_for_cache.replace('"', "_").replace("\\", "_"),

+ 1 - 0
bazel/cc_toolchains/clang_detected_variables.tpl.bzl

@@ -9,6 +9,7 @@ This file gets processed by a repository rule, substituting the
 """
 
 llvm_bindir = "{LLVM_BINDIR}"
+llvm_symbolizer = "{LLVM_SYMBOLIZER}"
 clang_bindir = "{CLANG_BINDIR}"
 clang_version = {CLANG_VERSION}
 clang_version_for_cache = "{CLANG_VERSION_FOR_CACHE}"

+ 22 - 0
bazel/cc_toolchains/defs.bzl

@@ -0,0 +1,22 @@
+# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+# Exceptions. See /LICENSE for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+"""Provides helpers for cc rules. Intended for general consumption."""
+
+load("@bazel_cc_toolchain//:clang_detected_variables.bzl", "llvm_symbolizer")
+
+def cc_env():
+    """Returns standard environment settings for a cc_binary."""
+    env = {"LLVM_SYMBOLIZER_PATH": llvm_symbolizer}
+
+    # On macOS, there's a nano zone allocation warning due to asan (arises
+    # in fastbuild/dbg). This suppresses the warning in `bazel run`.
+    #
+    # Concatenation of a dict with a select isn't supported, so we concatenate
+    # within the select.
+    # https://github.com/bazelbuild/bazel/issues/12457
+    return select({
+        "//bazel/cc_toolchains:macos_asan": env.update({"MallocNanoZone": "0"}),
+        "//conditions:default": env,
+    })

+ 0 - 32
bazel/macos_malloc/BUILD

@@ -1,32 +0,0 @@
-# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
-# Exceptions. See /LICENSE for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-load("@bazel_skylib//lib:selects.bzl", "selects")
-
-exports_files(["env.bzl"])
-
-selects.config_setting_group(
-    name = "macos_asan",
-    match_all = [":is_macos", ":macos_asan_build_modes"],
-)
-
-config_setting(
-    name = "is_macos",
-    constraint_values = ["@platforms//os:osx"],
-)
-
-selects.config_setting_group(
-    name = "macos_asan_build_modes",
-    match_any = [":dbg", ":fastbuild"],
-)
-
-config_setting(
-    name = "dbg",
-    values = {"compilation_mode": "dbg"},
-)
-
-config_setting(
-    name = "fastbuild",
-    values = {"compilation_mode": "fastbuild"},
-)

+ 0 - 16
bazel/macos_malloc/env.bzl

@@ -1,16 +0,0 @@
-# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
-# Exceptions. See /LICENSE for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-"""Provides support for macOS-specific malloc issues."""
-
-def macos_malloc_env():
-    """Disable nano malloc when running asan.
-
-    On macOS, there's a nano zone allocation warning due to asan (arises
-    in fastbuild/dbg). This suppresses the warning in `bazel run`.
-    """
-    return select({
-        "//bazel/macos_malloc:macos_asan": {"MallocNanoZone": "0"},
-        "//conditions:default": {},
-    })

+ 2 - 2
explorer/BUILD

@@ -2,7 +2,7 @@
 # Exceptions. See /LICENSE for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-load("//bazel/macos_malloc:env.bzl", "macos_malloc_env")
+load("//bazel/cc_toolchains:defs.bzl", "cc_env")
 
 package(default_visibility = [
     "//bazel/check_deps:__pkg__",
@@ -33,7 +33,7 @@ cc_library(
 cc_binary(
     name = "explorer",
     srcs = ["main_bin.cpp"],
-    env = macos_malloc_env(),
+    env = cc_env(),
     deps = [
         ":main",
         "//common:bazel_working_dir",

+ 2 - 0
toolchain/driver/BUILD

@@ -2,6 +2,7 @@
 # Exceptions. See /LICENSE for license information.
 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
+load("//bazel/cc_toolchains:defs.bzl", "cc_env")
 load("//bazel/fuzzing:rules.bzl", "cc_fuzz_test")
 
 package(default_visibility = ["//visibility:public"])
@@ -53,6 +54,7 @@ cc_fuzz_test(
 cc_binary(
     name = "carbon",
     srcs = ["driver_main.cpp"],
+    env = cc_env(),
     deps = [
         ":driver",
         "//common:bazel_working_dir",