PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/Homebrew/extend/ENV.rb

https://github.com/jayzawrotny/homebrew
Ruby | 291 lines | 220 code | 34 blank | 37 comment | 13 complexity | 8e22cb2f2d3174635a5652eca40b8366 MD5 | raw file
  1. module HomebrewEnvExtension
  2. # -w: keep signal to noise high
  3. SAFE_CFLAGS_FLAGS = "-w -pipe"
  4. def setup_build_environment
  5. # Clear CDPATH to avoid make issues that depend on changing directories
  6. delete('CDPATH')
  7. delete('CPPFLAGS')
  8. delete('LDFLAGS')
  9. self['MAKEFLAGS']="-j#{Hardware.processor_count}"
  10. unless HOMEBREW_PREFIX.to_s == '/usr/local'
  11. # /usr/local is already an -isystem and -L directory so we skip it
  12. self['CPPFLAGS'] = "-isystem #{HOMEBREW_PREFIX}/include"
  13. self['LDFLAGS'] = "-L#{HOMEBREW_PREFIX}/lib"
  14. # CMake ignores the variables above
  15. self['CMAKE_PREFIX_PATH'] = "#{HOMEBREW_PREFIX}"
  16. end
  17. if MACOS_VERSION >= 10.6 and (self['HOMEBREW_USE_LLVM'] or ARGV.include? '--use-llvm')
  18. self['CC'] = "#{MacOS.xcode_prefix}/usr/bin/llvm-gcc"
  19. self['CXX'] = "#{MacOS.xcode_prefix}/usr/bin/llvm-g++"
  20. cflags = ['-O4'] # link time optimisation baby!
  21. elsif MACOS_VERSION >= 10.6 and (self['HOMEBREW_USE_GCC'] or ARGV.include? '--use-gcc')
  22. self['CC'] = "#{MacOS.xcode_prefix}/usr/bin/gcc"
  23. self['CXX'] = "#{MacOS.xcode_prefix}/usr/bin/g++"
  24. cflags = ['-O3']
  25. else
  26. # If these aren't set, many formulae fail to build
  27. self['CC'] = '/usr/bin/cc'
  28. self['CXX'] = '/usr/bin/c++'
  29. cflags = ['-O3']
  30. end
  31. # In rare cases this may break your builds, as the tool for some reason wants
  32. # to use a specific linker. However doing this in general causes formula to
  33. # build more successfully because we are changing CC and many build systems
  34. # don't react properly to that.
  35. self['LD'] = self['CC']
  36. # Optimise all the way to eleven, references:
  37. # http://en.gentoo-wiki.com/wiki/Safe_Cflags/Intel
  38. # http://forums.mozillazine.org/viewtopic.php?f=12&t=577299
  39. # http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/i386-and-x86_002d64-Options.html
  40. # We don't set, eg. -msse3 because the march flag does that for us:
  41. # http://gcc.gnu.org/onlinedocs/gcc-4.3.3/gcc/i386-and-x86_002d64-Options.html
  42. if MACOS_VERSION >= 10.6
  43. case Hardware.intel_family
  44. when :nehalem, :penryn, :core2
  45. # the 64 bit compiler adds -mfpmath=sse for us
  46. cflags << "-march=core2"
  47. when :core
  48. cflags<<"-march=prescott"<<"-mfpmath=sse"
  49. end
  50. # gcc doesn't auto add msse4 or above (based on march flag) yet
  51. case Hardware.intel_family
  52. when :nehalem
  53. cflags << "-msse4" # means msse4.2 and msse4.1
  54. when :penryn
  55. cflags << "-msse4.1"
  56. end
  57. else
  58. # gcc 4.0 didn't support msse4
  59. case Hardware.intel_family
  60. when :nehalem, :penryn, :core2
  61. cflags<<"-march=nocona"
  62. when :core
  63. cflags<<"-march=prescott"
  64. end
  65. cflags<<"-mfpmath=sse"
  66. end
  67. self['CFLAGS'] = self['CXXFLAGS'] = "#{cflags*' '} #{SAFE_CFLAGS_FLAGS}"
  68. end
  69. def deparallelize
  70. remove 'MAKEFLAGS', /-j\d+/
  71. end
  72. alias_method :j1, :deparallelize
  73. # recommended by Apple, but, eg. wget won't compile with this flag, so…
  74. def fast
  75. remove_from_cflags(/-O./)
  76. append_to_cflags '-fast'
  77. end
  78. def O4
  79. # LLVM link-time optimization
  80. remove_from_cflags(/-O./)
  81. append_to_cflags '-O4'
  82. end
  83. def O3
  84. # Sometimes O4 just takes fucking forever
  85. remove_from_cflags(/-O./)
  86. append_to_cflags '-O3'
  87. end
  88. def O2
  89. # Sometimes O3 doesn't work or produces bad binaries
  90. remove_from_cflags(/-O./)
  91. append_to_cflags '-O2'
  92. end
  93. def Os
  94. # Sometimes you just want a small one
  95. remove_from_cflags(/-O./)
  96. append_to_cflags '-Os'
  97. end
  98. def Og
  99. # Sometimes you want a debug build
  100. remove_from_cflags(/-O./)
  101. append_to_cflags '-g -O0'
  102. end
  103. def gcc_4_0_1
  104. self['CC'] = self['LD'] = '/usr/bin/gcc-4.0'
  105. self['CXX'] = '/usr/bin/g++-4.0'
  106. self.O3
  107. remove_from_cflags '-march=core2'
  108. remove_from_cflags %r{-msse4(\.\d)?}
  109. end
  110. alias_method :gcc_4_0, :gcc_4_0_1
  111. def gcc_4_2
  112. # Sometimes you want to downgrade from LLVM to GCC 4.2
  113. self['CC']="/usr/bin/gcc-4.2"
  114. self['CXX']="/usr/bin/g++-4.2"
  115. self['LD']=self['CC']
  116. self.O3
  117. end
  118. def llvm
  119. self['CC'] = "#{MacOS.xcode_prefix}/usr/bin/llvm-gcc"
  120. self['CXX'] = "#{MacOS.xcode_prefix}/usr/bin/llvm-g++"
  121. self['LD'] = self['CC']
  122. self.O4
  123. end
  124. def fortran
  125. if self['FC']
  126. ohai "Building with an alternative Fortran compiler. This is unsupported."
  127. self['F77'] = self['FC'] unless self['F77']
  128. if ARGV.include? '--default-fortran-flags'
  129. self['FCFLAGS'] = self['CFLAGS'] unless self['FCFLAGS']
  130. self['FFFLAGS'] = self['CFLAGS'] unless self['FFFLAGS']
  131. elsif not self['FCFLAGS'] or self['FFLAGS']
  132. opoo <<-EOS
  133. No Fortran optimization information was provided. You may want to consider
  134. setting FCFLAGS and FFLAGS or pass the `--default-fortran-flags` option to
  135. `brew install` if your compiler is compatible with GCC.
  136. If you like the default optimization level of your compiler, ignore this
  137. warning.
  138. EOS
  139. end
  140. elsif `/usr/bin/which gfortran`.chomp.size > 0
  141. ohai <<-EOS
  142. Using Homebrew-provided fortran compiler.
  143. This may be changed by setting the FC environment variable.
  144. EOS
  145. self['FC'] = `/usr/bin/which gfortran`.chomp
  146. self['F77'] = self['FC']
  147. self['FCFLAGS'] = self['CFLAGS']
  148. self['FFLAGS'] = self['CFLAGS']
  149. else
  150. onoe <<-EOS
  151. This formula requires a fortran compiler, but we could not find one by
  152. looking at the FC environment variable or searching your PATH for `gfortran`.
  153. Please take one of the following actions:
  154. - Decide to use the build of gfortran 4.2.x provided by Homebrew using
  155. `brew install gfortran`
  156. - Choose another Fortran compiler by setting the FC environment variable:
  157. export FC=/path/to/some/fortran/compiler
  158. Using an alternative compiler may produce more efficient code, but we will
  159. not be able to provide support for build errors.
  160. EOS
  161. exit 1
  162. end
  163. end
  164. def osx_10_4
  165. self['MACOSX_DEPLOYMENT_TARGET']="10.4"
  166. remove_from_cflags(/ ?-mmacosx-version-min=10\.\d/)
  167. append_to_cflags('-mmacosx-version-min=10.4')
  168. end
  169. def osx_10_5
  170. self['MACOSX_DEPLOYMENT_TARGET']="10.5"
  171. remove_from_cflags(/ ?-mmacosx-version-min=10\.\d/)
  172. append_to_cflags('-mmacosx-version-min=10.5')
  173. end
  174. def minimal_optimization
  175. self['CFLAGS'] = self['CXXFLAGS'] = "-Os #{SAFE_CFLAGS_FLAGS}"
  176. end
  177. def no_optimization
  178. self['CFLAGS'] = self['CXXFLAGS'] = SAFE_CFLAGS_FLAGS
  179. end
  180. # Some configure scripts won't find libxml2 without help
  181. def libxml2
  182. append_to_cflags '-I/usr/include/libxml2'
  183. end
  184. def x11
  185. opoo "You do not have X11 installed, this formula may not build." if not MacOS.x11_installed?
  186. # There are some config scripts (e.g. freetype) here that should go in the path
  187. prepend 'PATH', '/usr/X11/bin', ':'
  188. # CPPFLAGS are the C-PreProcessor flags, *not* C++!
  189. append 'CPPFLAGS', '-I/usr/X11/include'
  190. append 'LDFLAGS', '-L/usr/X11/lib'
  191. # CMake ignores the variables above
  192. append 'CMAKE_PREFIX_PATH', '/usr/X11', ':'
  193. end
  194. alias_method :libpng, :x11
  195. # we've seen some packages fail to build when warnings are disabled!
  196. def enable_warnings
  197. remove_from_cflags '-w'
  198. end
  199. # Snow Leopard defines an NCURSES value the opposite of most distros
  200. # See: http://bugs.python.org/issue6848
  201. def ncurses_define
  202. append 'CPPFLAGS', "-DNCURSES_OPAQUE=0"
  203. end
  204. # Shortcuts for reading common flags
  205. def cc; self['CC'] or "gcc"; end
  206. def cxx; self['CXX'] or "g++"; end
  207. def cflags; self['CFLAGS']; end
  208. def cppflags;self['CPPLAGS']; end
  209. def ldflags; self['LDFLAGS']; end
  210. def m64
  211. append_to_cflags '-m64'
  212. append 'LDFLAGS', '-arch x86_64'
  213. end
  214. def m32
  215. append_to_cflags '-m32'
  216. append 'LDFLAGS', '-arch i386'
  217. end
  218. # i386 and x86_64 (no PPC)
  219. def universal_binary
  220. append_to_cflags '-arch i386 -arch x86_64'
  221. self.O3 if self['CFLAGS'].include? '-O4' # O4 seems to cause the build to fail
  222. append 'LDFLAGS', '-arch i386 -arch x86_64'
  223. # Can't mix "-march" for a 32-bit CPU with "-arch x86_64"
  224. remove_from_cflags(/-march=\S*/) if Hardware.is_32_bit?
  225. end
  226. def prepend key, value, separator = ' '
  227. # Value should be a string, but if it is a pathname then coerce it.
  228. value = value.to_s
  229. unless self[key].to_s.empty?
  230. self[key] = value + separator + self[key]
  231. else
  232. self[key] = value
  233. end
  234. end
  235. def append key, value, separator = ' '
  236. # Value should be a string, but if it is a pathname then coerce it.
  237. value = value.to_s
  238. unless self[key].to_s.empty?
  239. self[key] = self[key] + separator + value
  240. else
  241. self[key] = value
  242. end
  243. end
  244. def append_to_cflags f
  245. append 'CFLAGS', f
  246. append 'CXXFLAGS', f
  247. end
  248. def remove key, value
  249. return if self[key].nil?
  250. self[key] = self[key].sub value, '' # can't use sub! on ENV
  251. self[key] = nil if self[key].empty? # keep things clean
  252. end
  253. def remove_from_cflags f
  254. remove 'CFLAGS', f
  255. remove 'CXXFLAGS', f
  256. end
  257. end