Rakefile 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/descriptor.proto
  10. google/protobuf/duration.proto
  11. google/protobuf/empty.proto
  12. google/protobuf/field_mask.proto
  13. google/protobuf/source_context.proto
  14. google/protobuf/struct.proto
  15. google/protobuf/timestamp.proto
  16. google/protobuf/type.proto
  17. google/protobuf/wrappers.proto
  18. ]
  19. test_protos = %w[
  20. tests/basic_test.proto
  21. tests/basic_test_proto2.proto
  22. tests/generated_code.proto
  23. tests/generated_code_proto2.proto
  24. tests/multi_level_nesting_test.proto
  25. tests/test_import.proto
  26. tests/test_import_proto2.proto
  27. tests/test_ruby_package.proto
  28. tests/test_ruby_package_proto2.proto
  29. ]
  30. # These are omitted for now because we don't support proto2.
  31. proto2_protos = %w[
  32. google/protobuf/descriptor.proto
  33. google/protobuf/compiler/plugin.proto
  34. ]
  35. if !ENV['PROTOC'].nil?
  36. protoc_command = ENV['PROTOC']
  37. elsif system('../src/protoc --version')
  38. protoc_command = '../src/protoc'
  39. else
  40. protoc_command = 'protoc'
  41. end
  42. genproto_output = []
  43. # We won't have access to .. from within docker, but the proto files
  44. # will be there, thanks to the :genproto rule dependency for gem:native.
  45. unless ENV['IN_DOCKER'] == 'true'
  46. well_known_protos.each do |proto_file|
  47. input_file = "../src/" + proto_file
  48. output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
  49. genproto_output << output_file
  50. file output_file => input_file do |file_task|
  51. sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}"
  52. end
  53. end
  54. test_protos.each do |proto_file|
  55. output_file = proto_file.sub(/\.proto$/, "_pb.rb")
  56. genproto_output << output_file
  57. file output_file => proto_file do |file_task|
  58. sh "#{protoc_command} -I../src -I. -I./tests --ruby_out=. #{proto_file}"
  59. end
  60. end
  61. end
  62. if RUBY_PLATFORM == "java"
  63. task :clean => :require_mvn do
  64. system("mvn --batch-mode clean")
  65. end
  66. task :compile => :require_mvn do
  67. system("mvn --batch-mode package")
  68. end
  69. task :require_mvn do
  70. raise ArgumentError, "maven needs to be installed" if `which mvn` == ''
  71. end
  72. else
  73. unless ENV['IN_DOCKER'] == 'true'
  74. # We need utf8_range in-tree.
  75. FileUtils.mkdir_p("ext/google/protobuf_c/third_party/utf8_range")
  76. FileUtils.cp("../third_party/utf8_range/utf8_range.h", "ext/google/protobuf_c/third_party/utf8_range")
  77. FileUtils.cp("../third_party/utf8_range/naive.c", "ext/google/protobuf_c/third_party/utf8_range")
  78. FileUtils.cp("../third_party/utf8_range/range2-neon.c", "ext/google/protobuf_c/third_party/utf8_range")
  79. FileUtils.cp("../third_party/utf8_range/range2-sse.c", "ext/google/protobuf_c/third_party/utf8_range")
  80. FileUtils.cp("../third_party/utf8_range/LICENSE", "ext/google/protobuf_c/third_party/utf8_range")
  81. end
  82. Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
  83. unless RUBY_PLATFORM =~ /darwin/
  84. # TODO: also set "no_native to true" for mac if possible. As is,
  85. # "no_native" can only be set if the RUBY_PLATFORM doing
  86. # cross-compilation is contained in the "ext.cross_platform" array.
  87. ext.no_native = true
  88. end
  89. ext.ext_dir = "ext/google/protobuf_c"
  90. ext.lib_dir = "lib/google"
  91. ext.cross_compile = true
  92. ext.cross_platform = [
  93. 'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt',
  94. 'x86_64-linux', 'x86-linux',
  95. 'x86_64-darwin', 'arm64-darwin',
  96. ]
  97. end
  98. task 'gem:java' do
  99. sh "rm Gemfile.lock"
  100. require 'rake_compiler_dock'
  101. # Specify the repo root as the working and mount directory to provide access
  102. # to the java directory
  103. repo_root = File.realdirpath File.join(Dir.pwd, '..')
  104. RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root
  105. sudo apt-get install maven -y && \
  106. cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \
  107. bundle && \
  108. IN_DOCKER=true rake compile gem
  109. EOT
  110. end
  111. task 'gem:windows' do
  112. sh "rm Gemfile.lock"
  113. require 'rake_compiler_dock'
  114. ['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat|
  115. RakeCompilerDock.sh <<-"EOT", platform: plat
  116. bundle && \
  117. 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
  118. EOT
  119. end
  120. end
  121. if RUBY_PLATFORM =~ /darwin/
  122. task 'gem:native' do
  123. system "rake genproto"
  124. system "rake cross native gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0:2.6.0:2.5.1"
  125. end
  126. else
  127. task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
  128. end
  129. end
  130. task :genproto => genproto_output
  131. task :clean do
  132. sh "rm -f #{genproto_output.join(' ')}"
  133. end
  134. Gem::PackageTask.new(spec) do |pkg|
  135. end
  136. Rake::TestTask.new(:test => [:build, :genproto]) do |t|
  137. t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
  138. end
  139. # gc_test needs to be split out to ensure the generated file hasn't been
  140. # imported by other tests.
  141. Rake::TestTask.new(:gc_test => :build) do |t|
  142. t.test_files = FileList["tests/gc_test.rb"]
  143. end
  144. task :build => [:clean, :genproto, :compile]
  145. task :default => [:build]
  146. # vim:sw=2:et