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$/, ".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 clean")
48  end
49
50  task :compile do
51    system("mvn package")
52  end
53else
54  Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
55    ext.ext_dir = "ext/google/protobuf_c"
56    ext.lib_dir = "lib/google"
57    ext.cross_compile = true
58    ext.cross_platform = [
59      'x86-mingw32', 'x64-mingw32',
60      'x86_64-linux', 'x86-linux',
61      'universal-darwin'
62    ]
63  end
64
65  task 'gem:windows' do
66    require 'rake_compiler_dock'
67    RakeCompilerDock.sh "bundle && IN_DOCKER=true rake cross native gem RUBY_CC_VERSION=2.3.0:2.2.2:2.1.5:2.0.0"
68  end
69
70  if RUBY_PLATFORM =~ /darwin/
71    task 'gem:native' do
72      system "rake genproto"
73      system "rake cross native gem RUBY_CC_VERSION=2.3.0:2.2.2:2.1.5:2.0.0"
74    end
75  else
76    task 'gem:native' => [:genproto, 'gem:windows']
77  end
78end
79
80
81# Proto for tests.
82genproto_output << "tests/generated_code.rb"
83file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
84  sh "../src/protoc --ruby_out=. tests/generated_code.proto"
85end
86
87task :genproto => genproto_output
88
89task :clean do
90  sh "rm -f #{genproto_output.join(' ')}"
91end
92
93Gem::PackageTask.new(spec) do |pkg|
94end
95
96Rake::TestTask.new(:test => :build) do |t|
97  t.test_files = FileList["tests/*.rb"]
98end
99
100task :build => [:clean, :compile, :genproto]
101task :default => [:build]
102
103# vim:sw=2:et
104