From 475e1d0ccf480fa60763b783e95b898bcae32d1a Mon Sep 17 00:00:00 2001 From: yancl Date: Wed, 10 Oct 2018 01:25:07 +0800 Subject: [PATCH] add golang bazel build support (#132) --- src/BUILD.bazel | 7 +++- src/WORKSPACE | 34 +++++++++++++++++++ .../proto/agent/common/v1/BUILD.bazel | 20 +++++++++++ .../proto/agent/trace/v1/BUILD.bazel | 18 ++++++++++ src/opencensus/proto/metrics/v1/BUILD.bazel | 21 ++++++++++++ src/opencensus/proto/stats/v1/BUILD.bazel | 20 +++++++++++ src/opencensus/proto/trace/v1/BUILD.bazel | 30 ++++++++++++++++ 7 files changed, 149 insertions(+), 1 deletion(-) diff --git a/src/BUILD.bazel b/src/BUILD.bazel index 36a19bf..7563dbe 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -10,4 +10,9 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and -# limitations under the License. \ No newline at end of file +# limitations under the License. + +package(default_visibility = ["//visibility:public"]) + +load("@io_bazel_rules_go//go:def.bzl", "go_prefix") +go_prefix("github.com/census-instrumentation/opencensus-proto") diff --git a/src/WORKSPACE b/src/WORKSPACE index cf668c7..4a1d24b 100644 --- a/src/WORKSPACE +++ b/src/WORKSPACE @@ -54,3 +54,37 @@ http_archive( strip_prefix = "protobuf-3.5.1", urls = ["https://github.com/google/protobuf/archive/v3.5.1.tar.gz"], ) + +# go rules related +git_repository( + name = "io_bazel_rules_go", + remote = "https://github.com/bazelbuild/rules_go.git", + # we need rules_go of tag 0.12.0 + # which includes golang protobuf that supports "paths=source_relative" + # the "paths=source_relative" option for protoc-gen-go makes bazel works as expect + # when go_package was declared in proto files + # see https://github.com/bazelbuild/rules_go/blob/release-0.12/go/private/repositories.bzl#L75 + # for the included golang protobuf version and + # see https://github.com/golang/protobuf/pull/544 for "paths=source_relative" usage + tag = "0.12.0", +) + +load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains") +go_rules_dependencies() +go_register_toolchains() + +# golang protobuf rules related +git_repository( + name = "org_pubref_rules_protobuf", + commit = "5cae42382b620aa1e347ecf30b3e92fd0d97998c", # Jun 23, 2018 + remote = "https://github.com/pubref/rules_protobuf.git", +) + +load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_repositories") +go_proto_repositories( + excludes = [ + # we have specified protobuf above + # so ignore it in case version collision + "com_google_protobuf", + ], +) diff --git a/src/opencensus/proto/agent/common/v1/BUILD.bazel b/src/opencensus/proto/agent/common/v1/BUILD.bazel index 9e89e9f..97b3580 100644 --- a/src/opencensus/proto/agent/common/v1/BUILD.bazel +++ b/src/opencensus/proto/agent/common/v1/BUILD.bazel @@ -14,6 +14,8 @@ package(default_visibility = ["//visibility:public"]) +load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library") + proto_library( name = "common_proto", srcs = ["common.proto"], @@ -21,3 +23,21 @@ proto_library( "@com_google_protobuf//:timestamp_proto", ], ) + +go_proto_library( + name = "common_proto_go", + protos = ["common.proto"], + imports = [ + "external/com_google_protobuf/src", + ], + inputs = [ + "@com_google_protobuf//:well_known_protos", + ], + pb_options = [ + # omit the go_package declared in proto files to make bazel works as expect + "paths=source_relative", + ], + deps = [ + "@com_github_golang_protobuf//ptypes/timestamp:go_default_library", + ], +) diff --git a/src/opencensus/proto/agent/trace/v1/BUILD.bazel b/src/opencensus/proto/agent/trace/v1/BUILD.bazel index 887ba6e..80ff55c 100644 --- a/src/opencensus/proto/agent/trace/v1/BUILD.bazel +++ b/src/opencensus/proto/agent/trace/v1/BUILD.bazel @@ -15,6 +15,7 @@ package(default_visibility = ["//visibility:public"]) load("@grpc_java//:java_grpc_library.bzl", "java_grpc_library") +load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library") proto_library( name = "trace_service_proto", @@ -52,3 +53,20 @@ java_grpc_library( # deps = [ # ], #) + +go_proto_library( + name = "trace_service_proto_go", + protos = ["trace_service.proto"], + imports = [ + "external/com_google_protobuf/src", + ], + proto_deps = [ + "//opencensus/proto/agent/common/v1:common_proto_go", + "//opencensus/proto/trace/v1:trace_proto_go", + "//opencensus/proto/trace/v1:trace_config_proto_go", + ], + pb_options = [ + # omit the go_package declared in proto files to make bazel works as expect + "paths=source_relative", + ], +) diff --git a/src/opencensus/proto/metrics/v1/BUILD.bazel b/src/opencensus/proto/metrics/v1/BUILD.bazel index c88983a..9cbe63e 100644 --- a/src/opencensus/proto/metrics/v1/BUILD.bazel +++ b/src/opencensus/proto/metrics/v1/BUILD.bazel @@ -14,6 +14,8 @@ package(default_visibility = ["//visibility:public"]) +load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library") + proto_library( name = "metrics_proto", srcs = ["metrics.proto"], @@ -32,3 +34,22 @@ java_proto_library( name = "metrics_proto_java", deps = [":metrics_proto"], ) + +go_proto_library( + name = "metrics_proto_go", + protos = ["metrics.proto"], + imports = [ + "external/com_google_protobuf/src", + ], + inputs = [ + "@com_google_protobuf//:well_known_protos", + ], + pb_options = [ + # omit the go_package declared in proto files to make bazel works as expect + "paths=source_relative", + ], + deps = [ + "@com_github_golang_protobuf//ptypes/timestamp:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + ], +) diff --git a/src/opencensus/proto/stats/v1/BUILD.bazel b/src/opencensus/proto/stats/v1/BUILD.bazel index d3fce7f..3f2e709 100644 --- a/src/opencensus/proto/stats/v1/BUILD.bazel +++ b/src/opencensus/proto/stats/v1/BUILD.bazel @@ -14,6 +14,8 @@ package(default_visibility = ["//visibility:public"]) +load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library") + proto_library( name = "stats_proto", srcs = ["stats.proto"], @@ -32,3 +34,21 @@ java_proto_library( name = "stats_proto_java", deps = [":stats_proto"], ) + +go_proto_library( + name = "stats_proto_go", + protos = ["stats.proto"], + imports = [ + "external/com_google_protobuf/src", + ], + inputs = [ + "@com_google_protobuf//:well_known_protos", + ], + pb_options = [ + # omit the go_package declared in proto files to make bazel works as expect + "paths=source_relative", + ], + deps = [ + "@com_github_golang_protobuf//ptypes/timestamp:go_default_library", + ], +) diff --git a/src/opencensus/proto/trace/v1/BUILD.bazel b/src/opencensus/proto/trace/v1/BUILD.bazel index c7d7259..d189452 100644 --- a/src/opencensus/proto/trace/v1/BUILD.bazel +++ b/src/opencensus/proto/trace/v1/BUILD.bazel @@ -14,6 +14,8 @@ package(default_visibility = ["//visibility:public"]) +load("@org_pubref_rules_protobuf//go:rules.bzl", "go_proto_library") + proto_library( name = "trace_proto", srcs = ["trace.proto"], @@ -47,3 +49,31 @@ java_proto_library( name = "trace_config_proto_java", deps = [":trace_config_proto"], ) + +go_proto_library( + name = "trace_proto_go", + protos = ["trace.proto"], + imports = [ + "external/com_google_protobuf/src", + ], + inputs = [ + "@com_google_protobuf//:well_known_protos", + ], + pb_options = [ + # omit the go_package declared in proto files to make bazel works as expect + "paths=source_relative", + ], + deps = [ + "@com_github_golang_protobuf//ptypes/timestamp:go_default_library", + "@com_github_golang_protobuf//ptypes/wrappers:go_default_library", + ], +) + +go_proto_library( + name = "trace_config_proto_go", + protos = ["trace_config.proto"], + pb_options = [ + # omit the go_package declared in proto files to make bazel works as expect + "paths=source_relative", + ], +)