-
Notifications
You must be signed in to change notification settings - Fork 15.7k
/
Copy pathRakefile
213 lines (186 loc) · 6.86 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require "rubygems"
require "rubygems/package_task"
require "rake/extensiontask" unless RUBY_PLATFORM == "java"
require "rake/testtask"
import 'lib/google/tasks/ffi.rake'
spec = Gem::Specification.load("google-protobuf.gemspec")
well_known_protos = %w[
google/protobuf/any.proto
google/protobuf/api.proto
google/protobuf/descriptor.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
google/protobuf/compiler/plugin.proto
]
test_protos = %w[
tests/basic_test.proto
tests/basic_test_features.proto
tests/basic_test_proto2.proto
tests/generated_code.proto
tests/generated_code_proto2.proto
tests/generated_code_editions.proto
tests/multi_level_nesting_test.proto
tests/repeated_field_test.proto
tests/service_test.proto
tests/stress.proto
tests/test_import.proto
tests/test_import_proto2.proto
tests/test_ruby_package.proto
tests/test_ruby_package_proto2.proto
tests/utf8.proto
]
if !ENV['PROTOC'].nil?
protoc_command = ENV['PROTOC']
elsif system('../bazel-bin/protoc --version')
protoc_command = '../bazel-bin/protoc'
else
protoc_command = 'protoc'
end
tmp_protoc_out = "tmp/protoc-out"
directory tmp_protoc_out
genproto_output = []
# 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.
unless ENV['IN_DOCKER'] == 'true' or ENV['BAZEL'] == 'true'
well_known_protos.each do |proto_file|
input_file = File.join("../src/", proto_file)
output_basename = File.basename(proto_file).sub(/\.proto$/, "_pb.rb")
tmp_output_file = File.join(tmp_protoc_out, File.dirname(proto_file), output_basename)
# Generated _pb.rb for files in subdirectories of google/protobuf
# (eg: ../src/google/protobuf/compiler/plugin.proto) should all still end up
# in lib/google/protobuf.
output_file = File.join("lib/google/protobuf", output_basename)
genproto_output << output_file
file output_file => [input_file, tmp_protoc_out] do |file_task|
sh "#{protoc_command} -I../src --ruby_out=#{tmp_protoc_out} #{input_file}"
FileUtils.cp tmp_output_file, output_file
end
end
test_protos.each do |proto_file|
output_file = proto_file.sub(/\.proto$/, "_pb.rb")
genproto_output << output_file
file output_file => proto_file do |file_task|
sh "#{protoc_command} -I../src -I./tests --ruby_out=tests #{proto_file}"
end
end
end
task :copy_third_party do
unless File.exist? 'ext/google/protobuf_c/third_party/utf8_range'
FileUtils.mkdir_p 'ext/google/protobuf_c/third_party/utf8_range'
# We need utf8_range in-tree.
utf8_root = '../third_party/utf8_range'
%w[
utf8_range.h utf8_range.c utf8_range_sse.inc utf8_range_neon.inc LICENSE
].each do |file|
FileUtils.cp File.join(utf8_root, file),
"ext/google/protobuf_c/third_party/utf8_range"
end
end
end
if RUBY_PLATFORM == "java"
task :clean => :require_mvn do
system("mvn --batch-mode clean")
end
task :compile => :require_mvn do
system("mvn --batch-mode package")
end
task :require_mvn do
raise ArgumentError, "maven needs to be installed" if `which mvn` == ''
end
else
Rake::ExtensionTask.new("protobuf_c", spec) do |ext|
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
ext.ext_dir = "ext/google/protobuf_c"
ext.lib_dir = "lib/google"
ext.cross_compile = true
ext.cross_platform = [
'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt',
'x86_64-linux', 'x86-linux',
'x86_64-darwin', 'arm64-darwin',
]
ext.cross_compiling do |gem_spec|
# rake-compiler would call `spec.extensions.clear` which removes the `Rakefile` extension,
# that `rake` doesn't need to be a runtime dependency for native gems.
gem_spec.dependencies.delete_if { |dependency| dependency.name == 'rake' }
gem_spec.add_development_dependency 'rake', '>= 13'
end
end
task 'gem:java' do
sh "rm Gemfile.lock"
require 'rake_compiler_dock'
# Specify the repo root as the working and mount directory to provide access
# to the java directory
repo_root = File.realdirpath File.join(Dir.pwd, '..')
RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root
sudo apt-get install maven -y && \
cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \
bundle && \
IN_DOCKER=true rake compile gem
EOT
end
task 'gem:windows' do
sh "rm Gemfile.lock"
require 'rake_compiler_dock'
['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat|
RakeCompilerDock.sh <<-"EOT", platform: plat
bundle && \
IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0
EOT
end
end
if RUBY_PLATFORM =~ /darwin/
task 'gem:native' do
system "rake genproto"
system "rake cross native gem RUBY_CC_VERSION=3.1.0"
end
else
task 'gem:native' => [:genproto, 'gem:windows', 'gem:java']
end
end
task :genproto => genproto_output
task :clean do
sh "rm -f #{genproto_output.join(' ')}"
sh "rm -f google-protobuf-*gem"
sh "rm -f Gemfile.lock"
sh "rm -rf pkg"
sh "rm -rf tmp"
# Handles third_party and any platform specific directories built by FFI
Pathname('ext/google/protobuf_c').children.select(&:directory?).each do |dir|
sh "rm -rf #{dir}"
end
end
Gem::PackageTask.new(spec) do |pkg|
# When packaging the gem via `rake gem`, we only want to define
# "ext/google/protobuf_c/extconf.rb" as the extension to build the C
# extension.
pkg.gem_spec.extensions = pkg.gem_spec.extensions.map do |extension|
extension == "Rakefile" ? "ext/google/protobuf_c/extconf.rb" : extension
end
pkg.gem_spec.files.reject! { |f| f == "Rakefile" }
pkg.package_files.exclude("Rakefile")
end
# Skip build/genproto in Bazel builds, where we expect this to
# be done already.
Rake::TestTask.new(:test => ENV['BAZEL'] == 'true' ? [] : [:build, :genproto]) do |t|
t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb")
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 => ENV['BAZEL'] == 'true' ? [] : :build) do |t|
t.test_files = FileList["tests/gc_test.rb"]
end
task :build => [:clean, :genproto, :copy_third_party, :compile, :"ffi-protobuf:default"]
Rake::Task[:gem].enhance [:copy_third_party, :genproto]
task :default => [:build]
# vim:sw=2:et