defs.bzl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """Provides helpers for cc rules. Intended for general consumption."""
  5. load("@bazel_cc_toolchain//:clang_detected_variables.bzl", "llvm_symbolizer")
  6. def cc_env():
  7. """Returns standard environment settings for a cc_binary.
  8. In use, this looks like:
  9. ```
  10. load("//bazel/cc_toolchains:defs.bzl", "cc_env")
  11. cc_binary(
  12. ...
  13. env = cc_env(),
  14. )
  15. ```
  16. We're currently setting this on a target-by-target basis, mainly because
  17. it's difficult to modify default behaviors.
  18. """
  19. # Settings which apply cross-platform.
  20. # buildifier: disable=unsorted-dict-items
  21. common_env = {
  22. "LLVM_SYMBOLIZER_PATH": llvm_symbolizer,
  23. # Sanitizers don't use LLVM as fallback, but sometimes ASAN may be used
  24. # for UBSAN errors; we still set UBSAN in case it's directly used.
  25. "ASAN_SYMBOLIZER_PATH": llvm_symbolizer,
  26. "UBSAN_SYMBOLIZER_PATH": llvm_symbolizer,
  27. # Default to printing traces for UBSAN.
  28. "UBSAN_OPTIONS": "print_stacktrace=1",
  29. }
  30. # On macOS, there's a nano zone allocation warning when asan is enabled.
  31. # This suppresses the warning in `bazel run`.
  32. macos_env = {"MallocNanoZone": "0"}
  33. return common_env | select({
  34. "//bazel/cc_toolchains:macos_asan": macos_env,
  35. "//conditions:default": {},
  36. })