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' 17 18windows = RUBY_PLATFORM =~ /mingw|mswin/ 19bsd = RUBY_PLATFORM =~ /bsd/ 20 21grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..')) 22 23grpc_config = ENV['GRPC_CONFIG'] || 'opt' 24 25ENV['MACOSX_DEPLOYMENT_TARGET'] = '10.10' 26 27if ENV['AR'].nil? || ENV['AR'].size == 0 28 ENV['AR'] = RbConfig::CONFIG['AR'] 29end 30if ENV['CC'].nil? || ENV['CC'].size == 0 31 ENV['CC'] = RbConfig::CONFIG['CC'] 32end 33if ENV['CXX'].nil? || ENV['CXX'].size == 0 34 ENV['CXX'] = RbConfig::CONFIG['CXX'] 35end 36if ENV['LD'].nil? || ENV['LD'].size == 0 37 ENV['LD'] = ENV['CC'] 38end 39 40if RUBY_PLATFORM =~ /darwin/ 41 ENV['AR'] = 'libtool' 42 ENV['ARFLAGS'] = '-o' 43 end 44 45ENV['EMBED_OPENSSL'] = 'true' 46ENV['EMBED_ZLIB'] = 'true' 47ENV['EMBED_CARES'] = 'true' 48ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG'] 49ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64' if RUBY_PLATFORM =~ /darwin/ 50ENV['CPPFLAGS'] = '-DGPR_BACKWARDS_COMPATIBILITY_MODE' 51 52output_dir = File.expand_path(RbConfig::CONFIG['topdir']) 53grpc_lib_dir = File.join(output_dir, 'libs', grpc_config) 54ENV['BUILDDIR'] = output_dir 55 56unless windows 57 puts 'Building internal gRPC into ' + grpc_lib_dir 58 nproc = 4 59 nproc = Etc.nprocessors * 2 if Etc.respond_to? :nprocessors 60 make = bsd ? 'gmake' : 'make' 61 system("#{make} -j#{nproc} -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config} Q=") 62 exit 1 unless $? == 0 63end 64 65$CFLAGS << ' -I' + File.join(grpc_root, 'include') 66 67ext_export_file = File.join(grpc_root, 'src', 'ruby', 'ext', 'grpc', 'ext-export') 68$LDFLAGS << ' -Wl,--version-script="' + ext_export_file + '.gcc"' if RUBY_PLATFORM =~ /linux/ 69$LDFLAGS << ' -Wl,-exported_symbols_list,"' + ext_export_file + '.clang"' if RUBY_PLATFORM =~ /darwin/ 70 71$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows 72if grpc_config == 'gcov' 73 $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage' 74 $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic' 75end 76 77if grpc_config == 'dbg' 78 $CFLAGS << ' -O0 -ggdb3' 79end 80 81$LDFLAGS << ' -Wl,-wrap,memcpy' if RUBY_PLATFORM =~ /linux/ 82$LDFLAGS << ' -static-libgcc -static-libstdc++' if RUBY_PLATFORM =~ /linux/ 83$LDFLAGS << ' -static' if windows 84 85$CFLAGS << ' -std=c99 ' 86$CFLAGS << ' -Wall ' 87$CFLAGS << ' -Wextra ' 88$CFLAGS << ' -pedantic ' 89 90output = File.join('grpc', 'grpc_c') 91puts 'Generating Makefile for ' + output 92create_makefile(output) 93 94strip_tool = RbConfig::CONFIG['STRIP'] 95strip_tool = 'strip -x' if RUBY_PLATFORM =~ /darwin/ 96 97if grpc_config == 'opt' 98 File.open('Makefile.new', 'w') do |o| 99 o.puts 'hijack: all strip' 100 o.puts 101 File.foreach('Makefile') do |i| 102 o.puts i 103 end 104 o.puts 105 o.puts 'strip: $(DLLIB)' 106 o.puts "\t$(ECHO) Stripping $(DLLIB)" 107 o.puts "\t$(Q) #{strip_tool} $(DLLIB)" 108 end 109 File.rename('Makefile.new', 'Makefile') 110end 111