1# -*- ruby -*- 2require 'rake/extensiontask' 3require 'rspec/core/rake_task' 4require 'rubocop/rake_task' 5require 'bundler/gem_tasks' 6require 'fileutils' 7 8require_relative 'build_config.rb' 9 10load 'tools/distrib/rake_compiler_docker_image.rb' 11 12# Add rubocop style checking tasks 13RuboCop::RakeTask.new(:rubocop) do |task| 14 task.options = ['-c', 'src/ruby/.rubocop.yml'] 15 # add end2end tests to formatter but don't add generated proto _pb.rb's 16 task.patterns = ['src/ruby/{lib,spec}/**/*.rb', 'src/ruby/end2end/*.rb'] 17end 18 19spec = Gem::Specification.load('grpc.gemspec') 20 21Gem::PackageTask.new(spec) do |pkg| 22end 23 24# Add the extension compiler task 25Rake::ExtensionTask.new('grpc_c', spec) do |ext| 26 unless RUBY_PLATFORM =~ /darwin/ 27 # TODO: also set "no_native to true" for mac if possible. As is, 28 # "no_native" can only be set if the RUBY_PLATFORM doing 29 # cross-compilation is contained in the "ext.cross_platform" array. 30 ext.no_native = true 31 end 32 ext.source_pattern = '**/*.{c,h}' 33 ext.ext_dir = File.join('src', 'ruby', 'ext', 'grpc') 34 ext.lib_dir = File.join('src', 'ruby', 'lib', 'grpc') 35 ext.cross_compile = true 36 ext.cross_platform = [ 37 'x86-mingw32', 'x64-mingw32', 38 'x86_64-linux', 'x86-linux', 39 'universal-darwin' 40 ] 41 ext.cross_compiling do |spec| 42 spec.files = %w( etc/roots.pem grpc_c.32.ruby grpc_c.64.ruby ) 43 spec.files += Dir.glob('src/ruby/bin/**/*') 44 spec.files += Dir.glob('src/ruby/ext/**/*') 45 spec.files += Dir.glob('src/ruby/lib/**/*') 46 spec.files += Dir.glob('src/ruby/pb/**/*') 47 end 48end 49 50# Define the test suites 51SPEC_SUITES = [ 52 { id: :wrapper, title: 'wrapper layer', files: %w(src/ruby/spec/*.rb) }, 53 { id: :idiomatic, title: 'idiomatic layer', dir: %w(src/ruby/spec/generic), 54 tags: ['~bidi', '~server'] }, 55 { id: :bidi, title: 'bidi tests', dir: %w(src/ruby/spec/generic), 56 tag: 'bidi' }, 57 { id: :server, title: 'rpc server thread tests', dir: %w(src/ruby/spec/generic), 58 tag: 'server' }, 59 { id: :pb, title: 'protobuf service tests', dir: %w(src/ruby/spec/pb) } 60] 61namespace :suite do 62 SPEC_SUITES.each do |suite| 63 desc "Run all specs in the #{suite[:title]} spec suite" 64 RSpec::Core::RakeTask.new(suite[:id]) do |t| 65 ENV['COVERAGE_NAME'] = suite[:id].to_s 66 spec_files = [] 67 suite[:files].each { |f| spec_files += Dir[f] } if suite[:files] 68 69 if suite[:dir] 70 suite[:dir].each { |f| spec_files += Dir["#{f}/**/*_spec.rb"] } 71 end 72 helper = 'src/ruby/spec/spec_helper.rb' 73 spec_files << helper unless spec_files.include?(helper) 74 75 t.pattern = spec_files 76 t.rspec_opts = "--tag #{suite[:tag]}" if suite[:tag] 77 if suite[:tags] 78 t.rspec_opts = suite[:tags].map { |x| "--tag #{x}" }.join(' ') 79 end 80 end 81 end 82end 83 84desc 'Build the Windows gRPC DLLs for Ruby' 85task 'dlls' do 86 grpc_config = ENV['GRPC_CONFIG'] || 'opt' 87 verbose = ENV['V'] || '0' 88 89 env = 'CPPFLAGS="-D_WIN32_WINNT=0x600 -DNTDDI_VERSION=0x06000000 -DUNICODE -D_UNICODE -Wno-unused-variable -Wno-unused-result -DCARES_STATICLIB -Wno-error=conversion -Wno-sign-compare -Wno-parentheses -Wno-format -DWIN32_LEAN_AND_MEAN" ' 90 env += 'CFLAGS="-Wno-incompatible-pointer-types" ' 91 env += 'CXXFLAGS="-std=c++11 -fno-exceptions" ' 92 env += 'LDFLAGS=-static ' 93 env += 'SYSTEM=MINGW32 ' 94 env += 'EMBED_ZLIB=true ' 95 env += 'EMBED_OPENSSL=true ' 96 env += 'EMBED_CARES=true ' 97 env += 'BUILDDIR=/tmp ' 98 env += "V=#{verbose} " 99 out = GrpcBuildConfig::CORE_WINDOWS_DLL 100 101 w64 = { cross: 'x86_64-w64-mingw32', out: 'grpc_c.64.ruby', platform: 'x64-mingw32' } 102 w32 = { cross: 'i686-w64-mingw32', out: 'grpc_c.32.ruby', platform: 'x86-mingw32' } 103 104 [ w64, w32 ].each do |opt| 105 env_comp = "CC=#{opt[:cross]}-gcc " 106 env_comp += "CXX=#{opt[:cross]}-g++ " 107 env_comp += "LD=#{opt[:cross]}-gcc " 108 env_comp += "LDXX=#{opt[:cross]}-g++ " 109 run_rake_compiler opt[:platform], <<-EOT 110 gem update --system --no-document && \ 111 #{env} #{env_comp} make -j`nproc` #{out} && \ 112 #{opt[:cross]}-strip -x -S #{out} && \ 113 cp #{out} #{opt[:out]} 114 EOT 115 end 116 117end 118 119desc 'Build the native gem file under rake_compiler_dock' 120task 'gem:native' do 121 verbose = ENV['V'] || '0' 122 123 grpc_config = ENV['GRPC_CONFIG'] || 'opt' 124 ruby_cc_versions = ['3.0.0', '2.7.0', '2.6.0', '2.5.0', '2.4.0', '2.3.0'].join(':') 125 126 if RUBY_PLATFORM =~ /darwin/ 127 FileUtils.touch 'grpc_c.32.ruby' 128 FileUtils.touch 'grpc_c.64.ruby' 129 unless '2.5' == /(\d+\.\d+)/.match(RUBY_VERSION).to_s 130 fail "rake gem:native (the rake task to build the binary packages) is being " \ 131 "invoked on macos with ruby #{RUBY_VERSION}. The ruby macos artifact " \ 132 "build should be running on ruby 2.5." 133 end 134 system "rake cross native gem RUBY_CC_VERSION=#{ruby_cc_versions} V=#{verbose} GRPC_CONFIG=#{grpc_config}" 135 else 136 Rake::Task['dlls'].execute 137 ['x86-mingw32', 'x64-mingw32'].each do |plat| 138 run_rake_compiler plat, <<-EOT 139 gem update --system --no-document && \ 140 bundle && \ 141 rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem pkg/#{spec.full_name}.gem \ 142 RUBY_CC_VERSION=#{ruby_cc_versions} \ 143 V=#{verbose} \ 144 GRPC_CONFIG=#{grpc_config} 145 EOT 146 end 147 # Truncate grpc_c.*.ruby files because they're for Windows only. 148 File.truncate('grpc_c.32.ruby', 0) 149 File.truncate('grpc_c.64.ruby', 0) 150 ['x86_64-linux', 'x86-linux'].each do |plat| 151 run_rake_compiler plat, <<-EOT 152 gem update --system --no-document && \ 153 bundle && \ 154 rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem pkg/#{spec.full_name}.gem \ 155 RUBY_CC_VERSION=#{ruby_cc_versions} \ 156 V=#{verbose} \ 157 GRPC_CONFIG=#{grpc_config} 158 EOT 159 end 160 end 161end 162 163# Define dependencies between the suites. 164task 'suite:wrapper' => [:compile, :rubocop] 165task 'suite:idiomatic' => 'suite:wrapper' 166task 'suite:bidi' => 'suite:wrapper' 167task 'suite:server' => 'suite:wrapper' 168task 'suite:pb' => 'suite:server' 169 170desc 'Compiles the gRPC extension then runs all the tests' 171task all: ['suite:idiomatic', 'suite:bidi', 'suite:pb', 'suite:server'] 172task default: :all 173