Module: Stdenv

Includes:
SharedEnvExtension
Defined in:
Library/Homebrew/extend/ENV/std.rb,
Library/Homebrew/extend/os/mac/extend/ENV/std.rb,
Library/Homebrew/extend/os/linux/extend/ENV/std.rb

Constant Summary collapse

SAFE_CFLAGS_FLAGS =
"-w -pipe".freeze
DEFAULT_FLAGS =
"-march=core2 -msse4".freeze

Constants included from SharedEnvExtension

SharedEnvExtension::CC_FLAG_VARS, SharedEnvExtension::FC_FLAG_VARS, SharedEnvExtension::SANITIZED_VARS

Constants included from CompilerConstants

CompilerConstants::COMPILERS, CompilerConstants::COMPILER_SYMBOL_MAP, CompilerConstants::GNU_GCC_REGEXP, CompilerConstants::GNU_GCC_VERSIONS

Instance Method Summary collapse

Methods included from SharedEnvExtension

#append, #append_path, #append_to_cflags, #cc, #cflags, #compiler, #cppflags, #cxx, #cxxflags, #fc, #fcflags, #fflags, #fortran, #gcc_version_formula, #ld64, #ldflags, #ncurses_define, #no_weak_imports_support?, #permit_arch_flags, #permit_weak_imports, #prepend, #prepend_create_path, #prepend_path, #remove, #remove_cc_etc, #remove_from_cflags, #reset, #userpaths!, #warn_about_non_apple_gcc

Instance Method Details

#clangObject



118
119
120
121
122
123
124
125
# File 'Library/Homebrew/extend/ENV/std.rb', line 118

