From c3f03da4944241f46233ce159068f35c5bd5f9b2 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 18 Sep 2019 15:38:24 +0900 Subject: [EXPERIMENTAL] MakeMakefile::CXX for C++ --- ext/-test-/cxxanyargs/extconf.rb | 25 +++++---------------- lib/mkmf.rb | 47 ++++++++++++++++++++++++++++++++++------ win32/Makefile.sub | 1 + 3 files changed, 46 insertions(+), 27 deletions(-) diff --git a/ext/-test-/cxxanyargs/extconf.rb b/ext/-test-/cxxanyargs/extconf.rb index 0c5344a1e5..78dcb20815 100644 --- a/ext/-test-/cxxanyargs/extconf.rb +++ b/ext/-test-/cxxanyargs/extconf.rb @@ -1,21 +1,8 @@ # frozen_string_literal: false -cfg = RbConfig::CONFIG.merge( - 'hdrdir' => $hdrdir.quote, - 'src' => "#{CONFTEST_C}", - 'arch_hdrdir' => $arch_hdrdir.quote, - 'top_srcdir' => $top_srcdir.quote, - 'CC' => RbConfig::CONFIG['CXX'], - 'CFLAGS' => RbConfig::CONFIG['CXXFLAGS'], - 'INCFLAGS' => "#$INCFLAGS", - 'CPPFLAGS' => "#$CPPFLAGS", - 'ARCH_FLAG' => "#$ARCH_FLAG", - 'LDFLAGS' => "#$LDFLAGS", - 'LOCAL_LIBS' => "#$LOCAL_LIBS", - 'LIBS' => "#$LIBS" -) -cxx = RbConfig::expand(TRY_LINK.dup, cfg) -src = create_tmpsrc(<<~'begin') do |x| +cxx = MakeMakefile::CXX + +ok = cxx.try_compile(<<~'begin', "", lang: 'C++') do |x| #include "ruby/config.h" namespace { @@ -34,8 +21,6 @@ begin x.sub! %<#include "ruby.h">, '' end -begin - create_makefile("-test-/cxxanyargs") if xsystem(cxx) -ensure - log_src src +if ok + create_makefile("-test-/cxxanyargs") end diff --git a/lib/mkmf.rb b/lib/mkmf.rb index 692ceb43a9..dc0b23a007 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -434,6 +434,10 @@ EOM EOM end + def conftest_source + CONFTEST_C + end + def create_tmpsrc(src) src = "#{COMMON_HEADERS}\n#{src}" src = yield(src) if block_given? @@ -442,7 +446,7 @@ EOM src.sub!(/[^\n]\z/, "\\&\n") count = 0 begin - open(CONFTEST_C, "wb") do |cfile| + open(conftest_source, "wb") do |cfile| cfile.print src end rescue Errno::EACCES @@ -477,10 +481,10 @@ MSG end end - def link_command(ldflags, opt="", libpath=$DEFLIBPATH|$LIBPATH) + def link_config(ldflags, opt="", libpath=$DEFLIBPATH|$LIBPATH) librubyarg = $extmk ? $LIBRUBYARG_STATIC : "$(LIBRUBYARG)" conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, - 'src' => "#{CONFTEST_C}", + 'src' => "#{conftest_source}", 'arch_hdrdir' => $arch_hdrdir.quote, 'top_srcdir' => $top_srcdir.quote, 'INCFLAGS' => "#$INCFLAGS", @@ -491,21 +495,29 @@ MSG 'LOCAL_LIBS' => "#$LOCAL_LIBS #$libs", 'LIBS' => "#{librubyarg} #{opt} #$LIBS") conf['LIBPATH'] = libpathflag(libpath.map {|s| RbConfig::expand(s.dup, conf)}) + conf + end + + def link_command(ldflags, *opts) + conf = link_config(ldflags, *opts) RbConfig::expand(TRY_LINK.dup, conf) end - def cc_command(opt="") + def cc_config(opt="") conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote, 'arch_hdrdir' => $arch_hdrdir.quote, 'top_srcdir' => $top_srcdir.quote) + conf + end + + def cc_command(opt="") + conf = cc_config(opt) RbConfig::expand("$(CC) #$INCFLAGS #$CPPFLAGS #$CFLAGS #$ARCH_FLAG #{opt} -c #{CONFTEST_C}", conf) end def cpp_command(outfile, opt="") - conf = RbConfig::CONFIG.merge('hdrdir' => $hdrdir.quote, 'srcdir' => $srcdir.quote, - 'arch_hdrdir' => $arch_hdrdir.quote, - 'top_srcdir' => $top_srcdir.quote) + conf = cc_config(opt) if $universal and (arch_flag = conf['ARCH_FLAG']) and !arch_flag.empty? conf['ARCH_FLAG'] = arch_flag.gsub(/(?:\G|\s)-arch\s+\S+/, '') end @@ -2763,6 +2775,27 @@ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb realclean: distclean " + + C = self + + module CXX + include MakeMakefile + extend self + + # Logging.message "CXX_EXT=#{CONFIG['CXX_EXT']}\n" + + CONFTEST_CXX = "#{CONFTEST}.#{config_string('CXX_EXT') || CXX_EXT[0]}" + + def conftest_source + CONFTEST_CXX + end + + def cc_command(opt="") + conf = cc_config(opt) + RbConfig::expand("$(CXX) #$INCFLAGS #$CPPFLAGS #$CXXFLAGS #$ARCH_FLAG #{opt} -c #{CONFTEST_CXX}", + conf) + end + end end include MakeMakefile diff --git a/win32/Makefile.sub b/win32/Makefile.sub index 9b3cd4a391..d600783912 100644 --- a/win32/Makefile.sub +++ b/win32/Makefile.sub @@ -1041,6 +1041,7 @@ s,@COMPILE_CXX@,$$(CXX) $$(INCFLAGS) $$(CXXFLAGS) $$(CPPFLAGS) $$(COUTFLAG)$$(@) s,@ASSEMBLE_C@,$$(CC) $$(CFLAGS) $$(CPPFLAGS) -Fa$$(@) -c $$(CSRCFLAG)$$(<:\=/),;t t s,@ASSEMBLE_CXX@,$$(CXX) $$(CXXFLAGS) $$(CPPFLAGS) -Fa$$(@) -c -Tp$$(<:\=/),;t t s,@COMPILE_RULES@,{$$(*VPATH*)}.%s.%s: .%s.%s:,;t t +s,@CXX_EXT@,cpp,;t t s,@RULE_SUBST@,{.;$$(VPATH)}%s,;t t s,@TRY_LINK@,$$(CC) -Feconftest $$(INCFLAGS) -I$$(hdrdir) $$(CPPFLAGS) $$(CFLAGS) $$(src) $$(LOCAL_LIBS) $$(LIBS) -link $$(LDFLAGS) $$(LIBPATH) $$(XLDFLAGS),;t t s,@COMMON_LIBS@,$(COMMON_LIBS),;t t -- cgit v1.2.3