aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/brewkit.rb
blob: c8702c4b16034a386284822184712e2f117af45e (plain)
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
#  Copyright 2009 Max Howell <max@methylblue.com>
#
#  This file is part of Homebrew.
#
#  Homebrew is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Homebrew is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with Homebrew.  If not, see <http://www.gnu.org/licenses/>.
#
require 'osx/cocoa' # to get number of cores
require 'formula'
require 'hw.model'

ENV['MACOSX_DEPLOYMENT_TARGET']='10.5'
ENV['CFLAGS']='-O3 -w -pipe -fomit-frame-pointer -mmacosx-version-min=10.5'
ENV['LDFLAGS']='' # to be consistent, we ignore the environment usually already

# optimise all the way to eleven, references:
# http://en.gentoo-wiki.com/wiki/Safe_Cflags/Intel
# http://forums.mozillazine.org/viewtopic.php?f=12&t=577299
# http://gcc.gnu.org/onlinedocs/gcc-4.2.1/gcc/i386-and-x86_002d64-Options.html
case hw_model
  when :core1
    # Core DUO is a 32 bit chip
    ENV['CFLAGS']="#{ENV['CFLAGS']} -march=prescott -mfpmath=sse -msse3 -mmmx"
  when :core2
    # Core 2 DUO is a 64 bit chip
    # GCC 4.3 will have a -march=core2, but for now nocona is correct
    ENV['CFLAGS']="#{ENV['CFLAGS']} -march=nocona -mfpmath=sse -msse3 -mmmx"
    
    # OK so we're not doing 64 bit yet... but we will with Snow Leopard
    # -mfpmath=sse defaults to on for the x64 compiler
    #ENV['CFLAGS']="#{ENV['CFLAGS']} -march=nocona -msse3 -mmmx -m64"
    #ENV['LDFLAGS']="-arch x86_64"

  when :xeon
    # TODO what optimisations for xeon?

  when :ppc   then abort "Sorry, Homebrew does not support PowerPC architectures"
  when :dunno then abort "Sorry, Homebrew cannot determine what kind of Mac this is!"
end

ENV['CXXFLAGS']=ENV['CFLAGS']

# lets use gcc 4.2, it is newer and "better", at least I believe so, mail me
# if I'm wrong
ENV['CC']='gcc-4.2'
ENV['CXX']='g++-4.2'
ENV['MAKEFLAGS']="-j#{OSX::NSProcessInfo.processInfo.processorCount}"


unless HOMEBREW_PREFIX.to_s == '/usr/local'
  ENV['CPPFLAGS']="-I#{HOMEBREW_PREFIX}/include"
  ENV['LDFLAGS']="-L#{HOMEBREW_PREFIX}/lib"
end


# you can use these functions for packages that have build issues
module HomebrewEnvExtension
  def deparallelize
    remove 'MAKEFLAGS', /-j\d+/
  end
  def gcc_4_0_1
    self['CC']=nil
    self['CXX']=nil
  end
  def osx_10_4
    self['MACOSX_DEPLOYMENT_TARGET']=nil
    remove_from_cflags(/ ?-mmacosx-version-min=10\.\d/)
  end
  def generic_i386
     %w[-mfpmath=sse -msse3 -mmmx -march=\w+].each {|s| remove_from_cflags s}
  end
  def libxml2
    self['CXXFLAGS']=self['CFLAGS']+=' -I/usr/include/libxml2'
  end
  def libpng
    append 'CPPFLAGS', '-I/usr/X11R6/include'
    append 'LDFLAGS', '-L/usr/X11R6/lib'
  end
  # we've seen some packages fail to build when warnings are disabled!
  def enable_warnings
    remove_from_cflags '-w'
  end
  
private
  def append key, value
    ref=self[key]
    if ref.nil? or ref.empty?
      self[key]=value
    else
      self[key]=ref+' '+value
    end
  end
  def remove key, rx
    return if self[key].nil?
    # sub! doesn't work as "the string is frozen"
    self[key]=self[key].sub rx, ''
    self[key]=nil if self[key].empty? # keep things clean
  end
  def remove_from_cflags rx
    %w[CFLAGS CXXFLAGS].each {|key| remove key, rx}
  end
end

ENV.extend HomebrewEnvExtension

# remove MacPorts and Fink from the PATH, this prevents issues like:
# http://github.com/mxcl/homebrew/issues/#issue/13
paths=ENV['PATH'].split(':').reject do |p|
  p.squeeze! '/'
  p=~%r[^/opt/local] or p=~%r[^/sw]
end
ENV['PATH']=paths*':'


def inreplace(path, before, after)
  before=Regexp.escape before.to_s
  before.gsub! "/", "\\/" # I guess not escaped as delimiter varies
  after=after.to_s
  after.gsub! "\\", "\\\\"
  after.gsub! "/", "\\/"

  # TODO this sucks
  # either use 'ed', or allow regexp and use a proper ruby function
  Kernel.system "perl", "-pi", "-e", "s/#{before}/#{after}/g", path
end