Protocol Buffers - Google's data interchange format (grpc依赖) https://developers.google.com/protocol-buffers/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
6.1 KiB

require "rubygems"
require "rubygems/package_task"
require "rake/extensiontask" unless RUBY_PLATFORM == "java"
require "rake/testtask"
import 'lib/google/tasks/ffi.rake'
spec = Gem::Specification.load("google-protobuf.gemspec")
well_known_protos = %w[
google/protobuf/any.proto
google/protobuf/api.proto
google/protobuf/descriptor.proto
google/protobuf/duration.proto
google/protobuf/empty.proto
google/protobuf/field_mask.proto
google/protobuf/source_context.proto
google/protobuf/struct.proto
google/protobuf/timestamp.proto
google/protobuf/type.proto
google/protobuf/wrappers.proto
]
test_protos = %w[
tests/basic_test.proto
tests/basic_test_features.proto
tests/basic_test_proto2.proto
tests/generated_code.proto
tests/generated_code_proto2.proto
tests/generated_code_editions.proto
tests/multi_level_nesting_test.proto
tests/repeated_field_test.proto
Implement service & method descriptor lookup in Ruby (#15817) This PR implements lookup of service descriptor and method descriptor objects in Ruby as described in issue https://github.com/protocolbuffers/protobuf/issues/14891. It contains three implementations - one for the CRuby extension API, one for JRuby, and one for FFI. With this patch, * `DescriptorPool#lookup('fully.qualified.service.name')` works and returns a `Google::Protobuf::ServiceDescriptor` object * You can call `#options` on that to get the service options * You can call `#methods` on that to get the services' methods as `Google::Protobuf::MethodDescriptor` objects, * You can call `MethodDescriptor#options` to get method options * You can also get the streaming flags & input/output types of the method with `#input_type`, `#output_type`, `#client_streaming`, and `#server_streaming`. In order to make the FFI implementation work, I had to mark some more methods in the UPB header as exported - I guess that's something which will have to be done on the UPB side, like this https://github.com/protocolbuffers/upb/commit/01fed1cc1ba255bf22b49393ba054b8d270e6ba3 CC @dazuma & @haberman from the original issue, and @JasonLunn (since you work on protobuf it seems - small world!) I apologies for the large volume of copy-pasta'd code from the existing descriptor class implementations into the new ones - I felt this was probably better than designing new abstractions to reduce it off the bat though; this feels like it "fits in" with the existing implementation. Closes #15817 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/15817 from KJTsanaktsidis:ktsanaktsidis/add_service_method_descriptors 54d72184310d7d9bfc06d4a58956e3871170c43d PiperOrigin-RevId: 618221016
9 months ago
tests/service_test.proto
tests/stress.proto
tests/test_import.proto
tests/test_import_proto2.proto
tests/test_ruby_package.proto
tests/test_ruby_package_proto2.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')
protoc_command = '../bazel-bin/protoc'
else
protoc_command = 'protoc'
end
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")
genproto_output << output_file
file output_file => input_file do |file_task|
sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
end
end
test_protos.each do |proto_file|
output_file = proto_file.sub(/\.proto$/, "_pb.rb")
genproto_output << output_file
file output_file => proto_file do |file_task|
sh "#{protoc_command} -I../src -I./tests --ruby_out=tests #{proto_file}"
end
end
end
task :copy_third_party do
unless File.exist? 'ext/google/protobuf_c/third_party/utf8_range'
FileUtils.mkdir_p 'ext/google/protobuf_c/third_party/utf8_range'
# We need utf8_range in-tree.
utf8_root = '../third_party/utf8_range'
%w[
utf8_range.h utf8_range.c LICENSE
].each do |file|
FileUtils.cp File.join(utf8_root, file),
"ext/google/protobuf_c/third_party/utf8_range"
end
end
end
if RUBY_PLATFORM == "java"
task :clean => :require_mvn do
system("mvn --batch-mode clean")
end
task :compile => :require_mvn do
system("mvn --batch-mode package")
end
task :require_mvn do
raise ArgumentError, "maven needs to be installed" if `which mvn` == ''
end
else
Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
unless RUBY_PLATFORM =~ /darwin/
# TODO: also set "no_native to true" for mac if possible. As is,
# "no_native" can only be set if the RUBY_PLATFORM doing
# cross-compilation is contained in the "ext.cross_platform" array.
ext.no_native = true
end
ext.ext_dir = "ext/google/protobuf_c"
ext.lib_dir = "lib/google"
ext.cross_compile = true
ext.cross_platform = [
'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt',
'x86_64-linux', 'x86-linux',
'x86_64-darwin', 'arm64-darwin',
]
Add rake runtime dependency for ruby (#15203) We have a rake based extension `ext/google/protobuf_c/Rakefile` (added since 3.25.0), which depends on `rake`. However, when gem is installed by `bundle` command, `rake` is not guaranteed to be activated by `bundle` (e.g. when `BUNDLE_PATH` is not default and rake is not already installed in `BUNDLE_PATH`). Therefore, we have to explicit declare a runtime dependency on `rake` for the extension, or otherwise the installation will fail with `can't find gem rake (>= 0.a) with executable rake (Gem::GemNotFoundException)`. Here is a reproduction: ``` root@4d9cb6ba41b1:/build# cat Gemfile source 'https://rubygems.org' gem 'google-protobuf' root@4d9cb6ba41b1:/build# bundle config --local path vendor/bundle root@4d9cb6ba41b1:/build# bundle Fetching gem metadata from https://rubygems.org/........... Resolving dependencies... Installing google-protobuf 3.25.1 with native extensions Gem::Ext::BuildError: ERROR: Failed to build gem native extension. current directory: /build/vendor/bundle/ruby/3.3.0/gems/google-protobuf-3.25.1/ext/google/protobuf_c rake RUBYARCHDIR\=/build/vendor/bundle/ruby/3.3.0/extensions/aarch64-linux/3.3.0/google-protobuf-3.25.1 RUBYLIBDIR\=/build/vendor/bundle/ruby/3.3.0/extensions/aarch64-linux/3.3.0/google-protobuf-3.25.1 /usr/local/lib/ruby/3.3.0/rubygems.rb:259:in `find_spec_for_exe': can't find gem rake (>= 0.a) with executable rake (Gem::GemNotFoundException) from /usr/local/lib/ruby/3.3.0/rubygems.rb:278:in `activate_bin_path' from /usr/local/bin/rake:25:in `<main>' rake failed, exit code 1 Gem files will remain installed in /build/vendor/bundle/ruby/3.3.0/gems/google-protobuf-3.25.1 for inspection. Results logged to /build/vendor/bundle/ruby/3.3.0/extensions/aarch64-linux/3.3.0/google-protobuf-3.25.1/gem_make.out /usr/local/lib/ruby/3.3.0/rubygems/ext/builder.rb:125:in `run' /usr/local/lib/ruby/3.3.0/rubygems/ext/rake_builder.rb:30:in `build' /usr/local/lib/ruby/3.3.0/rubygems/ext/builder.rb:193:in `build_extension' /usr/local/lib/ruby/3.3.0/rubygems/ext/builder.rb:227:in `block in build_extensions' /usr/local/lib/ruby/3.3.0/rubygems/ext/builder.rb:224:in `each' /usr/local/lib/ruby/3.3.0/rubygems/ext/builder.rb:224:in `build_extensions' /usr/local/lib/ruby/3.3.0/rubygems/installer.rb:852:in `build_extensions' /usr/local/lib/ruby/3.3.0/bundler/rubygems_gem_installer.rb:76:in `build_extensions' /usr/local/lib/ruby/3.3.0/bundler/rubygems_gem_installer.rb:28:in `install' /usr/local/lib/ruby/3.3.0/bundler/source/rubygems.rb:205:in `install' /usr/local/lib/ruby/3.3.0/bundler/installer/gem_installer.rb:54:in `install' /usr/local/lib/ruby/3.3.0/bundler/installer/gem_installer.rb:16:in `install_from_spec' /usr/local/lib/ruby/3.3.0/bundler/installer/parallel_installer.rb:132:in `do_install' /usr/local/lib/ruby/3.3.0/bundler/installer/parallel_installer.rb:123:in `block in worker_pool' /usr/local/lib/ruby/3.3.0/bundler/worker.rb:62:in `apply_func' /usr/local/lib/ruby/3.3.0/bundler/worker.rb:57:in `block in process_queue' <internal:kernel>:187:in `loop' /usr/local/lib/ruby/3.3.0/bundler/worker.rb:54:in `process_queue' /usr/local/lib/ruby/3.3.0/bundler/worker.rb:90:in `block (2 levels) in create_threads' An error occurred while installing google-protobuf (3.25.1), and Bundler cannot continue. In Gemfile: google-protobuf ``` Closes #15203 COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/15203 from ntkme:ruby-rake-dependency 1318863022c4b0c9a22f267630a3e0138cf311e6 PiperOrigin-RevId: 600916613
10 months ago
ext.cross_compiling do |gem_spec|
# rake-compiler would call `spec.extensions.clear` which removes the `Rakefile` extension,
# that `rake` doesn't need to be a runtime dependency for native gems.
gem_spec.dependencies.delete_if { |dependency| dependency.name == 'rake' }
gem_spec.add_development_dependency 'rake', '>= 13'
end
end
task 'gem:java' do
sh "rm Gemfile.lock"
require 'rake_compiler_dock'
# Specify the repo root as the working and mount directory to provide access
# to the java directory
repo_root = File.realdirpath File.join(Dir.pwd, '..')
RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root
sudo apt-get install maven -y && \
cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \
bundle && \
IN_DOCKER=true rake compile gem
EOT
end
task 'gem:windows' do
sh "rm Gemfile.lock"
require 'rake_compiler_dock'
['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat|
RakeCompilerDock.sh <<-"EOT", platform: plat
bundle && \
IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0
EOT
end
end
if RUBY_PLATFORM =~ /darwin/
task 'gem:native' do
system "rake genproto"
system "rake cross native gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0"
end
else
task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
end
end
task :genproto => genproto_output
task :clean do
sh "rm -f #{genproto_output.join(' ')}"
sh "rm -f google-protobuf-*gem"
sh "rm -f Gemfile.lock"
sh "rm -rf pkg"
sh "rm -rf tmp"
# Handles third_party and any platform specific directories built by FFI
Pathname('ext/google/protobuf_c').children.select(&:directory?).each do |dir|
sh "rm -rf #{dir}"
end
end
Gem::PackageTask.new(spec) do |pkg|
end
# Skip build/genproto in Bazel builds, where we expect this to
# be done already.
Rake::TestTask.new(:test => ENV['BAZEL'] == 'true' ? [] : [:build, :genproto]) do |t|
t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
end
# gc_test needs to be split out to ensure the generated file hasn't been
# imported by other tests.
Rake::TestTask.new(:gc_test => ENV['BAZEL'] == 'true' ? [] : :build) do |t|
t.test_files = FileList["tests/gc_test.rb"]
end
task :build => [:clean, :genproto, :copy_third_party, :compile, :"ffi-protobuf:default"]
Rake::Task[:gem].enhance [:copy_third_party, :genproto]
task :default => [:build]
# vim:sw=2:et