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