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