• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.7'
26
27ENV['AR'] = RbConfig::CONFIG['AR'] + ' rcs'
28ENV['CC'] = RbConfig::CONFIG['CC']
29ENV['CXX'] = RbConfig::CONFIG['CXX']
30ENV['LD'] = ENV['CC']
31
32ENV['AR'] = 'libtool -o' if RUBY_PLATFORM =~ /darwin/
33
34ENV['EMBED_OPENSSL'] = 'true'
35ENV['EMBED_ZLIB'] = 'true'
36ENV['EMBED_CARES'] = 'true'
37ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG']
38ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64' if RUBY_PLATFORM =~ /darwin/
39ENV['CPPFLAGS'] = '-DGPR_BACKWARDS_COMPATIBILITY_MODE'
40
41output_dir = File.expand_path(RbConfig::CONFIG['topdir'])
42grpc_lib_dir = File.join(output_dir, 'libs', grpc_config)
43ENV['BUILDDIR'] = output_dir
44
45unless 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
52end
53
54$CFLAGS << ' -I' + File.join(grpc_root, 'include')
55$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows
56if grpc_config == 'gcov'
57  $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage'
58  $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic'
59end
60
61if grpc_config == 'dbg'
62  $CFLAGS << ' -O0 -ggdb3'
63end
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
73output = File.join('grpc', 'grpc_c')
74puts 'Generating Makefile for ' + output
75create_makefile(output)
76
77strip_tool = RbConfig::CONFIG['STRIP']
78strip_tool = 'strip -x' if RUBY_PLATFORM =~ /darwin/
79
80if 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')
93end
94