見出し画像

Bazel で Hello, world! を出力してみる

はじめに

こんにちは、KF です。

今回は、Bazel というビルドツールを軽く触ってみたので、その個人的メモになります。

Bazel の概要に関しては公式の Bazel の概要を参照していただけるとなんとなくわかるかと思います。

とはいえ、自分もまだよくわかっていないということはお伝えしておきます。

Hello, world! を出力してみる

今回は、Rust で Hello, world! を出力しています。

前提

下記の環境で実行しています。

  • Ubuntu 20.04.5 LTS

  • bazel 5.3.2

  • rustc 1.65.0

ディレクトリ構成は、下記の通りです。

$ tree
.
├── BUILD
├── WORKSPACE
└── src
    └── main.rs

インストール

まずは、Bazel をインストールします。

Ubuntu 以外のインストール方法については、公式の Bazel をインストールするを確認してみてください。

(今回、僕は apt を使って Bazel をインストールしていますが、Bazeliskでのインストールを推奨しているみたいです。)

$ sudo apt install bazel

プロジェクトの作成

次に、プロジェクトを作成します。

$ mkdir bazel-hello-world
$ cd bazel-hello-world

main.rs の作成

次に、main.rs を作成します。

$ mkdir src
$ touch src/main.rs

そして、main.rs に下記を追加します。

fn main() {
    println!("Hello, world!");
}

WORKSPACE と BUILD の作成

次に、WORKSPACE と BUILD を作成します。

$ touch {WORKSPACE,BUILD}

WORKSPACE には、Rules Rust のドキュメントを参考に、下記を追加します。

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# To find additional information on this release or newer ones visit:
# https://github.com/bazelbuild/rules_rust/releases
http_archive(
    name = "rules_rust",
    sha256 = "696b01deea96a5e549f1b5ae18589e1bbd5a1d71a36a243b5cf76a9433487cf2",
    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.11.0/rules_rust-v0.11.0.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")

rules_rust_dependencies()

rust_register_toolchains()

こちらを追加することで、Bazel で Rust ルールが使用できるようになるらしい。(いやムズいて)

具体的には下記のようなことをしてるっぽいが、間違ってたらごめんなさい。

  • http_archive で、rules_rust をダウンロード。

  • rules_rust_dependencies で、rules_rust で使用する依存関係をダウンロード。

  • rust_register_toolchains で、ツールチェーンのデフォルトセットの発行。

そして、BUILD には下記を追加します。
こちらは rules_rust で定義されている rust_binary を使用して、Rust バイナリをビルドするようにしています。

load("@rules_rust//rust:defs.bzl", "rust_binary")

rust_binary(
    name = "hello_world",
    srcs = ["src/main.rs"],
)

ビルドしてみる

プロジェクトのルートディレクトリで下記を実行します。

$ bazel build //:hello_world

すると、ビルドが成功しませんでした。

いやしないんかい...

~省略~
Error in fail: Attribute `edition` is required for //:hello_world.
ERROR: /home/kf/workspace/bazel-hello-world/BUILD:3:12: Analysis of target '//:hello_world' failed
ERROR: Analysis of target '//:hello_world' failed; build aborted:
INFO: Elapsed time: 7.012s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (69 packages loaded, 446 targets configur\
ed)

エラーを見ると、edition の指定が必要とあります。
rules_rust のドキュメントを見ると、rust_binary に edition の attribute があるので、そちらを指定してみます。

BUILD ファイルを下記のように編集します。

load("@rules_rust//rust:defs.bzl", "rust_binary")

rust_binary(
    name = "hello_world",
    srcs = ["src/main.rs"],
    edition = "2021", # 追加
)

再度、ビルドしてみます。

$ bazel build //:hello_world

今度は上手くビルドが成功しました!(ホッ)

INFO: Analyzed target //:hello_world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:hello_world up-to-date:
  bazel-bin/hello_world
INFO: Elapsed time: 0.289s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

実行してみる

最後に、実行もしてみます。プロジェクトのルートディレクトリで下記を実行します。

$ bazel run //:hello_world

こちらも上手く動作し、Hello, world! が出力されました!

INFO: Analyzed target //:hello_world (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:hello_world up-to-date:
  bazel-bin/hello_world
INFO: Elapsed time: 0.251s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Hello, world!

まとめ

今回は、Bazel で Hello, world! を出力してみました。

触ってみた感想としては、ところどころ難しい部分がありつつも、慣れれば使いやすいのかなとも思いました。

docker イメージのビルドにも使えるらしいので、今度触ってみようと思います。

参考

この記事が気に入ったらサポートをしてみませんか?