• 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/duration.proto
12  google/protobuf/empty.proto
13  google/protobuf/field_mask.proto
14  google/protobuf/source_context.proto
15  google/protobuf/struct.proto
16  google/protobuf/timestamp.proto
17  google/protobuf/type.proto
18  google/protobuf/wrappers.proto
19]
20
21# These are omitted for now because we don't support proto2.
22proto2_protos = %w[
23  google/protobuf/descriptor.proto
24  google/protobuf/compiler/plugin.proto
25]
26
27genproto_output = []
28
29# We won't have access to .. from within docker, but the proto files
30# will be there, thanks to the :genproto rule dependency for gem:native.
31unless ENV['IN_DOCKER'] == 'true'
32  well_known_protos.each do |proto_file|
33    input_file = "../src/" + proto_file
34    output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
35    genproto_output << output_file
36    file output_file => input_file do |file_task|
37      sh "../src/protoc -I../src --ruby_out=lib #{input_file}"
38    end
39  end
40end
41
42if RUBY_PLATFORM == "java"
43  if `which mvn` == ''
44    raise ArgumentError, "maven needs to be installed"
45  end
46  task :clean do
47    system("mvn --batch-mode clean")
48  end
49
50  task :compile do
51    system("mvn --batch-mode package")
52  end
53else
54  Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
55    unless RUBY_PLATFORM =~ /darwin/
56      # TODO: also set "no_native to true" for mac if possible. As is,
57      # "no_native" can only be set if the RUBY_PLATFORM doing
58      # cross-compilation is contained in the "ext.cross_platform" array.
59      ext.no_native = true
60    end
61    ext.ext_dir = "ext/google/protobuf_c"
62    ext.lib_dir = "lib/google"
63    ext.cross_compile = true
64    ext.cross_platform = [
65      'x86-mingw32', 'x64-mingw32',
66      'x86_64-linux', 'x86-linux',
67      'universal-darwin'
68    ]
69  end
70
71  task 'gem:windows' do
72    require 'rake_compiler_dock'
73    RakeCompilerDock.sh "bundle && IN_DOCKER=true rake cross native gem RUBY_CC_VERSION=2.6.0:2.5.0:2.4.0:2.3.0"
74  end
75
76  if RUBY_PLATFORM =~ /darwin/
77    task 'gem:native' do
78      system "rake genproto"
79      system "rake cross native gem RUBY_CC_VERSION=2.6.0:2.5.1:2.4.0:2.3.0"
80    end
81  else
82    task 'gem:native' => [:genproto, 'gem:windows']
83  end
84end
85
86
87# Proto for tests.
88genproto_output << "tests/generated_code.rb"
89genproto_output << "tests/generated_code_proto2.rb"
90genproto_output << "tests/test_import.rb"
91genproto_output << "tests/test_import_proto2.rb"
92genproto_output << "tests/test_ruby_package.rb"
93genproto_output << "tests/test_ruby_package_proto2.rb"
94genproto_output << "tests/basic_test.rb"
95genproto_output << "tests/basic_test_proto2.rb"
96genproto_output << "tests/wrappers.rb"
97file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
98  sh "../src/protoc --ruby_out=. tests/generated_code.proto"
99end
100
101file "tests/generated_code_proto2.rb" => "tests/generated_code_proto2.proto" do |file_task|
102  sh "../src/protoc --ruby_out=. tests/generated_code_proto2.proto"
103end
104
105file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
106  sh "../src/protoc --ruby_out=. tests/test_import.proto"
107end
108
109file "tests/test_import_proto2.rb" => "tests/test_import_proto2.proto" do |file_task|
110  sh "../src/protoc --ruby_out=. tests/test_import_proto2.proto"
111end
112
113file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task|
114  sh "../src/protoc --ruby_out=. tests/test_ruby_package.proto"
115end
116
117file "tests/test_ruby_package_proto2.rb" => "tests/test_ruby_package_proto2.proto" do |file_task|
118  sh "../src/protoc --ruby_out=. tests/test_ruby_package_proto2.proto"
119end
120
121file "tests/basic_test.rb" => "tests/basic_test.proto" do |file_task|
122  sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test.proto"
123end
124
125file "tests/basic_test_proto2.rb" => "tests/basic_test_proto2.proto" do |file_task|
126  sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test_proto2.proto"
127end
128
129file "tests/wrappers.rb" => "../src/google/protobuf/wrappers.proto" do |file_task|
130  sh "../src/protoc -I../src -I. --ruby_out=tests ../src/google/protobuf/wrappers.proto"
131end
132
133task :genproto => genproto_output
134
135task :clean do
136  sh "rm -f #{genproto_output.join(' ')}"
137end
138
139Gem::PackageTask.new(spec) do |pkg|
140end
141
142Rake::TestTask.new(:test => :build) do |t|
143  t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
144end
145
146# gc_test needs to be split out to ensure the generated file hasn't been
147# imported by other tests.
148Rake::TestTask.new(:gc_test => :build) do |t|
149  t.test_files = FileList["tests/gc_test.rb"]
150end
151
152task :build => [:clean, :compile, :genproto]
153task :default => [:build]
154
155# vim:sw=2:et
156