Rakefile 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if system('../src/protoc --version')
  24. protoc_command = '../src/protoc'
  25. else
  26. protoc_command = 'protoc'
  27. end
  28. genproto_output = []
  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.
  31. unless 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 "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
  38. end
  39. end
  40. end
  41. if RUBY_PLATFORM == "java"
  42. if `which mvn` == ''
  43. raise ArgumentError, "maven needs to be installed"
  44. end
  45. task :clean do
  46. system("mvn --batch-mode clean")
  47. end
  48. task :compile do
  49. system("mvn --batch-mode package")
  50. end
  51. else
  52. unless ENV['IN_DOCKER'] == 'true'
  53. # We need wyhash in-tree.
  54. FileUtils.mkdir_p("ext/google/protobuf_c/third_party/wyhash")
  55. FileUtils.cp("../third_party/wyhash/wyhash.h", "ext/google/protobuf_c/third_party/wyhash/wyhash.h")
  56. end
  57. Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
  58. unless RUBY_PLATFORM =~ /darwin/
  59. # TODO: also set "no_native to true" for mac if possible. As is,
  60. # "no_native" can only be set if the RUBY_PLATFORM doing
  61. # cross-compilation is contained in the "ext.cross_platform" array.
  62. ext.no_native = true
  63. end
  64. ext.ext_dir = "ext/google/protobuf_c"
  65. ext.lib_dir = "lib/google"
  66. ext.cross_compile = true
  67. ext.cross_platform = [
  68. 'x86-mingw32', 'x64-mingw32',
  69. 'x86_64-linux', 'x86-linux',
  70. 'universal-darwin'
  71. ]
  72. end
  73. task 'gem:windows' do
  74. require 'rake_compiler_dock'
  75. ['x86-mingw32', 'x64-mingw32', 'x86_64-linux', 'x86-linux'].each do |plat|
  76. RakeCompilerDock.sh <<-"EOT", platform: plat
  77. bundle && \
  78. 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
  79. EOT
  80. end
  81. end
  82. if RUBY_PLATFORM =~ /darwin/
  83. task 'gem:native' do
  84. system "rake genproto"
  85. 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"
  86. end
  87. else
  88. task 'gem:native' => [:genproto, 'gem:windows']
  89. end
  90. end
  91. # Proto for tests.
  92. genproto_output << "tests/generated_code.rb"
  93. genproto_output << "tests/generated_code_proto2.rb"
  94. genproto_output << "tests/test_import.rb"
  95. genproto_output << "tests/test_import_proto2.rb"
  96. genproto_output << "tests/test_ruby_package.rb"
  97. genproto_output << "tests/test_ruby_package_proto2.rb"
  98. genproto_output << "tests/basic_test.rb"
  99. genproto_output << "tests/basic_test_proto2.rb"
  100. genproto_output << "tests/multi_level_nesting_test.rb"
  101. genproto_output << "tests/wrappers.rb"
  102. file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
  103. sh "#{protoc_command} --ruby_out=. tests/generated_code.proto"
  104. end
  105. file "tests/generated_code_proto2.rb" => "tests/generated_code_proto2.proto" do |file_task|
  106. sh "#{protoc_command} --ruby_out=. tests/generated_code_proto2.proto"
  107. end
  108. file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
  109. sh "#{protoc_command} --ruby_out=. tests/test_import.proto"
  110. end
  111. file "tests/test_import_proto2.rb" => "tests/test_import_proto2.proto" do |file_task|
  112. sh "#{protoc_command} --ruby_out=. tests/test_import_proto2.proto"
  113. end
  114. file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task|
  115. sh "#{protoc_command} --ruby_out=. tests/test_ruby_package.proto"
  116. end
  117. file "tests/test_ruby_package_proto2.rb" => "tests/test_ruby_package_proto2.proto" do |file_task|
  118. sh "#{protoc_command} --ruby_out=. tests/test_ruby_package_proto2.proto"
  119. end
  120. file "tests/basic_test.rb" => "tests/basic_test.proto" do |file_task|
  121. sh "#{protoc_command} --experimental_allow_proto3_optional -I../src -I. --ruby_out=. tests/basic_test.proto"
  122. end
  123. file "tests/basic_test_proto2.rb" => "tests/basic_test_proto2.proto" do |file_task|
  124. sh "#{protoc_command} -I../src -I. --ruby_out=. tests/basic_test_proto2.proto"
  125. end
  126. file "tests/multi_level_nesting_test.rb" => "tests/multi_level_nesting_test.proto" do |file_task|
  127. sh "#{protoc_command} -I../src -I. --ruby_out=. tests/multi_level_nesting_test.proto"
  128. end
  129. file "tests/wrappers.rb" => "../src/google/protobuf/wrappers.proto" do |file_task|
  130. sh "#{protoc_command} -I../src -I. --ruby_out=tests ../src/google/protobuf/wrappers.proto"
  131. end
  132. task :genproto => genproto_output
  133. task :clean do
  134. sh "rm -f #{genproto_output.join(' ')}"
  135. end
  136. Gem::PackageTask.new(spec) do |pkg|
  137. end
  138. Rake::TestTask.new(:test => :build) do |t|
  139. t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
  140. end
  141. # gc_test needs to be split out to ensure the generated file hasn't been
  142. # imported by other tests.
  143. Rake::TestTask.new(:gc_test => :build) do |t|
  144. t.test_files = FileList["tests/gc_test.rb"]
  145. end
  146. task :build => [:clean, :compile, :genproto]
  147. task :default => [:build]
  148. # vim:sw=2:et