### InstallInfo Provider Example Source: https://buck2.build/docs/users/commands/install This example shows how the `InstallInfo` provider is structured, specifying the installer and the files to be installed. It is typically viewed using `buck2 audit providers`. ```python InstallInfo( installer = buck//src/com/facebook/buck/installer/apple:apple_installer, files = { "app_bundle;": , "options": } ) ``` -------------------------------- ### Example Android Instrumentation Test Setup Source: https://buck2.build/docs/prelude/rules/android/android_instrumentation_test This example demonstrates how to set up an android_binary, an android_instrumentation_apk, and an android_instrumentation_test rule. The instrumentation test rule depends on the instrumentation APK, which in turn depends on the main application binary. ```buck android_binary( name = 'messenger', manifest = 'AndroidManifest.xml', keystore = '//keystores:prod', package_type = 'release', proguard_config = 'proguard.cfg', deps = [ ... ], ) android_instrumentation_apk( name = 'messenger_test', manifest = 'AndroidInstrumentationManifest.xml', apk = ':messenger', deps = [ ... ], ) android_instrumentation_test( name = 'messenger_instrumentation_test', apk = ':messenger_test', ) ``` -------------------------------- ### Example JSON Output of Setup Command Source: https://buck2.build/docs/rule_authors/local_resources This JSON structure represents a pool of local resources ready for use. It includes an optional process ID (pid) and a list of available resource instances, each with a string alias mapped to its value. ```json { "pid": 42, "resources": [{"socket_address": "foo:1"}, {"socket_address": "bar:2"}] } ``` -------------------------------- ### Buck2 Install Command Usage Source: https://buck2.build/docs/users/commands/install This is the general usage syntax for the `buck2 install` command. It takes target(s) to build and install, with optional common options and additional arguments passed directly to the installer. ```bash buck2 install [OPTIONS] [TARGET]... [-- ...] ``` -------------------------------- ### Buckconfig File Example Source: https://buck2.build/docs/concepts/configurations Example of how to set a configuration value in a .buckconfig file to match a config_setting. ```ini [build] fastmode = true ``` -------------------------------- ### Install Buck2 from Source using Rustup Source: https://buck2.build/docs/getting_started/install Installs the latest nightly Rust toolchain and then installs Buck2 directly from its GitHub repository. Ensure your $PATH includes the Cargo bin directory. ```bash rustup install nightly-2026-02-28 ``` ```bash cargo +nightly-2026-02-28 install --git https://github.com/facebook/buck2.git buck2 ``` -------------------------------- ### Fish Shell Completion Setup Source: https://buck2.build/docs/users/commands/completion Run this command to enable fish shell completion for Buck2. This is a one-time setup. ```fish source (buck2 completion fish | psub) ``` -------------------------------- ### Simulator Resource Output JSON Source: https://buck2.build/docs/rule_authors/local_resources Example JSON output produced by a broker's setup command, detailing the available resources and their socket addresses. ```json { "pid": 42, "resources": [{"socket_address": "foo:1"}, {"socket_address": "bar:2"}] } ``` -------------------------------- ### Configure System Go Toolchains Source: https://buck2.build/docs/users/languages/go/toolchains Example configuration for system Go toolchains, including both the regular toolchain and the bootstrap toolchain. These are not recommended for production but can be used for examples and simple projects. ```bzl load("@prelude///toolchains/go:system_go_bootstrap_toolchain.bzl", "system_go_bootstrap_toolchain") load("@prelude///toolchains/go:system_go_toolchain.bzl", "system_go_toolchain") system_go_toolchain( name = "go", visibility = ["PUBLIC"], ) system_go_bootstrap_toolchain( name = "go_bootstrap", visibility = ["PUBLIC"], ) ``` -------------------------------- ### Example Usage of Kotlin Library with Plugins and Dependencies Source: https://buck2.build/docs/prelude/rules/kotlin/kotlin_library This example demonstrates how to define a kotlin_library, including source files, dependencies, and the integration of a specific compiler plugin like kotlinx.serialization. ```buck kotlin_library( name = ":example", srcs = glob(["*.kt"]), deps = [ # ... other dependencies including kotlinx-serialization-runtime.jar ], kotlin_compiler_plugins = { "path/to/kotlinx-serialization-compiler-plugin.jar": {}, }, ) ``` -------------------------------- ### Run Lazy Build Artifact Example Source: https://buck2.build/docs/bxl/how_tos/how_to_catch_building_artifacts_errors Execute the example BXL script for lazy artifact building using the buck2 command line. ```bash buck2 bxl lazy_build_artifact.bxl:build_artifact ``` -------------------------------- ### Buck Build File Example for Silly Binary Source: https://buck2.build/docs/rule_authors/anon_targets This is a Buck build file example demonstrating the usage of a 'silly_binary' rule, likely for a compile-and-link language scenario. ```buck ## BUCK ############## @load(":silly.bzl", "silly_binary") silly_binary( name = "hello", srcs = ["hello.sil", "world.sil"], ) ``` -------------------------------- ### Invoke BXL with Example Arguments Source: https://buck2.build/docs/bxl/how_tos/basic_how_tos Demonstrates invoking the example BXL function with specific values for its defined CLI arguments. ```bash buck2 bxl //myscript.bxl:example -- --bool_arg true --list-type 1 --list-type 2 --target //foo:bar ``` -------------------------------- ### Example: Defining an ocaml_library Source: https://buck2.build/docs/prelude/rules/ocaml/ocaml_library A basic example of how to define an ocaml_library target, specifying its name, source files, and dependencies. ```python ocaml_library( name='greeting', srcs=[ 'greeting.ml', ], deps=[ ':join', ], ) ``` -------------------------------- ### Example Usage of kotlin_library with Plugins and Dependencies Source: https://buck2.build/docs/prelude/rules/android/android_library An example demonstrating how to use the kotlin_library rule with source files, dependencies, and Kotlin compiler plugins. ```python kotlin_library( name = ":example", srcs = glob(["*.kt"]), deps = [ ``` -------------------------------- ### Bash Shell Completion Setup Source: https://buck2.build/docs/users/commands/completion Run this command to enable bash shell completion for Buck2. This is a one-time setup. ```bash source <(buck2 completion bash) ``` -------------------------------- ### Go Embed Sources Example (Dictionary) Source: https://buck2.build/docs/prelude/rules/go/go_test Example of embedding source files using a dictionary for custom naming. This allows for stable naming of generated targets. ```go embed_srcs = {"config.json": ":generated_config", "index.html": "templates/index.html"} ``` -------------------------------- ### Example Deps Query for Cxx Binary Source: https://buck2.build/docs/prelude/rules/cxx/cxx_binary Demonstrates how to use the experimental deps_query attribute to dynamically append dependencies. This example filters dependencies by a name regex. ```python "filter({name_regex}, deps('//foo:foo'))".format(name_regex='//.*') ``` -------------------------------- ### Example Usage of erlang_otp_binaries Source: https://buck2.build/docs/prelude/rules/erlang/erlang_otp_binaries Provides a basic example of how to use the erlang_otp_binaries rule, specifying the required binary paths. This is used to define a local Erlang toolchain. ```python erlang_otp_binaries( name = "local", erl = "local/erl", erlc = "local/erlc", escript = "local/escript", ) ``` -------------------------------- ### Contacts Parameter Example Source: https://buck2.build/docs/prelude/rules/core/filegroup An example demonstrating how to specify organizational contacts for a build rule. This helps in identifying responsible parties for rule maintenance or issues. ```buck contacts = [ 'Joe Sixpack', 'Erika Mustermann' ] ``` -------------------------------- ### Run Example BXL Script Source: https://buck2.build/docs/bxl/how_tos/how_to_cache_and_share_operations Execute an example BXL script that demonstrates the usage of anonymous targets. This command is run from a specific directory within the buck2 repository. ```bash buck2 bxl anon_bxl.bxl:eval_anon_bxl ``` -------------------------------- ### Run Lazy Build Artifact Failure Example Source: https://buck2.build/docs/bxl/how_tos/how_to_catch_building_artifacts_errors Execute the example BXL script that demonstrates a failure case for lazy artifact building using the buck2 command line. ```bash buck2 bxl lazy_build_artifact.bxl:build_artifact_fail ``` -------------------------------- ### LocalResourceInfo.setup Command Example Source: https://buck2.build/docs/api/build/LocalResourceInfo Illustrates the command to initialize a local resource and the expected JSON output format, including process ID and resource pool details. ```json { "pid": 42, "resources": [ {"socket_address": "foo:1"}, {"socket_address": "bar:2"} ] } ``` -------------------------------- ### Creating a Zip File Source: https://buck2.build/docs/prelude/rules/core/zip_file This example demonstrates how to create a zip file named 'example.zip'. It includes files from a glob pattern, a build target, and another zip file. It also shows how to exclude specific entries from the archive. ```buck zip_file( # The output will be "example.zip" name = 'example', srcs = # These files will be found in the zip under "dir/" glob(['dir/**/*']) + [ # Imagine this generates the output # "buck-out/gen/foo/hello.txt". This output will # be found in the zip at "hello.txt" '//some/other:target', ], zip_srcs = [ # The contents of this zip will be added to the generated zip. 'amazing-library-1.0-sources.zip', ], entries_to_exclude = [ "com/example/amazinglibrary/Source1.java", ], ) ``` -------------------------------- ### Get Artifact Basename Source: https://buck2.build/docs/api/build/Artifact Retrieves the base name of an artifact. For example, for an artifact at 'foo/bar', this returns 'bar'. ```python Artifact.basename: str ``` -------------------------------- ### Get Artifact Extension Source: https://buck2.build/docs/api/build/Artifact Retrieves the file extension of an artifact. For example, for 'foo/bar.sh', this is '.sh'. Returns an empty string if no extension is present. ```python Artifact.extension: str ``` -------------------------------- ### Example of prebuilt_cxx_library_group Source: https://buck2.build/docs/prelude/rules/cxx/prebuilt_cxx_library_group Demonstrates how to define a prebuilt_cxx_library_group rule, wrapping two libraries that must be linked together. This example shows configurations for static linking, static PIC linking, shared linking, and provided shared libraries. ```buck prebuilt_cxx_library_group( name = 'util', static_link = [ '-Wl,--start-group', '$(lib 0)', '$(lib 1)', '-Wl,--end-group', ], static_libs = [ 'lib/liba.a', 'lib/libb.a', ], static_pic_link = [ '-Wl,--start-group', '$(lib 0)', '$(lib 1)', '-Wl,--end-group', ], static_libs = [ 'lib/liba_pic.a', 'lib/libb_pic.a', ], shared_link = [ '$(rel-lib liba.so)', '$(rel-lib libb.so)', ], shared_libs = { 'liba.so': 'lib/liba.so', }, provided_shared_libs = { 'libb.so': 'lib/libb.so', }, ) ``` -------------------------------- ### LocalResourceInfo.setup Source: https://buck2.build/docs/api/build/LocalResourceInfo The command to execute for initializing a local resource. Running this command outputs JSON describing a pool of available local resources. ```APIDOC ## LocalResourceInfo.setup ### Description Command to run to initialize a local resource. Running this command writes a JSON to stdout. This JSON represents a pool of local resources which are ready to be used. ### Example JSON Output ```json { "pid": 42, "resources": [ {"socket_address": "foo:1"}, {"socket_address": "bar:2"} ] } ``` Where `"pid"` maps to a PID of a process which should be sent SIGTERM to release the pool of resources when they are no longer needed. `"resources"` maps to the pool of resources. When a local resource from this particular pool is needed for an execution command, a single entity will be reserved from the pool, for example `{"socket_address": "bar:2"}` and an environment variable with the name resolved using the mapping in `resource_env_vars` field and `"socket_address"` key will be added to the execution command. ### Type cmd_args ``` -------------------------------- ### Define a C/C++ Test Rule Source: https://buck2.build/docs/prelude/rules/cxx/cxx_test Use the cxx_test rule to build and execute C/C++ tests. This example demonstrates a basic setup for a gtest-based test. ```buck cxx_test( name = 'echo_test', srcs = [ 'echo_test.cpp', ], ) ``` -------------------------------- ### Get a specific attribute Source: https://buck2.build/docs/api/bxl/ConfiguredTargetNode Retrieves a specific attribute by key. Returns the default value if unset, or None if not defined. Does not return special attributes (starting with 'buck.'). ```python def ConfiguredTargetNode.get_attr( key: str, /, ) ``` ```python def _impl_attributes(ctx): target_node = ctx.uquery().eval("//foo:bar")[0] ctx.output.print(target_node.get_attr('my_attr')) ``` -------------------------------- ### Run the binary with dependency Source: https://buck2.build/docs/getting_started/tutorial_adding_dependencies Build and run the main binary using the `buck2 run` command, which will now include the `greeter_lib` dependency. ```bash buck2 run root//buck2_lab/greeter_bin:main ``` -------------------------------- ### Getting a Specific Attribute Source: https://buck2.build/docs/api/bxl/UnconfiguredTargetNode Retrieves a specific attribute by its key. Returns the default value if unset, or `None` if not defined by the rule. Does not return special attributes (those starting with 'buck.'). ```python def _impl_attributes(ctx): target_node = ctx.uquery().eval("//foo:bar")[0] ctx.output.print(target_node.get_attr('my_attr')) ``` -------------------------------- ### Example Usage of prebuilt_apple_framework Source: https://buck2.build/docs/prelude/rules/apple/prebuilt_apple_framework Demonstrates how to declare a prebuilt Apple framework. Specify the framework's name, its path, linkage preference, and visibility. ```python prebuilt_apple_framework( name = 'MyPrebuiltFramework', framework = 'myPrebuiltFramework.framework', preferred_linkage = 'static', visibility = [ 'PUBLIC' ] ) ``` -------------------------------- ### Get the index of an element in a list Source: https://buck2.build/docs/api/starlark/list Finds the first occurrence of a specified element within a list and returns its index. Optional start and end parameters can restrict the search range. Fails if the element is not found or if start/end indices are invalid. ```starlark x = ["b", "a", "n", "a", "n", "a"] x.index("a") == 1 # bAnana x.index("a", 2) == 3 # banAna x.index("a", -2) == 5 # bananA ``` -------------------------------- ### Example Usage Source: https://buck2.build/docs/prelude/rules/android/prebuilt_native_library Demonstrates how to use the prebuilt_native_library rule in conjunction with an android_library rule. ```APIDOC ## Example Most of the time, a `prebuilt_native_library` is private to the `android_library()` that uses it: ```python prebuilt_native_library( name = 'native_libs', native_libs = 'libs', ) android_library( name = 'my_lib', srcs = glob(['*.java']), deps = [ ':native_libs', ], ) ``` ``` -------------------------------- ### String startswith with prefix, start, and end Source: https://buck2.build/docs/api/starlark/str Tests if a string starts with a specified prefix within an optional start and end slice. Can also check against a tuple of prefixes. ```python "filename.sky".startswith("filename") == True ``` ```python "filename.sky".startswith("sky") == False ``` ```python 'abc'.startswith(('a', 'A')) == True ``` ```python 'ABC'.startswith(('a', 'A')) == True ``` ```python 'def'.startswith(('a', 'A')) == False ``` ```python "//foo".startswith("//", 0, 2) == True ``` ```python "//foo".startswith("foo", 2) == True ``` ```python "hello".startswith("ell", 1, 4) == True ``` ```python "hello".startswith("ell", 2, 4) == False ``` -------------------------------- ### Example Haskell Prebuilt Library Configuration Source: https://buck2.build/docs/prelude/rules/haskell/haskell_prebuilt_library Demonstrates how to configure a haskell_prebuilt_library rule, specifying static and shared interfaces, static libraries, and shared libraries. This is useful for integrating precompiled Haskell code into a Buck project. ```python prebuilt_haskell_library( name = 'file', static_interfaces = [ 'interfaces', ], shared_interfaces = [ 'interfaces_dyn', ], static_libs = [ 'libFileUtil.a', ], shared_libs = { 'libFileUtil.so': 'libFileUtil.so', }, ) ``` -------------------------------- ### Cxx Genrule Example Source: https://buck2.build/docs/prelude/rules/cxx/cxx_genrule An example of a cxx_genrule that creates a file and copies it to the output directory. ```python cxx_genrule( name = 'cxx_gen', type = 'epilog', cmd = 'touch finish.txt; cp finish.txt $OUT', out = 'finish.txt' ) ``` -------------------------------- ### Install dotslash with Cargo Source: https://buck2.build/docs/about/bootstrapping Install the `dotslash` tool using Cargo. This is a prerequisite for the bootstrapping process. ```bash cargo install --locked dotslash ``` -------------------------------- ### Initialize Buck2 Project and Create Directories Source: https://buck2.build/docs/getting_started/tutorial_first_build Initializes a new Buck2 project and sets up the necessary directory structure for a Rust binary application. ```bash buck2 init hello_world mkdir hello_world/buck2_lab cd hello_world/buck2_lab ``` ```bash mkdir greeter_bin ``` ```bash mkdir greeter_bin/src ``` -------------------------------- ### Example: Running test_suite() Targets Source: https://buck2.build/docs/prelude/rules/core/test_suite This shows the command-line execution of Buck to test the defined test_suite() targets and the expected output format for passing tests. ```bash $ buck test //:slow_tests ... RESULTS FOR //instrumentation_tests:instrumentation_tests //integration_tests:integration_tests PASS <100ms 1 Passed 0 Skipped 0 Failed //instrumentation_tests:instrumentation_tests PASS <100ms 1 Passed 0 Skipped 0 Failed //integration_tests:integration_tests TESTS PASSED ... $ buck test //:all_tests RESULTS FOR //instrumentation_tests:instrumentation_tests //integration_tests:integration_tests //unit_tests:unit_tests PASS <100ms 1 Passed 0 Skipped 0 Failed //instrumentation_tests:instrumentation_tests PASS <100ms 1 Passed 0 Skipped 0 Failed //integration_tests:integration_tests PASS <100ms 1 Passed 0 Skipped 0 Failed //unit_tests:unit_tests TESTS PASSED ``` -------------------------------- ### llvm_link_bitcode Example Source: https://buck2.build/docs/prelude/rules/cxx/llvm_link_bitcode Example of using llvm_link_bitcode to combine two LLVM bitcode object files. ```python llvm_link_bitcode( name = 'echo_test', srcs = [ 'echo_test.o', 'echo_other.o', ], ) ``` -------------------------------- ### Select Refinement Example Source: https://buck2.build/docs/api/build/Select Illustrates how Buck2 uses specificity to resolve select statements when multiple configurations match. Order in the dictionary does not matter; specificity always wins. ```starlark config_setting( name = "linux", constraint_values = ["//constraints:linux"], # 1 constraint ) config_setting( name = "linux-arm64", constraint_values = [ "//constraints:linux", "//constraints:arm64", # 2 constraints - more specific ], ) my_rule( srcs = select({ ":linux": ["generic_linux.cpp"], ":linux-arm64": ["optimized_arm64.cpp"], # This wins when both match }), ) ``` -------------------------------- ### Basic android_manifest() Example Source: https://buck2.build/docs/prelude/rules/android/android_manifest An example of an android_manifest() rule with no dependencies, specifying only the name and the skeleton manifest file. ```python android_manifest( name = 'my-manifest', skeleton = 'AndroidManifestSkeleton.xml', ) ``` -------------------------------- ### Example Materialized Output Path Source: https://buck2.build/docs/bxl/tutorial This is an example of a materialized output path for an index file generated by BXL. ```text buck-out/v2/gen-bxl/root/78ceb8c295d0ab4e/part3.bxl/__main__e0c0381aecee358a__/index.txt ``` -------------------------------- ### Build CXX Binary with Buck2 Source: https://buck2.build/docs/prelude/rules/cxx/cxx_binary Shows how to build the defined 'echo' C/C++ executable using the Buck2 build tool. Includes commands for building with and without stripping debug symbols. ```bash # To build without stripping: buck build :echo # To build with stripping debug symbols only: buck build :echo#strip-debug ``` -------------------------------- ### Example Deprecated Query Source: https://buck2.build/docs/prelude/rules/cxx/llvm_link_bitcode Illustrates example queries for the experimental 'deps_query' parameter, showing how to filter and combine dependencies. ```python "filter({name_regex}, deps('//foo:foo'))".format(name_regex='//.*') ``` ```python "attrfilter(annotation_processors, com.foo.Processor, deps('//foo:foo'))" ``` ```python "deps('//foo:foo', 1)" ``` -------------------------------- ### Link Windows Resource with C++ Binary Source: https://buck2.build/docs/prelude/rules/cxx/windows_resource This example shows how to link the compiled resources from a windows_resource rule into a cxx_binary target. Ensure the resource target is listed in the 'deps' of the binary. ```buck cxx_binary( name = "app", srcs = [ "main.cpp", ], deps = [ ":resources" ], ) ``` -------------------------------- ### filegroup Example Usage Source: https://buck2.build/docs/prelude/rules/core/filegroup An example demonstrating how to use the filegroup rule to export XML files and then access them with a genrule. ```APIDOC ## Example In this example a target exports `.xml` files from all subdirectories in `resources`. ```python filegroup( name = 'example', srcs = glob(['resources/**/*.xml']), ) genrule( name = 'process_xml', out = 'processed.xml', cmd = '$(exe //example:tool) -in $(location :example)/resources/file.xml > $OUT', ) ``` ``` -------------------------------- ### Run Example BXL Script Source: https://buck2.build/docs/bxl/how_tos/how_to_run_actions_based_on_the_content_of_artifact Command to execute a sample BXL script for dynamic actions from a specific directory. ```bash buck2 bxl dynamic.bxl:basic ``` -------------------------------- ### gen_aidl Example Usage Source: https://buck2.build/docs/prelude/rules/android/gen_aidl An example demonstrating how to use the gen_aidl rule in a build file, alongside an android_library rule. ```APIDOC ## Example Usage ```python android_library( name = 'lib', srcs = glob(['**/*.java']) + [':aidl'], manifest = '//res/org/opencv:manifest', deps = [ '//res/org/opencv:res', ], visibility = [ 'PUBLIC' ], ) gen_aidl( name = 'aidl', aidl = 'engine/OpenCVEngineInterface.aidl', import_path = 'java/', ) ``` ``` -------------------------------- ### Create and Execute a Test Script Source: https://buck2.build/docs/prelude/rules/shell/sh_test This example demonstrates how to create a simple shell script that responds to arguments and exits with a status code. It also shows how to make the script executable. ```bash # Create a simple script that prints out the resource $ cat > script.sh #!/bin/sh for arg in $@; do if [ "$arg" == "--pass" ]; then echo "Passed" exit 0; fi done echo "Failed" exit 1 # Make sure the script is executable $ chmod u+x script.sh ``` -------------------------------- ### Example Usage of lua_binary() and lua_library() Source: https://buck2.build/docs/prelude/rules/lua/lua_binary Demonstrates how to define a lua_binary() target and its associated lua_library() dependencies. The main_module parameter specifies the entry point for the executable. ```python lua_binary( name = 'tailer', main_module = 'tailer', deps = [ ':tailerutils', ], ) lua_library( name = 'tailerutils', srcs = glob(['*.lua']), ) ``` -------------------------------- ### Zsh Shell Completion Setup Source: https://buck2.build/docs/users/commands/completion Run this command to enable zsh shell completion for Buck2. This is a one-time setup. ```zsh source <(buck2 completion zsh) ``` -------------------------------- ### Define and Use a filegroup Source: https://buck2.build/docs/prelude/rules/core/filegroup This example shows how to define a filegroup that includes all `.xml` files from subdirectories and how to reference it in a `genrule`. The `genrule` uses `$(location)` to access a specific file within the filegroup. ```buck filegroup( name = 'example', srcs = glob(['resources/**/*.xml']), ) genrule( name = 'process_xml', out = 'processed.xml', cmd = '$(exe //example:tool) -in $(location :example)/resources/file.xml > $OUT', ) ``` -------------------------------- ### InstallInfo Source: https://buck2.build/docs/api/build A provider that can be constructed and have its fields accessed, typically returned by rules. It contains information about the installer and the files associated with an installation. ```APIDOC ## InstallInfo ### Description A provider that can be constructed and have its fields accessed. Returned by rules. Provides a number of fields that can be accessed: * `installer: Label` - field * `files: dict[str, Artifact]` - field ### Signature ```python def InstallInfo( installer: Label, files: dict[str, Artifact], ) -> InstallInfo ``` ``` -------------------------------- ### Define InstallInfo Provider Source: https://buck2.build/docs/api/build/globals A provider returned by rules to indicate installation information, including the installer label and a dictionary of files. ```python def InstallInfo( installer: Label, files: dict[str, Artifact], ) -> InstallInfo ```