Skip to content

Commit

Permalink
Bazelify ruby runtime (protocolbuffers#10525)
Browse files Browse the repository at this point in the history
This uses https://github.com/protocolbuffers/rules_ruby to fully Bazelify our ruby runtime code.  The Rakefile is left in place for now and is still used by our aarch64 tests.  With the current implementation ruby behaves similarly to our python wrapper, which selects whatever version is installed in the system.  Future enhancements will allow for more hermetic builds via Bazel flags to pin a specific version

Closes protocolbuffers#10525

COPYBARA_INTEGRATE_REVIEW=protocolbuffers#10525 from mkruskal-google:rules_ruby 97fa1f7
PiperOrigin-RevId: 499283908
  • Loading branch information
mkruskal-google authored and Copybara-Service committed Jan 3, 2023
1 parent bb96ec9 commit 633e8f7
Show file tree
Hide file tree
Showing 32 changed files with 600 additions and 368 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ ruby/tests/generated_code_proto2_pb.rb
ruby/tests/multi_level_nesting_test_pb.rb
ruby/tests/test_import_proto2_pb.rb
ruby/tests/test_ruby_package_proto2_pb.rb
ruby/Gemfile.lock
ruby/compatibility_tests/v3.0.0/protoc
ruby/compatibility_tests/v3.0.0/tests/generated_code_pb.rb
ruby/compatibility_tests/v3.0.0/tests/test_import_pb.rb
ruby/Gemfile.lock

# IntelliJ CLion Config files and build output
cmake/.idea
Expand Down
3 changes: 2 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ internal_ruby_proto_library(
name = "well_known_ruby_protos",
srcs = [":well_known_protos"],
includes = ["src"],
default_runtime = "",
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
Expand Down Expand Up @@ -519,7 +520,7 @@ internal_ruby_proto_library(
# The above must come first.
"src",
],
proto_deps = [":well_known_protos"],
deps = [":well_known_ruby_protos"],
visibility = [
"//conformance:__pkg__",
"//ruby:__subpackages__",
Expand Down
14 changes: 14 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,27 @@ bazel_skylib_workspace()
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()

load("@build_bazel_rules_apple//apple:repositories.bzl", "apple_rules_dependencies")
apple_rules_dependencies()

# For `kt_jvm_library`
load("@io_bazel_rules_kotlin//kotlin:repositories.bzl", "kotlin_repositories")
kotlin_repositories()

load("@io_bazel_rules_kotlin//kotlin:core.bzl", "kt_register_toolchains")
kt_register_toolchains()

load("@rules_ruby//ruby:defs.bzl", "ruby_runtime")
ruby_runtime("system_ruby")
register_toolchains("@system_ruby//:toolchain")

load("@system_ruby//:bundle.bzl", "ruby_bundle")
ruby_bundle(
name = "protobuf_bundle",
srcs = ["//ruby:google-protobuf.gemspec"],
gemfile = "//ruby:Gemfile",
)

load("@upb//bazel:workspace_deps.bzl", "upb_deps")
upb_deps()

Expand Down
12 changes: 5 additions & 7 deletions conformance/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Conformance testing for Protobuf.

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("//build_defs:internal_shell.bzl", "inline_sh_binary")
load(
Expand Down Expand Up @@ -317,19 +318,16 @@ cc_binary(
deps = [":conformance_objc_lib"],
)

inline_sh_binary(
ruby_binary(
name = "conformance_ruby",
testonly = 1,
testonly = True,
srcs = ["conformance_ruby.rb"],
cmd = "RUBYLIB=ruby/lib:conformance:src $(rootpath conformance_ruby.rb)",
visibility = ["//ruby:__subpackages__"],
deps = [
":conformance_ruby_proto",
"//:test_messages_proto2_ruby_proto",
"//:test_messages_proto3_ruby_proto",
"//:well_known_ruby_protos",
"//ruby:protobuf",
"//:test_messages_proto3_ruby_proto",
],
visibility = ["//ruby:__subpackages__"],
)

################################################################################
Expand Down
2 changes: 1 addition & 1 deletion conformance/conformance_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

require 'conformance_pb'
require 'conformance/conformance_pb'
require 'google/protobuf/test_messages_proto3_pb'
require 'google/protobuf/test_messages_proto2_pb'

Expand Down
7 changes: 1 addition & 6 deletions kokoro/linux/jruby92/common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ timeout_mins: 120

env_vars {
key: "CONTAINER_IMAGE"
value: "gcr.io/protobuf-build/ruby/linux:jruby-9.2.20.1-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
value: "gcr.io/protobuf-build/ruby/linux:jruby-9.2.20.1-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
}

env_vars {
key: "BAZEL_TARGETS"
value: "//ruby/..."
}

env_vars {
key: "BAZEL_EXTRA_FLAGS"
value: "--define=ruby_platform=java"
}

action {
define_artifacts {
regex: "**/sponge_log.*"
Expand Down
7 changes: 1 addition & 6 deletions kokoro/linux/jruby93/common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ timeout_mins: 120

env_vars {
key: "CONTAINER_IMAGE"
value: "gcr.io/protobuf-build/ruby/linux:jruby-9.3.4.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
value: "gcr.io/protobuf-build/ruby/linux:jruby-9.3.4.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
}

env_vars {
key: "BAZEL_TARGETS"
value: "//ruby/..."
}

env_vars {
key: "BAZEL_EXTRA_FLAGS"
value: "--define=ruby_platform=java"
}

action {
define_artifacts {
regex: "**/sponge_log.*"
Expand Down
7 changes: 1 addition & 6 deletions kokoro/linux/ruby26/common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ timeout_mins: 120

env_vars {
key: "CONTAINER_IMAGE"
value: "gcr.io/protobuf-build/ruby/linux:ruby-2.6.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
value: "gcr.io/protobuf-build/ruby/linux:ruby-2.6.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
}

env_vars {
key: "BAZEL_TARGETS"
value: "//ruby/..."
}

env_vars {
key: "BAZEL_EXTRA_FLAGS"
value: "--define=ruby_platform=c"
}

action {
define_artifacts {
regex: "**/sponge_log.*"
Expand Down
7 changes: 1 addition & 6 deletions kokoro/linux/ruby27/common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ timeout_mins: 120

env_vars {
key: "CONTAINER_IMAGE"
value: "gcr.io/protobuf-build/ruby/linux:ruby-2.7.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
value: "gcr.io/protobuf-build/ruby/linux:ruby-2.7.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
}

env_vars {
key: "BAZEL_TARGETS"
value: "//ruby/..."
}

env_vars {
key: "BAZEL_EXTRA_FLAGS"
value: "--define=ruby_platform=c"
}

action {
define_artifacts {
regex: "**/sponge_log.*"
Expand Down
7 changes: 1 addition & 6 deletions kokoro/linux/ruby30/common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ timeout_mins: 120

env_vars {
key: "CONTAINER_IMAGE"
value: "gcr.io/protobuf-build/ruby/linux:ruby-3.0.2-2f706fd1ab49f4e97af769388be486069b63efee"
value: "gcr.io/protobuf-build/ruby/linux:ruby-3.0.2-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
}

env_vars {
key: "BAZEL_TARGETS"
value: "//ruby/..."
}

env_vars {
key: "BAZEL_EXTRA_FLAGS"
value: "--define=ruby_platform=c"
}

action {
define_artifacts {
regex: "**/sponge_log.*"
Expand Down
7 changes: 1 addition & 6 deletions kokoro/linux/ruby31/common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ timeout_mins: 120

env_vars {
key: "CONTAINER_IMAGE"
value: "gcr.io/protobuf-build/ruby/linux:ruby-3.1.0-64e8944e4f18d7d6c9649112a8a93be57e693cd8"
value: "gcr.io/protobuf-build/ruby/linux:ruby-3.1.0-6fe0cedf2f7f54867de2c3a64f93c85661c9f8a4"
}

env_vars {
key: "BAZEL_TARGETS"
value: "//ruby/..."
}

env_vars {
key: "BAZEL_EXTRA_FLAGS"
value: "--define=ruby_platform=c"
}

action {
define_artifacts {
regex: "**/sponge_log.*"
Expand Down
7 changes: 1 addition & 6 deletions kokoro/macos/ruby26/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@
# Change to repo root
cd $(dirname $0)/../../..

# Prepare worker environment to run tests
KOKORO_INSTALL_RVM=yes
source kokoro/macos/prepare_build_macos_rc

cd ruby
./travis-test.sh ruby-2.6.0
bash -l kokoro/macos/test_ruby.sh ruby-2.6.0
7 changes: 1 addition & 6 deletions kokoro/macos/ruby27/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@
# Change to repo root
cd $(dirname $0)/../../..

# Prepare worker environment to run tests
KOKORO_INSTALL_RVM=yes
source kokoro/macos/prepare_build_macos_rc

cd ruby
./travis-test.sh ruby-2.7.0
bash -l kokoro/macos/test_ruby.sh ruby-2.7.0
7 changes: 1 addition & 6 deletions kokoro/macos/ruby30/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@
# Change to repo root
cd $(dirname $0)/../../..

# Prepare worker environment to run tests
KOKORO_INSTALL_RVM=yes
source kokoro/macos/prepare_build_macos_rc

cd ruby
./travis-test.sh ruby-3.0.2
bash -l kokoro/macos/test_ruby.sh ruby-3.0.2
7 changes: 1 addition & 6 deletions kokoro/macos/ruby31/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@ cd $(dirname $0)/../../..
# Fix locale issues in Monterey.
export LC_ALL=en_US.UTF-8

# Prepare worker environment to run tests
KOKORO_INSTALL_RVM=yes
source kokoro/macos/prepare_build_macos_rc

cd ruby
./travis-test.sh ruby-3.1.0
bash -l kokoro/macos/test_ruby.sh ruby-3.1.0
24 changes: 24 additions & 0 deletions kokoro/macos/test_ruby.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -eux

VERSION=$1

# Prepare worker environment to run tests
KOKORO_INSTALL_RVM=yes
source kokoro/macos/prepare_build_macos_rc

# wget is required for ruby compatibility tests.
brew install wget

# Configure system ruby.
# We need to disable unbound variable errors, due to a known issue described in
# https://github.com/rvm/rvm/issues/4618.
set +u
rvm install $VERSION
rvm use $VERSION
rvm current | grep -qe "${RUBY_VERSION}.*" || exit 1;
set -u

# Run tests
bazel test //ruby/... --test_env=KOKORO_RUBY_VERSION=$VERSION
75 changes: 58 additions & 17 deletions protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 @@ -485,6 +486,63 @@ 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 Expand Up @@ -699,23 +757,6 @@ def internal_php_proto_library(**kwargs):
**kwargs
)

def internal_ruby_proto_library(**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:
**kwargs: arguments that are passed to unsupported_proto_library.
"""

_source_proto_library(
lang = "ruby",
**kwargs
)

def check_protobuf_required_bazel_version():
"""For WORKSPACE files, to check the installed version of bazel.
Expand Down

0 comments on commit 633e8f7

Please sign in to comment.