blob: 3fe41155319fe8550e934ed689bf2fa5668f0214 [file] [log] [blame]
# Non-predefined variables must be escaped with "$$" (e.g. "$$FOO"); see
# https://bazel.build/reference/be/make-variables#predefined_label_variables.
_SCRIPT_TEMPLATE = """
# Find the directories where each of the four binaries are located.
PROTOC_PATH=$$(realpath $$(dirname $(rootpath {protoc_label})))
PROTOC_GEN_GO_GRPC_GATEWAY_PATH=$$(realpath $$(dirname \\
$(rootpath @com_github_grpc_ecosystem_grpc_gateway_v2//protoc-gen-grpc-gateway)))
PROTOC_GEN_GO_GRPC_PATH=$$(realpath $$(dirname \\
$(rootpath @org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc)))
PROTOC_GEN_GO_PATH=$$(realpath $$(dirname \\
$(rootpath @org_golang_google_protobuf//cmd/protoc-gen-go)))
PROTOC_GEN_TWIRP_PATH=$$(realpath $$(dirname \\
$(rootpath @com_github_twitchtv_twirp//protoc-gen-twirp)))
PROTOC_GEN_TWIRP_TYPESCRIPT_PATH=$$(realpath $$(dirname \\
$(rootpath @com_github_skia_dev_protoc_gen_twirp_typescript//:protoc-gen-twirp_typescript)))
# Add binaries to PATH.
export PATH=$$PROTOC_PATH:$$PATH
export PATH=$$PROTOC_GEN_GO_GRPC_GATEWAY_PATH:$$PATH
export PATH=$$PROTOC_GEN_GO_GRPC_PATH:$$PATH
export PATH=$$PROTOC_GEN_GO_PATH:$$PATH
export PATH=$$PROTOC_GEN_TWIRP_PATH:$$PATH
export PATH=$$PROTOC_GEN_TWIRP_TYPESCRIPT_PATH:$$PATH
# Change into the directory where Bazel was invoked.
cd $$BUILD_WORKING_DIRECTORY
protoc $$@
"""
[
genrule(
name = "gen_script_" + script_suffix,
outs = ["protoc_%s.sh" % script_suffix],
cmd = "echo '%s' > $@" % _SCRIPT_TEMPLATE.format(
protoc_label = protoc_label,
),
exec_tools = [
protoc_label,
"@com_github_grpc_ecosystem_grpc_gateway_v2//protoc-gen-grpc-gateway",
"@com_github_twitchtv_twirp//protoc-gen-twirp",
"@com_github_skia_dev_protoc_gen_twirp_typescript//:protoc-gen-twirp_typescript",
"@org_golang_google_protobuf//cmd/protoc-gen-go",
"@org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc",
],
)
for script_suffix, protoc_label in [
[
"linux_x64",
"@protoc_linux_x64//:bin/protoc",
],
[
"mac_x64",
"@protoc_mac_x64//:bin/protoc",
],
]
]
# Wrapper script around the "protoc" binary.
#
# The "protoc" binary requires plugins (that is, binaries named "protoc-gen-<TARGET LANGUAGE>") to
# be in PATH. This script adds all protoc plugin binaries used in this repository to PATH, then
# forwards all command-line arguments to the "protoc" binary.
#
# Here is the list of protoc plugin binaries used in this repository:
#
# - protoc-gen-go
# - protoc-gen-go-grpc
# - protoc-gen-twirp
# - protoc-gen-twirp_typescript
#
# Reference: https://bazel.build/reference/be/shell#sh_binary.
sh_binary(
name = "protoc",
srcs = select({
"//bazel/constraints:linux_x64": ["protoc_linux_x64.sh"],
"//bazel/constraints:mac_x64": ["protoc_mac_x64.sh"],
"//bazel/constraints:mac_arm64": ["protoc_mac_x64.sh"],
}),
data = select({
"//bazel/constraints:linux_x64": ["@protoc_linux_x64//:bin/protoc"],
"//bazel/constraints:mac_x64": ["@protoc_mac_x64//:bin/protoc"],
# The M1 Mac can run the x64 version of the binary in emulation mode.
"//bazel/constraints:mac_arm64": ["@protoc_mac_x64//:bin/protoc"],
}) +
# These Go binaries will be compiled for the correct execution platform.
[
"@com_github_grpc_ecosystem_grpc_gateway_v2//protoc-gen-grpc-gateway",
"@com_github_skia_dev_protoc_gen_twirp_typescript//:protoc-gen-twirp_typescript",
"@com_github_twitchtv_twirp//protoc-gen-twirp",
"@googleapis//google/api:annotations_proto",
"@org_golang_google_protobuf//cmd/protoc-gen-go",
"@org_golang_google_grpc_cmd_protoc_gen_go_grpc//:protoc-gen-go-grpc",
],
visibility = ["//visibility:public"],
)