Buck2 Documentation

repository·main·Indexed 26 days ago

https://github.com/facebook/buck2

Buck2 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.

Tokens
137.6K
Snippets
341
Records
871
Agent score
88%

What's inside Buck2

  1. Overview of Buck2 Android Prelude

    main
    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.
  2. Overview of Buck2 build system

    main
    Buck2 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.
  3. Overview of hmaptool

    main
    hmaptool 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.
  4. Overview of Distributed ThinLTO in Buck2

    main

    Buck2 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 parallel opt actions 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:

    1. thin_lto_prepare: (Implicitly part of the setup/compilation phase where bitcode is emitted).
    2. 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.
    3. thin_lto_opt: A parallelizable phase where the optimizer (e.g., LLVM opt) combines objects identified during the index step. In Buck2, these actions can be distributed remotely.
    4. thin_lto_link: The final step that takes the optimized outputs and links them into a final binary using a standard linker.
  5. Overview of Buck2 build system

    main

    Buck2 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.
  6. Overview of Starlark in Rust components

    main

    The project is divided into several crates. For most developers embedding Starlark, the starlark crate 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 of starlark; 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.
  7. Overview of DICE computation engine

    main
    DICE 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 using tokio executors, and duplicate requests to the same computations are automatically deduplicated.
  8. Overview of Buck2 Extension Language (BXL)

    main

    BXL 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.
  9. Use Allocative for memory introspection in Rust

    main
    The Allocative crate provides a lightweight memory profiler for Rust that enables object traversal and memory size introspection. To use it, you must implement the Allocative trait for the types you wish to measure (typically via a proc-macro). Once implemented, you can traverse Allocative values to collect their size and the sizes of all referenced objects.
  10. Understand the Buck daemon (buckd) architecture

    main

    Buck uses a persistent daemon process called buckd to reuse work between commands. When you execute most buck commands, the process running the command acts as a client that communicates with the buckd server 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
  11. Understand the distinction between Labels and Nodes in Buck2

    main

    In 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.