Module: Superenv Private

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Why superenv?

  1. Only specify the environment we need (NO LDFLAGS for cmake)
  2. Only apply compiler-specific options when we are calling that compiler
  3. Force all incpaths and libpaths into the cc instantiation (fewer bugs)
  4. Cater toolchain usage to specific Xcode versions
  5. Remove flags that we don't want or that will break builds
  6. Simpler code
  7. Simpler formulae that just work
  8. Build-system agnostic configuration of the toolchain

Constant Summary

Constants included from CompilerConstants

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_incompatible, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Methods included from SharedEnvExtension

#append, #append_path, #append_to_cccfg, #append_to_cflags, #append_to_rustflags, #bottle_arch, #build_bottle?, #cc, #cflags, #compiler, #cppflags, #cxx, #cxxflags, #debug_symbols?, #effective_arch, #fc, #fcflags, #fflags, #fortran, #ldflags, #prepend, #prepend_create_path, #prepend_path, #remove, #remove_cc_etc, #remove_from_cflags

Methods included from OS::Linux::SharedEnvExtension

#effective_arch

Methods included from OS::Linux::Superenv

#determine_dynamic_linker_path, #determine_rpath_paths

Methods included from OS::Mac::Superenv

#no_fixup_chains, #no_weak_imports

Class Method Details

.bin ⇒ Pathname?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



65
# File 'extend/ENV/super.rb', line 65

def self.bin; end

.enabled_for?(env) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


68
69
70
71
72
# File 'extend/ENV/super.rb', line 68

def self.enabled_for?(env)
  return false if env == "std"

  !bin.nil?
end

.extended(base) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



47
48
49
50
51
52
53
54
# File 'extend/ENV/super.rb', line 47

def self.extended(base)
  # Runtime signatures on the dependency setters resolve Formula on first call.
  require "formula"

  base.keg_only_deps = []
  base.deps = []
  base.run_time_deps = []
end

.shims_path ⇒ Pathname

The location of Homebrew's shims.

Returns:



60
61
62
# File 'extend/ENV/super.rb', line 60

def self.shims_path
  HOMEBREW_SHIMS_PATH/"super"
end

Instance Method Details

#cxx11 ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



335
336
337
338
# File 'extend/ENV/super.rb', line 335

def cxx11
  append_to_cccfg "x"
  append_to_cccfg "g" if homebrew_cc == "clang"
end

#deparallelize(&block) ⇒ T.untyped

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

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.

Parameters:

  • block (T.proc.returns(T.untyped), nil)

Returns:

  • (T.untyped)


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'extend/ENV/super.rb', line 302

def deparallelize(&block)
  old_makeflags = delete("MAKEFLAGS")
  old_make_jobs = delete("HOMEBREW_MAKE_JOBS")
  self["HOMEBREW_MAKE_JOBS"] = "1"
  if block
    begin
      yield
    ensure
      self["MAKEFLAGS"] = old_makeflags
      self["HOMEBREW_MAKE_JOBS"] = old_make_jobs
    end
  end

  old_makeflags
end

#deps ⇒ Array<Formula>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



31
# File 'extend/ENV/super.rb', line 31

def deps = build_environment_state.deps

#deps=(deps) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



34
35
36
# File 'extend/ENV/super.rb', line 34

def deps=(deps)
  build_environment_state(writable: true).deps = deps
end

#keg_only_deps ⇒ Array<Formula>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



23
# File 'extend/ENV/super.rb', line 23

def keg_only_deps = build_environment_state.keg_only_deps

#keg_only_deps=(deps) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



26
27
28
# File 'extend/ENV/super.rb', line 26

def keg_only_deps=(deps)
  build_environment_state(writable: true).keg_only_deps = deps
end

#libcxx ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



341
342
343
# File 'extend/ENV/super.rb', line 341

def libcxx
  append_to_cccfg "g" if compiler == :clang
end

#llvm_clang ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



221
222
223
224
225
# File 'extend/ENV/super.rb', line 221

def llvm_clang
  super
  self["CC"] = self["OBJC"] = "clang"
  self["CXX"] = self["OBJCXX"] = "clang++"
end

#make_jobs ⇒ Integer

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



319
320
321
322
# File 'extend/ENV/super.rb', line 319

def make_jobs
  self["MAKEFLAGS"] =~ /-\w*j(\d+)/
  [Regexp.last_match(1).to_i, 1].max
end

#O0(&block) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



358
359
360
361
362
363
364
# File 'extend/ENV/super.rb', line 358

def O0(&block)
  if block
    with_env(HOMEBREW_OPTIMIZATION_LEVEL: "O0", &block)
  else
    self["HOMEBREW_OPTIMIZATION_LEVEL"] = "O0"
  end
end

#O1(&block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • block (T.proc.void, nil)


367
368
369
370
371
372
373
# File 'extend/ENV/super.rb', line 367

