Rakefile 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. require "rubygems"
  2. require "rubygems/package_task"
  3. require "rake/extensiontask" unless RUBY_PLATFORM == "java"
  4. require "rake/testtask"
  5. spec = Gem::Specification.load("google-protobuf.gemspec")
  6. well_known_protos = %w[
  7. google/protobuf/any.proto
  8. google/protobuf/api.proto
  9. google/protobuf/duration.proto
  10. google/protobuf/empty.proto
  11. google/protobuf/field_mask.proto
  12. google/protobuf/source_context.proto
  13. google/protobuf/struct.proto
  14. google/protobuf/timestamp.proto
  15. google/protobuf/type.proto
  16. google/protobuf/wrappers.proto
  17. ]
  18. # These are omitted for now because we don't support proto2.
  19. proto2_protos = %w[
  20. google/protobuf/descriptor.proto
  21. google/protobuf/compiler/plugin.proto
  22. ]
  23. genproto_output = []
  24. # We won't have access to .. from within docker, but the proto files
  25. # will be there, thanks to the :genproto rule dependency for gem:native.
  26. unless ENV['IN_DOCKER'] == 'true'
  27. well_known_protos.each do |proto_file|
  28. input_file = "../src/" + proto_file
  29. output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
  30. genproto_output << output_file
  31. file output_file => input_file do |file_task|
  32. sh "../src/protoc -I../src --ruby_out=lib #{input_file}"
  33. end
  34. end
  35. end
  36. if RUBY_PLATFORM == "java"
  37. if `which mvn` == ''
  38. raise ArgumentError, "maven needs to be installed"
  39. end
  40. task :clean do
  41. system("mvn --batch-mode clean")
  42. end
  43. task :compile do
  44. system("mvn --batch-mode package")
  45. end
  46. else
  47. Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
  48. unless RUBY_PLATFORM =~ /darwin/
  49. # TODO: also set "no_native to true" for mac if possible. As is,
  50. # "no_native" can only be set if the RUBY_PLATFORM doing
  51. # cross-compilation is contained in the "ext.cross_platform" array.
  52. ext.no_native = true
  53. end
  54. ext.ext_dir = "ext/google/protobuf_c"
  55. ext.lib_dir = "lib/google"
  56. ext.cross_compile = true
  57. ext.cross_platform = [
  58. 'x86-mingw32', 'x64-mingw32',
  59. 'x86_64-linux', 'x86-linux',
  60. 'universal-darwin'
  61. ]
  62. end
  63. task 'gem:windows' do
  64. require 'rake_compiler_dock'
  65. ['x86-mingw32', 'x64-mingw32', 'x86_64-linux', 'x86-linux'].each do |plat|
  66. RakeCompilerDock.sh <<-"EOT", platform: plat
  67. bundle && \
  68. 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
  69. EOT
  70. end
  71. end
  72. if RUBY_PLATFORM =~ /darwin/
  73. task 'gem:native' do
  74. system "rake genproto"
  75. system "rake cross native gem RUBY_CC_VERSION=2.7.0:2.6.0:2.5.1:2.4.0:2.3.0"
  76. end
  77. else
  78. task 'gem:native' => [:genproto, 'gem:windows']
  79. end
  80. end
  81. # Proto for tests.
  82. genproto_output << "tests/generated_code.rb"
  83. genproto_output << "tests/generated_code_proto2.rb"
  84. genproto_output << "tests/test_import.rb"
  85. genproto_output << "tests/test_import_proto2.rb"
  86. genproto_output << "tests/test_ruby_package.rb"
  87. genproto_output << "tests/test_ruby_package_proto2.rb"
  88. genproto_output << "tests/basic_test.rb"
  89. genproto_output << "tests/basic_test_proto2.rb"
  90. genproto_output << "tests/wrappers.rb"
  91. file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
  92. sh "../src/protoc --ruby_out=. tests/generated_code.proto"
  93. end
  94. file "tests/generated_code_proto2.rb" => "tests/generated_code_proto2.proto" do |file_task|
  95. sh "../src/protoc --ruby_out=. tests/generated_code_proto2.proto"
  96. end
  97. file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
  98. sh "../src/protoc --ruby_out=. tests/test_import.proto"
  99. end
  100. file "tests/test_import_proto2.rb" => "tests/test_import_proto2.proto" do |file_task|
  101. sh "../src/protoc --ruby_out=. tests/test_import_proto2.proto"
  102. end
  103. file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task|
  104. sh "../src/protoc --ruby_out=. tests/test_ruby_package.proto"
  105. end
  106. file "tests/test_ruby_package_proto2.rb" => "tests/test_ruby_package_proto2.proto" do |file_task|
  107. sh "../src/protoc --ruby_out=. tests/test_ruby_package_proto2.proto"
  108. end
  109. file "tests/basic_test.rb" => "tests/basic_test.proto" do |file_task|
  110. sh "../src/protoc --experimental_allow_proto3_optional -I../src -I. --ruby_out=. tests/basic_test.proto"
  111. end
  112. file "tests/basic_test_proto2.rb" => "tests/basic_test_proto2.proto" do |file_task|
  113. sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test_proto2.proto"
  114. end
  115. file "tests/wrappers.rb" => "../src/google/protobuf/wrappers.proto" do |file_task|
  116. sh "../src/protoc -I../src -I. --ruby_out=tests ../src/google/protobuf/wrappers.proto"
  117. end
  118. task :genproto => genproto_output
  119. task :clean do
  120. sh "rm -f #{genproto_output.join(' ')}"
  121. end
  122. Gem::PackageTask.new(spec) do |pkg|
  123. end
  124. Rake::TestTask.new(:test => :build) do |t|
  125. t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
  126. end
  127. # gc_test needs to be split out to ensure the generated file hasn't been
  128. # imported by other tests.
  129. Rake::TestTask.new(:gc_test => :build) do |t|
  130. t.test_files = FileList["tests/gc_test.rb"]
  131. end
  132. task :build => [:clean, :compile, :genproto]
  133. task :default => [:build]
  134. # vim:sw=2:et