1# Copyright 2015 gRPC authors. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15require 'etc' 16require 'mkmf' 17require_relative '../../lib/grpc/version.rb' 18 19windows = RUBY_PLATFORM =~ /mingw|mswin/ 20windows_ucrt = RUBY_PLATFORM =~ /(mingw|mswin).*ucrt/ 21bsd = RUBY_PLATFORM =~ /bsd/ 22darwin = RUBY_PLATFORM =~ /darwin/ 23linux = RUBY_PLATFORM =~ /linux/ 24cross_compiling = ENV['RCD_HOST_RUBY_VERSION'] # set by rake-compiler-dock in build containers 25# TruffleRuby uses the Sulong LLVM runtime, which is different from Apple's. 26apple_toolchain = darwin && RUBY_ENGINE != 'truffleruby' 27 28grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..')) 29 30grpc_config = ENV['GRPC_CONFIG'] || 'opt' 31 32ENV['MACOSX_DEPLOYMENT_TARGET'] = '10.10' 33 34def debug_symbols_output_dir 35 d = ENV['GRPC_RUBY_DEBUG_SYMBOLS_OUTPUT_DIR'] 36 return nil if d.nil? or d.size == 0 37 d 38end 39 40def maybe_remove_strip_all_linker_flag(flags) 41 if debug_symbols_output_dir 42 # Hack to prevent automatic stripping during shared library linking. 43 # rake-compiler-dock sets the -s LDFLAG when building rubies for 44 # cross compilation, and this -s flag propagates into RbConfig. Stripping 45 # during the link is problematic because it prevents us from saving 46 # debug symbols. We want to first link our shared library, then save 47 # debug symbols, and only after that strip. 48 flags = flags.split(' ') 49 flags = flags.reject {|flag| flag == '-s'} 50 flags = flags.join(' ') 51 end 52 flags 53end 54 55def env_unset?(name) 56 ENV[name].nil? || ENV[name].size == 0 57end 58 59def inherit_env_or_rbconfig(name) 60 ENV[name] = inherit_rbconfig(name) if env_unset?(name) 61end 62 63def inherit_rbconfig(name, linker_flag: false) 64 value = RbConfig::CONFIG[name] || '' 65 if linker_flag 66 value = maybe_remove_strip_all_linker_flag(value) 67 end 68 p "extconf.rb setting ENV[#{name}] = #{value}" 69 ENV[name] = value 70end 71 72def env_append(name, string) 73 ENV[name] += ' ' + string 74end 75 76# build grpc C-core 77inherit_env_or_rbconfig 'AR' 78inherit_env_or_rbconfig 'CC' 79inherit_env_or_rbconfig 'CXX' 80inherit_env_or_rbconfig 'RANLIB' 81inherit_env_or_rbconfig 'STRIP' 82inherit_rbconfig 'CPPFLAGS' 83inherit_rbconfig('LDFLAGS', linker_flag: true) 84 85ENV['LD'] = ENV['CC'] if env_unset?('LD') 86ENV['LDXX'] = ENV['CXX'] if env_unset?('LDXX') 87 88if RUBY_ENGINE == 'truffleruby' 89 # ensure we can find the system's OpenSSL 90 env_append 'CPPFLAGS', RbConfig::CONFIG['cppflags'] 91end 92 93if apple_toolchain && !cross_compiling 94 ENV['AR'] = 'libtool' 95 ENV['ARFLAGS'] = '-o' 96end 97 98# Don't embed on TruffleRuby (constant-time crypto is unsafe with Sulong, slow build times) 99ENV['EMBED_OPENSSL'] = (RUBY_ENGINE != 'truffleruby').to_s 100# Don't embed on TruffleRuby (the system zlib is already linked for the zlib C extension, slow build times) 101ENV['EMBED_ZLIB'] = (RUBY_ENGINE != 'truffleruby').to_s 102 103ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG'] 104if apple_toolchain && !cross_compiling 105 if RUBY_PLATFORM =~ /arm64/ 106 ENV['ARCH_FLAGS'] = '-arch arm64' 107 else 108 ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64' 109 end 110end 111 112env_append 'CPPFLAGS', '-DGRPC_XDS_USER_AGENT_NAME_SUFFIX="\"RUBY\""' 113 114require_relative '../../lib/grpc/version' 115env_append 'CPPFLAGS', '-DGRPC_XDS_USER_AGENT_VERSION_SUFFIX="\"' + GRPC::VERSION + '\""' 116env_append 'CPPFLAGS', '-DGRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK=1' 117 118output_dir = File.expand_path(RbConfig::CONFIG['topdir']) 119grpc_lib_dir = File.join(output_dir, 'libs', grpc_config) 120ENV['BUILDDIR'] = output_dir 121 122strip_tool = RbConfig::CONFIG['STRIP'] 123strip_tool += ' -x' if apple_toolchain 124 125unless windows 126 puts 'Building internal gRPC into ' + grpc_lib_dir 127 nproc = 4 128 nproc = Etc.nprocessors if Etc.respond_to? :nprocessors 129 nproc_override = ENV['GRPC_RUBY_BUILD_PROCS'] 130 unless nproc_override.nil? or nproc_override.size == 0 131 nproc = nproc_override 132 puts "Overriding make parallelism to #{nproc}" 133 end 134 make = bsd ? 'gmake' : 'make' 135 cmd = "#{make} -j#{nproc} -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config} Q=" 136 puts "Building grpc native library: #{cmd}" 137 system(cmd) 138 exit 1 unless $? == 0 139end 140 141# C-core built, generate Makefile for ruby extension 142$LDFLAGS = maybe_remove_strip_all_linker_flag($LDFLAGS) 143$DLDFLAGS = maybe_remove_strip_all_linker_flag($DLDFLAGS) 144 145$CFLAGS << ' -DGRPC_RUBY_WINDOWS_UCRT' if windows_ucrt 146$CFLAGS << ' -I' + File.join(grpc_root, 'include') 147$CFLAGS << ' -g' 148 149def have_ruby_abi_version() 150 return true if RUBY_ENGINE == 'truffleruby' 151 # ruby_abi_version is only available in development versions: https://github.com/ruby/ruby/pull/6231 152 return false if RUBY_PATCHLEVEL >= 0 153 154 m = /(\d+)\.(\d+)/.match(RUBY_VERSION) 155 if m.nil? 156 puts "Failed to parse ruby version: #{RUBY_VERSION}. Assuming ruby_abi_version symbol is NOT present." 157 return false 158 end 159 major = m[1].to_i 160 minor = m[2].to_i 161 if major >= 3 and minor >= 2 162 puts "Ruby version #{RUBY_VERSION} >= 3.2. Assuming ruby_abi_version symbol is present." 163 return true 164 end 165 puts "Ruby version #{RUBY_VERSION} < 3.2. Assuming ruby_abi_version symbol is NOT present." 166 false 167end 168 169def ext_export_filename() 170 name = 'ext-export' 171 name += '-truffleruby' if RUBY_ENGINE == 'truffleruby' 172 name += '-with-ruby-abi-version' if have_ruby_abi_version() 173 name 174end 175 176ext_export_file = File.join(grpc_root, 'src', 'ruby', 'ext', 'grpc', ext_export_filename()) 177$LDFLAGS << ' -Wl,--version-script="' + ext_export_file + '.gcc"' if linux 178if apple_toolchain 179 $LDFLAGS << ' -weak_framework CoreFoundation' 180 $LDFLAGS << ' -Wl,-exported_symbols_list,"' + ext_export_file + '.clang"' 181end 182 183$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows 184if grpc_config == 'gcov' 185 $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage' 186 $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic' 187end 188 189if grpc_config == 'dbg' 190 $CFLAGS << ' -O0' 191end 192 193# Do not statically link standard libraries on TruffleRuby as this does not work when compiling to bitcode 194if linux && RUBY_ENGINE != 'truffleruby' 195 $LDFLAGS << ' -static-libgcc -static-libstdc++' 196end 197$LDFLAGS << ' -static' if windows 198 199$CFLAGS << ' -std=c11 ' 200$CFLAGS << ' -Wall ' 201$CFLAGS << ' -Wextra ' 202$CFLAGS << ' -pedantic ' 203 204output = File.join('grpc', 'grpc_c') 205puts "extconf.rb $LDFLAGS: #{$LDFLAGS}" 206puts "extconf.rb $DLDFLAGS: #{$DLDFLAGS}" 207puts "extconf.rb $CFLAGS: #{$CFLAGS}" 208puts 'Generating Makefile for ' + output 209create_makefile(output) 210 211ruby_major_minor = /(\d+\.\d+)/.match(RUBY_VERSION).to_s 212debug_symbols = "grpc-#{GRPC::VERSION}-#{RUBY_PLATFORM}-ruby-#{ruby_major_minor}.dbg" 213 214File.open('Makefile.new', 'w') do |o| 215 o.puts 'hijack_remove_unused_artifacts: all remove_unused_artifacts' 216 o.puts 217 o.write(File.read('Makefile')) 218 o.puts 219 o.puts 'remove_unused_artifacts: $(DLLIB)' 220 # Now that the extension library has been linked, we can remove unused artifacts 221 # that take up a lot of disk space. 222 rm_obj_cmd = "rm -rf #{File.join(output_dir, 'objs')}" 223 o.puts "\t$(ECHO) Removing unused object artifacts: #{rm_obj_cmd}" 224 o.puts "\t$(Q) #{rm_obj_cmd}" 225 rm_grpc_core_libs = "rm -f #{grpc_lib_dir}/*.a" 226 o.puts "\t$(ECHO) Removing unused grpc core libraries: #{rm_grpc_core_libs}" 227 o.puts "\t$(Q) #{rm_grpc_core_libs}" 228end 229File.rename('Makefile.new', 'Makefile') 230 231if grpc_config == 'opt' 232 File.open('Makefile.new', 'w') do |o| 233 o.puts 'hijack: all strip' 234 o.puts 235 o.write(File.read('Makefile')) 236 o.puts 237 o.puts 'strip: $(DLLIB)' 238 if debug_symbols_output_dir 239 o.puts "\t$(ECHO) Saving debug symbols in #{debug_symbols_output_dir}/#{debug_symbols}" 240 o.puts "\t$(Q) objcopy --only-keep-debug $(DLLIB) #{debug_symbols_output_dir}/#{debug_symbols}" 241 end 242 o.puts "\t$(ECHO) Stripping $(DLLIB)" 243 o.puts "\t$(Q) #{strip_tool} $(DLLIB)" 244 end 245 File.rename('Makefile.new', 'Makefile') 246end 247 248if ENV['GRPC_RUBY_TEST_ONLY_WORKAROUND_MAKE_INSTALL_BUG'] 249 # Note: this env var setting is intended to work around a problem observed 250 # with the ginstall command on grpc's macos automated test infrastructure, 251 # and is not guaranteed to work in the wild. 252 # Also see https://github.com/rake-compiler/rake-compiler/issues/210. 253 puts 'Overriding the generated Makefile install target to use cp' 254 File.open('Makefile.new', 'w') do |o| 255 File.foreach('Makefile') do |i| 256 if i.start_with?('INSTALL_PROG = ') 257 override = 'INSTALL_PROG = cp' 258 puts "Replacing generated Makefile line: |#{i}|, with: |#{override}|" 259 o.puts override 260 else 261 o.puts i 262 end 263 end 264 end 265 File.rename('Makefile.new', 'Makefile') 266end 267