Module: OS::Mac

Defined in:
Library/Homebrew/os/mac.rb,
Library/Homebrew/os/mac/sdk.rb,
Library/Homebrew/os/mac/xcode.rb,
Library/Homebrew/os/mac/version.rb,
Library/Homebrew/os/mac/xquartz.rb,
Library/Homebrew/cask/lib/hbc/macos.rb

Defined Under Namespace

Modules: CLT, XQuartz, Xcode Classes: SDK, SDKLocator, Version

Constant Summary collapse

STANDARD_COMPILERS =
{
  "2.0"   => { gcc_4_0_build: 4061 },
  "2.5"   => { gcc_4_0_build: 5370 },
  "3.1.4" => { gcc_4_0_build: 5493, gcc_4_2_build: 5577 },
  "3.2.6" => { gcc_4_0_build: 5494, gcc_4_2_build: 5666, clang: "1.7", clang_build: 77 },
  "4.0"   => { gcc_4_0_build: 5494, gcc_4_2_build: 5666, clang: "2.0", clang_build: 137 },
  "4.0.1" => { gcc_4_0_build: 5494, gcc_4_2_build: 5666, clang: "2.0", clang_build: 137 },
  "4.0.2" => { gcc_4_0_build: 5494, gcc_4_2_build: 5666, clang: "2.0", clang_build: 137 },
  "4.2"   => { clang: "3.0", clang_build: 211 },
  "4.3"   => { clang: "3.1", clang_build: 318 },
  "4.3.1" => { clang: "3.1", clang_build: 318 },
  "4.3.2" => { clang: "3.1", clang_build: 318 },
  "4.3.3" => { clang: "3.1", clang_build: 318 },
  "4.4"   => { clang: "4.0", clang_build: 421 },
  "4.4.1" => { clang: "4.0", clang_build: 421 },
  "4.5"   => { clang: "4.1", clang_build: 421 },
  "4.5.1" => { clang: "4.1", clang_build: 421 },
  "4.5.2" => { clang: "4.1", clang_build: 421 },
  "4.6"   => { clang: "4.2", clang_build: 425 },
  "4.6.1" => { clang: "4.2", clang_build: 425 },
  "4.6.2" => { clang: "4.2", clang_build: 425 },
  "4.6.3" => { clang: "4.2", clang_build: 425 },
  "5.0"   => { clang: "5.0", clang_build: 500 },
  "5.0.1" => { clang: "5.0", clang_build: 500 },
  "5.0.2" => { clang: "5.0", clang_build: 500 },
  "5.1"   => { clang: "5.1", clang_build: 503 },
  "5.1.1" => { clang: "5.1", clang_build: 503 },
  "6.0"   => { clang: "6.0", clang_build: 600 },
  "6.0.1" => { clang: "6.0", clang_build: 600 },
  "6.1"   => { clang: "6.0", clang_build: 600 },
  "6.1.1" => { clang: "6.0", clang_build: 600 },
  "6.2"   => { clang: "6.0", clang_build: 600 },
  "6.3"   => { clang: "6.1", clang_build: 602 },
  "6.3.1" => { clang: "6.1", clang_build: 602 },
  "6.3.2" => { clang: "6.1", clang_build: 602 },
  "6.4"   => { clang: "6.1", clang_build: 602 },
  "7.0"   => { clang: "7.0", clang_build: 700 },
  "7.0.1" => { clang: "7.0", clang_build: 700 },
  "7.1"   => { clang: "7.0", clang_build: 700 },
  "7.1.1" => { clang: "7.0", clang_build: 700 },
  "7.2"   => { clang: "7.0", clang_build: 700 },
  "7.2.1" => { clang: "7.0", clang_build: 700 },
  "7.3"   => { clang: "7.3", clang_build: 703 },
  "7.3.1" => { clang: "7.3", clang_build: 703 },
  "8.0"   => { clang: "8.0", clang_build: 800 },
  "8.1"   => { clang: "8.0", clang_build: 800 },
  "8.2"   => { clang: "8.0", clang_build: 800 },
  "8.2.1" => { clang: "8.0", clang_build: 800 },
  "8.3"   => { clang: "8.1", clang_build: 802 },
  "8.3.1" => { clang: "8.1", clang_build: 802 },
  "8.3.2" => { clang: "8.1", clang_build: 802 },
  "8.3.3" => { clang: "8.1", clang_build: 802 },
  "9.0"   => { clang: "9.0", clang_build: 900 },
  "9.0.1" => { clang: "9.0", clang_build: 900 },
  "9.1"   => { clang: "9.0", clang_build: 900 },
}.freeze
X11 =
XQuartz = Module.new
SYSTEM_DIRS =
[
      "/",
      "/Applications",
      "/Applications/Utilities",
      "/Incompatible Software",
      "/Library",
      "/Library/Application Support",
      "/Library/Audio",
      "/Library/Caches",
      "/Library/ColorPickers",
      "/Library/ColorSync",
      "/Library/Components",
      "/Library/Compositions",
      "/Library/Contextual Menu Items",
      "/Library/CoreMediaIO",
      "/Library/Desktop Pictures",
      "/Library/Developer",
      "/Library/Dictionaries",
      "/Library/DirectoryServices",
      "/Library/Documentation",
      "/Library/Extensions",
      "/Library/Filesystems",
      "/Library/Fonts",
      "/Library/Frameworks",
      "/Library/Graphics",
      "/Library/Image Capture",
      "/Library/Input Methods",
      "/Library/Internet Plug-Ins",
      "/Library/Java",
      "/Library/Keyboard Layouts",
      "/Library/Keychains",
      "/Library/LaunchAgents",
      "/Library/LaunchDaemons",
      "/Library/Logs",
      "/Library/Messages",
      "/Library/Modem Scripts",
      "/Library/OpenDirectory",
      "/Library/PDF Services",
      "/Library/Perl",
      "/Library/PreferencePanes",
      "/Library/Preferences",
      "/Library/Printers",
      "/Library/PrivilegedHelperTools",
      "/Library/Python",
      "/Library/QuickLook",
      "/Library/QuickTime",
      "/Library/Receipts",
      "/Library/Ruby",
      "/Library/Sandbox",
      "/Library/Screen Savers",
      "/Library/ScriptingAdditions",
      "/Library/Scripts",
      "/Library/Security",
      "/Library/Speech",
      "/Library/Spelling",
      "/Library/Spotlight",
      "/Library/StartupItems",
      "/Library/SystemProfiler",
      "/Library/Updates",
      "/Library/User Pictures",
      "/Library/Video",
      "/Library/WebServer",
      "/Library/Widgets",
      "/Library/iTunes",
      "/Network",
      "/System",
      "/System/Library",
      "/System/Library/Accessibility",
      "/System/Library/Accounts",
      "/System/Library/Address Book Plug-Ins",
      "/System/Library/Assistant",
      "/System/Library/Automator",
      "/System/Library/BridgeSupport",
      "/System/Library/Caches",
      "/System/Library/ColorPickers",
      "/System/Library/ColorSync",
      "/System/Library/Colors",
      "/System/Library/Components",
      "/System/Library/Compositions",
      "/System/Library/CoreServices",
      "/System/Library/DTDs",
      "/System/Library/DirectoryServices",
      "/System/Library/Displays",
      "/System/Library/Extensions",
      "/System/Library/Filesystems",
      "/System/Library/Filters",
      "/System/Library/Fonts",
      "/System/Library/Frameworks",
      "/System/Library/Graphics",
      "/System/Library/IdentityServices",
      "/System/Library/Image Capture",
      "/System/Library/Input Methods",
      "/System/Library/InternetAccounts",
      "/System/Library/Java",
      "/System/Library/KerberosPlugins",
      "/System/Library/Keyboard Layouts",
      "/System/Library/Keychains",
      "/System/Library/LaunchAgents",
      "/System/Library/LaunchDaemons",
      "/System/Library/LinguisticData",
      "/System/Library/LocationBundles",
      "/System/Library/LoginPlugins",
      "/System/Library/Messages",
      "/System/Library/Metadata",
      "/System/Library/MonitorPanels",
      "/System/Library/OpenDirectory",
      "/System/Library/OpenSSL",
      "/System/Library/Password Server Filters",
      "/System/Library/PerformanceMetrics",
      "/System/Library/Perl",
      "/System/Library/PreferencePanes",
      "/System/Library/Printers",
      "/System/Library/PrivateFrameworks",
      "/System/Library/QuickLook",
      "/System/Library/QuickTime",
      "/System/Library/QuickTimeJava",
      "/System/Library/Recents",
      "/System/Library/SDKSettingsPlist",
      "/System/Library/Sandbox",
      "/System/Library/Screen Savers",
      "/System/Library/ScreenReader",
      "/System/Library/ScriptingAdditions",
      "/System/Library/ScriptingDefinitions",
      "/System/Library/Security",
      "/System/Library/Services",
      "/System/Library/Sounds",
      "/System/Library/Speech",
      "/System/Library/Spelling",
      "/System/Library/Spotlight",
      "/System/Library/StartupItems",
      "/System/Library/SyncServices",
      "/System/Library/SystemConfiguration",
      "/System/Library/SystemProfiler",
      "/System/Library/Tcl",
      "/System/Library/TextEncodings",
      "/System/Library/User Template",
      "/System/Library/UserEventPlugins",
      "/System/Library/Video",
      "/System/Library/WidgetResources",
      "/User Information",
      "/Users",
      "/Volumes",
      "/bin",
      "/boot",
      "/cores",
      "/dev",
      "/etc",
      "/etc/X11",
      "/etc/opt",
      "/etc/sgml",
      "/etc/xml",
      "/home",
      "/libexec",
      "/lost+found",
      "/media",
      "/mnt",
      "/net",
      "/opt",
      "/private",
      "/private/etc",
      "/private/tftpboot",
      "/private/tmp",
      "/private/var",
      "/proc",
      "/root",
      "/sbin",
      "/srv",
      "/tmp",
      "/usr",
      "/usr/X11R6",
      "/usr/bin",
      "/usr/etc",
      "/usr/include",
      "/usr/lib",
      "/usr/libexec",
      "/usr/libexec/cups",
      "/usr/local",
      "/usr/local/Cellar",
      "/usr/local/Frameworks",
      "/usr/local/Library",
      "/usr/local/bin",
      "/usr/local/etc",
      "/usr/local/include",
      "/usr/local/lib",
      "/usr/local/libexec",
      "/usr/local/opt",
      "/usr/local/share",
      "/usr/local/share/man",
      "/usr/local/share/man/man1",
      "/usr/local/share/man/man2",
      "/usr/local/share/man/man3",
      "/usr/local/share/man/man4",
      "/usr/local/share/man/man5",
      "/usr/local/share/man/man6",
      "/usr/local/share/man/man7",
      "/usr/local/share/man/man8",
      "/usr/local/share/man/man9",
      "/usr/local/share/man/mann",
      "/usr/local/var",
      "/usr/local/var/lib",
      "/usr/local/var/lock",
      "/usr/local/var/run",
      "/usr/sbin",
      "/usr/share",
      "/usr/share/man",
      "/usr/share/man/man1",
      "/usr/share/man/man2",
      "/usr/share/man/man3",
      "/usr/share/man/man4",
      "/usr/share/man/man5",
      "/usr/share/man/man6",
      "/usr/share/man/man7",
      "/usr/share/man/man8",
      "/usr/share/man/man9",
      "/usr/share/man/mann",
      "/usr/src",
      "/var",
      "/var/cache",
      "/var/lib",
      "/var/lock",
      "/var/log",
      "/var/mail",
      "/var/run",
      "/var/spool",
      "/var/spool/mail",
      "/var/tmp",
    ]
