blob: 4760f33e3895e0c8ac42ab58dcc8081e436adf00 [file] [log] [blame]
Jan Tattermusch7897ae92017-06-07 22:57:36 +02001# Copyright 2015 gRPC authors.
nnoble097ef9b2014-12-01 17:06:10 -08002#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003# 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
nnoble097ef9b2014-12-01 17:06:10 -08006#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02007# http://www.apache.org/licenses/LICENSE-2.0
nnoble097ef9b2014-12-01 17:06:10 -08008#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009# 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.
nnoble097ef9b2014-12-01 17:06:10 -080014
J. Martin18e8b142017-02-23 18:52:46 -060015require 'etc'
nnoble097ef9b2014-12-01 17:06:10 -080016require 'mkmf'
17
18LIBDIR = RbConfig::CONFIG['libdir']
19INCLUDEDIR = RbConfig::CONFIG['includedir']
20
21HEADER_DIRS = [
Tim Emiolae2860c52015-01-16 02:58:41 -080022 # Search /opt/local (Mac source install)
23 '/opt/local/include',
nnoble097ef9b2014-12-01 17:06:10 -080024
Tim Emiolae2860c52015-01-16 02:58:41 -080025 # Search /usr/local (Source install)
26 '/usr/local/include',
nnoble097ef9b2014-12-01 17:06:10 -080027
Tim Emiolae2860c52015-01-16 02:58:41 -080028 # Check the ruby install locations
29 INCLUDEDIR
nnoble097ef9b2014-12-01 17:06:10 -080030]
31
32LIB_DIRS = [
Tim Emiolae2860c52015-01-16 02:58:41 -080033 # Search /opt/local (Mac source install)
34 '/opt/local/lib',
nnoble097ef9b2014-12-01 17:06:10 -080035
Tim Emiolae2860c52015-01-16 02:58:41 -080036 # Search /usr/local (Source install)
37 '/usr/local/lib',
nnoble097ef9b2014-12-01 17:06:10 -080038
Tim Emiolae2860c52015-01-16 02:58:41 -080039 # Check the ruby install locations
40 LIBDIR
nnoble097ef9b2014-12-01 17:06:10 -080041]
42
Nicolas "Pixel" Nobled51d1212016-01-31 11:33:19 +010043windows = RUBY_PLATFORM =~ /mingw|mswin/
Frank Groeneveldfbf81282017-10-12 08:27:14 +020044bsd = RUBY_PLATFORM =~ /bsd/
Nicolas "Pixel" Nobled51d1212016-01-31 11:33:19 +010045
murgatroid99d7e1a102015-12-18 11:16:16 -080046grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..'))
47
48grpc_config = ENV['GRPC_CONFIG'] || 'opt'
49
Nicolas Noble86cbe302016-02-05 15:08:12 -080050ENV['MACOSX_DEPLOYMENT_TARGET'] = '10.7'
51
Nicolas "Pixel" Noble791e93e2016-03-01 02:48:00 +010052ENV['AR'] = RbConfig::CONFIG['AR'] + ' rcs'
53ENV['CC'] = RbConfig::CONFIG['CC']
Craig Tiller9f3822c2017-04-10 15:07:22 -070054ENV['CXX'] = RbConfig::CONFIG['CXX']
Nicolas "Pixel" Noble791e93e2016-03-01 02:48:00 +010055ENV['LD'] = ENV['CC']
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010056
Nicolas "Pixel" Noble791e93e2016-03-01 02:48:00 +010057ENV['AR'] = 'libtool -o' if RUBY_PLATFORM =~ /darwin/
Nicolas Noble2bc107f2016-02-02 23:15:22 -080058
Nicolas "Pixel" Noble791e93e2016-03-01 02:48:00 +010059ENV['EMBED_OPENSSL'] = 'true'
60ENV['EMBED_ZLIB'] = 'true'
Yuchen Zengb1b21152016-08-12 11:27:30 -070061ENV['EMBED_CARES'] = 'true'
Nicolas "Pixel" Noble791e93e2016-03-01 02:48:00 +010062ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG']
63ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64' if RUBY_PLATFORM =~ /darwin/
Alexander Polcynebcc6b32017-12-08 15:39:53 -080064ENV['CPPFLAGS'] = '-DGPR_BACKWARDS_COMPATIBILITY_MODE'
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010065
Nicolas "Pixel" Noble791e93e2016-03-01 02:48:00 +010066output_dir = File.expand_path(RbConfig::CONFIG['topdir'])
67grpc_lib_dir = File.join(output_dir, 'libs', grpc_config)
68ENV['BUILDDIR'] = output_dir
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010069
Nicolas "Pixel" Nobled8b07cb2016-05-05 03:55:55 +020070unless windows
71 puts 'Building internal gRPC into ' + grpc_lib_dir
J. Martin18e8b142017-02-23 18:52:46 -060072 nproc = 4
73 nproc = Etc.nprocessors * 2 if Etc.respond_to? :nprocessors
Frank Groeneveldfbf81282017-10-12 08:27:14 +020074 make = bsd ? 'gmake' : 'make'
75 system("#{make} -j#{nproc} -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config} Q=")
Nicolas "Pixel" Nobled8b07cb2016-05-05 03:55:55 +020076 exit 1 unless $? == 0
77end
nnoble097ef9b2014-12-01 17:06:10 -080078
murgatroid99d7e1a102015-12-18 11:16:16 -080079$CFLAGS << ' -I' + File.join(grpc_root, 'include')
Nicolas "Pixel" Nobled51d1212016-01-31 11:33:19 +010080$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows
murgatroid99d7e1a102015-12-18 11:16:16 -080081if grpc_config == 'gcov'
82 $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage'
83 $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic'
murgatroid9951dbf902015-07-07 16:00:06 -070084end
85
Alexander Polcyn38ba85c2016-08-29 16:59:23 -070086if grpc_config == 'dbg'
Alexander Polcynd3594982018-04-05 03:12:27 -070087 $CFLAGS << ' -O0 -ggdb3'
Alexander Polcyn38ba85c2016-08-29 16:59:23 -070088end
89
Nicolas "Pixel" Nobled3554972016-02-03 02:11:43 +010090$LDFLAGS << ' -Wl,-wrap,memcpy' if RUBY_PLATFORM =~ /linux/
91$LDFLAGS << ' -static' if windows
Craig Tiller4bef7ce2016-02-02 08:38:43 -080092
murgatroid99fa0fa182015-07-07 18:02:00 -070093$CFLAGS << ' -std=c99 '
94$CFLAGS << ' -Wall '
95$CFLAGS << ' -Wextra '
96$CFLAGS << ' -pedantic '
murgatroid990b90c1e2015-07-07 17:44:46 -070097
Nicolas "Pixel" Nobleeade6e02016-01-29 22:53:31 +010098output = File.join('grpc', 'grpc_c')
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010099puts 'Generating Makefile for ' + output
100create_makefile(output)
murgatroid99d7e1a102015-12-18 11:16:16 -0800101
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100102strip_tool = RbConfig::CONFIG['STRIP']
Nicolas Noble2bc107f2016-02-02 23:15:22 -0800103strip_tool = 'strip -x' if RUBY_PLATFORM =~ /darwin/
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100104
Nicolas "Pixel" Noble17230442016-01-29 01:46:19 +0100105if grpc_config == 'opt'
106 File.open('Makefile.new', 'w') do |o|
107 o.puts 'hijack: all strip'
108 o.puts
109 File.foreach('Makefile') do |i|
110 o.puts i
111 end
112 o.puts
113 o.puts 'strip:'
114 o.puts "\t$(ECHO) Stripping $(DLLIB)"
115 o.puts "\t$(Q) #{strip_tool} $(DLLIB)"
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100116 end
Nicolas "Pixel" Noble17230442016-01-29 01:46:19 +0100117 File.rename('Makefile.new', 'Makefile')
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100118end