forked from SciRuby/nmatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extconf.rb
200 lines (158 loc) · 6.17 KB
/
extconf.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# = NMatrix
#
# A linear algebra library for scientific computation in Ruby.
# NMatrix is part of SciRuby.
#
# NMatrix was originally inspired by and derived from NArray, by
# Masahiro Tanaka: http://narray.rubyforge.org
#
# == Copyright Information
#
# SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
# NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
#
# Please see LICENSE.txt for additional copyright notices.
#
# == Contributing
#
# By contributing source code to SciRuby, you agree to be bound by
# our Contributor Agreement:
#
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
#
# == extconf.rb
#
# This file checks for ATLAS and other necessary headers, and
# generates a Makefile for compiling NMatrix.
require "mkmf"
# Function derived from NArray's extconf.rb.
def have_type(type, header=nil) #:nodoc:
printf "checking for %s... ", type
STDOUT.flush
src = <<"SRC"
#include <ruby.h>
SRC
src << <<"SRC" unless header.nil?
#include <#{header}>
SRC
r = try_link(src + <<"SRC")
int main() { return 0; }
int t() { #{type} a; return 0; }
SRC
unless r
print "no\n"
return false
end
$defs.push(format("-DHAVE_%s", type.upcase))
print "yes\n"
return true
end
# Function derived from NArray's extconf.rb.
def create_conf_h(file) #:nodoc:
print "creating #{file}\n"
File.open(file, 'w') do |hfile|
header_guard = file.upcase.sub(/\s|\./, '_')
hfile.puts "#ifndef #{header_guard}"
hfile.puts "#define #{header_guard}"
hfile.puts
# FIXME: Find a better way to do this:
hfile.puts "#define RUBY_2 1" if RUBY_VERSION >= '2.0'
for line in $defs
line =~ /^-D(.*)/
hfile.printf "#define %s 1\n", $1
end
hfile.puts
hfile.puts "#endif"
end
end
if RUBY_VERSION < '1.9'
raise(NotImplementedError, "Sorry, you need at least Ruby 1.9!")
else
#$INSTALLFILES = [['nmatrix.h', '$(archdir)'], ['nmatrix.hpp', '$(archdir)'], ['nmatrix_config.h', '$(archdir)'], ['nm_memory.h', '$(archdir)']]
if /cygwin|mingw/ =~ RUBY_PLATFORM
#$INSTALLFILES << ['libnmatrix.a', '$(archdir)']
end
end
if /cygwin|mingw/ =~ RUBY_PLATFORM
CONFIG["DLDFLAGS"] << " --output-lib libnmatrix.a"
end
$DEBUG = true
#not the right way to add this include directory
$CFLAGS = ["-Wall -Werror=return-type -I$(srcdir)/../nmatrix -I$(srcdir)/lapacke/include",$CFLAGS].join(" ")
$CXXFLAGS = ["-Wall -Werror=return-type -I$(srcdir)/../nmatrix -I$(srcdir)/lapacke/include",$CXXFLAGS].join(" ")
$CPPFLAGS = ["-Wall -Werror=return-type -I$(srcdir)/../nmatrix -I$(srcdir)/lapacke/include",$CPPFLAGS].join(" ")
# When adding objects here, make sure their directories are included in CLEANOBJS down at the bottom of extconf.rb.
# Why not just autogenerate this list from all .c/.cpp files in directory?
basenames = %w{nmatrix_lapacke math_lapacke lapacke}
$objs = basenames.map { |b| "#{b}.o" }
$srcs = basenames.map { |b| "#{b}.cpp" }
#CONFIG['CXX'] = 'clang++'
CONFIG['CXX'] = 'g++'
def find_newer_gplusplus #:nodoc:
print "checking for apparent GNU g++ binary with C++0x/C++11 support... "
[9,8,7,6,5,4,3].each do |minor|
ver = "4.#{minor}"
gpp = "g++-#{ver}"
result = `which #{gpp}`
next if result.empty?
CONFIG['CXX'] = gpp
puts ver
return CONFIG['CXX']
end
false
end
def gplusplus_version
cxxvar = proc { |n| `#{CONFIG['CXX']} -E -dM - </dev/null | grep #{n}`.chomp.split(' ')[2] }
major = cxxvar.call('__GNUC__')
minor = cxxvar.call('__GNUC_MINOR__')
patch = cxxvar.call('__GNUC_PATCHLEVEL__')
raise("unable to determine g++ version (match to get version was nil)") if major.nil? || minor.nil? || patch.nil?
"#{major}.#{minor}.#{patch}"
end
if CONFIG['CXX'] == 'clang++'
$CPP_STANDARD = 'c++11'
else
version = gplusplus_version
if version < '4.3.0' && CONFIG['CXX'] == 'g++' # see if we can find a newer G++, unless it's been overridden by user
if !find_newer_gplusplus
raise("You need a version of g++ which supports -std=c++0x or -std=c++11. If you're on a Mac and using Homebrew, we recommend using mac-brew-gcc.sh to install a more recent g++.")
end
version = gplusplus_version
end
if version < '4.7.0'
$CPP_STANDARD = 'c++0x'
else
$CPP_STANDARD = 'c++11'
end
puts "using C++ standard... #{$CPP_STANDARD}"
puts "g++ reports version... " + `#{CONFIG['CXX']} --version|head -n 1|cut -f 3 -d " "`
end
# add smmp in to get generic transp; remove smmp2 to eliminate funcptr transp
# For some reason, if we try to look for /usr/lib64/atlas on a Mac OS X Mavericks system, and the directory does not
# exist, it will give a linker error -- even if the lib dir is already correctly included with -L. So we need to check
# that Dir.exists?(d) for each.
ldefaults = {lapack: ["/usr/local/lib"].delete_if { |d| !Dir.exists?(d) } }
unless have_library("lapack")
dir_config("lapack", idefaults[:lapack], ldefaults[:lapack])
end
#have_func("rb_scan_args", "ruby.h")
# Order matters here: ATLAS has to go after LAPACK: http://mail.scipy.org/pipermail/scipy-user/2007-January/010717.html
$libs += " -llapack "
#To use the Intel MKL, comment out the line above, and also comment out the bit above with have_library and dir_config for lapack.
#Then add something like the line below (for exactly what linker flags to use see https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor ):
#$libs += " -L${MKLROOT}/lib/intel64 -lmkl_intel_lp64 -lmkl_core -lmkl_sequential "
# For release, these next two should both be changed to -O3.
$CFLAGS += " -O3" #" -O0 -g "
#$CFLAGS += " -static -O0 -g "
$CPPFLAGS += " -O3 -std=#{$CPP_STANDARD}" #" -O0 -g -std=#{$CPP_STANDARD} " #-fmax-errors=10 -save-temps
#$CPPFLAGS += " -static -O0 -g -std=#{$CPP_STANDARD} "
CONFIG['warnflags'].gsub!('-Wshorten-64-to-32', '') # doesn't work except in Mac-patched gcc (4.2)
CONFIG['warnflags'].gsub!('-Wdeclaration-after-statement', '')
CONFIG['warnflags'].gsub!('-Wimplicit-function-declaration', '')
create_conf_h("nmatrix_lapacke_config.h")
create_makefile("nmatrix_lapacke")
# to clean up object files in subdirectories:
open('Makefile', 'a') do |f|
clean_objs_paths = %w{ }.map { |d| "#{d}/*.#{CONFIG["OBJEXT"]}" }
f.write("CLEANOBJS := $(CLEANOBJS) #{clean_objs_paths.join(' ')}")
end