.map(&method(:Pathname))
.to_set
.freeze
UNDELETABLE_DIRS =

TODO: There should be a way to specify a containing

directory under which nothing can be deleted.
[
      "~/",
      "~/Applications",
      "~/Desktop",
      "~/Documents",
      "~/Downloads",
      "~/Mail",
      "~/Movies",
      "~/Music",
      "~/Music/iTunes",
      "~/Music/iTunes/iTunes Music",
      "~/Music/iTunes/Album Artwork",
      "~/News",
      "~/Pictures",
      "~/Pictures/Desktops",
      "~/Pictures/Photo Booth",
      "~/Pictures/iChat Icons",
      "~/Pictures/iPhoto Library",
      "~/Public",
      "~/Sites",
      "~/Library",
      "~/Library/.localized",
      "~/Library/Accessibility",
      "~/Library/Accounts",
      "~/Library/Address Book Plug-Ins",
      "~/Library/Application Scripts",
      "~/Library/Application Support",
      "~/Library/Application Support/Apple",
      "~/Library/Application Support/com.apple.AssistiveControl",
      "~/Library/Application Support/com.apple.QuickLook",
      "~/Library/Application Support/com.apple.TCC",
      "~/Library/Assistants",
      "~/Library/Audio",
      "~/Library/Automator",
      "~/Library/Autosave Information",
      "~/Library/Caches",
      "~/Library/Calendars",
      "~/Library/ColorPickers",
      "~/Library/ColorSync",
      "~/Library/Colors",
      "~/Library/Components",
      "~/Library/Compositions",
      "~/Library/Containers",
      "~/Library/Contextual Menu Items",
      "~/Library/Cookies",
      "~/Library/DTDs",
      "~/Library/Desktop Pictures",
      "~/Library/Developer",
      "~/Library/Dictionaries",
      "~/Library/DirectoryServices",
      "~/Library/Displays",
      "~/Library/Documentation",
      "~/Library/Extensions",
      "~/Library/Favorites",
      "~/Library/FileSync",
      "~/Library/Filesystems",
      "~/Library/Filters",
      "~/Library/FontCollections",
      "~/Library/Fonts",
      "~/Library/Frameworks",
      "~/Library/GameKit",
      "~/Library/Graphics",
      "~/Library/Group Containers",
      "~/Library/Icons",
      "~/Library/IdentityServices",
      "~/Library/Image Capture",
      "~/Library/Images",
      "~/Library/Input Methods",
      "~/Library/Internet Plug-Ins",
      "~/Library/InternetAccounts",
      "~/Library/iTunes",
      "~/Library/KeyBindings",
      "~/Library/Keyboard Layouts",
      "~/Library/Keychains",
      "~/Library/LaunchAgents",
      "~/Library/LaunchDaemons",
      "~/Library/LocationBundles",
      "~/Library/LoginPlugins",
      "~/Library/Logs",
      "~/Library/Mail",
      "~/Library/Mail Downloads",
      "~/Library/Messages",
      "~/Library/Metadata",
      "~/Library/Mobile Documents",
      "~/Library/MonitorPanels",
      "~/Library/OpenDirectory",
      "~/Library/PDF Services",
      "~/Library/PhonePlugins",
      "~/Library/Phones",
      "~/Library/PreferencePanes",
      "~/Library/Preferences",
      "~/Library/Printers",
      "~/Library/PrivateFrameworks",
      "~/Library/PubSub",
      "~/Library/QuickLook",
      "~/Library/QuickTime",
      "~/Library/Receipts",
      "~/Library/Recent Servers",
      "~/Library/Recents",
      "~/Library/Safari",
      "~/Library/Saved Application State",
      "~/Library/Screen Savers",
      "~/Library/ScreenReader",
      "~/Library/ScriptingAdditions",
      "~/Library/ScriptingDefinitions",
      "~/Library/Scripts",
      "~/Library/Security",
      "~/Library/Services",
      "~/Library/Sounds",
      "~/Library/Speech",
      "~/Library/Spelling",
      "~/Library/Spotlight",
      "~/Library/StartupItems",
      "~/Library/StickiesDatabase",
      "~/Library/Sync Services",
      "~/Library/SyncServices",
      "~/Library/SyncedPreferences",
      "~/Library/TextEncodings",
      "~/Library/User Pictures",
      "~/Library/Video",
      "~/Library/Voices",
      "~/Library/WebKit",
      "~/Library/WidgetResources",
      "~/Library/Widgets",
      "~/Library/Workflows",
    ]
