Skip to content

Instantly share code, notes, and snippets.

@suztomo
Last active June 9, 2023 15:09
Show Gist options
  • Save suztomo/1207d16d7678faa14ecc4a9194786edd to your computer and use it in GitHub Desktop.
Save suztomo/1207d16d7678faa14ecc4a9194786edd to your computer and use it in GitHub Desktop.

Normally, protoc compiler itself is compiled from source code when we use proto_library. However, we can speed up the build by using prebuilt protoc compiler.

Declare a repository for prebuilt protoc compiler in WORKSPACE:

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

http_archive(
  name = "com_google_protobuf_protoc_linux_x86_64",
  build_file_content = 'exports_files(["bin/protoc"])',
  sha256 = "179a759581bf4b32cc5edae4ffce6b8ee16ba4f4ab99ad3a309c31113f98d472",
  urls = ["https://github.com/protocolbuffers/protobuf/releases/download/v23.2/protoc-23.2-linux-x86_64.zip"],
)

where the zip file link is obtained from GitHub's Protobuf 23.2 release page "Asset" section: https://github.com/protocolbuffers/protobuf/releases/tag/v23.2.

Then you can run normal build command with --proto_compiler=@com_google_protobuf_protoc_linux_x86_64//:bin/protoc:

suztomo@suztomo:~/work_proto_lib$ bazelisk build --proto_compiler=@com_google_protobuf_protoc_linux_x86_64//:bin/protoc //google/type:postal_address_proto
INFO: Analyzed target //google/type:postal_address_proto (5 packages loaded, 8 targets configured).
INFO: Found 1 target...
Target //google/type:postal_address_proto up-to-date:
  bazel-bin/google/type/postal_address_proto-descriptor-set.proto.bin
INFO: Elapsed time: 0.379s, Critical Path: 0.02s

This skips around 50 seconds of protoc compilation time in my Linux 16 vCPU machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment