blob: c05e1159ea93bccb8d99bb6f8b10832c227eb4fe [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"
5
Chris Fallin91473dc2014-12-12 15:58:26 -08006spec = Gem::Specification.load("google-protobuf.gemspec")
Chris Fallin973f4252014-11-18 14:19:58 -08007
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +02008well_known_protos = %w[
9 google/protobuf/any.proto
10 google/protobuf/api.proto
Joshua Haberman16e16eb2021-07-31 11:13:08 -070011 google/protobuf/descriptor.proto
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020012 google/protobuf/duration.proto
13 google/protobuf/empty.proto
14 google/protobuf/field_mask.proto
15 google/protobuf/source_context.proto
16 google/protobuf/struct.proto
17 google/protobuf/timestamp.proto
18 google/protobuf/type.proto
19 google/protobuf/wrappers.proto
20]
21
Joshua Habermandd69a482021-05-17 22:40:33 -070022test_protos = %w[
23 tests/basic_test.proto
24 tests/basic_test_proto2.proto
25 tests/generated_code.proto
26 tests/generated_code_proto2.proto
27 tests/multi_level_nesting_test.proto
28 tests/test_import.proto
29 tests/test_import_proto2.proto
30 tests/test_ruby_package.proto
31 tests/test_ruby_package_proto2.proto
32]
33
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020034# These are omitted for now because we don't support proto2.
35proto2_protos = %w[
36 google/protobuf/descriptor.proto
37 google/protobuf/compiler/plugin.proto
38]
39
Adam Cozzette9c8c3de2022-04-27 10:40:01 -070040if !ENV['PROTOC'].nil?
41 protoc_command = ENV['PROTOC']
Mike Kruskaled5c57a2022-08-10 22:51:29 -070042elsif system('../bazel-bin/protoc --version')
43 protoc_command = '../bazel-bin/protoc'
James D68cb69e2021-04-12 13:06:44 -040044else
Adam Cozzette9c8c3de2022-04-27 10:40:01 -070045 protoc_command = 'protoc'
James D68cb69e2021-04-12 13:06:44 -040046end
47
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020048genproto_output = []
49
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +020050# We won't have access to .. from within docker, but the proto files
51# will be there, thanks to the :genproto rule dependency for gem:native.
Mike Kruskaled5c57a2022-08-10 22:51:29 -070052unless ENV['IN_DOCKER'] == 'true' or ENV['BAZEL'] == 'true'
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020053 well_known_protos.each do |proto_file|
54 input_file = "../src/" + proto_file
Josh Haberman4f197972016-07-25 01:47:50 -070055 output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020056 genproto_output << output_file
57 file output_file => input_file do |file_task|
James D68cb69e2021-04-12 13:06:44 -040058 sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020059 end
60 end
Joshua Habermandd69a482021-05-17 22:40:33 -070061
62 test_protos.each do |proto_file|
63 output_file = proto_file.sub(/\.proto$/, "_pb.rb")
64 genproto_output << output_file
65 file output_file => proto_file do |file_task|
zhangskz740c4b02021-09-22 09:16:09 -070066 sh "#{protoc_command} -I../src -I. -I./tests --ruby_out=. #{proto_file}"
Joshua Habermandd69a482021-05-17 22:40:33 -070067 end
68 end
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020069end
70
Isaiah Peng27e2b572014-12-24 15:48:41 +010071if RUBY_PLATFORM == "java"
Jason Lunn3581d852021-10-03 18:25:43 -040072 task :clean => :require_mvn do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050073 system("mvn --batch-mode clean")
Isaiah Peng27e2b572014-12-24 15:48:41 +010074 end
75
Jason Lunn3581d852021-10-03 18:25:43 -040076 task :compile => :require_mvn do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050077 system("mvn --batch-mode package")
Isaiah Peng27e2b572014-12-24 15:48:41 +010078 end
Jason Lunn3581d852021-10-03 18:25:43 -040079
80 task :require_mvn do
81 raise ArgumentError, "maven needs to be installed" if `which mvn` == ''
82 end
83
Isaiah Peng27e2b572014-12-24 15:48:41 +010084else
Joshua Habermanc153dd92022-01-21 15:46:56 -080085 unless ENV['IN_DOCKER'] == 'true'
86 # We need utf8_range in-tree.
Mike Kruskalb81def12022-10-10 16:44:44 -070087 if ENV['BAZEL'] == 'true'
88 utf8_root = '../external/utf8_range'
89 else
90 utf8_root = '../third_party/utf8_range'
91 end
Eric Salo3f36a912022-12-05 14:12:25 -080092 FileUtils.mkdir_p("ext/google/protobuf_c")
93 FileUtils.cp(utf8_root+"/utf8_range.h", "ext/google/protobuf_c")
94 FileUtils.cp(utf8_root+"/naive.c", "ext/google/protobuf_c")
95 FileUtils.cp(utf8_root+"/range2-neon.c", "ext/google/protobuf_c")
96 FileUtils.cp(utf8_root+"/range2-sse.c", "ext/google/protobuf_c")
Joshua Habermanc153dd92022-01-21 15:46:56 -080097 end
98
Isaiah Peng27e2b572014-12-24 15:48:41 +010099 Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
Jisi Liu9972e162018-04-25 09:52:30 -0700100 unless RUBY_PLATFORM =~ /darwin/
101 # TODO: also set "no_native to true" for mac if possible. As is,
102 # "no_native" can only be set if the RUBY_PLATFORM doing
103 # cross-compilation is contained in the "ext.cross_platform" array.
104 ext.no_native = true
105 end
Isaiah Peng27e2b572014-12-24 15:48:41 +0100106 ext.ext_dir = "ext/google/protobuf_c"
107 ext.lib_dir = "lib/google"
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100108 ext.cross_compile = true
109 ext.cross_platform = [
johnnyshields92548532022-05-14 02:50:29 +0900110 'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt',
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100111 'x86_64-linux', 'x86-linux',
Masaki Hara0a7a9a92021-01-25 15:04:50 +0900112 'x86_64-darwin', 'arm64-darwin',
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100113 ]
Isaiah Peng27e2b572014-12-24 15:48:41 +0100114 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800115
Jason Lunn3581d852021-10-03 18:25:43 -0400116 task 'gem:java' do
117 sh "rm Gemfile.lock"
118 require 'rake_compiler_dock'
119 # Specify the repo root as the working and mount directory to provide access
120 # to the java directory
121 repo_root = File.realdirpath File.join(Dir.pwd, '..')
122 RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root
123 sudo apt-get install maven -y && \
124 cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \
125 bundle && \
126 IN_DOCKER=true rake compile gem
127 EOT
128 end
129
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800130 task 'gem:windows' do
Jason Lunn3581d852021-10-03 18:25:43 -0400131 sh "rm Gemfile.lock"
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800132 require 'rake_compiler_dock'
johnnyshields92548532022-05-14 02:50:29 +0900133 ['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat|
Masaki Hara64f6c592020-04-17 15:31:47 +0900134 RakeCompilerDock.sh <<-"EOT", platform: plat
135 bundle && \
Deanna Garcia49589712022-12-28 12:24:43 -0800136 IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0:2.6.0
Masaki Hara64f6c592020-04-17 15:31:47 +0900137 EOT
138 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800139 end
Isaiah Peng27e2b572014-12-24 15:48:41 +0100140
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200141 if RUBY_PLATFORM =~ /darwin/
142 task 'gem:native' do
Nicolas "Pixel" Nobleedd29492016-05-04 02:29:17 +0200143 system "rake genproto"
Deanna Garcia49589712022-12-28 12:24:43 -0800144 system "rake cross native gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0:2.6.0"
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200145 end
146 else
Jason Lunn3581d852021-10-03 18:25:43 -0400147 task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200148 end
Josh Haberman513875d2016-03-03 14:08:54 -0800149end
150
Josh Haberman513875d2016-03-03 14:08:54 -0800151task :genproto => genproto_output
152
153task :clean do
154 sh "rm -f #{genproto_output.join(' ')}"
155end
156
Isaiah Peng27e2b572014-12-24 15:48:41 +0100157Gem::PackageTask.new(spec) do |pkg|
Chris Fallin973f4252014-11-18 14:19:58 -0800158end
159
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700160# Skip build/genproto in Bazel builds, where we expect this to
161# be done already.
162Rake::TestTask.new(:test => ENV['BAZEL'] == 'true' ? [] : [:build, :genproto]) do |t|
Harshit Choprad0535cc2017-08-25 12:11:15 -0700163 t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
Paul Yangcd5f49d2017-10-03 17:28:49 -0700164end
165
166# gc_test needs to be split out to ensure the generated file hasn't been
167# imported by other tests.
Mike Kruskaled5c57a2022-08-10 22:51:29 -0700168Rake::TestTask.new(:gc_test => ENV['BAZEL'] == 'true' ? [] : :build) do |t|
Paul Yangcd5f49d2017-10-03 17:28:49 -0700169 t.test_files = FileList["tests/gc_test.rb"]
Chris Fallin973f4252014-11-18 14:19:58 -0800170end
171
Joshua Habermandd69a482021-05-17 22:40:33 -0700172task :build => [:clean, :genproto, :compile]
Chris Fallin973f4252014-11-18 14:19:58 -0800173task :default => [:build]
174
175# vim:sw=2:et