Skip to content

Commit

Permalink
Extract common mkmf code into nmatrix/mkmf.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkn committed Jul 21, 2016
1 parent c85b0af commit 618f456
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 384 deletions.
119 changes: 9 additions & 110 deletions ext/nmatrix/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,48 +25,18 @@
# This file checks for ATLAS and other necessary headers, and
# generates a Makefile for compiling NMatrix.

require "mkmf"
require "nmatrix/mkmf"

# 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)'],
['ruby_constants.h', '$(archdir)']
]
if /cygwin|mingw/ =~ RUBY_PLATFORM
$INSTALLFILES << ['libnmatrix.a', '$(archdir)']
end
end
$INSTALLFILES = [
['nmatrix.h' , '$(archdir)'],
['nmatrix.hpp' , '$(archdir)'],
['nmatrix_config.h', '$(archdir)'],
['nm_memory.h' , '$(archdir)'],
['ruby_constants.h', '$(archdir)']
]

if /cygwin|mingw/ =~ RUBY_PLATFORM
CONFIG["DLDFLAGS"] << " --output-lib libnmatrix.a"
$INSTALLFILES << ['libnmatrix.a', '$(archdir)']
end

$DEBUG = true
Expand All @@ -79,79 +49,8 @@ def create_conf_h(file) #:nodoc:
$objs = basenames.map { |b| "#{b}.o" }
$srcs = basenames.map { |b| "#{b}.cpp" }

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
# Workaround for Windows having a different name for null device
null_device = (system 'echo "Hi" </dev/null') ? '/dev/null' : 'nul'

cxxvar = proc { |n| `#{CONFIG['CXX']} -E -dM - <#{null_device} | 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['CC'] == 'clang' && CONFIG['CXX'] != 'clang++'
puts "WARNING: CONFIG['CXX'] is not 'clang++' in spite of CONFIG['CC'] is 'clang'.",
"WARNING: Force to use clang++ together with clang."

CONFIG['CXX'] = 'clang++'
end

if CONFIG['CXX'] == 'clang++'
$CXX_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'
$CXX_STANDARD = 'c++0x'
else
$CXX_STANDARD = 'c++11'
end
puts "using C++ standard... #{$CXX_STANDARD}"
puts "g++ reports version... " + `#{CONFIG['CXX']} --version|head -n 1|cut -f 3 -d " "`
end

#$libs += " -lprofiler "

# For release, these next two should both be changed to -O3.
$CFLAGS += " -O3 "
#$CFLAGS += " -static -O0 -g "
$CXXFLAGS += " -O3 -std=#{$CXX_STANDARD} " #-fmax-errors=10 -save-temps
#$CXXFLAGS += " -static -O0 -g -std=#{$CXX_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', '')

have_func("rb_array_const_ptr", "ruby.h")
have_macro("FIX_CONST_VALUE_PTR", "ruby.h")
have_macro("RARRAY_CONST_PTR", "ruby.h")
have_macro("RARRAY_AREF", "ruby.h")

create_conf_h("nmatrix_config.h")
create_makefile("nmatrix")

Expand Down
102 changes: 3 additions & 99 deletions ext/nmatrix_atlas/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,11 @@
# 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 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
require "nmatrix/mkmf"

#$INSTALLFILES = [['nmatrix.h', '$(archdir)'], ['nmatrix.hpp', '$(archdir)'], ['nmatrix_config.h', '$(archdir)'], ['nm_memory.h', '$(archdir)']]
if /cygwin|mingw/ =~ RUBY_PLATFORM
CONFIG["DLDFLAGS"] << " --output-lib libnmatrix.a"
#$INSTALLFILES << ['libnmatrix.a', '$(archdir)']
end

$DEBUG = true
Expand All @@ -75,55 +44,6 @@ def create_conf_h(file) #:nodoc:
$objs = basenames.map { |b| "#{b}.o" }
$srcs = basenames.map { |b| "#{b}.cpp" }

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++'
$CXX_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'
$CXX_STANDARD = 'c++0x'
else
$CXX_STANDARD = 'c++11'
end
puts "using C++ standard... #{$CXX_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

# The next line allows the user to supply --with-atlas-dir=/usr/local/atlas,
# --with-atlas-lib or --with-atlas-include and tell the compiler where to look
# for ATLAS. The same for all the others
Expand Down Expand Up @@ -194,22 +114,6 @@ def gplusplus_version
$libs += " -llapack -lcblas -latlas "
#$libs += " -lprofiler "


# For release, these next two should both be changed to -O3.
$CFLAGS += " -O3" #" -O0 -g "
#$CFLAGS += " -static -O0 -g "
$CXXFLAGS += " -O3 -std=#{$CXX_STANDARD}" #" -O0 -g -std=#{$CXX_STANDARD} " #-fmax-errors=10 -save-temps
#$CPPFLAGS += " -static -O0 -g -std=#{$CXX_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', '')

have_func("rb_array_const_ptr", "ruby.h")
have_macro("FIX_CONST_VALUE_PTR", "ruby.h")
have_macro("RARRAY_CONST_PTR", "ruby.h")
have_macro("RARRAY_AREF", "ruby.h")

create_conf_h("nmatrix_atlas_config.h")
create_makefile("nmatrix_atlas")

Expand Down
72 changes: 1 addition & 71 deletions ext/nmatrix_fftw/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,7 @@
#
# This file checks FFTW3 and other necessary headers/shared objects.

require 'mkmf'

# 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

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
require 'nmatrix/mkmf'

fftw_libdir = RbConfig::CONFIG['libdir']
fftw_incdir = RbConfig::CONFIG['includedir']
Expand All @@ -82,28 +34,6 @@ def gplusplus_version
$CXXFLAGS = ["-Wall -Werror=return-type -I$(srcdir)/../nmatrix -I$(srcdir)/lapacke/include -std=c++11",$CXXFLAGS].join(" ")
$CPPFLAGS = ["-Wall -Werror=return-type -I$(srcdir)/../nmatrix -I$(srcdir)/lapacke/include -std=c++11",$CPPFLAGS].join(" ")

CONFIG['CXX'] = 'g++'

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

flags = " --include=#{fftw_incdir} --libdir=#{fftw_libdir}"

if have_library("fftw3")
Expand Down
Loading

0 comments on commit 618f456

Please sign in to comment.