Buck2 Documentation
repository·main·Indexed 26 days ago
https://github.com/facebook/buck2Buck2 is a high-performance, hermetic, multi-language build system designed for large-scale codebases with complex inter-language dependencies. It features a dynamic incremental computation engine (DICE), support for persistent workers (local and remote), and flexible toolchain configuration including bootstrapping capabilities. The system supports remote execution via integrations such as Buildbarn and BuildBuddy.
What's inside Buck2
- The Buck2 Android Prelude is a build system extension for Meta's Buck2 designed for Android applications and libraries. It manages the complete pipeline from Java/Kotlin source code to signed APKs and Android App Bundles (AABs). It includes support for multi-DEX optimization, AAPT2 resource processing, native library integration across multiple architectures, and testing frameworks like Robolectric and instrumentation tests.
Overview of DICE (Dynamic Incremental Computation Engine)
mainDICE is a dynamic incremental computation engine designed to support parallel computation. It is implemented as an incremental computation engine.Overview of Buck2 build system
mainBuck2 is a fast, multi-language build system developed by Meta. It automates the process of creating executable binaries from source code by compiling code, linking libraries, and packaging components. It is specifically designed for large monorepos and emphasizes correctness through hermeticity.Overview of hmaptool
mainhmaptool is a Python-based utility used to create and dump Xcode header maps. Header maps are binary files that map header names or paths to other locations. This tool provides a way to manage these binary files using JSON descriptions, making them easier to test and inspect compared to raw binary formats.Overview of Distributed ThinLTO in Buck2
mainBuck2 implements a distributed version of ThinLTO (Thin Link-Time Optimization) using Starlark rules defined in
dist_lto.bzl. Unlike monolithic LTO, which is single-threaded and memory-intensive, Buck2's ThinLTO implementation leverages Buck2's ability to distribute paralleloptactions across multiple machines via Remote Execution. This approach recaptures the parallelism of C/C++ compilation while still allowing for profitable cross-module optimizations.The ThinLTO process in Buck2 is split into three primary phases:
thin_lto_prepare: (Implicitly part of the setup/compilation phase where bitcode is emitted).thin_lto_index: An indexing step that analyzes compiler IR (LLVM bitcode) to determine which modules should be optimized together based on symbol call graphs and global access.thin_lto_opt: A parallelizable phase where the optimizer (e.g., LLVMopt) combines objects identified during the index step. In Buck2, these actions can be distributed remotely.thin_lto_link: The final step that takes the optimized outputs and links them into a final binary using a standard linker.
Overview of Buck2 build system
mainBuck2 is a fast, hermetic, multi-language build system designed by Meta. It is the successor to Buck1 and is optimized for high-performance builds and complex inter-dependencies across different programming languages.
Key characteristics:
- Fast: Minimizes overhead by calculating the critical path.
- Hermetic: When using Remote Execution, Buck2 enforces that all inputs are correctly declared, ensuring build correctness and reproducibility across different environments.
- Multi-language: Provides abstractions for interoperation between different languages (e.g., a Python library depending on an OCaml library).
- Scalable: Supports ultra-large repositories through filesystem virtualization and change watching.
- Extensible: Features a language-agnostic core and the Buck Extension Language (BXL) for build system self-introspection and automation.
Overview of Starlark in Rust components
mainThe project is divided into several crates. For most developers embedding Starlark, the
starlarkcrate is the primary entry point as it re-exports functionality from the others.starlark: The main library containing the evaluator, standard library, and debugger support. Use this to embed Starlark in your environment.starlark_derive: A proc-macro crate for Starlark macros (dependency ofstarlark; do not use directly).starlark_map: Provides memory-efficient ordered/unordered maps, sets, and other data structures.starlark_syntax: Provides the AST and parsing functions. Use this only if you need to manipulate the AST directly.starlark_lsp: Provides Language Server Protocol (LSP) support.starlark_bin: A binary providing a CLI for interactive evaluation, linting, and IDE features for vanilla Starlark.
Overview of DICE computation engine
mainDICE is a dynamic incremental computation engine designed for parallel computation. It serves as the core engine powering incremental graph transformations in Buck2. It is inspired by Adapton and Salsa and is intended to provide a generic computation API for any type of incremental computation. All computations in DICE are executed in parallel usingtokioexecutors, and duplicate requests to the same computations are automatically deduplicated.Overview of Buck2 Extension Language (BXL)
mainBXL is a Starlark-based scripting language that allows integrators to inspect and interact with the Buck2 graph. It provides a safe, controlled way to query, analyze, and build on top of Buck2 structures natively via Starlark.
Key capabilities include:
- Graph Introspection: Inspect the graph at unconfigured, configured, providers, and action stages.
- Incremental Execution: Leverages Buck2 core's incremental caching.
- Advanced Operations: Supports running actions, dynamic outputs, and anonymous targets.
- Extensibility: Allows custom command line arguments, output artifacts, and telemetry within scripts.
Use Allocative for memory introspection in Rust
mainTheAllocativecrate provides a lightweight memory profiler for Rust that enables object traversal and memory size introspection. To use it, you must implement theAllocativetrait for the types you wish to measure (typically via a proc-macro). Once implemented, you can traverseAllocativevalues to collect their size and the sizes of all referenced objects.Understand the Buck daemon (buckd) architecture
mainBuck uses a persistent daemon process called
buckdto reuse work between commands. When you execute mostbuckcommands, the process running the command acts as a client that communicates with thebuckdserver via a gRPC service.Note that a small subset of commands do not require the daemon and will work without it, such as:
buck help- CLI argument parsing failures
buck version
Understand the distinction between Labels and Nodes in Buck2
mainIn Buck2, Labels and Nodes serve two distinct roles in the build graph:
- Labels: Act as unique identifiers or 'addresses' used to reference targets. They tell Buck2 which target you are referring to (e.g.,
//buck2:buck2). - Nodes: Contain the actual data and information about the target, such as its attributes, dependencies, and what the target actually is.
- Labels: Act as unique identifiers or 'addresses' used to reference targets. They tell Buck2 which target you are referring to (e.g.,