Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
workspace(name = "io_bazel_rules_go")

load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
load("@io_bazel_rules_go//proto:def.bzl", "proto_register_toolchains")
go_rules_dependencies()
go_register_toolchains()
proto_register_toolchains()

# Needed for tests
load("@io_bazel_rules_go//tests:bazel_tests.bzl", "test_environment")
Expand Down
8 changes: 5 additions & 3 deletions examples/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["gs.go"],
visibility = ["//visibility:public"],
deps = [
"//examples/proto/gostyle:go_default_library",
"//examples/proto/lib:lib_proto",
"//examples/proto/lib:go_default_library",
],
visibility = ["//visibility:public"],
)

go_test(
name = "proto_test",
size = "small",
srcs = ["proto_test.go"],
deps = ["//examples/proto/lib:lib_proto"],
deps = [
"//examples/proto/lib:go_default_library",
],
)
19 changes: 15 additions & 4 deletions examples/proto/dep/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

go_proto_library(
proto_library(
name = "useful_proto",
srcs = ["useful.proto"],
rules_go_repo_only_for_internal_use = "@",
deps = [
"@com_google_protobuf//:duration_proto",
],
visibility = ["//visibility:public"],
)

go_proto_library(
name = "go_default_library",
proto = ":useful_proto",
deps = [
"@com_github_golang_protobuf//ptypes/duration:go_default_library",
],
visibility = ["//visibility:public"],
deps = ["@com_github_golang_protobuf//ptypes/duration:go_default_library"],
)
5 changes: 2 additions & 3 deletions examples/proto/dep/useful.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ syntax = "proto3";

package very.useful;

// TODO(pmbethe09): Figure out how to make go_package in local .proto files work.
// option go_package = "github.com/bazelbuild/rules_go/example/proto/dep/useful_proto";
option go_package = "github.com/bazelbuild/rules_go/examples/proto/dep";

import "ptypes/duration/duration.proto";
import "google/protobuf/duration.proto";

message VeryUseful {
string very = 1;
Expand Down
17 changes: 13 additions & 4 deletions examples/proto/gostyle/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

genrule(
name = "copy",
Expand All @@ -7,12 +8,20 @@ genrule(
cmd = "sed -e 's/ano.proto/any.proto/' $< > $@",
)

proto_library(
name = "gostyle",
srcs = [":copy"],
deps = [
"@com_google_protobuf//:any_proto",
],
visibility = ["//visibility:public"],
)

go_proto_library(
name = "go_default_library",
srcs = ["gostyle.gen.proto"],
rules_go_repo_only_for_internal_use = "@",
visibility = ["//visibility:public"],
proto = ":gostyle",
deps = [
"@com_github_golang_protobuf//ptypes/any:go_default_library",
],
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions examples/proto/gostyle/gostyle.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";

package gostyle;

option go_package = "github.com/bazelbuild/rules_go/examples/proto/gostyle";

import "google/protobuf/ano.proto";

message GoStyleObject {
Expand Down
23 changes: 16 additions & 7 deletions examples/proto/grpc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library")

go_proto_library(
proto_library(
name = "my_svc_proto",
srcs = ["my_svc.proto"],
has_services = 1,
rules_go_repo_only_for_internal_use = "@",
deps = [
"//examples/proto/lib:lib_proto",
"@com_google_protobuf//:any_proto",
"@com_google_protobuf//:empty_proto",
],
)

go_grpc_library(
name = "go_default_library",
proto = ":my_svc_proto",
importpath = "github.com/bazelbuild/rules_go/examples/proto/grpc/my_svc_proto",
deps = [
"//examples/proto/lib:go_default_library",
"@com_github_golang_protobuf//ptypes/any:go_default_library",
"@com_github_golang_protobuf//ptypes/empty:go_default_library",
],
Expand All @@ -17,8 +26,8 @@ go_binary(
name = "test_grpc",
srcs = ["main.go"],
deps = [
":my_svc_proto",
"//examples/proto/lib:lib_proto",
":go_default_library",
"//examples/proto/lib:go_default_library",
"@com_github_golang_protobuf//ptypes/any:go_default_library",
"@com_github_golang_protobuf//ptypes/empty:go_default_library",
"@org_golang_google_grpc//:go_default_library",
Expand Down
2 changes: 2 additions & 0 deletions examples/proto/grpc/my_svc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";

package examples.svc;

option go_package = "github.com/bazelbuild/rules_go/examples/proto/grpc/my_svc_proto";

import "examples/proto/lib/lib.proto";
import "google/protobuf/any.proto";
import "google/protobuf/empty.proto";
Expand Down
20 changes: 16 additions & 4 deletions examples/proto/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
load("@io_bazel_rules_go//proto:go_proto_library.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

go_proto_library(
proto_library(
name = "lib_proto",
srcs = [
"lib.proto",
"lib2.proto",
],
rules_go_repo_only_for_internal_use = "@",
deps = [
"//examples/proto/dep:useful_proto",
],
visibility = ["//visibility:public"],
)

go_proto_library(
name = "go_default_library",
proto = ":lib_proto",
deps = [
"//examples/proto/dep:go_default_library",
],
importpath = "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto",
visibility = ["//visibility:public"],
deps = ["//examples/proto/dep:useful_proto"],
)
3 changes: 1 addition & 2 deletions examples/proto/lib/lib.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ syntax = "proto3";

package examples.lib;

// TODO(pmbethe09): make this also work.
// option go_package = "github.com/bazelbuild/rules_go/example/proto/lib_proto";
option go_package = "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto";

import "examples/proto/dep/useful.proto";

Expand Down
2 changes: 2 additions & 0 deletions examples/proto/lib/lib2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ syntax = "proto3";

package examples.lib;

option go_package = "github.com/bazelbuild/rules_go/examples/proto/lib/lib_proto";

import "examples/proto/dep/useful.proto";
import "examples/proto/lib/lib.proto";

Expand Down
9 changes: 9 additions & 0 deletions go/private/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ def go_rules_dependencies():
importpath = "github.com/golang/protobuf",
commit = "8ee79997227bf9b34611aee7946ae64735e6fd93",
)
_maybe(native.http_archive,
name = "com_google_protobuf",
url = "https://codeload.github.com/google/protobuf/zip/054054c1523342294d50460d652ad2c767df627f",
strip_prefix = "protobuf-054054c1523342294d50460d652ad2c767df627f",
#sha256 = "ea23bbec9e86205b71ef647e1755ae0ec400aa76aeb5d13913d3fc3a37afbb5f",
type = "zip",
)

# Only used by deprecated go_proto_library implementation
_maybe(native.http_archive,
name = "com_github_google_protobuf",
url = "https://github.com/google/protobuf/archive/v3.4.0.tar.gz",
Expand Down
49 changes: 49 additions & 0 deletions proto/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load('//proto:toolchain.bzl', 'proto_toolchain', 'go_proto_toolchain')

proto_toolchain(
name = "proto_impl",
visibility = ["//visibility:public"],
)

toolchain(
name = "proto",
toolchain_type = "@io_bazel_rules_go//proto:proto",
exec_compatible_with = [],
target_compatible_with = [],
toolchain = "proto_impl",
)

go_proto_toolchain(
name = "go_proto_impl",
deps = [
"@com_github_golang_protobuf//proto:go_default_library",
],
visibility = ["//visibility:public"],
)

toolchain(
name = "go_proto",
toolchain_type = "@io_bazel_rules_go//proto:go_proto",
exec_compatible_with = [],
target_compatible_with = [],
toolchain = "go_proto_impl",
)

go_proto_toolchain(
name = "go_grpc_impl",
deps = [
"@com_github_golang_protobuf//proto:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_x_net//context:go_default_library",
],
options = ["plugins=grpc"],
visibility = ["//visibility:public"],
)

toolchain(
name = "go_grpc",
toolchain_type = "@io_bazel_rules_go//proto:go_grpc",
exec_compatible_with = [],
target_compatible_with = [],
toolchain = "go_grpc_impl",
)
94 changes: 94 additions & 0 deletions proto/def.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
load("@io_bazel_rules_go//go/private:common.bzl",
"go_importpath",
"RACE_MODE",
"NORMAL_MODE",
)
load("@io_bazel_rules_go//go/private:providers.bzl",
"get_library",
"GoLibrary",
"GoEmbed",
)
load("@io_bazel_rules_go//go/private:rules/prefix.bzl",
"go_prefix_default",
)

def _go_proto_library_impl(ctx):
go_proto_toolchain = ctx.toolchains[ctx.attr._toolchain]
importpath = go_importpath(ctx)
go_srcs = go_proto_toolchain.compile(ctx,
proto_toolchain = ctx.toolchains["@io_bazel_rules_go//proto:proto"],
go_proto_toolchain = go_proto_toolchain,
lib = ctx.attr.proto,
importpath = importpath,
)
go_toolchain = ctx.toolchains["@io_bazel_rules_go//go:toolchain"]
golib, goembed, _ = go_toolchain.actions.library(ctx,
go_toolchain = go_toolchain,
srcs = go_srcs,
deps = ctx.attr.deps + go_proto_toolchain.deps,
cgo_object = None,
embed = ctx.attr.embed,
want_coverage = ctx.coverage_instrumented(),
importpath = importpath,
)
return [
golib, goembed,
DefaultInfo(
files = depset([get_library(golib, NORMAL_MODE)]),
runfiles = golib.runfiles,
),
OutputGroupInfo(
race = depset([get_library(golib, RACE_MODE)]),
),
]

go_proto_library = rule(
_go_proto_library_impl,
attrs = {
"proto": attr.label(mandatory=True, providers=["proto"]),
"deps": attr.label_list(providers = [GoLibrary]),
"importpath": attr.string(),
"embed": attr.label_list(providers = [GoEmbed]),
"gc_goopts": attr.string_list(),
"_go_prefix": attr.label(default = go_prefix_default),
"_toolchain": attr.string(default = "@io_bazel_rules_go//proto:go_proto"),
},
toolchains = [
"@io_bazel_rules_go//go:toolchain",
"@io_bazel_rules_go//proto:proto",
"@io_bazel_rules_go//proto:go_proto",
],
)
"""
go_proto_library is a rule that takes a proto_library (in the proto
attribute) and produces a go library for it.
"""

go_grpc_library = rule(
_go_proto_library_impl,
attrs = {
"proto": attr.label(mandatory=True, providers=["proto"]),
"deps": attr.label_list(providers = [GoLibrary]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall nice work. Too bad about this though -- that you have to know your deps as opposed to the way aspects builds the shadow-graph for you.

"importpath": attr.string(),
"embed": attr.label_list(providers = [GoEmbed]),
"gc_goopts": attr.string_list(),
"_go_prefix": attr.label(default = go_prefix_default),
"_toolchain": attr.string(default = "@io_bazel_rules_go//proto:go_grpc"),
},
toolchains = [
"@io_bazel_rules_go//go:toolchain",
"@io_bazel_rules_go//proto:proto",
"@io_bazel_rules_go//proto:go_grpc",
],
)
"""
go_grpc_library is a rule that takes a proto_library (in the proto
attribute) and produces a go library that includes grpc services for it.
"""

def proto_register_toolchains():
native.register_toolchains(
"@io_bazel_rules_go//proto:proto",
"@io_bazel_rules_go//proto:go_proto",
"@io_bazel_rules_go//proto:go_grpc",
)
2 changes: 1 addition & 1 deletion proto/go_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,4 @@ def go_google_protobuf(name = _GO_GOOGLE_PROTOBUF):

def go_proto_repositories(shared = 1):
"""Add this to your WORKSPACE to pull in all of the needed dependencies."""
print("DEPRECATED: go_proto_repositories is redundant and will be removed soon")
print("DEPRECATED: go_proto_repositories is redundant and will be removed soon")
Loading