Module: SharedEnvExtension

Included in:
Superenv
Defined in:
Library/Homebrew/extend/ENV/shared.rb,
Library/Homebrew/extend/os/mac/extend/ENV/shared.rb,
Library/Homebrew/extend/os/linux/extend/ENV/shared.rb

Overview

Homebrew extends Ruby's ENV to make our code more readable. Implemented in SharedEnvExtension and either Superenv or Stdenv (depending on the build mode).

See Also:

Instance Method Summary collapse

Instance Method Details

#append(keys, value, separator = " ") ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
# File 'Library/Homebrew/extend/ENV/shared.rb', line 66

def append(keys, value, separator = " ")
  value = value.to_s
  Array(keys).each do |key|
    old = self[key]
    if old.nil? || old.empty?
      self[key] = value
    else
      self[key] += separator + value
    end
  end
end

#append_path(key, path) ⇒ Object


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

def append_path(key, path)
  self[key] = PATH.new(self[key]).append(path)
end

#append_to_cccfg(value) ⇒ Object


62
63
64
# File 'Library/Homebrew/extend/ENV/shared.rb', line 62

def append_to_cccfg(value)
  append("HOMEBREW_CCCFG", value, "")
end

#append_to_cflags(newflags) ⇒ Object


54
55
56
# File 'Library/Homebrew/extend/ENV/shared.rb', line 54

def append_to_cflags(newflags)
  append(CC_FLAG_VARS, newflags)
end

#ccObject


124
125
126
# File 'Library/Homebrew/extend/ENV/shared.rb', line 124

def cc
  self["CC"]
end

#cflagsObject


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

def cflags
  self["CFLAGS"]
end

#compilerSymbol

Outputs the current compiler.

# Do something only for the system clang
if ENV.compiler == :clang
  # modify CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS in one go:
  ENV.append_to_cflags "-I ./missing/includes"
end

Returns:


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'Library/Homebrew/extend/ENV/shared.rb', line 167

def compiler
  @compiler ||= if (cc = @cc)
    warn_about_non_apple_gcc($&) if cc =~ GNU_GCC_REGEXP
    fetch_compiler(cc, "--cc")
  elsif (cc = homebrew_cc)
    warn_about_non_apple_gcc($&) if cc =~ GNU_GCC_REGEXP
    compiler = fetch_compiler(cc, "HOMEBREW_CC")

    if @formula
      compilers = [compiler] + CompilerSelector.compilers
      compiler = CompilerSelector.select_for(@formula, compilers)
    end

    compiler
  elsif @formula
    CompilerSelector.select_for(@formula)
  else
    DevelopmentTools.default_compiler
  end
end

#cppflagsObject


140
141
142
# File 'Library/Homebrew/extend/ENV/shared.rb', line 140

def cppflags
  self["CPPFLAGS"]
end

#cxxObject


128
129
130
# File 'Library/Homebrew/extend/ENV/shared.rb', line 128

def cxx
  self["CXX"]
end

#cxxflagsObject


136
137
138
# File 'Library/Homebrew/extend/ENV/shared.rb', line 136

def cxxflags
  self["CXXFLAGS"]
end

#fcObject


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

def fc
  self["FC"]
end

#fcflagsObject


156
157
158
# File 'Library/Homebrew/extend/ENV/shared.rb', line 156

def fcflags
  self["FCFLAGS"]
end

#fflagsObject


152
153
154
# File 'Library/Homebrew/extend/ENV/shared.rb', line 152

def fflags
  self["FFLAGS"]
end

#fortranObject


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

def fortran
  # Ignore repeated calls to this function as it will misleadingly warn about
  # building with an alternative Fortran compiler without optimization flags,
  # despite it often being the Homebrew-provided one set up in the first call.
  return if @fortran_setup_done

  @fortran_setup_done = true

  flags = []

  if fc
    ohai "Building with an alternative Fortran compiler"
    puts "This is unsupported."
    self["F77"] ||= fc
  else
    if (gfortran = which("gfortran", (HOMEBREW_PREFIX/"bin").to_s))
      ohai "Using Homebrew-provided Fortran compiler."
    elsif (gfortran = which("gfortran", PATH.new(ORIGINAL_PATHS)))
      ohai "Using a Fortran compiler found at #{gfortran}."
    end
    if gfortran
      puts "This may be changed by setting the FC environment variable."
      self["FC"] = self["F77"] = gfortran
      flags = FC_FLAG_VARS
    end
  end

  flags.each { |key| self[key] = cflags }
  set_cpu_flags(flags)
