Skip to content

Instantly share code, notes, and snippets.

@shiponcs
Last active June 26, 2023 06:04
Show Gist options
  • Save shiponcs/e3bd91380157a15361dc3bd6cbd5e1af to your computer and use it in GitHub Desktop.
Save shiponcs/e3bd91380157a15361dc3bd6cbd5e1af to your computer and use it in GitHub Desktop.
Bazel Notes
  1. The root directory of the project is where WORKSPACE file is created.
  2. All path is relative to the root directory.
  3. BUILD is located per package.
stage3/
├── lib
│   ├── BUILD
│   ├── hello-time.cc
│   └── hello-time.h
├── main
│   ├── BUILD
│   ├── hello-greet.cc
│   ├── hello-greet.h
│   └── hello-world.cc
└── WORKSPACE

Here, stage3 is the root directory of the project. lib and main are two packages.

  1. Build a project (from the root of the project)
build //main:hello-world

//main says where the BUILD file is located and hello-world is the target defined inside that BUILD file. see //main is relative to the directory you ruinning the command from. Note: // represents the root directory(where the WORKSPACE file is located.

  1. (to be continued)

It is preferred to use bazelisk instead of using bazel directly. bazelisk is a wrapper around bazel that allows you to use different versions of bazel accross different projects.

Install Bazelisk in Ubuntu

  1. Delete/uninstall bazel if you had installed it previously:
sudo apt-get --purge remove bazel

If it doesn't work for you, sadly you have to remove it manually. You may follow this:

rm -rf ~/.cache/bazel~/.cache/bazel
rm -fr ~/.bazel ~/.bazelrc

And, also find the location of the binary file by which bazel and remove it. And, of course you can always run find / -name bazel and see what else you may need to remove.

  1. Install bazelisk in Ubuntu

The guide is here. But if you think the guide for Linux is not good for you, see mine:

  • Download the appropiate file for your system from here.
  • Open terminal at the location you downloaded the file and
      chmod +x <downloaded-file-name>
      sudo mv bazelisk-linux-amd64 /usr/local/bin/bazel
    What we did above: we gave the file execuation permission and then moved it to /usr/local/bin/ and renamed it to bazel so that you can call it as bazel directly from your terminal as, I hope, /usr/local/bin/ is already present in your PATH variable. Don't believe me. Run: bazel --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment