Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 1 | require "rubygems" |
| 2 | require "rubygems/package_task" |
| 3 | require "rake/extensiontask" unless RUBY_PLATFORM == "java" |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 4 | require "rake/testtask" |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 5 | import 'lib/google/tasks/ffi.rake' |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 6 | |
Chris Fallin | 91473dc | 2014-12-12 15:58:26 -0800 | [diff] [blame] | 7 | spec = Gem::Specification.load("google-protobuf.gemspec") |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 8 | |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 9 | well_known_protos = %w[ |
| 10 | google/protobuf/any.proto |
| 11 | google/protobuf/api.proto |
Joshua Haberman | 16e16eb | 2021-07-31 11:13:08 -0700 | [diff] [blame] | 12 | google/protobuf/descriptor.proto |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 13 | google/protobuf/duration.proto |
| 14 | google/protobuf/empty.proto |
| 15 | google/protobuf/field_mask.proto |
| 16 | google/protobuf/source_context.proto |
| 17 | google/protobuf/struct.proto |
| 18 | google/protobuf/timestamp.proto |
| 19 | google/protobuf/type.proto |
| 20 | google/protobuf/wrappers.proto |
| 21 | ] |
| 22 | |
Joshua Haberman | dd69a48 | 2021-05-17 22:40:33 -0700 | [diff] [blame] | 23 | test_protos = %w[ |
| 24 | tests/basic_test.proto |
Mike Kruskal | bca8fb6 | 2024-03-15 15:15:45 -0700 | [diff] [blame] | 25 | tests/basic_test_features.proto |
Joshua Haberman | dd69a48 | 2021-05-17 22:40:33 -0700 | [diff] [blame] | 26 | tests/basic_test_proto2.proto |
| 27 | tests/generated_code.proto |
| 28 | tests/generated_code_proto2.proto |
Mike Kruskal | bca8fb6 | 2024-03-15 15:15:45 -0700 | [diff] [blame] | 29 | tests/generated_code_editions.proto |
Joshua Haberman | dd69a48 | 2021-05-17 22:40:33 -0700 | [diff] [blame] | 30 | tests/multi_level_nesting_test.proto |
Joshua Haberman | 5cfc9e7 | 2024-01-03 13:03:49 -0800 | [diff] [blame] | 31 | tests/repeated_field_test.proto |
KJ Tsanaktsidis | 2082ee8 | 2024-03-22 10:32:08 -0700 | [diff] [blame] | 32 | tests/service_test.proto |
Joshua Haberman | 5cfc9e7 | 2024-01-03 13:03:49 -0800 | [diff] [blame] | 33 | tests/stress.proto |
Joshua Haberman | dd69a48 | 2021-05-17 22:40:33 -0700 | [diff] [blame] | 34 | tests/test_import.proto |
| 35 | tests/test_import_proto2.proto |
| 36 | tests/test_ruby_package.proto |
| 37 | tests/test_ruby_package_proto2.proto |
Joshua Haberman | b07e4b1 | 2024-07-17 08:05:12 -0700 | [diff] [blame] | 38 | tests/utf8.proto |
Joshua Haberman | dd69a48 | 2021-05-17 22:40:33 -0700 | [diff] [blame] | 39 | ] |
| 40 | |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 41 | # These are omitted for now because we don't support proto2. |
| 42 | proto2_protos = %w[ |
| 43 | google/protobuf/descriptor.proto |
| 44 | google/protobuf/compiler/plugin.proto |
| 45 | ] |
| 46 | |
Adam Cozzette | 9c8c3de | 2022-04-27 10:40:01 -0700 | [diff] [blame] | 47 | if !ENV['PROTOC'].nil? |
| 48 | protoc_command = ENV['PROTOC'] |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 49 | elsif system('../bazel-bin/protoc --version') |
| 50 | protoc_command = '../bazel-bin/protoc' |
James D | 68cb69e | 2021-04-12 13:06:44 -0400 | [diff] [blame] | 51 | else |
Adam Cozzette | 9c8c3de | 2022-04-27 10:40:01 -0700 | [diff] [blame] | 52 | protoc_command = 'protoc' |
James D | 68cb69e | 2021-04-12 13:06:44 -0400 | [diff] [blame] | 53 | end |
| 54 | |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 55 | genproto_output = [] |
| 56 | |
Nicolas "Pixel" Noble | 236b939 | 2016-04-30 02:14:18 +0200 | [diff] [blame] | 57 | # We won't have access to .. from within docker, but the proto files |
| 58 | # will be there, thanks to the :genproto rule dependency for gem:native. |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 59 | unless ENV['IN_DOCKER'] == 'true' or ENV['BAZEL'] == 'true' |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 60 | well_known_protos.each do |proto_file| |
| 61 | input_file = "../src/" + proto_file |
Josh Haberman | 4f19797 | 2016-07-25 01:47:50 -0700 | [diff] [blame] | 62 | output_file = "lib/" + proto_file.sub(/\.proto$/, "_pb.rb") |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 63 | genproto_output << output_file |
| 64 | file output_file => input_file do |file_task| |
James D | 68cb69e | 2021-04-12 13:06:44 -0400 | [diff] [blame] | 65 | sh "#{protoc_command} -I../src --ruby_out=lib #{input_file}" |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 66 | end |
| 67 | end |
Joshua Haberman | dd69a48 | 2021-05-17 22:40:33 -0700 | [diff] [blame] | 68 | |
| 69 | test_protos.each do |proto_file| |
| 70 | output_file = proto_file.sub(/\.proto$/, "_pb.rb") |
| 71 | genproto_output << output_file |
| 72 | file output_file => proto_file do |file_task| |
Joshua Haberman | 5cfc9e7 | 2024-01-03 13:03:49 -0800 | [diff] [blame] | 73 | sh "#{protoc_command} -I../src -I./tests --ruby_out=tests #{proto_file}" |
Joshua Haberman | dd69a48 | 2021-05-17 22:40:33 -0700 | [diff] [blame] | 74 | end |
| 75 | end |
Nicolas "Pixel" Noble | 1f8b6da | 2016-04-29 20:00:43 +0200 | [diff] [blame] | 76 | end |
| 77 | |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 78 | task :copy_third_party do |
| 79 | unless File.exist? 'ext/google/protobuf_c/third_party/utf8_range' |
| 80 | FileUtils.mkdir_p 'ext/google/protobuf_c/third_party/utf8_range' |
| 81 | # We need utf8_range in-tree. |
Adam Cozzette | 5ca8400 | 2023-11-08 15:36:33 -0800 | [diff] [blame] | 82 | utf8_root = '../third_party/utf8_range' |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 83 | %w[ |
Yibo Cai | d83ad15 | 2024-09-30 14:04:22 -0700 | [diff] [blame] | 84 | utf8_range.h utf8_range.c utf8_range_sse.inc utf8_range_neon.inc LICENSE |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 85 | ].each do |file| |
| 86 | FileUtils.cp File.join(utf8_root, file), |
| 87 | "ext/google/protobuf_c/third_party/utf8_range" |
| 88 | end |
| 89 | end |
| 90 | end |
| 91 | |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 92 | if RUBY_PLATFORM == "java" |
Jason Lunn | 3581d85 | 2021-10-03 18:25:43 -0400 | [diff] [blame] | 93 | task :clean => :require_mvn do |
Joshua Haberman | f1ce60e | 2016-12-03 11:51:25 -0500 | [diff] [blame] | 94 | system("mvn --batch-mode clean") |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 95 | end |
| 96 | |
Jason Lunn | 3581d85 | 2021-10-03 18:25:43 -0400 | [diff] [blame] | 97 | task :compile => :require_mvn do |
Joshua Haberman | f1ce60e | 2016-12-03 11:51:25 -0500 | [diff] [blame] | 98 | system("mvn --batch-mode package") |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 99 | end |
Jason Lunn | 3581d85 | 2021-10-03 18:25:43 -0400 | [diff] [blame] | 100 | |
| 101 | task :require_mvn do |
| 102 | raise ArgumentError, "maven needs to be installed" if `which mvn` == '' |
| 103 | end |
| 104 | |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 105 | else |
| 106 | Rake::ExtensionTask.new("protobuf_c", spec) do |ext| |
Jisi Liu | 9972e16 | 2018-04-25 09:52:30 -0700 | [diff] [blame] | 107 | unless RUBY_PLATFORM =~ /darwin/ |
| 108 | # TODO: also set "no_native to true" for mac if possible. As is, |
| 109 | # "no_native" can only be set if the RUBY_PLATFORM doing |
| 110 | # cross-compilation is contained in the "ext.cross_platform" array. |
| 111 | ext.no_native = true |
| 112 | end |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 113 | ext.ext_dir = "ext/google/protobuf_c" |
| 114 | ext.lib_dir = "lib/google" |
Nicolas "Pixel" Noble | bbb188a | 2016-02-06 00:55:45 +0100 | [diff] [blame] | 115 | ext.cross_compile = true |
| 116 | ext.cross_platform = [ |
johnnyshields | 9254853 | 2022-05-14 02:50:29 +0900 | [diff] [blame] | 117 | 'x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', |
Nicolas "Pixel" Noble | bbb188a | 2016-02-06 00:55:45 +0100 | [diff] [blame] | 118 | 'x86_64-linux', 'x86-linux', |
Masaki Hara | 0a7a9a9 | 2021-01-25 15:04:50 +0900 | [diff] [blame] | 119 | 'x86_64-darwin', 'arm64-darwin', |
Nicolas "Pixel" Noble | bbb188a | 2016-02-06 00:55:45 +0100 | [diff] [blame] | 120 | ] |
なつき | c17de66 | 2024-01-23 14:58:45 -0800 | [diff] [blame] | 121 | |
| 122 | ext.cross_compiling do |gem_spec| |
| 123 | # rake-compiler would call `spec.extensions.clear` which removes the `Rakefile` extension, |
| 124 | # that `rake` doesn't need to be a runtime dependency for native gems. |
| 125 | gem_spec.dependencies.delete_if { |dependency| dependency.name == 'rake' } |
| 126 | gem_spec.add_development_dependency 'rake', '>= 13' |
| 127 | end |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 128 | end |
Josh Haberman | af4aa9b | 2016-02-04 10:44:22 -0800 | [diff] [blame] | 129 | |
Jason Lunn | 3581d85 | 2021-10-03 18:25:43 -0400 | [diff] [blame] | 130 | task 'gem:java' do |
| 131 | sh "rm Gemfile.lock" |
| 132 | require 'rake_compiler_dock' |
| 133 | # Specify the repo root as the working and mount directory to provide access |
| 134 | # to the java directory |
| 135 | repo_root = File.realdirpath File.join(Dir.pwd, '..') |
| 136 | RakeCompilerDock.sh <<-"EOT", platform: 'jruby', rubyvm: :jruby, mountdir: repo_root, workdir: repo_root |
| 137 | sudo apt-get install maven -y && \ |
| 138 | cd java && mvn install -Dmaven.test.skip=true && cd ../ruby && \ |
| 139 | bundle && \ |
| 140 | IN_DOCKER=true rake compile gem |
| 141 | EOT |
| 142 | end |
| 143 | |
Josh Haberman | af4aa9b | 2016-02-04 10:44:22 -0800 | [diff] [blame] | 144 | task 'gem:windows' do |
Jason Lunn | 3581d85 | 2021-10-03 18:25:43 -0400 | [diff] [blame] | 145 | sh "rm Gemfile.lock" |
Josh Haberman | af4aa9b | 2016-02-04 10:44:22 -0800 | [diff] [blame] | 146 | require 'rake_compiler_dock' |
johnnyshields | 9254853 | 2022-05-14 02:50:29 +0900 | [diff] [blame] | 147 | ['x86-mingw32', 'x64-mingw32', 'x64-mingw-ucrt', 'x86_64-linux', 'x86-linux'].each do |plat| |
Masaki Hara | 64f6c59 | 2020-04-17 15:31:47 +0900 | [diff] [blame] | 148 | RakeCompilerDock.sh <<-"EOT", platform: plat |
| 149 | bundle && \ |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 150 | IN_DOCKER=true rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0 |
Masaki Hara | 64f6c59 | 2020-04-17 15:31:47 +0900 | [diff] [blame] | 151 | EOT |
| 152 | end |
Josh Haberman | af4aa9b | 2016-02-04 10:44:22 -0800 | [diff] [blame] | 153 | end |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 154 | |
Nicolas "Pixel" Noble | 236b939 | 2016-04-30 02:14:18 +0200 | [diff] [blame] | 155 | if RUBY_PLATFORM =~ /darwin/ |
| 156 | task 'gem:native' do |
Nicolas "Pixel" Noble | edd2949 | 2016-05-04 02:29:17 +0200 | [diff] [blame] | 157 | system "rake genproto" |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 158 | system "rake cross native gem RUBY_CC_VERSION=3.1.0:3.0.0:2.7.0" |
Nicolas "Pixel" Noble | 236b939 | 2016-04-30 02:14:18 +0200 | [diff] [blame] | 159 | end |
| 160 | else |
Jason Lunn | 3581d85 | 2021-10-03 18:25:43 -0400 | [diff] [blame] | 161 | task 'gem:native' => [:genproto, 'gem:windows', 'gem:java'] |
Nicolas "Pixel" Noble | 236b939 | 2016-04-30 02:14:18 +0200 | [diff] [blame] | 162 | end |
Josh Haberman | 513875d | 2016-03-03 14:08:54 -0800 | [diff] [blame] | 163 | end |
| 164 | |
Josh Haberman | 513875d | 2016-03-03 14:08:54 -0800 | [diff] [blame] | 165 | task :genproto => genproto_output |
| 166 | |
| 167 | task :clean do |
| 168 | sh "rm -f #{genproto_output.join(' ')}" |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 169 | sh "rm -f google-protobuf-*gem" |
| 170 | sh "rm -f Gemfile.lock" |
| 171 | sh "rm -rf pkg" |
| 172 | sh "rm -rf tmp" |
| 173 | # Handles third_party and any platform specific directories built by FFI |
| 174 | Pathname('ext/google/protobuf_c').children.select(&:directory?).each do |dir| |
| 175 | sh "rm -rf #{dir}" |
| 176 | end |
Josh Haberman | 513875d | 2016-03-03 14:08:54 -0800 | [diff] [blame] | 177 | end |
| 178 | |
Isaiah Peng | 27e2b57 | 2014-12-24 15:48:41 +0100 | [diff] [blame] | 179 | Gem::PackageTask.new(spec) do |pkg| |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 180 | end |
| 181 | |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 182 | # Skip build/genproto in Bazel builds, where we expect this to |
| 183 | # be done already. |
| 184 | Rake::TestTask.new(:test => ENV['BAZEL'] == 'true' ? [] : [:build, :genproto]) do |t| |
Harshit Chopra | d0535cc | 2017-08-25 12:11:15 -0700 | [diff] [blame] | 185 | t.test_files = FileList["tests/*.rb"].exclude("tests/gc_test.rb", "tests/common_tests.rb") |
Paul Yang | cd5f49d | 2017-10-03 17:28:49 -0700 | [diff] [blame] | 186 | end |
| 187 | |
| 188 | # gc_test needs to be split out to ensure the generated file hasn't been |
| 189 | # imported by other tests. |
Mike Kruskal | ed5c57a | 2022-08-10 22:51:29 -0700 | [diff] [blame] | 190 | Rake::TestTask.new(:gc_test => ENV['BAZEL'] == 'true' ? [] : :build) do |t| |
Paul Yang | cd5f49d | 2017-10-03 17:28:49 -0700 | [diff] [blame] | 191 | t.test_files = FileList["tests/gc_test.rb"] |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 192 | end |
| 193 | |
Jason Lunn | c52d80c | 2023-07-24 23:13:47 -0700 | [diff] [blame] | 194 | task :build => [:clean, :genproto, :copy_third_party, :compile, :"ffi-protobuf:default"] |
| 195 | Rake::Task[:gem].enhance [:copy_third_party, :genproto] |
Chris Fallin | 973f425 | 2014-11-18 14:19:58 -0800 | [diff] [blame] | 196 | task :default => [:build] |
| 197 | |
| 198 | # vim:sw=2:et |