Module: OS::Mac

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

Overview

Define OS::Mac on Linux for formula API compatibility.

Defined Under Namespace

Modules: CLT, XQuartz, Xcode Classes: BaseSDKLocator, CLTSDKLocator, SDK, Version, XcodeSDKLocator

Constant Summary collapse

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/Java/Extensions",
      "/Library/Java/JavaVirtualMachines",
      "/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_PATHS =

TODO: There should be a way to specify a containing directory under which nothing can be deleted.

[
      "~/",
      "~/Applications",
      "~/Applications/.localized",
      "~/Desktop",
      "~/Desktop/.localized",
      "~/Documents",
      "~/Documents/.localized",
      "~/Downloads",
      "~/Downloads/.localized",
      "~/Mail",
      "~/Movies",
      "~/Movies/.localized",
      "~/Music",
      "~/Music/.localized",
      "~/Music/iTunes",
      "~/Music/iTunes/iTunes Music",
      "~/Music/iTunes/Album Artwork",
      "~/News",
      "~/Pictures",
      "~/Pictures/.localized",
      "~/Pictures/Desktops",
      "~/Pictures/Photo Booth",
      "~/Pictures/iChat Icons",
      "~/Pictures/iPhoto Library",
      "~/Public",
      "~/Public/.localized",
      "~/Sites",
      "~/Sites/.localized",
      "~/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 { |path| Pathname(path.sub(%r{^~(?=(/|$))}, Dir.home)).expand_path }
.to_set
.union(SYSTEM_DIRS)
.freeze
X11 =

rubocop:disable Style/MutableConstant

XQuartz = Module.new

Class Method Summary collapse

Class Method Details

.active_developer_dirObject



73
74
75
# File 'brew/Library/Homebrew/os/mac.rb', line 73

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

.app_with_bundle_id(*ids) ⇒ Object



162
163
164
165
166
167
# File 'brew/Library/Homebrew/os/mac.rb', line 162

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

.full_versionObject

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



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

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

.full_version=(version) ⇒ Object



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

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

.languageObject



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

def language
  languages.first
end

.languagesObject



61
62
63
64
65
66
67
# File 'brew/Library/Homebrew/os/mac.rb', line 61

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



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

def latest_sdk_version  # TODO: bump version when new Xcode macOS SDK is released

  Version.new "10.15"
end

.latest_stable_versionObject



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

def latest_stable_version  # TODO: bump version when new macOS is released and also update
  # references in docs/Installation.md and
  # https://github.com/Homebrew/install/blob/master/install

  Version.new "10.15"
end

.macports_or_finkObject

See these issues for some history:

  • https://github.com/Homebrew/legacy-homebrew/issues/13
  • https://github.com/Homebrew/legacy-homebrew/issues/41
  • https://github.com/Homebrew/legacy-homebrew/issues/48


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

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



169
170
171
172
173
# File 'brew/Library/Homebrew/os/mac.rb', line 169

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

.mdfind_query(*ids) ⇒ Object



181
182
183
# File 'brew/Library/Homebrew/os/mac.rb', line 181

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

.outdated_release?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
# File 'brew/Library/Homebrew/os/mac.rb', line 50

def outdated_release?  # TODO: bump version when new macOS is released and also update
  # references in docs/Installation.md and
  # https://github.com/Homebrew/install/blob/master/install

  version < "10.13"
end

.pkgutil_info(id) ⇒ Object



175
176
177
178
179
# File 'brew/Library/Homebrew/os/mac.rb', line 175

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

.pre_mavericks_accessibility_dotfileObject



189
190
191
# File 'brew/Library/Homebrew/os/mac.rb', line 189

def pre_mavericks_accessibility_dotfile
  @pre_mavericks_accessibility_dotfile ||= Pathname.new("/private/var/db/.AccessibilityAPIEnabled")
end

.preferred_archObject



154
155
156
157
158
159
160
# File 'brew/Library/Homebrew/os/mac.rb', line 154

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

.prerelease?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'brew/Library/Homebrew/os/mac.rb', line 57

def prerelease?
  version > latest_stable_version
end

.sdk(v = nil) ⇒ Object

If a specific SDK is requested:

  1. The requested SDK is returned, if it’s installed.
  2. If the requested SDK is not installed, the newest SDK (if any SDKs are available) is returned.
  3. If no SDKs are available, nil is returned.

If no specific SDK is requested, the SDK matching the OS version is returned, if available. Otherwise, the latest SDK is returned.



87
88
89
90
91
92
93
94
95
# File 'brew/Library/Homebrew/os/mac.rb', line 87

def sdk(v = nil)
  @locator ||= if CLT.installed? && CLT.provides_sdk?
    CLTSDKLocator.new
  else
    XcodeSDKLocator.new
  end

  @locator.sdk_if_applicable(v)
end

.sdk_path(v = nil) ⇒ Object

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



98
99
100
101
# File 'brew/Library/Homebrew/os/mac.rb', line 98

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

.sdk_path_if_needed(_v = nil) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'brew/Library/Homebrew/os/mac.rb', line 103

def sdk_path_if_needed(v = nil)
  # Prefer CLT SDK when both Xcode and the CLT are installed.
  # Expected results:
  # 1. On Xcode-only systems, return the Xcode SDK.
  # 2. On Xcode-and-CLT systems where headers are provided by the system, return nil.
  # 3. On CLT-only systems with no CLT SDK, return nil.
  # 4. On CLT-only systems with a CLT SDK, where headers are provided by the system, return nil.
  # 5. On CLT-only systems with a CLT SDK, where headers are not provided by the system, return the CLT SDK.

  # If there's no CLT SDK, return early
  return if MacOS::CLT.installed? && !MacOS::CLT.provides_sdk?  # If the CLT is installed and headers are provided by the system, return early

  return if MacOS::CLT.installed? && !MacOS::CLT.separate_header_package?

  sdk_path(v)
end

.system_dir?(dir) ⇒ Boolean

Returns:

  • (Boolean)


384
385
386
# File 'brew/Library/Homebrew/cask/macos.rb', line 384

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

.tcc_dbObject



185
186
187
# File 'brew/Library/Homebrew/os/mac.rb', line 185

def tcc_db
  @tcc_db ||= Pathname.new("/Library/Application Support/com.apple.TCC/TCC.db")
end

.undeletable?(path) ⇒ Boolean

Returns:

  • (Boolean)


388
389
390
# File 'brew/Library/Homebrew/cask/macos.rb', line 388

def undeletable?(path)
  UNDELETABLE_PATHS.include?(Pathname.new(path).expand_path)
end

.versionObject

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



23
24
25
# File 'brew/Library/Homebrew/os/mac.rb', line 23

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