.map { |x| Pathname(x.sub(%r{^~(?=(/|$))}, Dir.home)).expand_path }
.to_set
.union(SYSTEM_DIRS)
.freeze

Class Method Summary collapse

Class Method Details

.active_developer_dirObject



69
70
71
# File 'Library/Homebrew/os/mac.rb', line 69

def active_developer_dir
  @active_developer_dir ||= Utils.popen_read("/usr/bin/xcode-select", "-print-path").strip
end

.app_with_bundle_id(*ids) ⇒ Object



233
234
235
236
237
238
# File 'Library/Homebrew/os/mac.rb', line 233

def app_with_bundle_id(*ids)
  path = mdfind(*ids)
         .reject { |p| p.include?("/Backups.backupdb/") }
         .first
  Pathname.new(path) unless path.nil? || path.empty?
end

.catObject



53
54
55
# File 'Library/Homebrew/os/mac.rb', line 53

def cat
  version.to_sym
end

.compilers_standard?Boolean

Returns:

  • (Boolean)


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'Library/Homebrew/os/mac.rb', line 216

def compilers_standard?
  STANDARD_COMPILERS.fetch(Xcode.version.to_s).all? do |method, build|
    send(:"#{method}_version") == build
  end
rescue IndexError
  onoe <<~EOS
    Homebrew doesn't know what compiler versions ship with your version
    of Xcode (#{Xcode.version}). Please `brew update` and if that doesn't
    help, file an issue with the output of `brew --config`:
      https://github.com/Homebrew/brew/issues

    Note that we only track stable, released versions of Xcode.

    Thanks!
  EOS
end

.full_versionObject

This can be compared to numerics, strings, or symbols using the standard Ruby Comparable methods.



25
26
27
# File 'Library/Homebrew/os/mac.rb', line 25

def full_version
  @full_version ||= Version.new((ENV["HOMEBREW_MACOS_VERSION"] || ENV["HOMEBREW_OSX_VERSION"]).chomp)
end

.full_version=(version) ⇒ Object



29
30
31
32
# File 'Library/Homebrew/os/mac.rb', line 29

def full_version=(version)
  @full_version = Version.new(version.chomp)
  @version = nil
end

.languageObject



65
66
67
# File 'Library/Homebrew/os/mac.rb', line 65

def language
  languages.first
end

.languagesObject



57
58
59
60
61
62
63
# File 'Library/Homebrew/os/mac.rb', line 57

def languages
  @languages ||= [
    *ARGV.value("language")&.split(","),
    *ENV["HOMEBREW_LANGUAGES"]&.split(","),
    *Open3.capture2("defaults", "read", "-g", "AppleLanguages")[0].scan(/[^ \n"(),]+/),
  ].uniq
end

.latest_sdk_versionObject



34
35
36
37
# File 'Library/Homebrew/os/mac.rb', line 34

def latest_sdk_version
  # TODO: bump version when new Xcode macOS SDK is released
  Version.new "10.13"
end

.latest_stable_versionObject



39
40
41
42
# File 'Library/Homebrew/os/mac.rb', line 39

def latest_stable_version
  # TODO: bump version when new macOS is released
  Version.new "10.13"
end

.macports_or_finkObject



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
# File 'Library/Homebrew/os/mac.rb', line 114

def macports_or_fink
  paths = []

  # First look in the path because MacPorts is relocatable and Fink
  # may become relocatable in the future.
  %w[port fink].each do |ponk|
    path = which(ponk)
    paths << path unless path.nil?
  end

  # Look in the standard locations, because even if port or fink are
  # not in the path they can still break builds if the build scripts
  # have these paths baked in.
  %w[/sw/bin/fink /opt/local/bin/port].each do |ponk|
    path = Pathname.new(ponk)
    paths << path if path.exist?
  end

  # Finally, some users make their MacPorts or Fink directories
  # read-only in order to try out Homebrew, but this doesn't work as
  # some build scripts error out when trying to read from these now
  # unreadable paths.
  %w[/sw /opt/local].map { |p| Pathname.new(p) }.each do |path|
    paths << path if path.exist? && !path.readable?
  end

  paths.uniq
end

.mdfind(*ids) ⇒ Object



240
241
242
243
244
245
# File 'Library/Homebrew/os/mac.rb', line 240

def mdfind(*ids)
  return [] unless OS.mac?
  (@mdfind ||= {}).fetch(ids) do
    @mdfind[ids] = Utils.popen_read("/usr/bin/mdfind", mdfind_query(*ids)).split("\n")
  end
end

.mdfind_query(*ids) ⇒ Object



253
254
255
# File 'Library/Homebrew/os/mac.rb', line 253

def mdfind_query(*ids)
  ids.map! { |id| "kMDItemCFBundleIdentifier == #{id}" }.join(" || ")
end

.outdated_release?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'Library/Homebrew/os/mac.rb', line 44

def outdated_release?
  # TODO: bump version when new macOS is released
  version < "10.11"
end

.pkgutil_info(id) ⇒ Object



247
248
249
250
251
# File 'Library/Homebrew/os/mac.rb', line 247

def pkgutil_info(id)
  (@pkginfo ||= {}).fetch(id) do |key|
    @pkginfo[key] = Utils.popen_read("/usr/sbin/pkgutil", "--pkg-info", key).strip
  end
end

.prefer_64_bit?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
# File 'Library/Homebrew/os/mac.rb', line 143

def prefer_64_bit?
  if ENV["HOMEBREW_PREFER_64_BIT"] && version == :leopard
    Hardware::CPU.is_64_bit?
  else
    Hardware::CPU.is_64_bit? && version > :leopard
  end
end

.preferred_archObject



151
152
153
154
155
156
157
# File 'Library/Homebrew/os/mac.rb', line 151

def preferred_arch
  if prefer_64_bit?
    Hardware::CPU.arch_64_bit
  else
    Hardware::CPU.arch_32_bit
  end
end

.prerelease?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'Library/Homebrew/os/mac.rb', line 49

def prerelease?
  version > latest_stable_version
end

.sdk(v = nil) ⇒ Object

If a specific SDK is requested

a) The requested SDK is returned, if it's installed.
b) If the requested SDK is not installed, the newest SDK (if any SDKs
   are available) is returned.
c) If no SDKs are available, nil is returned.