def O1(&block)
  if block
    with_env(HOMEBREW_OPTIMIZATION_LEVEL: "O1", &block)
  else
    self["HOMEBREW_OPTIMIZATION_LEVEL"] = "O1"
  end
end

#O3(&block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • block (T.proc.void, nil)


376
377
378
379
380
381
382
# File 'extend/ENV/super.rb', line 376

def O3(&block)
  if block
    with_env(HOMEBREW_OPTIMIZATION_LEVEL: "O3", &block)
  else
    self["HOMEBREW_OPTIMIZATION_LEVEL"] = "O3"
  end
end

#permit_arch_flags ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



325
326
327
# File 'extend/ENV/super.rb', line 325

def permit_arch_flags
  append_to_cccfg "K"
end

#refurbish_args ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



351
352
353
# File 'extend/ENV/super.rb', line 351

def refurbish_args
  append_to_cccfg "O"
end

#reset ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



75
76
77
78
79
80
# File 'extend/ENV/super.rb', line 75

def reset
  super
  # Configure scripts generated by autoconf 2.61 or later export as_nl, which
  # we use as a heuristic for running under configure
  delete("as_nl")
end

#run_time_deps ⇒ Array<Formula>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



39
# File 'extend/ENV/super.rb', line 39

def run_time_deps = build_environment_state.run_time_deps

#run_time_deps=(deps) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



42
43
44
# File 'extend/ENV/super.rb', line 42

def run_time_deps=(deps)
  build_environment_state(writable: true).run_time_deps = deps
end

#runtime_cpu_detection ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



330
331
332
# File 'extend/ENV/super.rb', line 330

def runtime_cpu_detection
  append_to_cccfg "d"
end

#set_debug_symbols ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



346
347
348
# File 'extend/ENV/super.rb', line 346

def set_debug_symbols
  append_to_cccfg "D"
end

#setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false, debug_symbols: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • formula (Formula, nil) (defaults to: nil)
  • cc (String, nil) (defaults to: nil)
  • build_bottle (Boolean, nil) (defaults to: false)
  • bottle_arch (String, nil) (defaults to: nil)
  • testing_formula (Boolean) (defaults to: false)
  • debug_symbols (Boolean, nil) (defaults to: false)


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
# File 'extend/ENV/super.rb', line 92

