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