blob: d8ac763e0974cdbfb17c0e7c6dfad944d2dea98e [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']
42elsif system('../src/protoc --version')
43 protoc_command = '../src/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.
Nicolas "Pixel" Noble1f8b6da2016-04-29 20:00:43 +020052unless ENV['IN_DOCKER'] == 'true'
53 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.
87 FileUtils.mkdir_p("ext/google/protobuf_c/third_party/utf8_range")
88 FileUtils.cp("../third_party/utf8_range/utf8_range.h", "ext/google/protobuf_c/third_party/utf8_range")
Joshua Haberman349d74d2022-03-05 11:55:57 -080089 FileUtils.cp("../third_party/utf8_range/naive.c", "ext/google/protobuf_c/third_party/utf8_range")
90 FileUtils.cp("../third_party/utf8_range/range2-neon.c", "ext/google/protobuf_c/third_party/utf8_range")
91 FileUtils.cp("../third_party/utf8_range/range2-sse.c", "ext/google/protobuf_c/third_party/utf8_range")
Joshua Habermanc153dd92022-01-21 15:46:56 -080092 FileUtils.cp("../third_party/utf8_range/LICENSE", "ext/google/protobuf_c/third_party/utf8_range")
93 end
94
Isaiah Peng27e2b572014-12-24 15:48:41 +010095 Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
Jisi Liu9972e162018-04-25 09:52:30 -070096 unless RUBY_PLATFORM =~ /darwin/
97 # TODO: also set "no_native to true" for mac if possible. As is,
98 # "no_native" can only be set if the RUBY_PLATFORM doing
99 # cross-compilation is contained in the "ext.cross_platform" array.
100 ext.no_native = true
101 end
Isaiah Peng27e2b572014-12-24 15:48:41 +0100102 ext.ext_dir = "ext/google/protobuf_c"
103 ext.lib_dir = "lib/google"
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100104 ext.cross_compile = true
105 ext.cross_platform = [
johnnyshieldsabe77ae2022-05-14 11:41:32 +0900106 'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt',
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100107 'x86_64-linux', 'x86-linux',
Masaki Hara0a7a9a92021-01-25 15:04:50 +0900108 'x86_64-darwin', 'arm64-darwin',
Nicolas "Pixel" Noblebbb188a2016-02-06 00:55:45 +0100109 ]
Isaiah Peng27e2b572014-12-24 15:48:41 +0100110 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800111
Jason Lunn3581d852021-10-03 18:25:43 -0400112 task 'gem:java' do
113 sh "rm Gemfile.lock"
114 require 'rake_compiler_dock'
115 # Specify the repo root as the working and mount directory to provide access
116 # to the java directory
117 repo_root = File.realdirpath File.join(Dir.pwd, '..')
118 RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root
119 sudo apt-get install maven -y && \
120 cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \
121 bundle && \
122 IN_DOCKER=true rake compile gem
123 EOT
124 end
125
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800126 task 'gem:windows' do
Jason Lunn3581d852021-10-03 18:25:43 -0400127 sh "rm Gemfile.lock"
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800128 require 'rake_compiler_dock'
johnnyshieldsabe77ae2022-05-14 11:41:32 +0900129 ['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat|
Masaki Hara64f6c592020-04-17 15:31:47 +0900130 RakeCompilerDock.sh <<-"EOT", platform: plat
131 bundle && \
Marco Concetto Rudilossoabdfd092022-03-17 16:38:36 +0000132 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:2.5.0
Masaki Hara64f6c592020-04-17 15:31:47 +0900133 EOT
134 end
Josh Habermanaf4aa9b2016-02-04 10:44:22 -0800135 end
Isaiah Peng27e2b572014-12-24 15:48:41 +0100136
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200137 if RUBY_PLATFORM =~ /darwin/
138 task 'gem:native' do
Nicolas "Pixel" Nobleedd29492016-05-04 02:29:17 +0200139 system "rake genproto"
Marco Concetto Rudilossoabdfd092022-03-17 16:38:36 +0000140 system "rake cross native gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0:2.6.0:2.5.1"
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200141 end
142 else
Jason Lunn3581d852021-10-03 18:25:43 -0400143 task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
Nicolas "Pixel" Noble236b9392016-04-30 02:14:18 +0200144 end
Josh Haberman513875d2016-03-03 14:08:54 -0800145end
146
Josh Haberman513875d2016-03-03 14:08:54 -0800147task :genproto => genproto_output
148
149task :clean do
150 sh "rm -f #{genproto_output.join(' ')}"
151end
152
Isaiah Peng27e2b572014-12-24 15:48:41 +0100153Gem::PackageTask.new(spec) do |pkg|
Chris Fallin973f4252014-11-18 14:19:58 -0800154end
155
Joshua Habermandd69a482021-05-17 22:40:33 -0700156Rake::TestTask.new(:test => [:build, :genproto]) do |t|
Harshit Choprad0535cc2017-08-25 12:11:15 -0700157 t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
Paul Yangcd5f49d2017-10-03 17:28:49 -0700158end
159
160# gc_test needs to be split out to ensure the generated file hasn't been
161# imported by other tests.
162Rake::TestTask.new(:gc_test => :build) do |t|
163 t.test_files = FileList["tests/gc_test.rb"]
Chris Fallin973f4252014-11-18 14:19:58 -0800164end
165
Joshua Habermandd69a482021-05-17 22:40:33 -0700166task :build => [:clean, :genproto, :compile]
Chris Fallin973f4252014-11-18 14:19:58 -0800167task :default => [:build]
168
169# vim:sw=2:et