Rakefile 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
  72. unless RUBY_PLATFORM =~ /darwin/
  73. # TODO: also set "no_native to true" for mac if possible. As is,
  74. # "no_native" can only be set if the RUBY_PLATFORM doing
  75. # cross-compilation is contained in the "ext.cross_platform" array.
  76. ext.no_native = true
  77. end
  78. ext.ext_dir = "ext/google/protobuf_c"
  79. ext.lib_dir = "lib/google"
  80. ext.cross_compile = true
  81. ext.cross_platform = [
  82. 'x86-mingw32', 'x64-mingw32',
  83. 'x86_64-linux', 'x86-linux',
  84. 'x86_64-darwin', 'arm64-darwin',
  85. ]
  86. end
  87. task 'gem:java' do
  88. sh "rm Gemfile.lock"
  89. require 'rake_compiler_dock'
  90. # Specify the repo root as the working and mount directory to provide access
  91. # to the java directory
  92. repo_root = File.realdirpath File.join(Dir.pwd, '..')
  93. RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root
  94. sudo apt-get install maven -y && \
  95. cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \
  96. bundle && \
  97. IN_DOCKER=true rake compile gem
  98. EOT
  99. end
  100. task 'gem:windows' do
  101. sh "rm Gemfile.lock"
  102. require 'rake_compiler_dock'
  103. ['x86-mingw32', 'x64-mingw32', 'x86_64-linux', 'x86-linux'].each do |plat|
  104. RakeCompilerDock.sh <<-"EOT", platform: plat
  105. bundle && \
  106. 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
  107. EOT
  108. end
  109. end
  110. if RUBY_PLATFORM =~ /darwin/
  111. task 'gem:native' do
  112. system "rake genproto"
  113. system "rake cross native gem RUBY_CC_VERSION=3.0.0:2.7.0:2.6.0:2.5.1"
  114. end
  115. else
  116. task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
  117. end
  118. end
  119. task :genproto => genproto_output
  120. task :clean do
  121. sh "rm -f #{genproto_output.join(' ')}"
  122. end
  123. Gem::PackageTask.new(spec) do |pkg|
  124. end
  125. Rake::TestTask.new(:test => [:build, :genproto]) do |t|
  126. t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
  127. end
  128. # gc_test needs to be split out to ensure the generated file hasn't been
  129. # imported by other tests.
  130. Rake::TestTask.new(:gc_test => :build) do |t|
  131. t.test_files = FileList["tests/gc_test.rb"]
  132. end
  133. task :build => [:clean, :genproto, :compile]
  134. task :default => [:build]
  135. # vim:sw=2:et