Skip to content

nakamasato/bazel-training

Repository files navigation

Bazel Training

Prerequisite

bazel installed

bazel --version
bazel 6.0.0

Basics

  1. bazel concept and terminology

    1. Workspace: a directory on your filesystem that contains the source files for the software you want to build. Each workspace directory has a text file named WORKSPACE.
    2. Repository: The directory containing the WORKSPACE file is the root of the main repository, also called @.
    3. Package: The primary unit of code organization in a repository. A package is defined as a directory containing a file named BUILD or BUILD.bazel.
    4. Target: A package is a container. The elements of a package are called targets.
    5. Labels:
      • @myrepo//my/app/main:app_binary = //my/app/main:app_binary inside @myrepo
      • my/app/main: un-qualified package name
      • @myrepo//my/app/main: full-qualified package name
      • app_binary or :app_binary inside @myrepo//my/app/main
  2. Repository rules (WORKSPACE):

    1. Git git_repository
    2. Http http_archive
    3. util maybe

Contents

  1. Build Go with Gazelle (v0.1.0)
  2. Build Protobuf (v0.2.0)
  3. Build Python (v0.3.0)
  4. Build Java (v0.4.0)
  5. Build Python with Poetry (v0.5.0)
  6. Build Python with Proto (v0.6.0)

Cheatsheet

  1. Bazel
    1. bazel clean: clean up the cache.
  2. Java:
    1. build: bazel build //java:App
    2. run: bazel run //java:App
    3. check dep: bazel query --notool_deps --noimplicit_deps "deps(//java:App)" --output graph
  3. Python: bazel run //python:main
  4. Go:
    1. build: bazel build //go/cmd: Build a package cmd.
    2. run: bazel run //go/cmd: Run a package cmd.
    3. bazel run //:gazelle: Generate build file.
    4. bazel run //:gazelle -- update-repos -from_file=go.mod: Update go_repository in WORKSPACE from go.mod.

FAQ

  1. How to upgrade http_archive version?

References