blob: fde98a91fe3748fdeaa50bfb808c2f55129184f5 [file] [log] [blame]
Isaiah Peng27e2b572014-12-24 15:48:41 +01001require "rubygems"
2require "rubygems/package_task"
3require "rake/extensiontask" unless RUBY_PLATFORM == "java"
Chris Fallin973f4252014-11-18 14:19:58 -08004require "rake/testtask"
Jason Lunnc52d80c2023-07-24 23:13:47 -07005import 'lib/google/tasks/ffi.rake'
Chris Fallin973f4252014-11-18 14:19:58 -08006
Chris Fallin91473dc2014-12-12 15:58:26 -08007spec = Gem::Specification.load("google-protobuf.gemspec")
Chris Fallin973f4252014-11-18 14:19:58 -08008
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +02009well_known_protos = %w[
10 google/protobuf/any.proto
11 google/protobuf/api.proto
Joshua Haberman16e16eb2021-07-31 11:13:08 -070012 google/protobuf/descriptor.proto
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020013 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
Joshua Habermandd69a482021-05-17 22:40:33 -070023test_protos = %w[
24 tests/basic_test.proto
Mike Kruskalbca8fb62024-03-15 15:15:45 -070025 tests/basic_test_features.proto
Joshua Habermandd69a482021-05-17 22:40:33 -070026 tests/basic_test_proto2.proto
27 tests/generated_code.proto
28 tests/generated_code_proto2.proto
Mike Kruskalbca8fb62024-03-15 15:15:45 -070029 tests/generated_code_editions.proto
Joshua Habermandd69a482021-05-17 22:40:33 -070030 tests/multi_level_nesting_test.proto
Joshua Haberman5cfc9e72024-01-03 13:03:49 -080031 tests/repeated_field_test.proto
KJ Tsanaktsidis2082ee82024-03-22 10:32:08 -070032 tests/service_test.proto
Joshua Haberman5cfc9e72024-01-03 13:03:49 -080033 tests/stress.proto
Joshua Habermandd69a482021-05-17 22:40:33 -070034 tests/test_import.proto
35 tests/test_import_proto2.proto
36 tests/test_ruby_package.proto
37 tests/test_ruby_package_proto2.proto
Joshua Habermanb07e4b12024-07-17 08:05:12 -070038 tests/utf8.proto
Joshua Habermandd69a482021-05-17 22:40:33 -070039]
40
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020041# 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
Adam Cozzette9c8c3de2022-04-27 10:40:01 -070047if !ENV['PROTOC'].nil?
48 protoc_command = ENV['PROTOC']
Mike Kruskaled5c57a2022-08-10 22:51:29 -070049elsif system('../bazel-bin/protoc --version')
50 protoc_command = '../bazel-bin/protoc'
James D68cb69e2021-04-12 13:06:44 -040051else
Adam Cozzette9c8c3de2022-04-27 10:40:01 -070052 protoc_command = 'protoc'
James D68cb69e2021-04-12 13:06:44 -040053end
54
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020055genproto_output = []
56
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +020057# 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.
Mike Kruskaled5c57a2022-08-10 22:51:29 -070059unless ENV['IN_DOCKER'] == 'true' or ENV['BAZEL'] == 'true'
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020060 well_known_protos.each do |proto_file|
61 input_file = "../src/" + proto_file
Josh Haberman4f197972016-07-25 01:47:50 -070062 output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020063 genproto_output << output_file
64 file output_file => input_file do |file_task|
James D68cb69e2021-04-12 13:06:44 -040065 sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020066 end
67 end
Joshua Habermandd69a482021-05-17 22:40:33 -070068
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|
Joshua Haberman5cfc9e72024-01-03 13:03:49 -080073 sh "#{protoc_command} -I../src -I./tests --ruby_out=tests #{proto_file}"
Joshua Habermandd69a482021-05-17 22:40:33 -070074 end
75 end
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020076end
77
Jason Lunnc52d80c2023-07-24 23:13:47 -070078task :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.
Adam Cozzette5ca84002023-11-08 15:36:33 -080082 utf8_root = '../third_party/utf8_range'
Jason Lunnc52d80c2023-07-24 23:13:47 -070083 %w[
Yibo Caid83ad152024-09-30 14:04:22 -070084 utf8_range.h utf8_range.c utf8_range_sse.inc utf8_range_neon.inc LICENSE
Jason Lunnc52d80c2023-07-24 23:13:47 -070085 ].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
Isaiah Peng27e2b572014-12-24 15:48:41 +010092if RUBY_PLATFORM == "java"
Jason Lunn3581d852021-10-03 18:25:43 -040093 task :clean => :require_mvn do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050094 system("mvn --batch-mode clean")
Isaiah Peng27e2b572014-12-24 15:48:41 +010095 end
96
Jason Lunn3581d852021-10-03 18:25:43 -040097 task :compile => :require_mvn do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050098 system("mvn --batch-mode package")
Isaiah Peng27e2b572014-12-24 15:48:41 +010099 end
Jason Lunn3581d852021-10-03 18:25:43 -0400100
101 task :require_mvn do
102 raise ArgumentError, "maven needs to be installed" if `which mvn` == ''
103 end
104
Isaiah Peng27e2b572014-12-24 15:48:41 +0100105else
106 Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
Jisi Liu9972e162018-04-25 09:52:30 -0700107 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
Isaiah Peng27e2b572014-12-24 15:48:41 +0100113 ext.ext_dir = "ext/google/protobuf_c"
114 ext.lib_dir = "lib/google"
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100115 ext.cross_compile = true
116 ext.cross_platform = [
johnnyshields92548532022-05-14 02:50:29 +0900117 'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt',
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100118 'x86_64-linux', 'x86-linux',
Masaki Hara0a7a9a92021-01-25 15:04:50 +0900119 'x86_64-darwin', 'arm64-darwin',
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100120 ]
なつきc17de662024-01-23 14:58:45 -0800121
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
Isaiah Peng27e2b572014-12-24 15:48:41 +0100128 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800129
Jason Lunn3581d852021-10-03 18:25:43 -0400130 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
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800144 task 'gem:windows' do
Jason Lunn3581d852021-10-03 18:25:43 -0400145 sh "rm Gemfile.lock"
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800146 require 'rake_compiler_dock'
johnnyshields92548532022-05-14 02:50:29 +0900147 ['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat|
Masaki Hara64f6c592020-04-17 15:31:47 +0900148 RakeCompilerDock.sh <<-"EOT", platform: plat
149 bundle && \
Jason Lunnc52d80c2023-07-24 23:13:47 -0700150 IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0
Masaki Hara64f6c592020-04-17 15:31:47 +0900151 EOT
152 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800153 end
Isaiah Peng27e2b572014-12-24 15:48:41 +0100154
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200155 if RUBY_PLATFORM =~ /darwin/
156 task 'gem:native' do
Nicolas "Pixel" Nobleedd29492016-05-04 02:29:17 +0200157 system "rake genproto"
Jason Lunnc52d80c2023-07-24 23:13:47 -0700158 system "rake cross native gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0"
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200159 end
160 else
Jason Lunn3581d852021-10-03 18:25:43 -0400161 task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200162 end
Josh Haberman513875d2016-03-03 14:08:54 -0800163end
164
Josh Haberman513875d2016-03-03 14:08:54 -0800165task :genproto => genproto_output
166
167task :clean do
168 sh "rm -f #{genproto_output.join(' ')}"
Jason Lunnc52d80c2023-07-24 23:13:47 -0700169 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
Josh Haberman513875d2016-03-03 14:08:54 -0800177end
178
Isaiah Peng27e2b572014-12-24 15:48:41 +0100179Gem::PackageTask.new(spec) do |pkg|
Chris Fallin973f4252014-11-18 14:19:58 -0800180end
181
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700182# 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|
Harshit Choprad0535cc2017-08-25 12:11:15 -0700185 t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
Paul Yangcd5f49d2017-10-03 17:28:49 -0700186end
187
188# gc_test needs to be split out to ensure the generated file hasn't been
189# imported by other tests.
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700190Rake::TestTask.new(:gc_test => ENV['BAZEL'] == 'true' ? [] : :build) do |t|
Paul Yangcd5f49d2017-10-03 17:28:49 -0700191 t.test_files = FileList["tests/gc_test.rb"]
Chris Fallin973f4252014-11-18 14:19:58 -0800192end
193
Jason Lunnc52d80c2023-07-24 23:13:47 -0700194task :build => [:clean, :genproto, :copy_third_party, :compile, :"ffi-protobuf:default"]
195Rake::Task[:gem].enhance [:copy_third_party, :genproto]
Chris Fallin973f4252014-11-18 14:19:58 -0800196task :default => [:build]
197
198# vim:sw=2:et