C and C++ rules

C and C++ are compiled by the pinned Zig acting as the compiler, assembler and archiver. No host compiler is read and no action has a PATH.

The toolchain #

toolchains/BUCK
# toolchains/BUCK
load("@buildrules//rules/cxx:toolchain.bzl", "cxx_toolchains")

cxx_toolchains()

cxx_toolchains() declares toolchains//:cxx, which the Buck2 prelude's cxx_library and cxx_binary use unchanged.

Rules #

RuleWhat it does
c_exeone C file to a static musl executable
archive_filesnamed files out of a sha256-pinned source archive
staged_filessources copied under buck-out, so action digests do not depend on where the repository is mounted
prefix_header, prefixed_archive_check, checked_cxx_libraryrename every global symbol of a vendored library under a prefix and prove that nothing escaped the prefix
c_checkrun a built executable on the execution platform; passes only on exit 0 and exact stdout
glibc_floor_checka validation that the binary needs no glibc symbol version newer than a given one

native_library #

native_library builds a C, C++ and assembly static library (lib<name>.a) for the target platform, from the package's files and from a pinned upstream tarball, and gives a dependent its exported headers and its archives in link order. A Mojo package takes the target in natives = {...}.

BUCK
load("@buildrules//rules/native:defs.bzl", "native_library")

native_library(
    name = "zlib",
    upstream = {"url": "https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz", "sha256": "<64 hex digits>", "size": 1512791, "strip": "zlib-1.3.1", "files": ZLIB_FILES},
    srcs = ZLIB_SRCS,
    includes = ["."],
    exported_includes = ["."],
    cflags = ["-O3"] + select({"buildrules//platforms:linux": ["-D_LARGEFILE64_SOURCE=1"], "buildrules//platforms:macos": []}),
)
ArgumentWhat it is
upstreama pinned tarball: url (https), sha256, size in bytes, strip (the directory every file is below) and files, the explicit list that is staged as one tree. Nothing outside files is an input. The download is keyed by its sha256 and size, not by the url
srcssources: a file of upstream.files or of the package. .c, .cc .cpp .cxx .c++ and assembly .S (preprocessed) and .s, assembled by the pinned Zig
hdrsprivate headers of the package: a list, or a dict of include name to file, so a header is included by a path it does not have in the package
exported_headersheaders of the package a dependent includes: a list, or a dict of include name to file
includesdirectories of the upstream tree given to this library's compiles as -I; . is the tree
exported_includesdirectories of the upstream tree given as -I to the compiles of a dependent
cflags, cxxflags, asm_flagsflags of the C, the C++ and the assembly compiles, after the defaults, so a later -O3 wins over the default -O2
depsother native_library targets: this library sees their exported headers and includes, and a program that links it links their archives too, after the archives of its unit graph, a shared one once after its last user
exported_depslike deps, and a dependent of this library sees their exported headers too
cxx_runtimeoptional statement that the sources are C++ (a program that links the library must name c++ in the link of its unit (a field of the mopm unit graph), and then links the C++ runtime once); a statement that is not what the sources are is refused
sonamethe archive's file name; the default is lib<name>.a
asm_splitpaths of .S sources (a directory ending in /, or one file ending in .S; a pattern that stops inside a name is refused) whose preprocessed text has its ; turned into line breaks before it is assembled: Mach-O arm64 assembly reads ; as a comment, so a macro of ;-joined instructions keeps only its first; it runs on every platform for the named sources, so a ; inside a string or a # comment there breaks the assemble, loudly, and a .S that includes a header the library ships as exported_headers fails with file not found
prefix{"name": "p", "min": N}: every global symbol the library defines is renamed to start with p, C and assembly, ELF and Mach-O alike (the assembly of a Mach-O library names its symbols with a leading underscore, which the renaming header covers); the archive check holds that nothing defined lacks the prefix. The compiler-rt builtins the library defines are exempt (an exact list in archive_check); allow never excuses a defined symbol. extra_symbols lists names the library refers to and does not define (a weak hook a program defines), renamed too. cxx_namespaces (identifiers that do not start with a digit, never std) names C++ namespaces whose mangled names it may define without the prefix, which a header of macros cannot rename
allowsymbols the archive check may find undefined
validatetest-only: False makes the archive check an output of the target and not a validation, so a test can count the failed action (ci/faulty); leave it True
file_prefixthe directory mapped to . in every path the compiler records; the default is the package
target_compatible_withas for any target; the platforms the library and its checks exist on (the fixtures of ci/negative use it)
visibilityas for any target

srcs, hdrs, exported_headers, includes, exported_includes, cflags, cxxflags and asm_flags may each be a select() on the target platform's OS and CPU: buildrules//platforms:linux, :macos, :x86_64 and :aarch64 are separate constraint values, and :is_linux_x86_64, :is_macos_aarch64 select a pair. Every arm is checked when the package is loaded.

Example: AWS-LC #

third_party/aws-lc builds AWS-LC's libcrypto from a pinned source archive as crypto[static-pic], with every symbol prefixed. Three gates must pass: the symbol check, a known-answer self-test run through the public API, and the glibc floor. Known defects (a wrong tarball hash, a wrong arithmetic helper, a dropped allow-list entry, a missing renaming header, a wrong expected output, a too-low floor) each turn the build red.

Limit. Linux x86_64 for the toolchain; a static library built by native_library is also built for macOS arm64.