Skip to content

Commit

Permalink
Fix C++ compiler selection (#530)
Browse files Browse the repository at this point in the history
* Remove unused function from extconf.rb

* Force to use clang++ together with clang

* Extract common mkmf code into nmatrix/mkmf.rb
  • Loading branch information
mrkn authored and translunar committed Jul 22, 2016
1 parent e677640 commit cc357ca
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 474 deletions.
145 changes: 9 additions & 136 deletions ext/nmatrix/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +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 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)'],
['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 @@ -111,73 +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['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
134 changes: 3 additions & 131 deletions ext/nmatrix_atlas/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,74 +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 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
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 @@ -107,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 @@ -226,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
Loading

0 comments on commit cc357ca

Please sign in to comment.