def clang
  super
  replace_in_cflags(/-Xarch_#{Hardware::CPU.arch_32_bit} (-march=\S*)/, '\1')
  # Clang mistakenly enables AES-NI on plain Nehalem
  map = Hardware::CPU.optimization_flags
  map = map.merge(nehalem: "-march=native -Xclang -target-feature -Xclang -aes")
  set_cpu_cflags "-march=native", map
end

#cxx11Object



164
165
166
167
168
169
170
171
172
173
# File 'Library/Homebrew/extend/ENV/std.rb', line 164

def cxx11
  if compiler == :clang
    append "CXX", "-std=c++11"
    append "CXX", "-stdlib=libc++"
  elsif gcc_with_cxx11_support?(compiler)
    append "CXX", "-std=c++11"
  else
    raise "The selected compiler doesn't support C++11: #{compiler}"
  end
end

#define_cflags(val) ⇒ Object

Convenience method to set all C compiler flags in one shot.



191
192
193
# File 'Library/Homebrew/extend/ENV/std.rb', line 191

def define_cflags(val)
  CC_FLAG_VARS.each { |key| self[key] = val }
end

#deparallelizeObject

Removes the MAKEFLAGS environment variable, causing make to use a single job. This is useful for makefiles with race conditions. When passed a block, MAKEFLAGS is removed only for the duration of the block and is restored after its completion.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'Library/Homebrew/extend/ENV/std.rb', line 68

def deparallelize
  old = self["MAKEFLAGS"]
  remove "MAKEFLAGS", /-j\d+/
  if block_given?
    begin
      yield
    ensure
      self["MAKEFLAGS"] = old
    end
  end

  old
end

#determine_ccObject



90
91
92
93
# File 'Library/Homebrew/extend/ENV/std.rb', line 90

def determine_cc
  s = super
  DevelopmentTools.locate(s) || Pathname.new(s)
end

#determine_cxxObject



96
97
98
99
# File 'Library/Homebrew/extend/ENV/std.rb', line 96

def determine_cxx
  dir, base = determine_cc.split
  dir/base.to_s.sub("gcc", "g++").sub("clang", "clang++")
end

#determine_pkg_config_libdirObject



56
57
58
59
60
61
62
63
# File 'Library/Homebrew/extend/ENV/std.rb', line 56

def determine_pkg_config_libdir
  PATH.new(
    HOMEBREW_PREFIX/"lib/pkgconfig",
    HOMEBREW_PREFIX/"share/pkgconfig",
    homebrew_extra_pkg_config_paths,
    "/usr/lib/pkgconfig",
  ).existing
end

#effective_archObject



213
214
215
216
217
218
219
220
221
222
223
224
# File 'Library/Homebrew/extend/ENV/std.rb', line 213

def effective_arch
  if ARGV.build_bottle?
    ARGV.bottle_arch || Hardware.oldest_cpu
  elsif Hardware::CPU.intel? && !Hardware::CPU.sse4?
    # If the CPU doesn't support SSE4, we cannot trust -march=native or
    # -march=<cpu family> to do the right thing because we might be running
    # in a VM or on a Hackintosh.
    Hardware.oldest_cpu
  else
    Hardware::CPU.family
  end
end

#enable_warningsObject

we've seen some packages fail to build when warnings are disabled!



138
139
140
# File 'Library/Homebrew/extend/ENV/std.rb', line 138

def enable_warnings
  remove_from_cflags "-w"
end

#gcc_4_0Object



101
102
103
104
# File 'Library/Homebrew/extend/ENV/std.rb', line 101

def gcc_4_0
  super
  set_cpu_cflags "-march=nocona -mssse3"
end

#gcc_4_2Object



106
107
108
109
# File 'Library/Homebrew/extend/ENV/std.rb', line 106

def gcc_4_2
  super
  set_cpu_cflags
end

#generic_minimal_optimizationObject



130
131
132
# File 'Library/Homebrew/extend/ENV/std.rb', line 130

def minimal_optimization
  define_cflags "-Os #{SAFE_CFLAGS_FLAGS}"
end

#generic_no_optimizationObject



135
136
137
# File 'Library/Homebrew/extend/ENV/std.rb', line 135

def no_optimization
  define_cflags SAFE_CFLAGS_FLAGS
end

#generic_set_cpu_flagsObject

Sets architecture-specific flags for every environment variable given in the list `flags`.



208
209
210
211
212
213
214
215
216
217
# File 'Library/Homebrew/extend/ENV/std.rb', line 208

def set_cpu_flags(flags, default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags)
  cflags =~ /(-Xarch_#{Hardware::CPU.arch_32_bit} )-march=/
  xarch = Regexp.last_match(1).to_s
  remove flags, /(-Xarch_#{Hardware::CPU.arch_32_bit} )?-march=\S*/
  remove flags, /( -Xclang \S+)+/
  remove flags, /-mssse3/
  remove flags, /-msse4(\.\d)?/
  append flags, xarch unless xarch.empty?
  append flags, map.fetch(effective_arch, default)
end

#generic_setup_build_environmentObject



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
# File 'Library/Homebrew/extend/ENV/std.rb', line 50

def setup_build_environment(formula = nil)
  super

  PATH.new(ENV["HOMEBREW_PATH"]).each { |p| prepend_path "PATH", p }

  # Set the default pkg-config search path, overriding the built-in paths
  # Anything in PKG_CONFIG_PATH is searched before paths in this variable
  self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir

  self["MAKEFLAGS"] = "-j#{make_jobs}"

  unless HOMEBREW_PREFIX.to_s == "/usr/local"
    # /usr/local is already an -isystem and -L directory so we skip it
    self["CPPFLAGS"] = "-isystem#{HOMEBREW_PREFIX}/include"
    self["LDFLAGS"] = "-L#{HOMEBREW_PREFIX}/lib"
    # CMake ignores the variables above
    self["CMAKE_PREFIX_PATH"] = HOMEBREW_PREFIX.to_s
  end

  frameworks = HOMEBREW_PREFIX.join("Frameworks")
  if frameworks.directory?
    append "CPPFLAGS", "-F#{frameworks}"
    append "LDFLAGS", "-F#{frameworks}"
    self["CMAKE_FRAMEWORK_PATH"] = frameworks.to_s
  end

  # Os is the default Apple uses for all its stuff so let's trust them
  define_cflags "-Os #{SAFE_CFLAGS_FLAGS}"

  append "LDFLAGS", "-Wl,-headerpad_max_install_names"

  send(compiler)

  return unless cc =~ GNU_GCC_REGEXP
  gcc_formula = gcc_version_formula($&)
  append_path "PATH", gcc_formula.opt_bin.to_s
end

#homebrew_extra_pkg_config_pathsObject



52
53
54
# File 'Library/Homebrew/extend/ENV/std.rb', line 52

def homebrew_extra_pkg_config_paths
  []
end

#libcxxObject



175
176
177
# File 'Library/Homebrew/extend/ENV/std.rb', line 175

def libcxx
  append "CXX", "-stdlib=libc++" if compiler == :clang
end

#libstdcxxObject



179
180
181
# File 'Library/Homebrew/extend/ENV/std.rb', line 179

def libstdcxx
  append "CXX", "-stdlib=libstdc++" if compiler == :clang
end

#libxml2Object

Some configure scripts won't find libxml2 without help



104
105
106
107
108
109
110
111
# File 'Library/Homebrew/extend/os/mac/extend/ENV/std.rb', line 104

def libxml2
  if MacOS::CLT.installed?
    append "CPPFLAGS", "-I/usr/include/libxml2"
  else
    # Use the includes form the sdk
    append "CPPFLAGS", "-I#{MacOS.sdk_path}/usr/include/libxml2"
  end
end

#m32Object



147
148
149
150
# File 'Library/Homebrew/extend/ENV/std.rb', line 147

def m32
  append_to_cflags "-m32"
  append "LDFLAGS", "-arch #{Hardware::CPU.arch_32_bit}"
end

#m64Object



142
143
144
145
# File 'Library/Homebrew/extend/ENV/std.rb', line 142

def m64
  append_to_cflags "-m64"
  append "LDFLAGS", "-arch #{Hardware::CPU.arch_64_bit}"
end

#macosxsdk(version = MacOS.version) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'Library/Homebrew/extend/os/mac/extend/ENV/std.rb', line 80

def macosxsdk(version = MacOS.version)
  # Sets all needed lib and include dirs to CFLAGS, CPPFLAGS, LDFLAGS.
  remove_macosxsdk
  version = version.to_s
  append_to_cflags("-mmacosx-version-min=#{version}")
  self["CPATH"] = "#{HOMEBREW_PREFIX}/include"
  prepend "LDFLAGS", "-L#{HOMEBREW_PREFIX}/lib"

  return unless (sdk = MacOS.sdk_path(version)) && !MacOS::CLT.installed?
  # Extra setup to support Xcode 4.3+ without CLT.
  self["SDKROOT"] = sdk
  # Tell clang/gcc where system include's are:
  append_path "CPATH", "#{sdk}/usr/include"
  # The -isysroot is needed, too, because of the Frameworks
  append_to_cflags "-isysroot #{sdk}"
  append "CPPFLAGS", "-isysroot #{sdk}"
  # And the linker needs to find sdk/usr/lib
  append "LDFLAGS", "-isysroot #{sdk}"
  # Needed to build cmake itself and perhaps some cmake projects:
  append_path "CMAKE_PREFIX_PATH", "#{sdk}/usr"
  append_path "CMAKE_FRAMEWORK_PATH", "#{sdk}/System/Library/Frameworks"
end

#make_jobsObject



231
232
233
234
235
236
237
238
# File 'Library/Homebrew/extend/ENV/std.rb', line 231

def make_jobs
  # '-j' requires a positive integral argument
  if (jobs = self["HOMEBREW_MAKE_JOBS"].to_i).positive?
    jobs
  else
    Hardware::CPU.cores
  end
end

#minimal_optimizationObject



127
128
129
# File 'Library/Homebrew/extend/ENV/std.rb', line 127

def minimal_optimization
  define_cflags "-Os #{SAFE_CFLAGS_FLAGS}"
end

#no_optimizationObject



132
133
134
# File 'Library/Homebrew/extend/ENV/std.rb', line 132

def no_optimization
  define_cflags SAFE_CFLAGS_FLAGS
end

#no_weak_importsObject



140
141
142
# File 'Library/Homebrew/extend/os/mac/extend/ENV/std.rb', line 140

def no_weak_imports
  append "LDFLAGS", "-Wl,-no_weak_imports" if no_weak_imports_support?
end

#refurbish_argsObject

This method does nothing in stdenv since there's no arg refurbishment



242
# File 'Library/Homebrew/extend/ENV/std.rb', line 242

def refurbish_args; end

#remove_macosxsdk(version = MacOS.version) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'Library/Homebrew/extend/os/mac/extend/ENV/std.rb', line 58

def remove_macosxsdk(version = MacOS.version)
  # Clear all lib and include dirs from CFLAGS, CPPFLAGS, LDFLAGS that were
  # previously added by macosxsdk
  version = version.to_s
  remove_from_cflags(/ ?-mmacosx-version-min=10\.\d+/)
  delete("CPATH")
  remove "LDFLAGS", "-L#{HOMEBREW_PREFIX}/lib"

  return unless (sdk = MacOS.sdk_path(version)) && !MacOS::CLT.installed?
  delete("SDKROOT")
  remove_from_cflags "-isysroot #{sdk}"
  remove "CPPFLAGS", "-isysroot #{sdk}"
  remove "LDFLAGS", "-isysroot #{sdk}"
  if HOMEBREW_PREFIX.to_s == "/usr/local"
    delete("CMAKE_PREFIX_PATH")
  else
    # It was set in setup_build_environment, so we have to restore it here.
    self["CMAKE_PREFIX_PATH"] = HOMEBREW_PREFIX.to_s
  end
  remove "CMAKE_FRAMEWORK_PATH", "#{sdk}/System/Library/Frameworks"
end

#replace_in_cflags(before, after) ⇒ Object



184
185
186
187
188
# File 'Library/Homebrew/extend/ENV/std.rb', line 184

def replace_in_cflags(before, after)
  CC_FLAG_VARS.each do |key|
    self[key] = self[key].sub(before, after) if key?(key)
  end
end

#set_cpu_cflags(default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags) ⇒ Object



227
228
229
# File 'Library/Homebrew/extend/ENV/std.rb', line 227

def set_cpu_cflags(default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags)
  set_cpu_flags CC_FLAG_VARS, default, map
end

#set_cpu_flags(flags, default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags) ⇒ Object

Sets architecture-specific flags for every environment variable given in the list `flags`.



198
199
200
201
202
203
204
205
206
207
# File 'Library/Homebrew/extend/ENV/std.rb', line 198

def set_cpu_flags(flags, default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags)
  cflags =~ /(-Xarch_#{Hardware::CPU.arch_32_bit} )-march=/
  xarch = Regexp.last_match(1).to_s
  remove flags, /(-Xarch_#{Hardware::CPU.arch_32_bit} )?-march=\S*/
  remove flags, /( -Xclang \S+)+/
  remove flags, /-mssse3/
  remove flags, /-msse4(\.\d)?/
  append flags, xarch unless xarch.empty?
  append flags, map.fetch(effective_arch, default)
end

#setup_build_environment(formula = nil) ⇒ Object



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
# File 'Library/Homebrew/extend/ENV/std.rb', line 13

def setup_build_environment(formula = nil)
  super

  PATH.new(ENV["HOMEBREW_PATH"]).each { |p| prepend_path "PATH", p }

  # Set the default pkg-config search path, overriding the built-in paths
  # Anything in PKG_CONFIG_PATH is searched before paths in this variable
  self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir

  self["MAKEFLAGS"] = "-j#{make_jobs}"

  unless HOMEBREW_PREFIX.to_s == "/usr/local"
    # /usr/local is already an -isystem and -L directory so we skip it
    self["CPPFLAGS"] = "-isystem#{HOMEBREW_PREFIX}/include"
    self["LDFLAGS"] = "-L#{HOMEBREW_PREFIX}/lib"
    # CMake ignores the variables above
    self["CMAKE_PREFIX_PATH"] = HOMEBREW_PREFIX.to_s
  end

  frameworks = HOMEBREW_PREFIX.join("Frameworks")
  if frameworks.directory?
    append "CPPFLAGS", "-F#{frameworks}"
    append "LDFLAGS", "-F#{frameworks}"
    self["CMAKE_FRAMEWORK_PATH"] = frameworks.to_s
  end

  # Os is the default Apple uses for all its stuff so let's trust them
  define_cflags "-Os #{SAFE_CFLAGS_FLAGS}"

  append "LDFLAGS", "-Wl,-headerpad_max_install_names"

  send(compiler)

  return unless cc =~ GNU_GCC_REGEXP
  gcc_formula = gcc_version_formula($&)
  append_path "PATH", gcc_formula.opt_bin.to_s
end

#universal_binaryObject



152
153
154
155
156
157
158
159
160
161
162
# File 'Library/Homebrew/extend/ENV/std.rb', line 152

def universal_binary
  check_for_compiler_universal_support

  append_to_cflags Hardware::CPU.universal_archs.as_arch_flags
  append "LDFLAGS", Hardware::CPU.universal_archs.as_arch_flags

  return if compiler == :clang
  return unless Hardware.is_32_bit?
  # Can't mix "-march" for a 32-bit CPU  with "-arch x86_64"
  replace_in_cflags(/-march=\S*/, "-Xarch_#{Hardware::CPU.arch_32_bit} \\0")
end

#x11Object



210
# File 'Library/Homebrew/extend/ENV/std.rb', line 210

def x11; end