Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 1 | # Copyright 2015, Google Inc. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 2 | # 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 | |
J. Martin | 18e8b14 | 2017-02-23 18:52:46 -0600 | [diff] [blame] | 30 | require 'etc' |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 31 | require 'mkmf' |
| 32 | |
| 33 | LIBDIR = RbConfig::CONFIG['libdir'] |
| 34 | INCLUDEDIR = RbConfig::CONFIG['includedir'] |
| 35 | |
| 36 | HEADER_DIRS = [ |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 37 | # Search /opt/local (Mac source install) |
| 38 | '/opt/local/include', |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 39 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 40 | # Search /usr/local (Source install) |
| 41 | '/usr/local/include', |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 42 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 43 | # Check the ruby install locations |
| 44 | INCLUDEDIR |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 45 | ] |
| 46 | |
| 47 | LIB_DIRS = [ |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 48 | # Search /opt/local (Mac source install) |
| 49 | '/opt/local/lib', |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 50 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 51 | # Search /usr/local (Source install) |
| 52 | '/usr/local/lib', |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 53 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 54 | # Check the ruby install locations |
| 55 | LIBDIR |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 56 | ] |
| 57 | |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 58 | windows = RUBY_PLATFORM =~ /mingw|mswin/ |
| 59 | |
murgatroid99 | d7e1a10 | 2015-12-18 11:16:16 -0800 | [diff] [blame] | 60 | grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..')) |
| 61 | |
| 62 | grpc_config = ENV['GRPC_CONFIG'] || 'opt' |
| 63 | |
Nicolas Noble | 86cbe30 | 2016-02-05 15:08:12 -0800 | [diff] [blame] | 64 | ENV['MACOSX_DEPLOYMENT_TARGET'] = '10.7' |
| 65 | |
Nicolas "Pixel" Noble | 791e93e | 2016-03-01 02:48:00 +0100 | [diff] [blame] | 66 | ENV['AR'] = RbConfig::CONFIG['AR'] + ' rcs' |
| 67 | ENV['CC'] = RbConfig::CONFIG['CC'] |
Craig Tiller | 9f3822c | 2017-04-10 15:07:22 -0700 | [diff] [blame] | 68 | ENV['CXX'] = RbConfig::CONFIG['CXX'] |
Nicolas "Pixel" Noble | 791e93e | 2016-03-01 02:48:00 +0100 | [diff] [blame] | 69 | ENV['LD'] = ENV['CC'] |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 70 | |
Nicolas "Pixel" Noble | 791e93e | 2016-03-01 02:48:00 +0100 | [diff] [blame] | 71 | ENV['AR'] = 'libtool -o' if RUBY_PLATFORM =~ /darwin/ |
Nicolas Noble | 2bc107f | 2016-02-02 23:15:22 -0800 | [diff] [blame] | 72 | |
Nicolas "Pixel" Noble | 791e93e | 2016-03-01 02:48:00 +0100 | [diff] [blame] | 73 | ENV['EMBED_OPENSSL'] = 'true' |
| 74 | ENV['EMBED_ZLIB'] = 'true' |
Yuchen Zeng | b1b2115 | 2016-08-12 11:27:30 -0700 | [diff] [blame] | 75 | ENV['EMBED_CARES'] = 'true' |
Nicolas "Pixel" Noble | 791e93e | 2016-03-01 02:48:00 +0100 | [diff] [blame] | 76 | ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG'] |
| 77 | ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64' if RUBY_PLATFORM =~ /darwin/ |
| 78 | ENV['CFLAGS'] = '-DGPR_BACKWARDS_COMPATIBILITY_MODE' |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 79 | |
Nicolas "Pixel" Noble | 791e93e | 2016-03-01 02:48:00 +0100 | [diff] [blame] | 80 | output_dir = File.expand_path(RbConfig::CONFIG['topdir']) |
| 81 | grpc_lib_dir = File.join(output_dir, 'libs', grpc_config) |
| 82 | ENV['BUILDDIR'] = output_dir |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 83 | |
Nicolas "Pixel" Noble | d8b07cb | 2016-05-05 03:55:55 +0200 | [diff] [blame] | 84 | unless windows |
| 85 | puts 'Building internal gRPC into ' + grpc_lib_dir |
J. Martin | 18e8b14 | 2017-02-23 18:52:46 -0600 | [diff] [blame] | 86 | nproc = 4 |
| 87 | nproc = Etc.nprocessors * 2 if Etc.respond_to? :nprocessors |
Craig Tiller | f85aabf | 2017-04-10 14:06:15 -0700 | [diff] [blame] | 88 | system("make -j#{nproc} -C #{grpc_root} #{grpc_lib_dir}/libgrpc.a CONFIG=#{grpc_config} Q=") |
Nicolas "Pixel" Noble | d8b07cb | 2016-05-05 03:55:55 +0200 | [diff] [blame] | 89 | exit 1 unless $? == 0 |
| 90 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 91 | |
murgatroid99 | d7e1a10 | 2015-12-18 11:16:16 -0800 | [diff] [blame] | 92 | $CFLAGS << ' -I' + File.join(grpc_root, 'include') |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 93 | $LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows |
murgatroid99 | d7e1a10 | 2015-12-18 11:16:16 -0800 | [diff] [blame] | 94 | if grpc_config == 'gcov' |
| 95 | $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage' |
| 96 | $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic' |
murgatroid99 | 51dbf90 | 2015-07-07 16:00:06 -0700 | [diff] [blame] | 97 | end |
| 98 | |
Alexander Polcyn | 38ba85c | 2016-08-29 16:59:23 -0700 | [diff] [blame] | 99 | if grpc_config == 'dbg' |
| 100 | $CFLAGS << ' -O0' |
| 101 | end |
| 102 | |
Nicolas "Pixel" Noble | d355497 | 2016-02-03 02:11:43 +0100 | [diff] [blame] | 103 | $LDFLAGS << ' -Wl,-wrap,memcpy' if RUBY_PLATFORM =~ /linux/ |
| 104 | $LDFLAGS << ' -static' if windows |
Craig Tiller | 4bef7ce | 2016-02-02 08:38:43 -0800 | [diff] [blame] | 105 | |
murgatroid99 | fa0fa18 | 2015-07-07 18:02:00 -0700 | [diff] [blame] | 106 | $CFLAGS << ' -std=c99 ' |
| 107 | $CFLAGS << ' -Wall ' |
| 108 | $CFLAGS << ' -Wextra ' |
| 109 | $CFLAGS << ' -pedantic ' |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 110 | $CFLAGS << ' -Wno-format ' |
murgatroid99 | 0b90c1e | 2015-07-07 17:44:46 -0700 | [diff] [blame] | 111 | |
Nicolas "Pixel" Noble | eade6e0 | 2016-01-29 22:53:31 +0100 | [diff] [blame] | 112 | output = File.join('grpc', 'grpc_c') |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 113 | puts 'Generating Makefile for ' + output |
| 114 | create_makefile(output) |
murgatroid99 | d7e1a10 | 2015-12-18 11:16:16 -0800 | [diff] [blame] | 115 | |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 116 | strip_tool = RbConfig::CONFIG['STRIP'] |
Nicolas Noble | 2bc107f | 2016-02-02 23:15:22 -0800 | [diff] [blame] | 117 | strip_tool = 'strip -x' if RUBY_PLATFORM =~ /darwin/ |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 118 | |
Nicolas "Pixel" Noble | 1723044 | 2016-01-29 01:46:19 +0100 | [diff] [blame] | 119 | if grpc_config == 'opt' |
| 120 | File.open('Makefile.new', 'w') do |o| |
| 121 | o.puts 'hijack: all strip' |
| 122 | o.puts |
| 123 | File.foreach('Makefile') do |i| |
| 124 | o.puts i |
| 125 | end |
| 126 | o.puts |
| 127 | o.puts 'strip:' |
| 128 | o.puts "\t$(ECHO) Stripping $(DLLIB)" |
| 129 | o.puts "\t$(Q) #{strip_tool} $(DLLIB)" |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 130 | end |
Nicolas "Pixel" Noble | 1723044 | 2016-01-29 01:46:19 +0100 | [diff] [blame] | 131 | File.rename('Makefile.new', 'Makefile') |
Nicolas "Pixel" Noble | e7a91a2 | 2016-01-28 07:46:08 +0100 | [diff] [blame] | 132 | end |