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