1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
#!/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -W0
$:.unshift "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8"
require 'pathname'
require 'set'
require 'stringio'
class Logger
def initialize
@io = StringIO.new
end
def puts(*args)
@io.puts(*args)
end
def log!
return unless ENV.key? 'HOMEBREW_CC_LOG_PATH'
path = "#{ENV['HOMEBREW_CC_LOG_PATH']}.cc"
puts
File.open(path, File::WRONLY | File::APPEND | File::CREAT) { |f| f.write(@io.string) }
end
end
LOGGER = Logger.new
class Cmd
attr_reader :brewfix, :brewtmp, :sdkroot
def initialize path, args
@arg0 = File.basename(path).freeze
@args = args.freeze
@brewfix = ENV['HOMEBREW_PREFIX']
@brewtmp = ENV['HOMEBREW_TEMP']
@sdkroot = ENV['HOMEBREW_SDKROOT']
end
def mode
if @arg0 == 'cpp' or @arg0 == 'ld'
@arg0.to_sym
elsif @args.include? '-c'
if @arg0 =~ /(?:c|g|clang)\+\+/
:cxx
else
:cc
end
elsif @args.include? '-E'
:ccE
else
if @arg0 =~ /(?:c|g|clang)\+\+/
:cxxld
else
:ccld
end
end
end
def tool
@tool ||= case @arg0
when 'ld' then 'ld'
when 'cpp' then 'cpp'
when /\w\+\+(-\d\.\d)?$/
case ENV['HOMEBREW_CC']
when /clang/
'clang++'
when /llvm-gcc/
'llvm-g++-4.2'
when /gcc(-\d\.\d)?$/
'g++' + $1.to_s
end
else
# 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.
ENV['HOMEBREW_CC']
end
end
def args
if @args.length == 1 and @args[0] == '-v'
# Don't add linker arguments if -v passed as sole option. This stops gcc
# -v with no other arguments from outputting a linker error. Some
# software uses gcc -v (wrongly) to sniff the GCC version.
return @args.dup
end
if !cccfg?("O") || tool == "ld" || configure?
args = @args.dup
else
args = refurbished_args
end
if sdkroot
if tool == "ld"
args << "-syslibroot" << sdkroot
else
args << "--sysroot=#{sdkroot}"
end
end
allflags = case mode
when :ccld
cflags + args + cppflags + ldflags
when :cxxld
cxxflags + args + cppflags + ldflags
when :cc
cflags + args + cppflags
when :cxx
cxxflags + args + cppflags
when :ccE
args + cppflags
when :cpp
args + cppflags
when :ld
ldflags + args
end.compact
make_fuss(allflags)
allflags
end
def refurbished_args
lset = Set.new(libpath + syslibpath)
iset = Set.new(cpath.flatten)
args = []
whittler = @args.each
loop do
case arg = whittler.next
when '-arch', /^-Xarch_/
whittler.next
when '-m32'
# If ENV.m32 was set, we allow the "-m32" flag, but we don't add anything
args << '-m32' if cccfg? '3'
when /^-g\d?/, /^-gstabs\d+/, '-gstabs+', /^-ggdb\d?/, '-gdwarf-2',
/^-march=.+/, /^-mtune=.+/, /^-mcpu=.+/, '-m64',
/^-O[0-9zs]?$/, '-fast', '-no-cpp-precomp',
'-pedantic', '-pedantic-errors'
when '-fopenmp', '-lgomp', '-mno-fused-madd', '-fforce-addr', '-fno-defer-pop',
'-mno-dynamic-no-pic', '-fearly-inlining', '-finline-functions-called-once',
/^-finline-limit/, /^-f(?:no-)?check-new/, '-fno-delete-null-pointer-checks',
'-fcaller-saves', '-fthread-jumps', '-fno-reorder-blocks'
# clang doesn't support these flags
args << arg if not tool =~ /^clang/
when /^-W.*/
args << arg if arg =~ /^-W[alp],/ or arg =~ /^-Wno-/
when '-macosx_version_min', '-dylib_install_name'
args << "-Wl,#{arg},#{whittler.next}"
when '-multiply_definedsuppress'
args << "-Wl,-multiply_defined,suppress"
when '-undefineddynamic_lookup'
args << "-Wl,-undefined,dynamic_lookup"
when /^-isysroot/
# We set the sysroot
whittler.next
when '-dylib'
args << "-Wl,#{arg}"
when /^-I(.+)?/
# Support both "-Ifoo" (one argument) and "-I foo" (two arguments)
val = chuzzle($1) || whittler.next
path = canonical_path(val)
args << "-I#{val}" if keep?(path) and iset.add?(path)
when /^-L(.+)?/
val = chuzzle($1) || whittler.next
path = canonical_path(val)
args << "-L#{val}" if keep?(path) and lset.add?(path)
else
args << arg
end
end
args
end
def keep? path
case path
when %r{^#{Regexp.escape(brewfix)}}, %r{^#{Regexp.escape(brewtmp)}}
# maybe homebrew is installed to /sw or /opt/brew
true
when %r{^/opt}, %r{^/sw}, %r{/usr/X11}
false
else
true
end
end
def cflags
args = []
return args unless cccfg? 'O' or configure?
args << '-pipe'
args << '-w' unless configure?
args.concat(optflags)
args.concat(archflags)
args << "-std=#{@arg0}" if @arg0 =~ /c[89]9/
args
end
def cxxflags
args = cflags
args << '-std=c++11' if cccfg? 'x'
args << '-stdlib=libc++' if cccfg? 'g'
args << '-stdlib=libstdc++' if cccfg? 'h'
args
end
def optflags
args = []
args << "-#{ENV['HOMEBREW_OPTIMIZATION_LEVEL']}"
args.concat ENV['HOMEBREW_OPTFLAGS'].split(' ') if ENV['HOMEBREW_OPTFLAGS']
args
end
def archflags
args = []
args.concat ENV['HOMEBREW_ARCHFLAGS'].split(' ') if cccfg? 'u'
args
end
def syspath
if sdkroot
%W{#{sdkroot}/usr #{sdkroot}/usr/local}
else
%W{/usr /usr/local}
end
end
def syslibpath
# We reject brew's lib as we explicitly add this as a -L flag, thus it
# is given higher priority by cc, so it surpasses the system libpath.
# NOTE this only counts if Homebrew is installed at /usr/local
syspath.reject { |d| d == brewfix }.map! { |d| File.join(d, "lib") }
end
def cpath
cpath = path_split("CMAKE_PREFIX_PATH").map! { |d| File.join(d, "include") }
cpath += path_split("CMAKE_INCLUDE_PATH")
opt = cpath.grep(%r{^#{Regexp.escape(brewfix)}/opt})
sys = cpath - opt
[sys, opt]
end
def libpath
libpath = path_split("CMAKE_PREFIX_PATH").map! { |d| File.join(d, "lib") }
libpath += path_split("CMAKE_LIBRARY_PATH")
libpath -= syslibpath
libpath
end
def ldflags
args = path_flags("-L", libpath)
case mode
when :ld then args << '-headerpad_max_install_names'
when :ccld then args << '-Wl,-headerpad_max_install_names'
when :cxxld
args << '-Wl,-headerpad_max_install_names'
args << '-stdlib=libc++' if cccfg? 'g'
args << '-stdlib=libstdc++' if cccfg? 'h'
end
args
end
def cppflags
sys, opt = cpath
# we want our keg-only includes to be found before system includes *and*
# before any other includes the build-system adds
path_flags("-isystem", sys) + path_flags("-I", opt)
end
def make_fuss args
return unless make_fuss?
dels = @args - args
adds = args - @args
LOGGER.puts "superenv removed: #{dels*' '}" unless dels.empty?
LOGGER.puts "superenv added: #{adds*' '}" unless adds.empty?
end
def make_fuss?
cccfg? 'O' and not configure?
end
def configure?
# configure scripts generated with autoconf 2.61 or later export as_nl
ENV.key? 'as_nl'
end
def cccfg? flags
flags.split('').all?{|c| ENV['HOMEBREW_CCCFG'].include? c } if ENV['HOMEBREW_CCCFG']
end
def canonical_path(path)
path = Pathname.new(path)
path = path.realpath if path.exist?
path.to_s
end
def path_flags(prefix, paths)
paths = paths.uniq.select { |path| File.directory?(path) }
paths.map! { |path| prefix + path }
end
def path_split(key)
ENV.fetch(key) { "" }.split(File::PATH_SEPARATOR)
end
def chuzzle(val)
return val if val.nil?
val = val.chomp
return val unless val.empty?
end
end
if __FILE__ == $PROGRAM_NAME
##################################################################### sanity
abort "The build-tool has reset ENV. --env=std required." unless ENV['HOMEBREW_BREW_FILE']
if (cc = ENV["HOMEBREW_CC"]).nil? || cc.empty? || cc == "cc"
# those values are not allowed
ENV['HOMEBREW_CC'] = 'clang'
end
####################################################################### main
LOGGER.puts "#{File.basename($0)} called with: #{ARGV.join(" ")}"
cmd = Cmd.new($0, ARGV)
tool, args = cmd.tool, cmd.args
LOGGER.puts "superenv executed: #{tool} #{args.join(" ")}"
LOGGER.log!
exec "xcrun", tool, *args
end
|