diff options
Diffstat (limited to 'lib/mkmf.rb')
| -rw-r--r-- | lib/mkmf.rb | 111 |
1 files changed, 86 insertions, 25 deletions
diff --git a/lib/mkmf.rb b/lib/mkmf.rb index 92af95d423..37ee4a70d9 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -40,10 +40,27 @@ class Array # :nodoc: end ## -# mkmf.rb is used by Ruby C extensions to generate a Makefile which will +# \Module \MakeMakefile is used by Ruby C extensions to generate a Makefile which will # correctly compile and link the C extension to Ruby and a third-party # library. module MakeMakefile + + target_rbconfig = nil + ARGV.delete_if do |arg| + opt = arg.delete_prefix("--target-rbconfig=") + unless opt == arg + target_rbconfig = opt + end + end + if target_rbconfig + # Load the RbConfig for the target platform into this module. + # Cross-compiling needs the same version of Ruby. + Kernel.load target_rbconfig, self + else + # The RbConfig for the target platform where the built extension runs. + RbConfig = ::RbConfig + end + #### defer until this module become global-state free. # def self.extended(obj) # obj.init_mkmf @@ -402,7 +419,7 @@ MESSAGE # disable ASAN leak reporting - conftest programs almost always don't bother # to free their memory. - envs['ASAN_OPTIONS'] = "detect_leaks=0" unless ENV.key?('ASAN_OPTIONS') + envs['LSAN_OPTIONS'] = "detect_leaks=0" unless ENV.key?('LSAN_OPTIONS') return envs, expand[commands] end @@ -556,24 +573,30 @@ MSG conf) end - def cpp_command(outfile, opt="") + def cpp_config(opt) 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 + conf + end + + def cpp_command(outfile, opt="") + conf = cpp_config(opt) RbConfig::expand("$(CPP) #$INCFLAGS #$CPPFLAGS #$CFLAGS #{opt} #{CONFTEST_C} #{outfile}", conf) end def libpathflag(libpath=$DEFLIBPATH|$LIBPATH) + libpathflags = nil libpath.map{|x| case x when "$(topdir)", /\A\./ LIBPATHFLAG else - LIBPATHFLAG+RPATHFLAG + libpathflags ||= [LIBPATHFLAG, RPATHFLAG].grep(/\S/).join(" ") end % x.quote - }.join + }.join(" ") end def werror_flag(opt = nil) @@ -586,9 +609,9 @@ MSG yield(opt, opts) end - def try_link0(src, opt = "", **opts, &b) # :nodoc: + def try_link0(src, opt = "", ldflags: "", **opts, &b) # :nodoc: exe = CONFTEST+$EXEEXT - cmd = link_command("", opt) + cmd = link_command(ldflags, opt) if $universal require 'tmpdir' Dir.mktmpdir("mkmf_", oldtmpdir = ENV["TMPDIR"]) do |tmpdir| @@ -732,7 +755,7 @@ MSG # :nodoc: def try_ldflags(flags, werror: $mswin, **opts) - try_link(MAIN_DOES_NOTHING, flags, werror: werror, **opts) + try_link(MAIN_DOES_NOTHING, "", ldflags: flags, werror: werror, **opts) end # :startdoc: @@ -842,7 +865,7 @@ int main() {printf("%"PRI_CONFTEST_PREFIX"#{neg ? 'd' : 'u'}\\n", conftest_const v } unless strvars.empty? - prepare << "char " << strvars.map {|v| "#{v}[1024]"}.join(", ") << "; " + prepare << "char " << strvars.map {|v| %[#{v}[1024] = ""]}.join(", ") << "; " end when nil call = "" @@ -908,20 +931,12 @@ SRC xpopen(cpp_command('', opt)) do |f| if Regexp === pat puts(" ruby -ne 'print if #{pat.inspect}'") - f.grep(pat) {|l| + !f.grep(pat) {|l| puts "#{f.lineno}: #{l}" - return true - } - false + }.empty? else puts(" egrep '#{pat}'") - begin - stdin = $stdin.dup - $stdin.reopen(f) - system("egrep", pat) - ensure - $stdin.reopen(stdin) - end + system("egrep", pat, in: f) end end ensure @@ -1950,7 +1965,7 @@ SRC if pkgconfig = with_config("#{pkg}-config") and find_executable0(pkgconfig) # if and only if package specific config command is given elsif ($PKGCONFIG ||= - (pkgconfig = with_config("pkg-config") {config_string("PKG_CONFIG") || "pkg-config"}) && + (pkgconfig = with_config("pkg-config") {config_string("PKG_CONFIG") || ENV["PKG_CONFIG"] || "pkg-config"}) && find_executable0(pkgconfig) && pkgconfig) and xsystem([*envs, $PKGCONFIG, "--exists", pkg]) # default to pkg-config command @@ -1966,7 +1981,20 @@ SRC opts = Array(opts).map { |o| "--#{o}" } opts = xpopen([*envs, pkgconfig, *opts, *args], err:[:child, :out], &:read) Logging.open {puts opts.each_line.map{|s|"=> #{s.inspect}"}} - opts.strip if $?.success? + if $?.success? + opts = opts.strip + libarg, libpath = LIBARG, LIBPATHFLAG.strip + opts = opts.shellsplit.map { |s| + if s.start_with?('-l') + libarg % s[2..] + elsif s.start_with?('-L') + libpath % s[2..] + else + s + end + }.quote.join(" ") + opts + end } end orig_ldflags = $LDFLAGS @@ -2350,6 +2378,19 @@ RULES # directory, i.e. the current directory. It is included as part of the # +VPATH+ and added to the list of +INCFLAGS+. # + # Yields the configuration part of the makefile to be generated, as an array + # of strings, if the block is given. The returned value will be used the + # new configuration part. + # + # create_makefile('foo') {|conf| + # [ + # *conf, + # "MACRO_YOU_NEED = something", + # ] + # } + # + # If "depend" file exist in the source directory, that content will be + # included in the generated makefile, with formatted by depend_rules method. def create_makefile(target, srcprefix = nil) $target = target libpath = $DEFLIBPATH|$LIBPATH @@ -2466,16 +2507,19 @@ TIMESTAMP_DIR = #{$extout && $extmk ? '$(extout)/.timestamp' : '.'} sodir = $extout ? '$(TARGET_SO_DIR)' : '$(RUBYARCHDIR)' n = '$(TARGET_SO_DIR)$(TARGET)' cleanobjs = ["$(OBJS)"] + cleanlibs = [] if $extmk %w[bc i s].each {|ex| cleanobjs << "$(OBJS:.#{$OBJEXT}=.#{ex})"} end if target config_string('cleanobjs') {|t| cleanobjs << t.gsub(/\$\*/, "$(TARGET)#{deffile ? '-$(arch)': ''}")} + cleanlibs << '$(TARGET_SO)' end + config_string('cleanlibs') {|t| cleanlibs << t.gsub(/\$\*/) {n}} conf << "\ TARGET_SO_DIR =#{$extout ? " $(RUBYARCHDIR)/" : ''} TARGET_SO = $(TARGET_SO_DIR)$(DLLIB) -CLEANLIBS = #{'$(TARGET_SO) ' if target}#{config_string('cleanlibs') {|t| t.gsub(/\$\*/) {n}}} +CLEANLIBS = #{cleanlibs.join(' ')} CLEANOBJS = #{cleanobjs.join(' ')} *.bak TARGET_SO_DIR_TIMESTAMP = #{timestamp_file(sodir, target_prefix)} " #" @@ -2547,7 +2591,7 @@ static: #{$extmk && !$static ? "all" : %[$(STATIC_LIB)#{$extout ? " install-rb" dest = "#{dir}/#{File.basename(f)}" mfile.print("do-install-rb#{sfx}: #{dest}\n") mfile.print("#{dest}: #{f} #{timestamp_file(dir, target_prefix)}\n") - mfile.print("\t$(Q) $(#{$extout ? 'COPY' : 'INSTALL_DATA'}) #{f} $(@D)\n") + mfile.print("\t$(Q) $(#{$extout ? 'COPY' : 'INSTALL_DATA'}) #{f} $@\n") if defined?($installed_list) and !$extout mfile.print("\t@echo #{dest}>>$(INSTALLED_LIST)\n") end @@ -2889,7 +2933,7 @@ MESSAGE ## # Argument which will add a library path to the linker - LIBPATHFLAG = config_string('LIBPATHFLAG') || ' -L%s' + LIBPATHFLAG = config_string('LIBPATHFLAG') || '-L%s' ## # Argument which will add a runtime library path to the linker @@ -2986,15 +3030,32 @@ realclean: distclean def cc_command(opt="") conf = cc_config(opt) + cxx_command(opt, conf) RbConfig::expand("$(CXX) #$INCFLAGS #$CPPFLAGS #$CXXFLAGS #$ARCH_FLAG #{opt} -c #{CONFTEST_CXX}", conf) end + def cpp_command(outfile, opt="") + conf = cpp_config(opt) + cxx = cxx_command(opt, conf) + cpp = conf['CPP'].sub(/(\A|\s)#{Regexp.quote(conf['CC'])}(?=\z|\s)/) { + "#$1#{cxx}" + } + RbConfig::expand("#{cpp} #$INCFLAGS #$CPPFLAGS #$CXXFLAGS #{opt} #{CONFTEST_CXX} #{outfile}", + conf) + end + def link_command(ldflags, *opts) conf = link_config(ldflags, *opts) RbConfig::expand(TRY_LINK_CXX.dup, conf) end + def cxx_command(opt="", conf = cc_config(opt)) + cxx = conf['CXX'] + raise Errno::ENOENT, "C++ compiler not found" if !cxx or cxx == 'false' + cxx + end + # :startdoc: end |
