shims/linux/super/cc: Make changes for Linux

This commit is contained in:
Shaun Jackman 2018-05-16 11:33:39 -07:00
parent abac6dfaf8
commit 16e5799f86

View File

@ -14,7 +14,7 @@ require "pathname"
require "set" require "set"
class Cmd class Cmd
attr_reader :config, :prefix, :cellar, :opt, :tmpdir, :sysroot, :deps attr_reader :config, :prefix, :cellar, :opt, :tmpdir, :deps
attr_reader :archflags, :optflags, :keg_regex, :formula_prefix attr_reader :archflags, :optflags, :keg_regex, :formula_prefix
def initialize(arg0, args) def initialize(arg0, args)
@ -25,7 +25,6 @@ class Cmd
@cellar = ENV["HOMEBREW_CELLAR"] @cellar = ENV["HOMEBREW_CELLAR"]
@opt = ENV["HOMEBREW_OPT"] @opt = ENV["HOMEBREW_OPT"]
@tmpdir = ENV["HOMEBREW_TEMP"] @tmpdir = ENV["HOMEBREW_TEMP"]
@sysroot = ENV["HOMEBREW_SDKROOT"]
@archflags = ENV.fetch("HOMEBREW_ARCHFLAGS") { "" }.split(" ") @archflags = ENV.fetch("HOMEBREW_ARCHFLAGS") { "" }.split(" ")
@optflags = ENV.fetch("HOMEBREW_OPTFLAGS") { "" }.split(" ") @optflags = ENV.fetch("HOMEBREW_OPTFLAGS") { "" }.split(" ")
@deps = Set.new(ENV.fetch("HOMEBREW_DEPENDENCIES") { "" }.split(",")) @deps = Set.new(ENV.fetch("HOMEBREW_DEPENDENCIES") { "" }.split(","))
@ -37,6 +36,8 @@ class Cmd
def mode def mode
if @arg0 == "cpp" || @arg0 == "ld" if @arg0 == "cpp" || @arg0 == "ld"
@arg0.to_sym @arg0.to_sym
elsif @arg0 == "gold" || @arg0 == "ld.gold"
:ld
elsif @args.include? "-c" elsif @args.include? "-c"
if @arg0 =~ /(?:c|g|clang)\+\+/ if @arg0 =~ /(?:c|g|clang)\+\+/
:cxx :cxx
@ -59,6 +60,7 @@ class Cmd
def tool def tool
@tool ||= case @arg0 @tool ||= case @arg0
when "ld" then "ld" when "ld" then "ld"
when "gold", "ld.gold" then "ld.gold"
when "cpp" then "cpp" when "cpp" then "cpp"
when /llvm_(clang(\+\+)?)/ when /llvm_(clang(\+\+)?)/
"#{ENV["HOMEBREW_PREFIX"]}/opt/llvm/bin/#{$1}" "#{ENV["HOMEBREW_PREFIX"]}/opt/llvm/bin/#{$1}"
@ -74,13 +76,9 @@ class Cmd
else else
# Note that this is a universal fallback, so that we'll always invoke # Note that this is a universal fallback, so that we'll always invoke
# HOMEBREW_CC regardless of what name under which the tool was invoked. # HOMEBREW_CC regardless of what name under which the tool was invoked.
if ENV["HOMEBREW_CC"] == "llvm_clang"
"#{ENV["HOMEBREW_PREFIX"]}/opt/llvm/bin/clang"
else
ENV["HOMEBREW_CC"] ENV["HOMEBREW_CC"]
end end
end end
end
def args def args
if @args.length == 1 && @args[0] == "-v" if @args.length == 1 && @args[0] == "-v"
@ -96,14 +94,6 @@ class Cmd
args = refurbished_args args = refurbished_args
end end
if sysroot
if tool == "ld"
args << "-syslibroot" << sysroot
else
args << "-isysroot" << sysroot << "--sysroot=#{sysroot}"
end
end
case mode case mode
when :ccld when :ccld
cflags + args + cppflags + ldflags cflags + args + cppflags + ldflags
@ -123,7 +113,7 @@ class Cmd
end end
def refurbished_args def refurbished_args
@lset = Set.new(library_paths + system_library_paths) @lset = Set.new(library_paths)
@iset = Set.new(isystem_paths + include_paths) @iset = Set.new(isystem_paths + include_paths)
args = [] args = []
@ -162,6 +152,8 @@ class Cmd
/^-O[0-9zs]?$/, "-fast", "-no-cpp-precomp", /^-O[0-9zs]?$/, "-fast", "-no-cpp-precomp",
"-pedantic", "-pedantic-errors", "-Wno-long-double", "-pedantic", "-pedantic-errors", "-Wno-long-double",
"-Wno-unused-but-set-variable" "-Wno-unused-but-set-variable"
when "-liconv", "-lintl"
# Not needed on Linux.
when "-fopenmp", "-lgomp", "-mno-fused-madd", "-fforce-addr", "-fno-defer-pop", when "-fopenmp", "-lgomp", "-mno-fused-madd", "-fforce-addr", "-fno-defer-pop",
"-mno-dynamic-no-pic", "-fearly-inlining", /^-f(?:no-)?inline-functions-called-once/, "-mno-dynamic-no-pic", "-fearly-inlining", /^-f(?:no-)?inline-functions-called-once/,
/^-finline-limit/, /^-f(?:no-)?check-new/, "-fno-delete-null-pointer-checks", /^-finline-limit/, /^-f(?:no-)?check-new/, "-fno-delete-null-pointer-checks",
@ -183,18 +175,10 @@ class Cmd
args << arg args << arg
when /^-W.*/ when /^-W.*/
# prune warnings # prune warnings
when "-macosx_version_min", "-dylib_install_name"
args << "-Wl,#{arg},#{enum.next}"
when "-multiply_definedsuppress" when "-multiply_definedsuppress"
args << "-Wl,-multiply_defined,suppress" args << "-Wl,-multiply_defined,suppress"
when "-undefineddynamic_lookup" when "-undefineddynamic_lookup"
args << "-Wl,-undefined,dynamic_lookup" args << "-Wl,-undefined,dynamic_lookup"
when /^-isysroot/, /^--sysroot/
sdk = enum.next
# We set the sysroot for macOS SDKs
args << "-isysroot" << sdk unless sdk.downcase.include? "osx"
when "-dylib"
args << "-Wl,#{arg}"
when /^-I(.+)?/ when /^-I(.+)?/
# Support both "-Ifoo" (one argument) and "-I foo" (two arguments) # Support both "-Ifoo" (one argument) and "-I foo" (two arguments)
val = chuzzle($1) || enum.next val = chuzzle($1) || enum.next
@ -204,6 +188,11 @@ class Cmd
val = chuzzle($1) || enum.next val = chuzzle($1) || enum.next
path = canonical_path(val) path = canonical_path(val)
args << "-L#{val}" if keep?(path) && @lset.add?(path) args << "-L#{val}" if keep?(path) && @lset.add?(path)
when /^-R(.+)?/
# -R is not the correct way to pass rpaths to the linker
path = chuzzle($1) || enum.next
wl = "-Wl," unless mode == :ld
args << "#{wl}-rpath=" if keep?(path)
else else
args << arg args << arg
end end
@ -220,11 +209,8 @@ class Cmd
elsif path.start_with?(cellar) || path.start_with?(opt) elsif path.start_with?(cellar) || path.start_with?(opt)
dep = path[keg_regex, 2] dep = path[keg_regex, 2]
dep && @deps.include?(dep) dep && @deps.include?(dep)
elsif path.start_with?(prefix)
true
else else
# ignore MacPorts, Boxen's Homebrew, X11, fink path.start_with?(prefix, tmpdir)
!path.start_with?("/opt/local", "/opt/boxen/homebrew", "/opt/X11", "/sw", "/usr/X11")
end end
end end
@ -255,16 +241,12 @@ class Cmd
end end
def ldflags def ldflags
args = path_flags("-L", library_paths) wl = "-Wl," unless mode == :ld
case mode args = []
when :ld args += ["#{wl}--dynamic-linker=#{dynamic_linker_path}"] if dynamic_linker_path
args << "-headerpad_max_install_names" args << "-B#{@opt}/glibc/lib" unless mode == :ld
args << "-no_weak_imports" if no_weak_imports? args += path_flags("-L", library_paths)
when :ccld, :cxxld args += rpath_flags("#{wl}-rpath=", rpath_paths)
args << "-Wl,-headerpad_max_install_names"
args << "-Wl,-no_weak_imports" if no_weak_imports?
end
args
end end
def isystem_paths def isystem_paths
@ -279,10 +261,12 @@ class Cmd
path_split("HOMEBREW_LIBRARY_PATHS") path_split("HOMEBREW_LIBRARY_PATHS")
end end
def system_library_paths def rpath_paths
paths = ["#{sysroot}/usr/lib"] path_split("HOMEBREW_RPATH_PATHS")
paths << "/usr/local/lib" unless sysroot || ENV["SDKROOT"] end
paths
def dynamic_linker_path
chuzzle(ENV["HOMEBREW_DYNAMIC_LINKER"])
end end
def configure? def configure?
@ -310,10 +294,6 @@ class Cmd
config.include?("K") config.include?("K")
end end
def no_weak_imports?
config.include?("w")
end
def canonical_path(path) def canonical_path(path)
path = Pathname.new(path) path = Pathname.new(path)
path = path.realpath if path.exist? path = path.realpath if path.exist?
@ -325,6 +305,12 @@ class Cmd
paths.map! { |path| prefix + path } paths.map! { |path| prefix + path }
end end
# Unlike path_flags, do not prune non-existant directories.
# formula.lib for example does not yet exist, but should not be pruned.
def rpath_flags(prefix, paths)
paths.uniq.map { |path| prefix + path }
end
def path_split(key) def path_split(key)
ENV.fetch(key) { "" }.split(File::PATH_SEPARATOR) ENV.fetch(key) { "" }.split(File::PATH_SEPARATOR)
end end
@ -355,7 +341,7 @@ if __FILE__ == $PROGRAM_NAME
if (cc = ENV["HOMEBREW_CC"]).nil? || cc.empty? || cc == "cc" if (cc = ENV["HOMEBREW_CC"]).nil? || cc.empty? || cc == "cc"
# those values are not allowed # those values are not allowed
ENV["HOMEBREW_CC"] = "clang" ENV["HOMEBREW_CC"] = "gcc"
end end
####################################################################### main ####################################################################### main
@ -368,6 +354,13 @@ if __FILE__ == $PROGRAM_NAME
log(basename, ARGV, tool, args) log(basename, ARGV, tool, args)
args << { :close_others => false } superbin = Pathname.new(__FILE__).dirname.realpath
exec "#{dirname}/xcrun", tool, *args paths = ENV["PATH"].split(":").reject do |x|
path = Pathname.new(x)
path.directory? && path.realpath == superbin
end
paths.unshift "#{ENV["HOMEBREW_PREFIX"]}/bin"
ENV["PATH"] = paths.join(":")
args << { :close_others => false }
exec tool, *args
end end