If no specific SDK is requested

a) For Xcode >= 7, the latest SDK is returned even if the latest SDK is
   named after a newer OS version than the running OS. The
   MACOSX_DEPLOYMENT_TARGET must be set to the OS for which you're
   actually building (usually the running OS version).
   https://github.com/Homebrew/legacy-homebrew/pull/50355
   https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/WhatsNewXcode/Articles/Introduction.html#//apple_ref/doc/uid/TP40004626
   Section "About SDKs and Simulator"
b) For Xcode < 7, proceed as if the SDK for the running OS version had
   specifically been requested according to the rules above.


89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'Library/Homebrew/os/mac.rb', line 89

def sdk(v = nil)
  @locator ||= SDKLocator.new
  begin
    sdk = if v.nil?
      (Xcode.version.to_i >= 7) ? @locator.latest_sdk : @locator.sdk_for(version)
    else
      @locator.sdk_for v
    end
  rescue SDKLocator::NoSDKError
    sdk = @locator.latest_sdk
  end
  # Only return an SDK older than the OS version if it was specifically requested
  sdk if v || (!sdk.nil? && sdk.version >= version)
end

.sdk_path(v = nil) ⇒ Object

Returns the path to an SDK or nil, following the rules set by #sdk.



105
106
107
108
# File 'Library/Homebrew/os/mac.rb', line 105

def sdk_path(v = nil)
  s = sdk(v)
  s&.path
end

.system_dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


373
374
375
# File 'Library/Homebrew/cask/lib/hbc/macos.rb', line 373

def system_dir?(dir)
  SYSTEM_DIRS.include?(Pathname.new(dir).expand_path)
end

.undeletable?(dir) ⇒ Boolean

Returns:

  • (Boolean)


377
378
379
# File 'Library/Homebrew/cask/lib/hbc/macos.rb', line 377

def undeletable?(dir)
  UNDELETABLE_DIRS.include?(Pathname.new(dir).expand_path)
end

.versionObject

This can be compared to numerics, strings, or symbols using the standard Ruby Comparable methods.



19
20
21
# File 'Library/Homebrew/os/mac.rb', line 19

def version
  @version ||= Version.new(full_version.to_s[/10\.\d+/])
end