Skip to content

Instantly share code, notes, and snippets.

@izzyleung

izzyleung/BUILD Secret

Created July 16, 2018 23:07
Show Gist options
  • Save izzyleung/2fe240e470ad5f7c270b29fcb972c6ca to your computer and use it in GitHub Desktop.
Save izzyleung/2fe240e470ad5f7c270b29fcb972c6ca to your computer and use it in GitHub Desktop.
[HACKS] How to directly use `py_proto_library` in Bazel.

Caution

This is a hack, there is absolutely no guaratee that this hack will work in later version of Bazel/Protobuf releases.

You have been warned.

Preperation

mkdir proto && mv proto.BUILD proto/BUILD && mv definition.proto proto

Run the test program

bazel run program

# cc_binary(
# name = "protoc",
# srcs = ["@com_google_protobuf//src/google/protobuf/compiler/main.cc"],
# linkopts = ["-lpthread", "-lm"],
# visibility = ["//visibility:public"],
# deps = ["@com_google_protobuf//:protoc_lib"],
# )
py_library(
name = "protobuf_python",
deps = ["@com_google_protobuf//:protobuf_python"],
visibility = ["//visibility:public"],
)
py_binary(
name = "program",
srcs = ["program.py"],
deps = ["//proto:py_pb_lib"],
)
syntax = "proto3";
message Request {
string name = 1;
}
load("@com_google_protobuf//:protobuf.bzl", "py_proto_library")
proto_library(
name = "pb_lib",
srcs = ["definition.proto"],
)
py_proto_library(
name = "py_pb_lib",
srcs = ["definition.proto"],
protoc = "@com_google_protobuf//:protoc",
visibility = ["//visibility:public"],
)
genrule(
name = "copy_six",
srcs = ["six-1.10.0/six.py"],
outs = ["six.py"],
cmd = "cp $< $(@)",
)
py_library(
name = "six",
srcs = ["six.py"],
srcs_version = "PY2AND3",
visibility = ["//visibility:public"],
)
http_archive(
name = "com_google_protobuf",
sha256 = "1f8b9b202e9a4e467ff0b0f25facb1642727cdf5e69092038f15b37c75b99e45",
strip_prefix = "protobuf-3.5.1",
urls = ["https://github.com/google/protobuf/archive/v3.5.1.zip"],
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "six_archive",
build_file = "@//:six.BUILD",
sha256 = "105f8d68616f8248e24bf0e9372ef04d3cc10104f1980f54d57b2ce73a5ad56a",
urls = ["https://pypi.python.org/packages/source/s/six/six-1.10.0.tar.gz#md5=34eed507548117b2ab523ab14b2f8b55"],
)
bind(
name = "six",
actual = "@six_archive//:six",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment