Skip to content

Bazel API ​

Everything the xclang Bazel module offers. How to use it is in Bazel; this page is the list. The module needs Bazel 9 and rules_cc 0.2.25. It is packages/bazel of the repository, and is published to bazel.clice.io for every release.

Toolchains ​

The module registers, for every host, one C++ toolchain per target: @xclang//bazel/toolchains:<host> for the host itself, and <host>-to-<target> for the others. Each is the host archive configured for the target, and only the archive of the machine Bazel runs on is downloaded. The macOS targets have a toolchain on macOS hosts only; elsewhere, their toolchain fails the build and says why.

Every toolchain:

  • builds Windows programs named .exe, and shared libraries named .dll;
  • links libraries statically into tests and programs (static by default);
  • compiles the headers of other repositories as system headers (-isystem);
  • redacts __DATE__, __TIME__ and __TIMESTAMP__;
  • puts no absolute path on a command line (no absolute paths).

Platforms ​

@xclang//platforms:<target>, for --platforms:

platformalsotarget
x86_64-unknown-linux-gnux86_64-linux-gnuLinux x64, glibc 2.17
aarch64-unknown-linux-gnuaarch64-linux-gnuLinux arm64, glibc 2.17
x86_64-w64-mingw32x86_64-w64-windows-gnuWindows x64, MinGW-w64 (UCRT)
aarch64-w64-mingw32aarch64-w64-windows-gnuWindows arm64, MinGW-w64 (UCRT)
aarch64-apple-darwinarm64-apple-darwinmacOS arm64, from macOS hosts
x86_64-apple-darwinmacOS x64, from macOS hosts

Each has @platforms//os, @platforms//cpu and a C library, @xclang//platforms/libc:glibc, :mingw or :macosx. The toolchains ask a platform for os and cpu only, so a platform of one's own with those works too.

Features ​

featuredefault
asan, tsan, ubsan, lsanoffsanitizers, for the whole build (--features=asan); asan compiles and links with the ASan libc++. Not for Windows targets
cpp_modulesoffC++20 modules: module_interfaces of a cc_library/cc_binary, scanned by clang-scan-deps; needs --experimental_cpp_modules
gc_sectionsLinux targets: on in opt; Windows targets: offlld's --gc-sections (why not for Windows)
thinlto_cacheonthe linker's ThinLTO cache, with flags only when XCLANG_THINLTO_CACHE names a directory
generate_dsym_fileoffmacOS targets: the link makes <name>.dSYM (output group dsyms); --apple_generate_dsym turns it on for the build
supports_dynamic_linkeroffBazel's dynamic linking of libraries into tests and programs; off, they link statically
release_striponwhat <name>.stripped does: --strip-unneeded for ELF and COFF, --strip-all for Mach-O (strip)

A feature is turned on for a build with --features=<name>, off with --features=-<name>, and for one target with features = ["<name>"] or ["-<name>"].

Targets and Rules ​

label
@xclang//bazel:stdthe std and std.compat modules of libc++, for the target platform, as a library to depend on; built with the --cxxopt of the build
xclang_debug_symbols(name, binary, gsymutil_args) in @xclang//bazel:debug_symbols.bzl<binary>.gsym for every target, by llvm-gsymutil with one thread (the same file each run; gsymutil_args come after --num-threads=1), and <binary>.dSYM for macOS ones (with generate_dsym_file on the cc_binary); llvm-gsymutil's output in the output group gsym_log
@xclang//bazel:llvm-gsymutilthe llvm-gsymutil of the machine's toolchain, for bazel run: bazel run @xclang//bazel:llvm-gsymutil -- <absolute .gsym> --address=0x<address>
xclang_resource_dir(name, srcs) in @xclang//bazel:resource_dir.bzlclang's resource directory laid out as lib/clang/<major>/ in the rule's package, for a program bin/<name> of that package; @libclang's by default

Repositories ​

From the module extension, use_extension("@xclang//bazel:extensions.bzl", "xclang"); each is fetched only when a build uses it.

repository
@libclangthe target platform's libclang: @libclang//:clangBasic, :LLVMSupport, ... with their link interfaces; :headers; :resource_dir; :AllTargetsInfos, :AllTargetsDescs, :AllTargetsAsmParsers, :AllTargetsDisassemblers. With --features=asan, the ASan build
@libclang_asanthe ASan build (Linux x64 and macOS arm64 targets)
@libclang_<target>the libclang of one target
@llvm_option_incthe option tables of clang, lld, llvm-lib and llvm-dlltool, as a header-only library

Environment ​

Given with --repo_env=<name>=<value>:

variable
XCLANG_THINLTO_CACHEan absolute directory for the ThinLTO cache of the linker; the module makes it (the ThinLTO cache)
XCLANG_ROOTan unpacked host toolchain to use instead of the release's
XCLANG_LIBCLANG_ROOT, XCLANG_LIBCLANG_ASAN_ROOTan unpacked host libclang, or its ASan build, to use instead of the release's
BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1Bazel's own: turns rules_cc's detection of a local compiler off, recommended

The Release ​

packages/bazel/bazel/versions.bzl names the release the module downloads, and the sha256 of each of its archives, from the SHA256SUMS of the release. The version of the module is the release's.