blob: 82b6d313c866208127ad20f105ac8484328eaa09 [file] [log] [blame]
Craig Tiller6169d5f2016-03-31 07:46:18 -07001# Copyright 2015, Google Inc.
nnoble097ef9b2014-12-01 17:06:10 -08002# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30require 'mkmf'
31
32LIBDIR = RbConfig::CONFIG['libdir']
33INCLUDEDIR = RbConfig::CONFIG['includedir']
34
35HEADER_DIRS = [
Tim Emiolae2860c52015-01-16 02:58:41 -080036 # Search /opt/local (Mac source install)
37 '/opt/local/include',
nnoble097ef9b2014-12-01 17:06:10 -080038
Tim Emiolae2860c52015-01-16 02:58:41 -080039 # Search /usr/local (Source install)
40 '/usr/local/include',
nnoble097ef9b2014-12-01 17:06:10 -080041
Tim Emiolae2860c52015-01-16 02:58:41 -080042 # Check the ruby install locations
43 INCLUDEDIR
nnoble097ef9b2014-12-01 17:06:10 -080044]
45
46LIB_DIRS = [
Tim Emiolae2860c52015-01-16 02:58:41 -080047 # Search /opt/local (Mac source install)
48 '/opt/local/lib',
nnoble097ef9b2014-12-01 17:06:10 -080049
Tim Emiolae2860c52015-01-16 02:58:41 -080050 # Search /usr/local (Source install)
51 '/usr/local/lib',
nnoble097ef9b2014-12-01 17:06:10 -080052
Tim Emiolae2860c52015-01-16 02:58:41 -080053 # Check the ruby install locations
54 LIBDIR
nnoble097ef9b2014-12-01 17:06:10 -080055]
56
Nicolas "Pixel" Nobled51d1212016-01-31 11:33:19 +010057windows = RUBY_PLATFORM =~ /mingw|mswin/
58
murgatroid99d7e1a102015-12-18 11:16:16 -080059grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..'))
60
61grpc_config = ENV['GRPC_CONFIG'] || 'opt'
62
63if ENV.key?('GRPC_LIB_DIR')
64 grpc_lib_dir = File.join(grpc_root, ENV['GRPC_LIB_DIR'])
65else
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010066 grpc_lib_dir = File.join(grpc_root, 'libs', grpc_config)
murgatroid990f1c42e2015-07-08 12:54:31 -070067end
68
Nicolas Noble86cbe302016-02-05 15:08:12 -080069ENV['MACOSX_DEPLOYMENT_TARGET'] = '10.7'
70
Nicolas "Pixel" Nobled51d1212016-01-31 11:33:19 +010071unless File.exist?(File.join(grpc_lib_dir, 'libgrpc.a')) or windows
Nicolas Noble2bc107f2016-02-02 23:15:22 -080072 ENV['AR'] = RbConfig::CONFIG['AR'] + ' rcs'
73 ENV['CC'] = RbConfig::CONFIG['CC']
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010074 ENV['LD'] = ENV['CC']
75
Nicolas Noble2bc107f2016-02-02 23:15:22 -080076 ENV['AR'] = 'libtool -o' if RUBY_PLATFORM =~ /darwin/
77
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010078 ENV['EMBED_OPENSSL'] = 'true'
79 ENV['EMBED_ZLIB'] = 'true'
Nicolas "Pixel" Noble5219c6d2016-02-05 22:22:29 +010080 ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG']
Nicolas Noble2bc107f2016-02-02 23:15:22 -080081 ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64' if RUBY_PLATFORM =~ /darwin/
Craig Tiller71ea4a12016-02-04 15:06:41 -080082 ENV['CFLAGS'] = '-DGPR_BACKWARDS_COMPATIBILITY_MODE'
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010083
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010084 output_dir = File.expand_path(RbConfig::CONFIG['topdir'])
85 grpc_lib_dir = File.join(output_dir, 'libs', grpc_config)
86 ENV['BUILDDIR'] = output_dir
87
88 puts 'Building internal gRPC into ' + grpc_lib_dir
Nicolas "Pixel" Nobleb8e9e9c2016-01-30 18:35:13 +010089 system("make -j -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config}")
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +010090 exit 1 unless $? == 0
murgatroid99d7e1a102015-12-18 11:16:16 -080091end
nnoble097ef9b2014-12-01 17:06:10 -080092
murgatroid99d7e1a102015-12-18 11:16:16 -080093$CFLAGS << ' -I' + File.join(grpc_root, 'include')
Nicolas "Pixel" Nobled51d1212016-01-31 11:33:19 +010094$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows
murgatroid99d7e1a102015-12-18 11:16:16 -080095if grpc_config == 'gcov'
96 $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage'
97 $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic'
murgatroid9951dbf902015-07-07 16:00:06 -070098end
99
Nicolas "Pixel" Nobled3554972016-02-03 02:11:43 +0100100$LDFLAGS << ' -Wl,-wrap,memcpy' if RUBY_PLATFORM =~ /linux/
101$LDFLAGS << ' -static' if windows
Craig Tiller4bef7ce2016-02-02 08:38:43 -0800102
murgatroid99fa0fa182015-07-07 18:02:00 -0700103$CFLAGS << ' -std=c99 '
104$CFLAGS << ' -Wall '
105$CFLAGS << ' -Wextra '
106$CFLAGS << ' -pedantic '
107$CFLAGS << ' -Werror '
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100108$CFLAGS << ' -Wno-format '
murgatroid990b90c1e2015-07-07 17:44:46 -0700109
Nicolas "Pixel" Nobleeade6e02016-01-29 22:53:31 +0100110output = File.join('grpc', 'grpc_c')
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100111puts 'Generating Makefile for ' + output
112create_makefile(output)
murgatroid99d7e1a102015-12-18 11:16:16 -0800113
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100114strip_tool = RbConfig::CONFIG['STRIP']
Nicolas Noble2bc107f2016-02-02 23:15:22 -0800115strip_tool = 'strip -x' if RUBY_PLATFORM =~ /darwin/
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100116
Nicolas "Pixel" Noble17230442016-01-29 01:46:19 +0100117if grpc_config == 'opt'
118 File.open('Makefile.new', 'w') do |o|
119 o.puts 'hijack: all strip'
120 o.puts
121 File.foreach('Makefile') do |i|
122 o.puts i
123 end
124 o.puts
125 o.puts 'strip:'
126 o.puts "\t$(ECHO) Stripping $(DLLIB)"
127 o.puts "\t$(Q) #{strip_tool} $(DLLIB)"
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100128 end
Nicolas "Pixel" Noble17230442016-01-29 01:46:19 +0100129 File.rename('Makefile.new', 'Makefile')
Nicolas "Pixel" Noblee7a91a22016-01-28 07:46:08 +0100130end