def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false,
                            debug_symbols: false)
  super
  send(compiler)

  self["HOMEBREW_ENV"] = "super"
  self["MAKEFLAGS"] ||= "-j#{determine_make_jobs}"
  self["RUSTC_WRAPPER"] = "#{HOMEBREW_SHIMS_PATH}/shared/rustc_wrapper"
  self["HOMEBREW_RUSTFLAGS"] = Hardware.rustflags_target_cpu(effective_arch)
  path = PATH.new(Superenv.bin)
  # Formula dependencies can override standard tools.
  path.append(deps.map(&:opt_bin))
  path.append(homebrew_extra_paths)
  path.append("/usr/bin", "/bin", "/usr/sbin", "/sbin")
  begin
    if (path_cc = homebrew_cc) && path_cc.match?(GNU_GCC_REGEXP)
      path.append(gcc_version_formula(path_cc).opt_bin)
    end
  rescue FormulaUnavailableError
    # Don't fail and don't add these formulae to the path if they don't exist.
    nil
  end
  self["PATH"] = path.existing&.to_s
  self["PKG_CONFIG_PATH"] = PATH.new(
    deps.map { |d| d.opt_lib/"pkgconfig" },
    deps.map { |d| d.opt_share/"pkgconfig" },
  ).existing&.to_s
  self["PKG_CONFIG_LIBDIR"] = (determine_pkg_config_libdir || "").to_s
  self["HOMEBREW_CCCFG"] = determine_cccfg
  self["HOMEBREW_OPTIMIZATION_LEVEL"] = compiler.match?(GNU_GCC_REGEXP) ? "O2" : "Os"
  self["HOMEBREW_BREW_FILE"] = HOMEBREW_BREW_FILE.to_s
  self["HOMEBREW_PREFIX"] = HOMEBREW_PREFIX.to_s
  self["HOMEBREW_CELLAR"] = HOMEBREW_CELLAR.to_s
  self["HOMEBREW_OPT"] = "#{HOMEBREW_PREFIX}/opt"
  self["HOMEBREW_TEMP"] = HOMEBREW_TEMP.to_s
  self["HOMEBREW_OPTFLAGS"] = begin
    Hardware::CPU.optimization_flags.fetch(effective_arch)
  rescue KeyError
    odebug "Building a bottle for custom architecture (#{effective_arch})..."
    Hardware::CPU.arch_flag(effective_arch)
  end
  self["HOMEBREW_MAKE_JOBS"] = determine_make_jobs.to_s
  self["CMAKE_PREFIX_PATH"] = PATH.new(
    Superenv.bin&.parent,
    keg_only_deps.map(&:opt_prefix),
    HOMEBREW_PREFIX.to_s,
  ).existing&.to_s
  self["CMAKE_FRAMEWORK_PATH"] = PATH.new(
    deps.map(&:opt_frameworks),
    homebrew_extra_cmake_frameworks_paths,
  ).existing&.to_s
  self["CMAKE_INCLUDE_PATH"] = PATH.new(homebrew_extra_cmake_include_paths).existing&.to_s
  self["CMAKE_LIBRARY_PATH"] = PATH.new(homebrew_extra_cmake_library_paths).existing&.to_s
  self["ACLOCAL_PATH"] = PATH.new(
    keg_only_deps.map { |d| d.opt_share/"aclocal" },
    HOMEBREW_PREFIX/"share/aclocal",
  ).existing&.to_s
  self["M4"] = "#{HOMEBREW_PREFIX}/opt/m4/bin/m4" if deps.any? { |d| d.name == "libtool" }
  self["HOMEBREW_ISYSTEM_PATHS"] = PATH.new(
    HOMEBREW_PREFIX/"include",
    homebrew_extra_isystem_paths,
  ).existing&.to_s
  self["HOMEBREW_INCLUDE_PATHS"] = PATH.new(keg_only_deps.map(&:opt_include)).existing&.to_s
  library_paths = []
  if compiler.match?(GNU_GCC_REGEXP)
    # Add path to GCC runtime libs for version being used to compile,
    # so that the linker will find those libs before any that may be linked in $HOMEBREW_PREFIX/lib.
    # https://github.com/Homebrew/brew/pull/11459#issuecomment-851075936
    begin
      f = gcc_version_formula(compiler.to_s)
    rescue FormulaUnavailableError
      nil
    else
      library_paths << (f.opt_lib/"gcc"/f.version.major) if f.any_version_installed?
    end
  end
  # Don't add `llvm` to library paths; this leads to undesired linkage to LLVM's `libunwind`
  library_paths += keg_only_deps.reject { |dep| dep.name.match?(/^llvm(@\d+)?$/) }
                                .map(&:opt_lib)
  library_paths << (HOMEBREW_PREFIX/"lib")
  library_paths += homebrew_extra_library_paths
  self["HOMEBREW_LIBRARY_PATHS"] = PATH.new(library_paths).existing&.to_s
  self["HOMEBREW_DEPENDENCIES"] = deps.map(&:name).join(",")
  self["HOMEBREW_FORMULA_PREFIX"] = formula.prefix.to_s if formula
  # Prevent the OpenSSL rust crate from building a vendored OpenSSL.
  # https://github.com/sfackler/rust-openssl/blob/994e5ff8c63557ab2aa85c85cc6956b0b0216ca7/openssl/src/lib.rs#L65
  self["OPENSSL_NO_VENDOR"] = "1"
  # Prevent Go from automatically downloading a newer toolchain than the one that we have.
  # https://tip.golang.org/doc/toolchain
  self["GOTOOLCHAIN"] = "local"
  # Prevent maturin from automatically downloading its own rust
  self["MATURIN_NO_INSTALL_RUST"] = "1"
  # Prevent Python packages from using bundled libraries by default.
  # Currently for hidapi, pyzmq and pynacl
  self["HIDAPI_SYSTEM_HIDAPI"] = "1"
  self["PYZMQ_NO_BUNDLE"] = "1"
  self["SODIUM_INSTALL"] = "system"
  # Set defaults for Bundler installs
  self["BUNDLE_FORCE_RUBY_PLATFORM"] = "true"
  self["BUNDLE_VERSION"] = "system"
  self["BUNDLE_WITHOUT"] = "development:test"
  # Set defaults for opam
  self["OPAMNODEPEXTS"] = "1"
  self["OPAMYES"] = "1"

  set_debug_symbols if debug_symbols

  # The HOMEBREW_CCCFG ENV variable is used by the ENV/cc tool to control
  # compiler flag stripping. It consists of a string of characters which act
  # as flags. Some of these flags are mutually exclusive.
  #
  # O - Enables argument refurbishing. Only active under the
  #     make/bsdmake wrappers currently.
  # x - Enable C++11 mode.
  # g - Enable "-stdlib=libc++" for clang.
  # h - Enable "-stdlib=libstdc++" for clang.
  # K - Don't strip -arch <arch>, -m32, or -m64
  # d - Don't strip -march=<target>. Use only in formulae that
  #     have runtime detection of CPU features.
  # D - Generate debugging information
  # w - Pass `-no_weak_imports` to the linker
  # f - Pass `-no_fixup_chains` to `ld` whenever it
  #     is invoked with `-undefined dynamic_lookup`
  # c - Pass `-ld_classic` to `ld` whenever it is invoked
  #     with `-dead_strip_dylibs`
  # b - Pass `-mbranch-protection=standard` to the compiler
end