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