0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Bazel https://bazel.build/

Posted at

Bazel を使ったプロジェクトに出るくわした時のためのメモ。Bazel というのは Make のようなビルド用のツールらしい。

インストール

お薦めにしたがって Bazelisk を使った。Mac OS の場合

brew install bazelisk

で bazelisk と bazel コマンドの両方がインストールされる。(bazel 直接のインストールとの違いは謎)

Bazel の基本

Bazel Tutorial: Build a C++ Project を読みながら基本的な使い方をメモする。

Bazel を使ったプロジェクトには WORKSPACE と BUILD の二つのファイルがある。

  • WORKSPACE:
    • プロジェクトのルートに置く。
    • // はこのディレクトリを指す。
  • BUILD:
    • BUILD.bazel と書いても良い
    • bazel コマンドへの指示を書く。
    • WORKSPACE や BUILD など、Bazelisk のファイルは Starlark Language で書かれている。

その他の基本は Getting Started の Build concepts 以下に書かれてある。

bazel コマンドと BUILD ファイルの使い方

bazel コマンドの書式

bazel コマンド (target)

例えば以下のようなファイルが main/BUILD にあるとする。

load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
    name = "hello-world",
    srcs = ["hello-world.cc"],
)

これは、hello-world.cc から hello-world コマンドを生成するという意味。以下のコマンドでビルドと実行ができる。

bazel run //main:hello-world
  • コマンドには build, run, test, clean などがある。
  • target //main:hello-world の意味。
    • // は WORKSPACE のあるパス。
    • : の前の main は package (ディレクトリ) の名前。
    • : の後ろの hello-world は target の名前。この場合は cc_binary の name= で指定された名前。
    • 相対パス指定もできる。
      • bazel run main:hello-world
      • cd main && bazel run :hello-world
    • その他の書式は Labels 参照。
  • cc_binary は rule と呼ばれる。コマンドを作成する cc_binary のほか cc_library など色々ある。
    • Build Encyclopedia に様々な Rule の紹介がある。
    • 色々な言語の Rule はこのページの右のメニューから辿れる。
      *

BUILD ファイルの書き方

  • BUILD Style Guide に詳しく書いてある。
  • Buildifier というのを使うと整形してくれる。
    • インストール方法: go install github.com/bazelbuild/buildtools/buildifier@latest

疑問

  • 例えば bazel build を実行した時に何が行われるのかよく分からない。
  • 同じ label に対して bazel build した時と bazel run した時の違いはどう定義されているか?
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?