Skip to content

Latest commit

 

History

History
 
 

go

go

Rule Description
go_proto_compile Generates *.go protobuf artifacts
go_grpc_compile Generates *.go protobuf+gRPC artifacts
go_proto_library Generates *.go protobuf library
go_grpc_library Generates *.go protobuf+gRPC library

go_proto_compile

Generates *.go protobuf artifacts

WORKSPACE

load("@build_stack_rules_proto//go:deps.bzl", "go_proto_compile")

go_proto_compile()

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

go_rules_dependencies()

go_register_toolchains()

BUILD.bazel

load("@build_stack_rules_proto//go:go_proto_compile.bzl", "go_proto_compile")

go_proto_compile(
    name = "person_go_proto",
    deps = ["@build_stack_rules_proto//example/proto:person_proto"],
)

IMPLEMENTATION

load("//:compile.bzl", "proto_compile")
load("//:plugin.bzl", "proto_plugin")

def go_proto_compile(**kwargs):
    # If importpath specified, declare a custom plugin that should correctly
    # predict the output location.
    importpath = kwargs.get("importpath")
    if importpath and not kwargs.get("plugins"):
        name = kwargs.get("name")
        name_plugin = name + "_plugin"
        proto_plugin(
            name = name_plugin,
            outputs = ["{package}/%s/{basename}.pb.go" % importpath],
            tool = "@com_github_golang_protobuf//protoc-gen-go",
        )
        kwargs["plugins"] = [name_plugin]
        kwargs.pop("importpath")

    # Define the default plugin if still not defined
    if not kwargs.get("plugins"):
        kwargs["plugins"] = [str(Label("//go:go"))]

    proto_compile(
        **kwargs
    )

Mandatory Attributes

Name Type Default Description
deps list<ProtoInfo> [] List of labels that provide a ProtoInfo (native.proto_library)

Optional Attributes

Name Type Default Description
plugins list<ProtoPluginInfo> [] List of labels that provide a ProtoPluginInfo
plugin_options list<string> [] List of additional 'global' plugin options (applies to all plugins)
outputs list<generated file> [] List of additional expected generated file outputs
has_services bool False If the proto files(s) have a service rpc, generate grpc outputs
protoc executable file @com_google_protobuf//:protoc The protocol compiler tool
verbose int 0 1: show command, 2: show sandbox after, 3: show sandbox before
include_imports bool True Pass the --include_imports argument to the protoc_plugin
include_source_info bool True Pass the --include_source_info argument to the protoc_plugin
transitive bool False Generated outputs for *.proto directly named in deps AND all transitive proto_library dependencies
importpath string None Importpath for the generated artifacts
importmap string_dict None A dictionary of the form { K: V} that dictates the importpath V for a matching imported proto file K

go_grpc_compile

Generates *.go protobuf+gRPC artifacts

WORKSPACE

load("@build_stack_rules_proto//go:deps.bzl", "go_grpc_compile")

go_grpc_compile()

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

go_rules_dependencies()

go_register_toolchains()

BUILD.bazel

load("@build_stack_rules_proto//go:go_grpc_compile.bzl", "go_grpc_compile")

go_grpc_compile(
    name = "greeter_go_grpc",
    deps = ["@build_stack_rules_proto//example/proto:greeter_grpc"],
)

IMPLEMENTATION

load("//:compile.bzl", "proto_compile")
load("//:plugin.bzl", "proto_plugin")

def go_grpc_compile(**kwargs):
    # If importpath specified, declare a custom plugin that should correctly
    # predict the output location.
    importpath = kwargs.get("importpath")
    if importpath and not kwargs.get("plugins"):
        name = kwargs.get("name")
        name_plugin = name + "_plugin"
        proto_plugin(
            name = name_plugin,
            outputs = ["{package}/%s/{basename}.pb.go" % importpath],
            tool = "@com_github_golang_protobuf//protoc-gen-go",
        )
        kwargs["plugins"] = [name_plugin]
        kwargs.pop("importpath")

    # Define the default plugin if still not defined
    if not kwargs.get("plugins"):
        kwargs["plugins"] = [str(Label("//go:grpc_go"))]

    proto_compile(
        **kwargs
    )

Mandatory Attributes

Name Type Default Description
deps list<ProtoInfo> [] List of labels that provide a ProtoInfo (native.proto_library)

Optional Attributes

Name Type Default Description
plugins list<ProtoPluginInfo> [] List of labels that provide a ProtoPluginInfo
plugin_options list<string> [] List of additional 'global' plugin options (applies to all plugins)
outputs list<generated file> [] List of additional expected generated file outputs
has_services bool False If the proto files(s) have a service rpc, generate grpc outputs
protoc executable file @com_google_protobuf//:protoc The protocol compiler tool
verbose int 0 1: show command, 2: show sandbox after, 3: show sandbox before
include_imports bool True Pass the --include_imports argument to the protoc_plugin
include_source_info bool True Pass the --include_source_info argument to the protoc_plugin
transitive bool False Generated outputs for *.proto directly named in deps AND all transitive proto_library dependencies
importpath string None Importpath for the generated artifacts
importmap string_dict None A dictionary of the form { K: V} that dictates the importpath V for a matching imported proto file K

go_proto_library

Generates *.go protobuf library

