Rakefile 5.1 KB

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