Rakefile 4.74 KB
Newer Older
1 2 3
require "rubygems"
require "rubygems/package_task"
require "rake/extensiontask" unless RUBY_PLATFORM == "java"
Chris Fallin's avatar
Chris Fallin committed
4 5
require "rake/testtask"

6
spec = Gem::Specification.load("google-protobuf.gemspec")
Chris Fallin's avatar
Chris Fallin committed
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
well_known_protos = %w[
  google/protobuf/any.proto
  google/protobuf/api.proto
  google/protobuf/duration.proto
  google/protobuf/empty.proto
  google/protobuf/field_mask.proto
  google/protobuf/source_context.proto
  google/protobuf/struct.proto
  google/protobuf/timestamp.proto
  google/protobuf/type.proto
  google/protobuf/wrappers.proto
]

# These are omitted for now because we don't support proto2.
proto2_protos = %w[
  google/protobuf/descriptor.proto
  google/protobuf/compiler/plugin.proto
]

genproto_output = []

29 30
# We won't have access to .. from within docker, but the proto files
# will be there, thanks to the :genproto rule dependency for gem:native.
31 32 33
unless ENV['IN_DOCKER'] == 'true'
  well_known_protos.each do |proto_file|
    input_file = "../src/" + proto_file
34
    output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb")
35 36 37 38 39 40 41
    genproto_output << output_file
    file output_file => input_file do |file_task|
      sh "../src/protoc -I../src --ruby_out=lib #{input_file}"
    end
  end
end

42
if RUBY_PLATFORM == "java"
43 44 45
  if `which mvn` == ''
    raise ArgumentError, "maven needs to be installed"
  end
46
  task :clean do
47
    system("mvn --batch-mode clean")
48
  end
Chris Fallin's avatar
Chris Fallin committed
49

50
  task :compile do
51
    system("mvn --batch-mode package")
52 53 54
  end
else
  Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
55 56 57 58 59 60
    unless RUBY_PLATFORM =~ /darwin/
      # TODO: also set "no_native to true" for mac if possible. As is,
      # "no_native" can only be set if the RUBY_PLATFORM doing
      # cross-compilation is contained in the "ext.cross_platform" array.
      ext.no_native = true
    end
61 62
    ext.ext_dir = "ext/google/protobuf_c"
    ext.lib_dir = "lib/google"
63 64 65 66 67 68
    ext.cross_compile = true
    ext.cross_platform = [
      'x86-mingw32', 'x64-mingw32',
      'x86_64-linux', 'x86-linux',
      'universal-darwin'
    ]
69
  end
70 71 72

  task 'gem:windows' do
    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
Chris Fallin's avatar
Chris Fallin committed
75

76 77
  if RUBY_PLATFORM =~ /darwin/
    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 81 82 83
    end
  else
    task 'gem:native' => [:genproto, 'gem:windows']
  end
84 85 86 87 88
end


# Proto for tests.
genproto_output << "tests/generated_code.rb"
89
genproto_output << "tests/generated_code_proto2.rb"
90
genproto_output << "tests/test_import.rb"
91
genproto_output << "tests/test_import_proto2.rb"
92
genproto_output << "tests/test_ruby_package.rb"
93 94 95
genproto_output << "tests/test_ruby_package_proto2.rb"
genproto_output << "tests/basic_test.rb"
genproto_output << "tests/basic_test_proto2.rb"
96
genproto_output << "tests/wrappers.rb"
97 98 99 100
file "tests/generated_code.rb" => "tests/generated_code.proto" do |file_task|
  sh "../src/protoc --ruby_out=. tests/generated_code.proto"
end

101 102 103 104
file "tests/generated_code_proto2.rb" => "tests/generated_code_proto2.proto" do |file_task|
  sh "../src/protoc --ruby_out=. tests/generated_code_proto2.proto"
end

105 106 107 108
file "tests/test_import.rb" => "tests/test_import.proto" do |file_task|
  sh "../src/protoc --ruby_out=. tests/test_import.proto"
end

109 110 111 112
file "tests/test_import_proto2.rb" => "tests/test_import_proto2.proto" do |file_task|
  sh "../src/protoc --ruby_out=. tests/test_import_proto2.proto"
end

113 114 115 116
file "tests/test_ruby_package.rb" => "tests/test_ruby_package.proto" do |file_task|
  sh "../src/protoc --ruby_out=. tests/test_ruby_package.proto"
end

117 118 119 120 121
file "tests/test_ruby_package_proto2.rb" => "tests/test_ruby_package_proto2.proto" do |file_task|
  sh "../src/protoc --ruby_out=. tests/test_ruby_package_proto2.proto"
end

file "tests/basic_test.rb" => "tests/basic_test.proto" do |file_task|
122
  sh "../src/protoc -I../src -I. --ruby_out=. tests/basic_test.proto"
123 124 125
end

file "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"
127 128
end

129 130 131 132
file "tests/wrappers.rb" => "../src/google/protobuf/wrappers.proto" do |file_task|
  sh "../src/protoc -I../src -I. --ruby_out=tests ../src/google/protobuf/wrappers.proto"
end

133 134 135 136 137 138
task :genproto => genproto_output

task :clean do
  sh "rm -f #{genproto_output.join(' ')}"
end

Chris Fallin's avatar
Chris Fallin committed
139 140 141
Gem::PackageTask.new(spec) do |pkg|
end

142
Rake::TestTask.new(:test => :build) do |t|
143
  t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
144 145 146 147 148 149
end

# gc_test needs to be split out to ensure the generated file hasn't been
# imported by other tests.
Rake::TestTask.new(:gc_test => :build) do |t|
  t.test_files = FileList["tests/gc_test.rb"]
150 151
end

152
task :build => [:clean, :compile, :genproto]
Chris Fallin's avatar
Chris Fallin committed
153 154 155
task :default => [:build]

# vim:sw=2:et