WORKSPACE

load("@build_stack_rules_proto//go:deps.bzl", "go_proto_library")

go_proto_library()

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

go_rules_dependencies()

go_register_toolchains()

BUILD.bazel

load("@build_stack_rules_proto//go:go_proto_library.bzl", "go_proto_library")

go_proto_library(
    name = "person_go_library",
    go_deps = [
        "@com_github_golang_protobuf//ptypes/any:go_default_library",
    ],
    importpath = "github.com/stackb/rules_proto/go/example/go_proto_library/person",
    deps = ["@build_stack_rules_proto//example/proto:person_proto"],
)

IMPLEMENTATION

load("//go:go_proto_compile.bzl", "go_proto_compile")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//go:utils.bzl", "get_importmappings")

def go_proto_library(**kwargs):
    name = kwargs.get("name")
    deps = kwargs.get("deps")
    importpath = kwargs.get("importpath")
    visibility = kwargs.get("visibility")
    go_deps = kwargs.get("go_deps", [])

    name_pb = name + "_pb"

    go_proto_compile(
        name = name_pb,
        deps = deps,
        transitive = kwargs.pop("transitive", True),
        plugin_options = get_importmappings(kwargs.pop("importmap", {})),
        visibility = visibility,
    )

    go_library(
        name = name,
        srcs = [name_pb],
        deps = go_deps + [
            "@com_github_golang_protobuf//proto:go_default_library",
        ],
        importpath = importpath,
        visibility = visibility,
    )

Mandatory Attributes

Name Type Default Description
deps list<ProtoInfo> [] List of labels that provide a ProtoInfo (native.proto_library)

Optional Attributes

Name Type Default Description
plugins list<ProtoPluginInfo> [] List of labels that provide a ProtoPluginInfo
plugin_options list<string> [] List of additional 'global' plugin options (applies to all plugins)
outputs list<generated file> [] List of additional expected generated file outputs
has_services bool False If the proto files(s) have a service rpc, generate grpc outputs
protoc executable file @com_google_protobuf//:protoc The protocol compiler tool
verbose int 0 1: show command, 2: show sandbox after, 3: show sandbox before
include_imports bool True Pass the --include_imports argument to the protoc_plugin
include_source_info bool True Pass the --include_source_info argument to the protoc_plugin
transitive bool False Generated outputs for *.proto directly named in deps AND all transitive proto_library dependencies
importpath string None Importpath for the generated artifacts
importmap string_dict None A dictionary of the form { K: V} that dictates the importpath V for a matching imported proto file K

go_grpc_library

Generates *.go protobuf+gRPC library

WORKSPACE

load("@build_stack_rules_proto//go:deps.bzl", "go_grpc_library")

go_grpc_library()

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

go_rules_dependencies()

go_register_toolchains()

BUILD.bazel

load("@build_stack_rules_proto//go:go_grpc_library.bzl", "go_grpc_library")

go_grpc_library(
    name = "greeter_go_library",
    go_deps = [
        "@com_github_golang_protobuf//ptypes/any:go_default_library",
    ],
    importpath = "github.com/stackb/rules_proto/go/example/go_grpc_library/greeter",
    deps = ["@build_stack_rules_proto//example/proto:greeter_grpc"],
)

IMPLEMENTATION

load("//go:go_grpc_compile.bzl", "go_grpc_compile")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("//go:utils.bzl", "get_importmappings")

def go_grpc_library(**kwargs):
    name = kwargs.get("name")
    deps = kwargs.get("deps")
    importpath = kwargs.get("importpath")
    visibility = kwargs.get("visibility")
    go_deps = kwargs.get("go_deps", [])

    name_pb = name + "_pb"

    go_grpc_compile(
        name = name_pb,
        deps = deps,
        transitive = kwargs.pop("transitive", True),
        plugin_options = get_importmappings(kwargs.pop("importmap", {})),
        visibility = visibility,
    )

    go_library(
        name = name,
        srcs = [name_pb],
        deps = go_deps + [
            "@com_github_golang_protobuf//proto:go_default_library",
            "@org_golang_google_grpc//:go_default_library",
            "@org_golang_x_net//context:go_default_library",
        ],
        importpath = importpath,
        visibility = visibility,
    )

Mandatory Attributes

Name Type Default Description
deps list<ProtoInfo> [] List of labels that provide a ProtoInfo (native.proto_library)

Optional Attributes

Name Type Default Description
plugins list<ProtoPluginInfo> [] List of labels that provide a ProtoPluginInfo
plugin_options list<string> [] List of additional 'global' plugin options (applies to all plugins)
outputs list<generated file> [] List of additional expected generated file outputs
has_services bool False If the proto files(s) have a service rpc, generate grpc outputs
protoc executable file @com_google_protobuf//:protoc The protocol compiler tool
verbose int 0 1: show command, 2: show sandbox after, 3: show sandbox before
include_imports bool True Pass the --include_imports argument to the protoc_plugin
include_source_info bool True Pass the --include_source_info argument to the protoc_plugin
transitive bool False Generated outputs for *.proto directly named in deps AND all transitive proto_library dependencies
importpath string None Importpath for the generated artifacts
importmap string_dict None A dictionary of the form { K: V} that dictates the importpath V for a matching imported proto file K