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
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.
- Compiler, assembler, archiver:
zig cc,zig c++,zig arandzig ranlib. - Target
x86_64-linux-gnu.2.34, a glibc 2.34 floor that matches the Rust rules, with thex86_64CPU baseline and-fPIC. - Debug information is off (
-g0), because it would record the action's working directory. nm,objcopy,stripand the prelude's Python helper tools are refusals that name the tool. A build that reaches one fails with a clear message instead of searching the worker for it.- Dependency files, header maps and thin LTO are off for the same reason.
Rules #
| Rule | What it does |
|---|---|
c_exe | one C file to a static musl executable |
archive_files | named files out of a sha256-pinned source archive |
staged_files | sources copied under buck-out, so action digests do not depend on where the repository is mounted |
prefix_header, prefixed_archive_check, checked_cxx_library | rename every global symbol of a vendored library under a prefix and prove that nothing escaped the prefix |
c_check | run a built executable on the execution platform; passes only on exit 0 and exact stdout |
glibc_floor_check | a 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 = {...}.
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": []}),
)| Argument | What it is |
|---|---|
upstream | a 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 |
srcs | sources: 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 |
hdrs | private 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_headers | headers of the package a dependent includes: a list, or a dict of include name to file |
includes | directories of the upstream tree given to this library's compiles as -I; . is the tree |
exported_includes | directories of the upstream tree given as -I to the compiles of a dependent |
cflags, cxxflags, asm_flags | flags of the C, the C++ and the assembly compiles, after the defaults, so a later -O3 wins over the default -O2 |
deps | other 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_deps | like deps, and a dependent of this library sees their exported headers too |
cxx_runtime | optional 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 |
soname | the archive's file name; the default is lib<name>.a |
asm_split | paths 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 |
allow | symbols the archive check may find undefined |
validate | test-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_prefix | the directory mapped to . in every path the compiler records; the default is the package |
target_compatible_with | as for any target; the platforms the library and its checks exist on (the fixtures of ci/negative use it) |
visibility | as 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.
- One static archive per target, behind
archive_check: deterministic members, no debug section, no build path, and no undefined symbol the final link would lack. - The provider
NativeLibraryInfoholds the archive, the archives of the link in order, the exported include directories and whether the C++ runtime is linked. - macOS arm64: the same pinned Zig, on the Linux execution platform, compiles for
aarch64-macos; an assembly file is chosen by the CPU like any other source. The C++ runtime a macOS program links is the host's.
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.
native_library is also built for macOS arm64.