Skip to content
Closed
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
4 changes: 2 additions & 2 deletions ruby/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ genrule(
set -eux
mkdir tmp
for src in $(SRCS); do
cp --parents -L "$$src" tmp
mkdir -p "tmp/$$(dirname $$src)" && cp -L "$$src" "tmp/$$src"
done
mkdir -p "tmp/ruby/ext/google/protobuf_c/third_party/utf8_range"
for utf in $(execpaths //third_party/utf8_range:utf8_range_srcs) $(execpath //third_party/utf8_range:LICENSE); do
Expand Down Expand Up @@ -210,7 +210,7 @@ genrule(
set -eux
mkdir tmp
for src in $(SRCS); do
cp --parents -L "$$src" "tmp"
mkdir -p "tmp/$$(dirname $$src)" && cp -L "$$src" "tmp/$$src"
done
mkdir -p "tmp/ruby/ext/google/protobuf_c/third_party/utf8_range"
for utf in $(execpaths //third_party/utf8_range:utf8_range_srcs) $(execpath //third_party/utf8_range:LICENSE); do
Expand Down
35 changes: 25 additions & 10 deletions ruby/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ well_known_protos = %w[
google/protobuf/timestamp.proto
google/protobuf/type.proto
google/protobuf/wrappers.proto
google/protobuf/compiler/plugin.proto
]

test_protos = %w[
Expand All @@ -38,12 +39,6 @@ test_protos = %w[
tests/utf8.proto
]

# These are omitted for now because we don't support proto2.
proto2_protos = %w[
google/protobuf/descriptor.proto
google/protobuf/compiler/plugin.proto
]

if !ENV['PROTOC'].nil?
protoc_command = ENV['PROTOC']
elsif system('../bazel-bin/protoc --version')
Expand All @@ -52,17 +47,27 @@ else
protoc_command = 'protoc'
end

tmp_protoc_out = "tmp/protoc-out"

directory tmp_protoc_out

genproto_output = []

# We won't have access to .. from within docker, but the proto files
# will be there, thanks to the :genproto rule dependency for gem:native.
unless ENV['IN_DOCKER'] == 'true' or ENV['BAZEL'] == 'true'
well_known_protos.each do |proto_file|
input_file = "../src/" + proto_file
output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
input_file = File.join("../src/", proto_file)
output_basename = File.basename(proto_file).sub(/\.proto$/, "_pb.rb")
tmp_output_file = File.join(tmp_protoc_out, File.dirname(proto_file), output_basename)
# Generated _pb.rb for files in subdirectories of google/protobuf
# (eg: ../src/google/protobuf/compiler/plugin.proto) should all still end up
# in lib/google/protobuf.
output_file = File.join("lib/google/protobuf", output_basename)
genproto_output << output_file
file output_file => input_file do |file_task|
sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
file output_file => [input_file, tmp_protoc_out] do |file_task|
sh "#{protoc_command} -I../src --ruby_out=#{tmp_protoc_out} #{input_file}"
FileUtils.cp tmp_output_file, output_file
end
end

Expand Down Expand Up @@ -177,6 +182,16 @@ task :clean do
end

Gem::PackageTask.new(spec) do |pkg|
# When packaging the gem via `rake gem`, we only want to define
# "ext/google/protobuf_c/extconf.rb" as the extension to build the C
# extension.
pkg.gem_spec.extensions = pkg.gem_spec.extensions.map do |extension|
extension == "Rakefile" ? "ext/google/protobuf_c/extconf.rb" : extension
end

pkg.gem_spec.files.reject! { |f| f == "Rakefile" }

pkg.package_files.exclude("Rakefile")
end

# Skip build/genproto in Bazel builds, where we expect this to
Expand Down
14 changes: 11 additions & 3 deletions ruby/google-protobuf.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ Gem::Specification.new do |s|
s.files += Dir.glob('ext/**/*').reject do |file|
File.basename(file) =~ /^(BUILD\.bazel)$/
end
s.extensions = %w[
ext/google/protobuf_c/extconf.rb
ext/google/protobuf_c/Rakefile

# When installing this gem from git via bundler
# (ie: 'gem "google-protobuf", git: "https://.../protobuf.git"' in your
# Gemfile), Rakefile is necessary so the prerequisite tasks run to copy
# third party C libraries and generate well known protobufs. When building
# the gem via `rake gem`, these steps will have already occurred, and so we
# replace the `Rakefile` extension with `ext/google/protobuf_c/extconf.rb`.
# See the `Gem::PackageTask.new` declaration in `Rakefile` for more details.
s.extensions = [
File.exist?("Rakefile") ? "Rakefile" : "ext/google/protobuf_c/extconf.rb",
"ext/google/protobuf_c/Rakefile"
]
end
s.required_ruby_version = '>= 3.1'
Expand Down
Loading