end

#ldflagsObject


144
145
146
# File 'Library/Homebrew/extend/ENV/shared.rb', line 144

def ldflags
  self["LDFLAGS"]
end

#ncurses_defineObject

Snow Leopard defines an NCURSES value the opposite of most distros. Currently only used by aalib in core.


204
205
206
# File 'Library/Homebrew/extend/ENV/shared.rb', line 204

def ncurses_define
  append "CPPFLAGS", "-DNCURSES_OPAQUE=0"
end

#no_weak_imports_support?Boolean

Returns:

  • (Boolean)

4
5
6
7
8
9
10
11
# File 'Library/Homebrew/extend/os/mac/extend/ENV/shared.rb', line 4

def no_weak_imports_support?
  return false unless compiler == :clang

  return false if MacOS::Xcode.version && MacOS::Xcode.version < "8.0"
  return false if MacOS::CLT.version && MacOS::CLT.version < "8.0"

  true
end

#permit_arch_flagsObject


298
# File 'Library/Homebrew/extend/ENV/shared.rb', line 298

def permit_arch_flags; end

#permit_weak_importsObject

A no-op until we enable this by default again (which we may never do).


301
# File 'Library/Homebrew/extend/ENV/shared.rb', line 301

def permit_weak_imports; end

#prepend(keys, value, separator = " ") ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
# File 'Library/Homebrew/extend/ENV/shared.rb', line 78

def prepend(keys, value, separator = " ")
  value = value.to_s
  Array(keys).each do |key|
    old = self[key]
    self[key] = if old.nil? || old.empty?
      value
    else
      value + separator + old
    end
  end
end

#prepend_create_path(key, path) ⇒ Object


107
108
109
110
111
# File 'Library/Homebrew/extend/ENV/shared.rb', line 107

def prepend_create_path(key, path)
  path = Pathname.new(path) unless path.is_a? Pathname
  path.mkpath
  prepend_path key, path
end

#prepend_path(key, path) ⇒ Object

Prepends a directory to PATH. Is the formula struggling to find the pkgconfig file? Point it to it. This is done automatically for keg-only formulae.

ENV.prepend_path "PKG_CONFIG_PATH", "#{Formula["glib"].opt_lib}/pkgconfig"

Prepending a system path such as /usr/bin is a no-op so that requirements don't accidentally override superenv shims or formulae's bin directories, e.g.

ENV.prepend_path "PATH", which("emacs").dirname

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

def prepend_path(key, path)
  return if %w[/usr/bin /bin /usr/sbin /sbin].include? path.to_s

  self[key] = PATH.new(self[key]).prepend(path)
end

#remove(keys, value) ⇒ Object


113
114
115
116
117
118
119
120
121
122
# File 'Library/Homebrew/extend/ENV/shared.rb', line 113

def remove(keys, value)
  return if value.nil?

  Array(keys).each do |key|
    next unless self[key]

    self[key] = self[key].sub(value, "")
    delete(key) if self[key].empty?
  end
end

#remove_cc_etcObject


45
46
47
48
49
50
51
52
# File 'Library/Homebrew/extend/ENV/shared.rb', line 45

def remove_cc_etc
  keys = %w[CC CXX OBJC OBJCXX LD CPP CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS LDFLAGS CPPFLAGS]
  removed = Hash[*keys.flat_map { |key| [key, self[key]] }]
  keys.each do |key|
    delete(key)
  end
  removed
end

#remove_from_cflags(val) ⇒ Object


58
59
60
# File 'Library/Homebrew/extend/ENV/shared.rb', line 58

def remove_from_cflags(val)
  remove CC_FLAG_VARS, val
end