blob: 2d020e7173bad72f235d60e6b338a2dd7e5a4245 [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
11 google/protobuf/duration.proto
12 google/protobuf/empty.proto
13 google/protobuf/field_mask.proto
14 google/protobuf/source_context.proto
15 google/protobuf/struct.proto
16 google/protobuf/timestamp.proto
17 google/protobuf/type.proto
18 google/protobuf/wrappers.proto
19]
20
21# These are omitted for now because we don't support proto2.
22proto2_protos = %w[
23 google/protobuf/descriptor.proto
24 google/protobuf/compiler/plugin.proto
25]
26
27genproto_output = []
28
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +020029# We won't have access to .. from within docker, but the proto files
30# will be there, thanks to the :genproto rule dependency for gem:native.
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020031unless ENV['IN_DOCKER'] == 'true'
32 well_known_protos.each do |proto_file|
33 input_file = "../src/" + proto_file
Josh Haberman4f197972016-07-25 01:47:50 -070034 output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020035 genproto_output << output_file
36 file output_file => input_file do |file_task|
37 sh "../src/protoc -I../src --ruby_out=lib #{input_file}"
38 end
39 end
40end
41
Isaiah Peng27e2b572014-12-24 15:48:41 +010042if RUBY_PLATFORM == "java"
Adam Greened55733c2015-05-01 11:54:29 -070043 if `which mvn` == ''
44 raise ArgumentError, "maven needs to be installed"
45 end
Isaiah Peng27e2b572014-12-24 15:48:41 +010046 task :clean do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050047 system("mvn --batch-mode clean")
Isaiah Peng27e2b572014-12-24 15:48:41 +010048 end
49
50 task :compile do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050051 system("mvn --batch-mode package")
Isaiah Peng27e2b572014-12-24 15:48:41 +010052 end
53else
Joshua Haberman9abf6e22021-01-13 12:16:25 -080054 unless ENV['IN_DOCKER'] == 'true'
55 # We need wyhash in-tree.
56 FileUtils.mkdir_p("ext/google/protobuf_c/third_party/wyhash")
57 FileUtils.cp("../third_party/wyhash/wyhash.h", "ext/google/protobuf_c/third_party/wyhash/wyhash.h")
58 end
59
Isaiah Peng27e2b572014-12-24 15:48:41 +010060 Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
Jisi Liu9972e162018-04-25 09:52:30 -070061 unless RUBY_PLATFORM =~ /darwin/
62 # TODO: also set "no_native to true" for mac if possible. As is,
63 # "no_native" can only be set if the RUBY_PLATFORM doing
64 # cross-compilation is contained in the "ext.cross_platform" array.
65 ext.no_native = true
66 end
Isaiah Peng27e2b572014-12-24 15:48:41 +010067 ext.ext_dir = "ext/google/protobuf_c"
68 ext.lib_dir = "lib/google"
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +010069 ext.cross_compile = true
70 ext.cross_platform = [
71 'x86-mingw32', 'x64-mingw32',
72 'x86_64-linux', 'x86-linux',
73 'universal-darwin'
74 ]
Isaiah Peng27e2b572014-12-24 15:48:41 +010075 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -080076
77 task 'gem:windows' do
78 require 'rake_compiler_dock'
Masaki Hara64f6c592020-04-17 15:31:47 +090079 ['x86-mingw32', 'x64-mingw32', 'x86_64-linux', 'x86-linux'].each do |plat|
80 RakeCompilerDock.sh <<-"EOT", platform: plat
81 bundle && \
Joshua Haberman9abf6e22021-01-13 12:16:25 -080082 IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=3.0.0:2.7.0:2.6.0:2.5.0:2.4.0:2.3.0
Masaki Hara64f6c592020-04-17 15:31:47 +090083 EOT
84 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -080085 end
Isaiah Peng27e2b572014-12-24 15:48:41 +010086
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +020087 if RUBY_PLATFORM =~ /darwin/
88 task 'gem:native' do
Nicolas "Pixel" Nobleedd29492016-05-04 02:29:17 +020089 system "rake genproto"
Joshua Haberman9abf6e22021-01-13 12:16:25 -080090 system "rake cross native gem RUBY_CC_VERSION=3.0.0:2.7.0:2.6.0:2.5.1:2.4.0:2.3.0"
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +020091 end
92 else
93 task 'gem:native' => [:genproto, 'gem:windows']
94 end
Josh Haberman513875d2016-03-03 14:08:54 -080095end
96
97
98# Proto for tests.
99genproto_output << "tests/generated_code.rb"
Harshit Choprad0535cc2017-08-25 12:11:15 -0700100genproto_output << "tests/generated_code_proto2.rb"
Josh Haberman6d922332016-07-25 00:29:47 -0700101genproto_output << "tests/test_import.rb"
Harshit Choprad0535cc2017-08-25 12:11:15 -0700102genproto_output << "tests/test_import_proto2.rb"
Paul Yang9ccc3e52018-05-17 17:11:06 -0700103genproto_output << "tests/test_ruby_package.rb"
Harshit Choprad0535cc2017-08-25 12:11:15 -0700104genproto_output << "tests/test_ruby_package_proto2.rb"
105genproto_output << "tests/basic_test.rb"
106genproto_output << "tests/basic_test_proto2.rb"
Joe Bolingere4bbca12019-03-27 09:44:43 -0700107genproto_output << "tests/wrappers.rb"
Josh Haberman513875d2016-03-03 14:08:54 -0800108file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
109 sh "../src/protoc --ruby_out=. tests/generated_code.proto"
110end
111
Harshit Choprad0535cc2017-08-25 12:11:15 -0700112file "tests/generated_code_proto2.rb" => "tests/generated_code_proto2.proto" do |file_task|
113 sh "../src/protoc --ruby_out=. tests/generated_code_proto2.proto"
114end
115
Josh Haberman6d922332016-07-25 00:29:47 -0700116file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
117 sh "../src/protoc --ruby_out=. tests/test_import.proto"
118end
119
Harshit Choprad0535cc2017-08-25 12:11:15 -0700120file "tests/test_import_proto2.rb" => "tests/test_import_proto2.proto" do |file_task|
121 sh "../src/protoc --ruby_out=. tests/test_import_proto2.proto"
122end
123
Paul Yang9ccc3e52018-05-17 17:11:06 -0700124file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task|
125 sh "../src/protoc --ruby_out=. tests/test_ruby_package.proto"
126end
127
Harshit Choprad0535cc2017-08-25 12:11:15 -0700128file "tests/test_ruby_package_proto2.rb" => "tests/test_ruby_package_proto2.proto" do |file_task|
129 sh "../src/protoc --ruby_out=. tests/test_ruby_package_proto2.proto"
130end
131
132file "tests/basic_test.rb" => "tests/basic_test.proto" do |file_task|
Joshua Haberman6b759682020-04-23 12:54:25 -0700133 sh "../src/protoc --experimental_allow_proto3_optional -I../src -I. --ruby_out=. tests/basic_test.proto"
Harshit Choprad0535cc2017-08-25 12:11:15 -0700134end
135
136file "tests/basic_test_proto2.rb" => "tests/basic_test_proto2.proto" do |file_task|
Joe Bolinger7f14ea92019-02-27 20:26:14 -0800137 sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test_proto2.proto"
Harshit Choprad0535cc2017-08-25 12:11:15 -0700138end
139
Joe Bolingere4bbca12019-03-27 09:44:43 -0700140file "tests/wrappers.rb" => "../src/google/protobuf/wrappers.proto" do |file_task|
141 sh "../src/protoc -I../src -I. --ruby_out=tests ../src/google/protobuf/wrappers.proto"
142end
143
Josh Haberman513875d2016-03-03 14:08:54 -0800144task :genproto => genproto_output
145
146task :clean do
147 sh "rm -f #{genproto_output.join(' ')}"
148end
149
Isaiah Peng27e2b572014-12-24 15:48:41 +0100150Gem::PackageTask.new(spec) do |pkg|
Chris Fallin973f4252014-11-18 14:19:58 -0800151end
152
153Rake::TestTask.new(:test => :build) do |t|
Harshit Choprad0535cc2017-08-25 12:11:15 -0700154 t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
Paul Yangcd5f49d2017-10-03 17:28:49 -0700155end
156
157# gc_test needs to be split out to ensure the generated file hasn't been
158# imported by other tests.
159Rake::TestTask.new(:gc_test => :build) do |t|
160 t.test_files = FileList["tests/gc_test.rb"]
Chris Fallin973f4252014-11-18 14:19:58 -0800161end
162
Josh Haberman513875d2016-03-03 14:08:54 -0800163task :build => [:clean, :compile, :genproto]
Chris Fallin973f4252014-11-18 14:19:58 -0800164task :default => [:build]
165
166# vim:sw=2:et