Bazel Examples

repository·main·Indexed 21 days ago

https://github.com/bazelbuild/examples

A collection of practical examples and tutorials for the Bazel build system. Includes demonstrations for various languages (C++, Java, Rust), platforms (Android), and web frameworks (Next.js, React, Vue), as well as detailed guides on using Bzlmod for dependency management, module extensions, and toolchain registration.

Tokens
33.5K
Snippets
131
Records
174
Agent score
75%

What's inside bazelbuild-examples

  1. Overview of Frontend development with Bazel

    main
    This directory provides various examples for building JavaScript applications using Bazel. It utilizes rules_js, which is based on the pnpm package manager. The folder is structured as a pnpm workspace, enabling monorepo support where npm packages can depend on one another within the repository.
  2. Overview of the React Webpack Bazel example

    main

    This example demonstrates how to transpile and bundle a React JS application using Bazel. The build process is split into two distinct stages:

    1. Transpilation: Handled by rules_swc, this stage converts JSX files into plain JavaScript files.
    2. Bundling: Handled by rules_webpack, this stage takes the plain JS files and bundles them into a single JS file. It also utilizes an HTML template plugin (configured within the Webpack rule) to generate an entry point for the browser.

    Note that there are two Webpack configurations present:

    • webpack.bazel.config.js: The specific configuration used by Bazel.
    • webpack.config.js: A sample configuration used to demonstrate the differences between a standard Webpack setup and a Bazel-integrated setup.
  3. Overview of Rust build examples

    main

    This package provides various examples for building Rust code using Bazel. It covers common use cases such as cross-compilation, compiler optimization, FFI (Foreign Function Interface), dependency management (Cargo workspaces, direct Bazel dependencies, and vendoring), gRPC client/server implementation, and OCI container image creation.

    Platform Support:

    • Linux and MacOS: Fully supported.
    • Windows: Not officially supported. It is recommended to use WSL (Windows Subsystem for Linux) or follow the official Bazel on Windows guide.
  4. Explore Bazel rule implementation examples

    main

    This directory provides a collection of examples for extending Bazel by writing custom rules. The examples are categorized into basic rule features and advanced scenarios to help you understand how to manipulate actions, manage dependencies, and handle outputs.

    Basic Rule Features

    Use these examples to learn the fundamentals of rule implementation:

    • Rule Creation: empty/ shows the absolute minimum required to define a rule.
    • Attributes: attributes/ demonstrates how to define and use rule attributes.
    • Actions:
      • actions.run: Using a binary target as an implicit dependency and executing it.
      • actions.write: Generating a file directly.
      • expand_template: Generating files based on a template.
      • shell command: Executing shell commands for text processing.
    • Dependency Information:
      • mandatory provider: Accessing information from a dependency via a mandatory provider.
      • optional provider: Accessing information from a dependency via an optional provider.
      • depsets: Using depset to gather transitive information from dependencies.

    Advanced Rule Scenarios

    Use these examples for more complex control over rule behavior:

    • Execution & Testing: executable/ for executable rules, test rule/ for test rules, and runfiles/ for rules requiring specific files at runtime.
    • Dependency Management: computed dependencies/ where implicit dependencies are determined by rule attributes.
    • Output Management:
      • predeclared outputs: Using attr.output_list and outputs.
      • implicit output: Handling outputs that are only available when explicitly requested.
    • Aspects: aspect/ demonstrates how to use an aspect to collect information from other rules in the dependency graph.
  5. Generate values.xml from google-services.json using tools_android

    main
    Firebase Cloud Messaging (FCM) requires app-specific information (API key, app ID, project ID, etc.) to be present in the res/values/values.xml resource file. You can use the tools_android repository to automate the generation of this values.xml file from the google-services.json file downloaded from your Firebase console.
  6. Find Bazel examples by language or platform

    main

    The repository is organized into specialized cookbooks for different technologies. You can find specific implementation patterns for:

    • Android: Jetpack Compose, Firebase Cloud Messaging, NDK, and Roboelectric tests with Kotlin.
    • Java: Using Java with Maven.
    • Rust: Building Rust projects.
    • Frontend: Next.js, React, React with Webpack, and Vue.
    • Rule & Macro Writing: Advanced patterns for extending Bazel.
  7. Understand Bazel "Make" variables

    main
    Bazel supports "Make" variables that can be used in attributes marked "Subject to 'Make Variable' substitution". These variables allow you to reference build-time information like compilation modes, directories, and file paths directly within rule attributes. Variables are categorized into predefined variables (available to all rules), genrule-specific variables, and custom variables defined via Starlark.
  8. Understand the Android NDK build graph

    main

    The Android NDK example is structured into three main layers of Bazel targets:

    1. JNI/C++ Layer: JNI and C++ source files are compiled into a cc_library target located at //app/src/main:jni_lib.
    2. Android Library Layer: Java sources, resource files, and assets are bundled into an android_library target at //app/src/main:lib. This target has a dependency on the //app/src/main:jni_lib target.
    3. Android Binary Layer: The final APK is produced by the android_binary target at //app/src/main:app, which depends on the //app/src/main:lib target.

    Note: The build graph for this example omits Google Maven AAR dependencies.