Closed5

Bazelで遊んでみる

showheyshowhey

https://christina04.hatenablog.com/entry/using-bazel-to-build-protobuf
を元に,Bazelでprotobufのビルドを行ってみる

まずはbazelinkをインストールする
https://www.flywheel.jp/topics/bazel_version_1_1_0/

理由としては

ただ、私はBazelを直接インストールするよりもBazeliskをインストールしたほうが良いと考えています。BazeliskはBazelのラッパーで、プロジェクトごとに適切なバージョンのBazel使い分ける事ができます(Rubyのrbenvや、Pythonのpyenvに似ています)。例えば、TensorFlowはBazel 1.1.0、KubernetesはBazel 0.23.2を使用しており、各プロジェクトごとに対応するバージョンのBazelをインストールするのは面倒です。Bazeliskは設定ファイル( .bazelversion )や環境変数( USE_BAZEL_VERSION )などから、各プロジェクトごとに適切なバージョンのBazelをダウンロードし、使用してくれます。

らしい

showheyshowhey

WORKSPACEに追加したコードが動かない

http_archive(
    name = "com_google_protobuf",
    sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
    strip_prefix = "protobuf-3.14.0",
    urls = [
        "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
        "https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
    ],
)

load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
showheyshowhey

bazel run //protoを実行したところ以下のエラーに遭遇

ERROR: no such package '@@com_google_protobuf//': The repository '@@com_google_protobuf' could not be resolved: Repository '@@com_google_protobuf' is not defined
ERROR: /private/var/tmp/_bazel_showhey/6f3471cc6869c958d8c7dd4063add40d/external/io_bazel_rules_go/proto/BUILD.bazel:127:20: no such package '@@com_google_protobuf//': The repository '@@com_google_protobuf' could not be resolved: Repository '@@com_google_protobuf' is not defined and referenced by '@@io_bazel_rules_go//proto:protoc'
ERROR: Analysis of target '//proto:proto' failed; build aborted: Analysis failed

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

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "7c76d6236b28ff695aa28cf35f95de317a9472fd1fb14ac797c9bf684f09b37c",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.44.2/rules_go-v0.44.2.zip",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.44.2/rules_go-v0.44.2.zip",
    ],
)

http_archive(
    name = "bazel_gazelle",
    sha256 = "32938bda16e6700063035479063d9d24c60eda8d79fd4739563f50d331cb3209",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.35.0/bazel-gazelle-v0.35.0.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.35.0/bazel-gazelle-v0.35.0.tar.gz",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

http_archive(
    name = "com_google_protobuf",
    sha256 = "930c2c3b5ecc6c9c12615cf5ad93f1cd6e12d0aba862b572e076259970ac3a53",
    strip_prefix = "protobuf-3.21.12",
    urls = [
        "https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.21.12.tar.gz",
        "https://github.com/protocolbuffers/protobuf/archive/v3.21.12.tar.gz",
    ],
)



go_rules_dependencies()

go_register_toolchains(version = "1.20.10")

gazelle_dependencies()


load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

protobuf_deps()

############################################################
# Define your own dependencies here using go_repository.
# Else, dependencies declared by rules_go/gazelle will be used.
# The first declaration of an external repository "wins".
############################################################

でいけた

このスクラップは2024/07/05にクローズされました