• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1require "rubygems"
2require "rubygems/package_task"
3require "rake/extensiontask" unless RUBY_PLATFORM == "java"
4require "rake/testtask"
5import 'lib/google/tasks/ffi.rake'
6
7spec = Gem::Specification.load("google-protobuf.gemspec")
8
9well_known_protos = %w[
10  google/protobuf/any.proto
11  google/protobuf/api.proto
12  google/protobuf/descriptor.proto
13  google/protobuf/duration.proto
14  google/protobuf/empty.proto
15  google/protobuf/field_mask.proto
16  google/protobuf/source_context.proto
17  google/protobuf/struct.proto
18  google/protobuf/timestamp.proto
19  google/protobuf/type.proto
20  google/protobuf/wrappers.proto
21]
22
23test_protos = %w[
24  tests/basic_test.proto
25  tests/basic_test_features.proto
26  tests/basic_test_proto2.proto
27  tests/generated_code.proto
28  tests/generated_code_proto2.proto
29  tests/generated_code_editions.proto
30  tests/multi_level_nesting_test.proto
31  tests/repeated_field_test.proto
32  tests/service_test.proto
33  tests/stress.proto
34  tests/test_import.proto
35  tests/test_import_proto2.proto
36  tests/test_ruby_package.proto
37  tests/test_ruby_package_proto2.proto
38  tests/utf8.proto
39]
40
41# These are omitted for now because we don't support proto2.
42proto2_protos = %w[
43  google/protobuf/descriptor.proto
44  google/protobuf/compiler/plugin.proto
45]
46
47if !ENV['PROTOC'].nil?
48  protoc_command = ENV['PROTOC']
49elsif system('../bazel-bin/protoc --version')
50  protoc_command = '../bazel-bin/protoc'
51else
52  protoc_command = 'protoc'
53end
54
55genproto_output = []
56
57# We won't have access to .. from within docker, but the proto files
58# will be there, thanks to the :genproto rule dependency for gem:native.
59unless ENV['IN_DOCKER'] == 'true' or ENV['BAZEL'] == 'true'
60  well_known_protos.each do |proto_file|
61    input_file = "../src/" + proto_file
62    output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
63    genproto_output << output_file
64    file output_file => input_file do |file_task|
65      sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
66    end
67  end
68
69  test_protos.each do |proto_file|
70    output_file = proto_file.sub(/\.proto$/, "_pb.rb")
71    genproto_output << output_file
72    file output_file => proto_file do |file_task|
73      sh "#{protoc_command} -I../src -I./tests --ruby_out=tests  #{proto_file}"
74    end
75  end
76end
77
78task :copy_third_party do
79  unless File.exist? 'ext/google/protobuf_c/third_party/utf8_range'
80    FileUtils.mkdir_p 'ext/google/protobuf_c/third_party/utf8_range'
81    # We need utf8_range in-tree.
82    utf8_root = '../third_party/utf8_range'
83    %w[
84      utf8_range.h utf8_range.c LICENSE
85    ].each do |file|
86      FileUtils.cp File.join(utf8_root, file),
87                   "ext/google/protobuf_c/third_party/utf8_range"
88    end
89  end
90end
91
92if RUBY_PLATFORM == "java"
93  task :clean => :require_mvn do
94    system("mvn --batch-mode clean")
95  end
96
97  task :compile => :require_mvn do
98    system("mvn --batch-mode package")
99  end
100
101  task :require_mvn do
102    raise ArgumentError, "maven needs to be installed" if `which mvn` == ''
103  end
104
105else
106  Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
107    unless RUBY_PLATFORM =~ /darwin/
108      # TODO: also set "no_native to true" for mac if possible. As is,
109      # "no_native" can only be set if the RUBY_PLATFORM doing
110      # cross-compilation is contained in the "ext.cross_platform" array.
111      ext.no_native = true
112    end
113    ext.ext_dir = "ext/google/protobuf_c"
114    ext.lib_dir = "lib/google"
115    ext.cross_compile = true
116    ext.cross_platform = [
117      'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt',
118      'x86_64-linux', 'x86-linux',
119      'x86_64-darwin', 'arm64-darwin',
120    ]
121
122    ext.cross_compiling do |gem_spec|
123      # rake-compiler would call `spec.extensions.clear` which removes the `Rakefile` extension,
124      # that `rake` doesn't need to be a runtime dependency for native gems.
125      gem_spec.dependencies.delete_if { |dependency| dependency.name == 'rake' }
126      gem_spec.add_development_dependency 'rake', '>= 13'
127    end
128  end
129
130  task 'gem:java' do
131    sh "rm Gemfile.lock"
132    require 'rake_compiler_dock'
133    # Specify the repo root as the working and mount directory to provide access
134    # to the java directory
135    repo_root = File.realdirpath File.join(Dir.pwd, '..')
136    RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root
137      sudo apt-get install maven -y && \
138      cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \
139      bundle && \
140      IN_DOCKER=true rake compile gem
141    EOT
142  end
143
144  task 'gem:windows' do
145    sh "rm Gemfile.lock"
146    require 'rake_compiler_dock'
147    ['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat|
148      RakeCompilerDock.sh <<-"EOT", platform: plat
149        bundle && \
150        IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0
151      EOT
152    end
153  end
154
155  if RUBY_PLATFORM =~ /darwin/
156    task 'gem:native' do
157      system "rake genproto"
158      system "rake cross native gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0"
159    end
160  else
161    task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
162  end
163end
164
165task :genproto => genproto_output
166
167task :clean do
168  sh "rm -f #{genproto_output.join(' ')}"
169  sh "rm -f google-protobuf-*gem"
170  sh "rm -f Gemfile.lock"
171  sh "rm -rf pkg"
172  sh "rm -rf tmp"
173  # Handles third_party and any platform specific directories built by FFI
174  Pathname('ext/google/protobuf_c').children.select(&:directory?).each do |dir|
175    sh "rm -rf #{dir}"
176  end
177end
178
179Gem::PackageTask.new(spec) do |pkg|
180end
181
182# Skip build/genproto in Bazel builds, where we expect this to
183# be done already.
184Rake::TestTask.new(:test => ENV['BAZEL'] == 'true' ? [] : [:build, :genproto]) do |t|
185  t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
186end
187
188# gc_test needs to be split out to ensure the generated file hasn't been
189# imported by other tests.
190Rake::TestTask.new(:gc_test => ENV['BAZEL'] == 'true' ? [] : :build) do |t|
191  t.test_files = FileList["tests/gc_test.rb"]
192end
193
194task :build => [:clean, :genproto, :copy_third_party, :compile, :"ffi-protobuf:default"]
195Rake::Task[:gem].enhance [:copy_third_party, :genproto]
196task :default => [:build]
197
198# vim:sw=2:et
199