Skip to content

Commit

Permalink
refactor: relocate ruby targets under the //ruby package
Browse files Browse the repository at this point in the history
Fixes #14569
  • Loading branch information
alexeagle committed Oct 31, 2023
1 parent 3667102 commit 9cf7e87
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 101 deletions.
40 changes: 1 addition & 39 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ load("@rules_java//java:defs.bzl", "java_lite_proto_library", "java_proto_librar
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS")
load(":protobuf.bzl", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library", "internal_ruby_proto_library")
load(":protobuf.bzl", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library")

licenses(["notice"])

Expand Down Expand Up @@ -150,17 +150,6 @@ filegroup(
visibility = ["//visibility:public"],
)

internal_ruby_proto_library(
name = "well_known_ruby_protos",
srcs = [":well_known_protos"],
default_runtime = "",
includes = ["src"],
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
],
)

################################################################################
# Protocol Buffers Compiler
################################################################################
Expand Down Expand Up @@ -524,33 +513,6 @@ internal_php_proto_library(
],
)

internal_ruby_proto_library(
name = "test_messages_proto2_ruby_proto",
testonly = 1,
srcs = ["//src/google/protobuf:test_messages_proto2.proto"],
includes = ["src/google/protobuf"],
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
],
)

internal_ruby_proto_library(
name = "test_messages_proto3_ruby_proto",
testonly = 1,
srcs = ["//src/google/protobuf:test_messages_proto3.proto"],
includes = [
"src/google/protobuf",
# The above must come first.
"src",
],
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
],
deps = [":well_known_ruby_protos"],
)

filegroup(
name = "bzl_srcs",
srcs = glob(["**/*.bzl"]),
Expand Down
3 changes: 2 additions & 1 deletion conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library", "cc_proto_library", "objc_library")
load("@rules_ruby//ruby:defs.bzl", "ruby_binary")
load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library", "internal_ruby_proto_library")
load("//ruby:defs.bzl", "internal_ruby_proto_library")
load("//:protobuf.bzl", "internal_csharp_proto_library", "internal_objc_proto_library", "internal_php_proto_library", "internal_py_proto_library")
load("//build_defs:internal_shell.bzl", "inline_sh_binary")
load(
"@rules_pkg//:mappings.bzl",
Expand Down
61 changes: 3 additions & 58 deletions protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ load("@bazel_skylib//lib:versions.bzl", "versions")
load("@rules_cc//cc:defs.bzl", "objc_library")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_ruby//ruby:defs.bzl", "ruby_library")

def _GetPath(ctx, path):
if ctx.label.workspace_root:
Expand Down Expand Up @@ -272,6 +271,9 @@ _proto_gen = rule(
implementation = _proto_gen_impl,
)

# Expose for //ruby
internal_only_proto_gen = _proto_gen

def _internal_gen_well_known_protos_java_impl(ctx):
args = ctx.actions.args()

Expand Down Expand Up @@ -488,63 +490,6 @@ def internal_objc_proto_library(
**kwargs
)

def internal_ruby_proto_library(
name,
srcs = [],
deps = [],
includes = ["."],
default_runtime = "@com_google_protobuf//ruby:protobuf",
protoc = "@com_google_protobuf//:protoc",
testonly = None,
visibility = ["//visibility:public"],
**kwargs):
"""Bazel rule to create a Ruby protobuf library from proto source files
NOTE: the rule is only an internal workaround to generate protos. The
interface may change and the rule may be removed when bazel has introduced
the native rule.
Args:
name: the name of the ruby_proto_library.
srcs: the .proto files to compile.
deps: a list of dependency labels; must be a internal_ruby_proto_library.
includes: a string indicating the include path of the .proto files.
default_runtime: the RubyProtobuf runtime
protoc: the label of the protocol compiler to generate the sources.
testonly: common rule attribute (see:
https://bazel.build/reference/be/common-definitions#common-attributes)
visibility: the visibility of the generated files.
**kwargs: other keyword arguments that are passed to ruby_library.
"""

# Note: we need to run the protoc build twice to get separate targets for
# the generated header and the source files.
_proto_gen(
name = name + "_genproto",
srcs = srcs,
deps = [s + "_genproto" for s in deps],
langs = ["ruby"],
includes = includes,
protoc = protoc,
testonly = testonly,
visibility = visibility,
tags = ["manual"],
)

deps = []
if default_runtime:
deps.append(default_runtime)
ruby_library(
name = name,
srcs = [name + "_genproto"],
deps = deps,
testonly = testonly,
visibility = visibility,
includes = includes,
**kwargs
)

# When canonical labels are in use, use additional "@" prefix
_canonical_label_prefix = "@" if str(Label("//:protoc")).startswith("@@") else ""

Expand Down
41 changes: 39 additions & 2 deletions ruby/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,48 @@ load("@bazel_skylib//lib:selects.bzl", "selects")
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_ruby//ruby:defs.bzl", "ruby_library")
load("//build_defs:internal_shell.bzl", "inline_sh_binary")
load("//:protobuf.bzl", "internal_ruby_proto_library")
load(":defs.bzl", "internal_ruby_proto_library")
load("//conformance:defs.bzl", "conformance_test")
load("//:protobuf_version.bzl", "PROTOBUF_RUBY_VERSION")

internal_ruby_proto_library(
name = "well_known_ruby_protos",
srcs = ["//:well_known_protos"],
default_runtime = "",
includes = ["src"],
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
],
)

internal_ruby_proto_library(
name = "test_messages_proto2_ruby_proto",
testonly = 1,
srcs = ["//src/google/protobuf:test_messages_proto2.proto"],
includes = ["src/google/protobuf"],
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
],
)

