1require "rubygems" 2require "rubygems/package_task" 3require "rake/extensiontask" unless RUBY_PLATFORM == "java" 4require "rake/testtask" 5 6spec = Gem::Specification.load("google-protobuf.gemspec") 7 8well_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 29# 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. 31unless ENV['IN_DOCKER'] == 'true' 32 well_known_protos.each do |proto_file| 33 input_file = "../src/" + proto_file 34 output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb") 35 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 42if RUBY_PLATFORM == "java" 43 if `which mvn` == '' 44 raise ArgumentError, "maven needs to be installed" 45 end 46 task :clean do 47 system("mvn --batch-mode clean") 48 end 49 50 task :compile do 51 system("mvn --batch-mode package") 52 end 53else 54 Rake::ExtensionTask.new("protobuf_c", spec) do |ext| 55 unless RUBY_PLATFORM =~ /darwin/ 56 # TODO: also set "no_native to true" for mac if possible. As is, 57 # "no_native" can only be set if the RUBY_PLATFORM doing 58 # cross-compilation is contained in the "ext.cross_platform" array. 59 ext.no_native = true 60 end 61 ext.ext_dir = "ext/google/protobuf_c" 62 ext.lib_dir = "lib/google" 63 ext.cross_compile = true 64 ext.cross_platform = [ 65 'x86-mingw32', 'x64-mingw32', 66 'x86_64-linux', 'x86-linux', 67 'universal-darwin' 68 ] 69 end 70 71 task 'gem:windows' do 72 require 'rake_compiler_dock' 73 ['x86-mingw32', 'x64-mingw32', 'x86_64-linux', 'x86-linux'].each do |plat| 74 RakeCompilerDock.sh <<-"EOT", platform: plat 75 bundle && \ 76 IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=2.7.0:2.6.0:2.5.0:2.4.0:2.3.0 77 EOT 78 end 79 end 80 81 if RUBY_PLATFORM =~ /darwin/ 82 task 'gem:native' do 83 system "rake genproto" 84 system "rake cross native gem RUBY_CC_VERSION=2.7.0:2.6.0:2.5.1:2.4.0:2.3.0" 85 end 86 else 87 task 'gem:native' => [:genproto, 'gem:windows'] 88 end 89end 90 91 92# Proto for tests. 93genproto_output << "tests/generated_code.rb" 94genproto_output << "tests/generated_code_proto2.rb" 95genproto_output << "tests/test_import.rb" 96genproto_output << "tests/test_import_proto2.rb" 97genproto_output << "tests/test_ruby_package.rb" 98genproto_output << "tests/test_ruby_package_proto2.rb" 99genproto_output << "tests/basic_test.rb" 100genproto_output << "tests/basic_test_proto2.rb" 101genproto_output << "tests/wrappers.rb" 102file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task| 103 sh "../src/protoc --ruby_out=. tests/generated_code.proto" 104end 105 106file "tests/generated_code_proto2.rb" => "tests/generated_code_proto2.proto" do |file_task| 107 sh "../src/protoc --ruby_out=. tests/generated_code_proto2.proto" 108end 109 110file "tests/test_import.rb" => "tests/test_import.proto" do |file_task| 111 sh "../src/protoc --ruby_out=. tests/test_import.proto" 112end 113 114file "tests/test_import_proto2.rb" => "tests/test_import_proto2.proto" do |file_task| 115 sh "../src/protoc --ruby_out=. tests/test_import_proto2.proto" 116end 117 118file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task| 119 sh "../src/protoc --ruby_out=. tests/test_ruby_package.proto" 120end 121 122file "tests/test_ruby_package_proto2.rb" => "tests/test_ruby_package_proto2.proto" do |file_task| 123 sh "../src/protoc --ruby_out=. tests/test_ruby_package_proto2.proto" 124end 125 126file "tests/basic_test.rb" => "tests/basic_test.proto" do |file_task| 127 sh "../src/protoc --experimental_allow_proto3_optional -I../src -I. --ruby_out=. tests/basic_test.proto" 128end 129 130file "tests/basic_test_proto2.rb" => "tests/basic_test_proto2.proto" do |file_task| 131 sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test_proto2.proto" 132end 133 134file "tests/wrappers.rb" => "../src/google/protobuf/wrappers.proto" do |file_task| 135 sh "../src/protoc -I../src -I. --ruby_out=tests ../src/google/protobuf/wrappers.proto" 136end 137 138task :genproto => genproto_output 139 140task :clean do 141 sh "rm -f #{genproto_output.join(' ')}" 142end 143 144Gem::PackageTask.new(spec) do |pkg| 145end 146 147Rake::TestTask.new(:test => :build) do |t| 148 t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb") 149end 150 151# gc_test needs to be split out to ensure the generated file hasn't been 152# imported by other tests. 153Rake::TestTask.new(:gc_test => :build) do |t| 154 t.test_files = FileList["tests/gc_test.rb"] 155end 156 157task :build => [:clean, :compile, :genproto] 158task :default => [:build] 159 160# vim:sw=2:et 161