blob: 523d4de458b7bb9642e9009cf715bcaba58729a9 [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
James D68cb69e2021-04-12 13:06:44 -040040if system('../src/protoc --version')
41 protoc_command = '../src/protoc'
42else
43 protoc_command = 'protoc'
44end
45
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020046genproto_output = []
47
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +020048# We won't have access to .. from within docker, but the proto files
49# will be there, thanks to the :genproto rule dependency for gem:native.
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020050unless ENV['IN_DOCKER'] == 'true'
51 well_known_protos.each do |proto_file|
52 input_file = "../src/" + proto_file
Josh Haberman4f197972016-07-25 01:47:50 -070053 output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020054 genproto_output << output_file
55 file output_file => input_file do |file_task|
James D68cb69e2021-04-12 13:06:44 -040056 sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020057 end
58 end
Joshua Habermandd69a482021-05-17 22:40:33 -070059
60 test_protos.each do |proto_file|
61 output_file = proto_file.sub(/\.proto$/, "_pb.rb")
62 genproto_output << output_file
63 file output_file => proto_file do |file_task|
64 sh "#{protoc_command} -I../src -I. --ruby_out=. #{proto_file}"
65 end
66 end
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020067end
68
Isaiah Peng27e2b572014-12-24 15:48:41 +010069if RUBY_PLATFORM == "java"
Adam Greened55733c2015-05-01 11:54:29 -070070 if `which mvn` == ''
71 raise ArgumentError, "maven needs to be installed"
72 end
Isaiah Peng27e2b572014-12-24 15:48:41 +010073 task :clean do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050074 system("mvn --batch-mode clean")
Isaiah Peng27e2b572014-12-24 15:48:41 +010075 end
76
77 task :compile do
Joshua Habermanf1ce60e2016-12-03 11:51:25 -050078 system("mvn --batch-mode package")
Isaiah Peng27e2b572014-12-24 15:48:41 +010079 end
80else
Joshua Haberman9abf6e22021-01-13 12:16:25 -080081 unless ENV['IN_DOCKER'] == 'true'
82 # We need wyhash in-tree.
83 FileUtils.mkdir_p("ext/google/protobuf_c/third_party/wyhash")
84 FileUtils.cp("../third_party/wyhash/wyhash.h", "ext/google/protobuf_c/third_party/wyhash/wyhash.h")
85 end
86
Isaiah Peng27e2b572014-12-24 15:48:41 +010087 Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
Jisi Liu9972e162018-04-25 09:52:30 -070088 unless RUBY_PLATFORM =~ /darwin/
89 # TODO: also set "no_native to true" for mac if possible. As is,
90 # "no_native" can only be set if the RUBY_PLATFORM doing
91 # cross-compilation is contained in the "ext.cross_platform" array.
92 ext.no_native = true
93 end
Isaiah Peng27e2b572014-12-24 15:48:41 +010094 ext.ext_dir = "ext/google/protobuf_c"
95 ext.lib_dir = "lib/google"
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +010096 ext.cross_compile = true
97 ext.cross_platform = [
98 'x86-mingw32', 'x64-mingw32',
99 'x86_64-linux', 'x86-linux',
100 'universal-darwin'
101 ]
Isaiah Peng27e2b572014-12-24 15:48:41 +0100102 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800103
104 task 'gem:windows' do
105 require 'rake_compiler_dock'
Masaki Hara64f6c592020-04-17 15:31:47 +0900106 ['x86-mingw32', 'x64-mingw32', 'x86_64-linux', 'x86-linux'].each do |plat|
107 RakeCompilerDock.sh <<-"EOT", platform: plat
108 bundle && \
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800109 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 +0900110 EOT
111 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800112 end
Isaiah Peng27e2b572014-12-24 15:48:41 +0100113
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200114 if RUBY_PLATFORM =~ /darwin/
115 task 'gem:native' do
Nicolas "Pixel" Nobleedd29492016-05-04 02:29:17 +0200116 system "rake genproto"
Joshua Haberman9abf6e22021-01-13 12:16:25 -0800117 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 +0200118 end
119 else
120 task 'gem:native' => [:genproto, 'gem:windows']
121 end
Josh Haberman513875d2016-03-03 14:08:54 -0800122end
123
Josh Haberman513875d2016-03-03 14:08:54 -0800124task :genproto => genproto_output
125
126task :clean do
127 sh "rm -f #{genproto_output.join(' ')}"
128end
129
Isaiah Peng27e2b572014-12-24 15:48:41 +0100130Gem::PackageTask.new(spec) do |pkg|
Chris Fallin973f4252014-11-18 14:19:58 -0800131end
132
Joshua Habermandd69a482021-05-17 22:40:33 -0700133Rake::TestTask.new(:test => [:build, :genproto]) do |t|
Harshit Choprad0535cc2017-08-25 12:11:15 -0700134 t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
Paul Yangcd5f49d2017-10-03 17:28:49 -0700135end
136
137# gc_test needs to be split out to ensure the generated file hasn't been
138# imported by other tests.
139Rake::TestTask.new(:gc_test => :build) do |t|
140 t.test_files = FileList["tests/gc_test.rb"]
Chris Fallin973f4252014-11-18 14:19:58 -0800141end
142
Joshua Habermandd69a482021-05-17 22:40:33 -0700143task :build => [:clean, :genproto, :compile]
Chris Fallin973f4252014-11-18 14:19:58 -0800144task :default => [:build]
145
146# vim:sw=2:et