internal_ruby_proto_library(
name = "test_messages_proto3_ruby_proto",
testonly = 1,
srcs = ["//src/google/protobuf:test_messages_proto3.proto"],
includes = [
"src/google/protobuf",
# The above must come first.
"src",
],
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
],
deps = [":well_known_ruby_protos"],
)

################################################################################
# Ruby Runtime
################################################################################
Expand Down
2 changes: 1 addition & 1 deletion ruby/compatibility_tests/v3.0.0/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
load("@rules_ruby//ruby:defs.bzl", "ruby_test")
load("//:protobuf.bzl", "internal_ruby_proto_library")
load("//ruby:defs.bzl", "internal_ruby_proto_library")

internal_ruby_proto_library(
name = "test_ruby_protos",
Expand Down
64 changes: 64 additions & 0 deletions ruby/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Definition of ruby rules for Bazel
These are in the //ruby subpackage to avoid leaking a dependency on the
protocolbuffers/rules_ruby fork to Bazel users who don't use ruby.
"""
load("@rules_ruby//ruby:defs.bzl", "ruby_library")
load("//:protobuf.bzl", "internal_only_proto_gen")

def internal_ruby_proto_library(
name,
srcs = [],
deps = [],
includes = ["."],
default_runtime = "@com_google_protobuf//ruby:protobuf",
protoc = "@com_google_protobuf//:protoc",
testonly = None,
visibility = ["//visibility:public"],
**kwargs):
"""Bazel rule to create a Ruby protobuf library from proto source files
NOTE: the rule is only an internal workaround to generate protos. The
interface may change and the rule may be removed when bazel has introduced
the native rule.
Args:
name: the name of the ruby_proto_library.
srcs: the .proto files to compile.
deps: a list of dependency labels; must be a internal_ruby_proto_library.
includes: a string indicating the include path of the .proto files.
default_runtime: the RubyProtobuf runtime
protoc: the label of the protocol compiler to generate the sources.
testonly: common rule attribute (see:
https://bazel.build/reference/be/common-definitions#common-attributes)
visibility: the visibility of the generated files.
**kwargs: other keyword arguments that are passed to ruby_library.
"""

# Note: we need to run the protoc build twice to get separate targets for
# the generated header and the source files.
internal_only_proto_gen(
name = name + "_genproto",
srcs = srcs,
deps = [s + "_genproto" for s in deps],
langs = ["ruby"],
includes = includes,
protoc = protoc,
testonly = testonly,
visibility = visibility,
tags = ["manual"],
)

deps = []
if default_runtime:
deps.append(default_runtime)
ruby_library(
name = name,
srcs = [name + "_genproto"],
deps = deps,
testonly = testonly,
visibility = visibility,
includes = includes,
**kwargs
)

0 comments on commit 9cf7e87

Please sign in to comment.