| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "os/mac/version" | 
					
						
							|  |  |  | require "os/mac/xcode" | 
					
						
							|  |  |  | require "os/mac/xquartz" | 
					
						
							| 
									
										
										
										
											2015-12-04 13:58:22 -08:00
										 |  |  | require "os/mac/sdk" | 
					
						
							| 
									
										
										
										
											2017-09-27 02:08:23 -07:00
										 |  |  | require "os/mac/keg" | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | module OS | 
					
						
							|  |  |  |   module Mac | 
					
						
							| 
									
										
										
										
											2016-09-24 20:11:54 +02:00
										 |  |  |     module_function | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-19 21:46:20 +09:00
										 |  |  |     # rubocop:disable Naming/ConstantName | 
					
						
							|  |  |  |     # rubocop:disable Style/MutableConstant | 
					
						
							|  |  |  |     ::MacOS = self | 
					
						
							|  |  |  |     # rubocop:enable Naming/ConstantName | 
					
						
							|  |  |  |     # rubocop:enable Style/MutableConstant | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 17:53:43 +01:00
										 |  |  |     raise "Loaded OS::Mac on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     # This can be compared to numerics, strings, or symbols | 
					
						
							|  |  |  |     # using the standard Ruby Comparable methods. | 
					
						
							|  |  |  |     def version | 
					
						
							| 
									
										
										
										
											2015-10-16 16:41:14 +08:00
										 |  |  |       @version ||= Version.new(full_version.to_s[/10\.\d+/]) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # This can be compared to numerics, strings, or symbols | 
					
						
							|  |  |  |     # using the standard Ruby Comparable methods. | 
					
						
							|  |  |  |     def full_version | 
					
						
							| 
									
										
										
										
											2016-09-19 09:17:35 +01:00
										 |  |  |       @full_version ||= Version.new((ENV["HOMEBREW_MACOS_VERSION"] || ENV["HOMEBREW_OSX_VERSION"]).chomp) | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 09:00:53 +01:00
										 |  |  |     def full_version=(version) | 
					
						
							|  |  |  |       @full_version = Version.new(version.chomp) | 
					
						
							|  |  |  |       @version = nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-17 19:53:38 +00:00
										 |  |  |     def latest_sdk_version | 
					
						
							|  |  |  |       # TODO: bump version when new Xcode macOS SDK is released | 
					
						
							| 
									
										
										
										
											2019-06-04 16:11:18 -07:00
										 |  |  |       Version.new "10.15" | 
					
						
							| 
									
										
										
										
											2017-11-17 19:53:38 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def latest_stable_version | 
					
						
							| 
									
										
										
										
											2018-09-03 17:00:02 +01:00
										 |  |  |       # TODO: bump version when new macOS is released and also update | 
					
						
							| 
									
										
										
										
											2018-09-06 11:48:13 +01:00
										 |  |  |       # references in docs/Installation.md and | 
					
						
							|  |  |  |       # https://github.com/Homebrew/install/blob/master/install | 
					
						
							| 
									
										
										
										
											2019-09-28 10:39:57 +02:00
										 |  |  |       Version.new "10.15" | 
					
						
							| 
									
										
										
										
											2015-10-23 00:07:12 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-09 10:21:25 +00:00
										 |  |  |     def outdated_release? | 
					
						
							| 
									
										
										
										
											2018-09-03 17:00:02 +01:00
										 |  |  |       # TODO: bump version when new macOS is released and also update | 
					
						
							| 
									
										
										
										
											2018-09-06 11:48:13 +01:00
										 |  |  |       # references in docs/Installation.md and | 
					
						
							|  |  |  |       # https://github.com/Homebrew/install/blob/master/install | 
					
						
							| 
									
										
										
										
											2019-09-28 10:39:57 +02:00
										 |  |  |       version < "10.13" | 
					
						
							| 
									
										
										
										
											2016-01-09 10:21:25 +00:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-17 19:53:38 +00:00
										 |  |  |     def prerelease? | 
					
						
							|  |  |  |       version > latest_stable_version | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-10 04:24:55 +02:00
										 |  |  |     def languages | 
					
						
							| 
									
										
										
										
											2017-10-07 16:27:29 +02:00
										 |  |  |       @languages ||= [ | 
					
						
							|  |  |  |         *ARGV.value("language")&.split(","), | 
					
						
							|  |  |  |         *ENV["HOMEBREW_LANGUAGES"]&.split(","), | 
					
						
							|  |  |  |         *Open3.capture2("defaults", "read", "-g", "AppleLanguages")[0].scan(/[^ \n"(),]+/), | 
					
						
							|  |  |  |       ].uniq | 
					
						
							| 
									
										
										
										
											2016-09-10 04:24:55 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-09 00:11:43 +02:00
										 |  |  |     def language | 
					
						
							| 
									
										
										
										
											2016-09-10 04:24:55 +02:00
										 |  |  |       languages.first | 
					
						
							| 
									
										
										
										
											2016-09-09 00:11:43 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-01 18:36:46 -05:00
										 |  |  |     def active_developer_dir | 
					
						
							| 
									
										
										
										
											2014-12-16 15:27:36 -05:00
										 |  |  |       @active_developer_dir ||= Utils.popen_read("/usr/bin/xcode-select", "-print-path").strip | 
					
						
							| 
									
										
										
										
											2014-05-01 18:36:46 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:43:59 +01:00
										 |  |  |     def sdk_root_needed? | 
					
						
							|  |  |  |       if MacOS::CLT.installed? | 
					
						
							|  |  |  |         # If there's no CLT SDK, return false | 
					
						
							|  |  |  |         return false unless MacOS::CLT.provides_sdk? | 
					
						
							|  |  |  |         # If the CLT is installed and headers are provided by the system, return false | 
					
						
							|  |  |  |         return false unless MacOS::CLT.separate_header_package? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |     # 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 | 
					
						
							| 
									
										
										
										
											2016-03-30 05:13:41 -07:00
										 |  |  |     #      are available) is returned. | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |     #   3. If no SDKs are available, nil is returned. | 
					
						
							|  |  |  |     # | 
					
						
							| 
									
										
										
										
											2020-03-08 20:08:33 +00:00
										 |  |  |     # If no specific SDK is requested, the SDK matching the OS version is returned, | 
					
						
							|  |  |  |     # if available. Otherwise, the latest SDK is returned. | 
					
						
							| 
									
										
										
										
											2016-03-30 05:13:41 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-23 04:34:21 -07:00
										 |  |  |     def sdk(v = nil) | 
					
						
							| 
									
										
										
										
											2020-03-08 20:09:06 +00:00
										 |  |  |       @locator ||= if CLT.installed? && CLT.provides_sdk? | 
					
						
							| 
									
										
										
										
											2018-06-12 14:55:31 -07:00
										 |  |  |         CLTSDKLocator.new | 
					
						
							| 
									
										
										
										
											2020-03-08 20:09:06 +00:00
										 |  |  |       else | 
					
						
							|  |  |  |         XcodeSDKLocator.new | 
					
						
							| 
									
										
										
										
											2018-06-12 14:55:31 -07:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-26 17:57:11 -07:00
										 |  |  |       @locator.sdk_if_applicable(v) | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:43:59 +01:00
										 |  |  |     def sdk_for_formula(f, v = nil) | 
					
						
							|  |  |  |       # If the formula requires Xcode, don't return the CLT SDK | 
					
						
							|  |  |  |       return Xcode.sdk if f.requirements.any? { |req| req.is_a? XcodeRequirement } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       sdk(v) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |     # Returns the path to an SDK or nil, following the rules set by {.sdk}. | 
					
						
							| 
									
										
										
										
											2016-03-23 04:34:21 -07:00
										 |  |  |     def sdk_path(v = nil) | 
					
						
							| 
									
										
										
										
											2015-12-04 13:58:22 -08:00
										 |  |  |       s = sdk(v) | 
					
						
							| 
									
										
										
										
											2017-09-24 19:24:46 +01:00
										 |  |  |       s&.path | 
					
						
							| 
									
										
										
										
											2015-12-04 13:58:22 -08:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 09:32:55 -07:00
										 |  |  |     def sdk_path_if_needed(v = nil) | 
					
						
							| 
									
										
										
										
											2020-03-08 20:09:06 +00:00
										 |  |  |       # Prefer CLT SDK when both Xcode and the CLT are installed. | 
					
						
							| 
									
										
										
										
											2018-08-07 17:56:42 -07:00
										 |  |  |       # 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. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-07 16:43:59 +01:00
										 |  |  |       return unless sdk_root_needed? | 
					
						
							| 
									
										
										
										
											2018-08-07 17:56:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-24 09:32:55 -07:00
										 |  |  |       sdk_path(v) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     # See these issues for some history: | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |     # | 
					
						
							|  |  |  |     # - https://github.com/Homebrew/legacy-homebrew/issues/13 | 
					
						
							|  |  |  |     # - https://github.com/Homebrew/legacy-homebrew/issues/41 | 
					
						
							|  |  |  |     # - https://github.com/Homebrew/legacy-homebrew/issues/48 | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     def macports_or_fink | 
					
						
							|  |  |  |       paths = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # First look in the path because MacPorts is relocatable and Fink | 
					
						
							|  |  |  |       # may become relocatable in the future. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |       %w[port fink].each do |ponk| | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |         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. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |       %w[/sw/bin/fink /opt/local/bin/port].each do |ponk| | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |         path = Pathname.new(ponk) | 
					
						
							|  |  |  |         paths << path if path.exist? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 16:31:56 +00:00
										 |  |  |       # Finally, some users make their MacPorts or Fink directories | 
					
						
							| 
									
										
										
										
											2017-09-10 16:32:09 +00:00
										 |  |  |       # read-only in order to try out Homebrew, but this doesn't work as | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |       # some build scripts error out when trying to read from these now | 
					
						
							|  |  |  |       # unreadable paths. | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |       %w[/sw /opt/local].map { |p| Pathname.new(p) }.each do |path| | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |         paths << path if path.exist? && !path.readable? | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       paths.uniq | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def preferred_arch | 
					
						
							| 
									
										
										
										
											2019-01-08 19:13:46 +00:00
										 |  |  |       if Hardware::CPU.is_64_bit? | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |         Hardware::CPU.arch_64_bit | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         Hardware::CPU.arch_32_bit | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-01 20:47:26 -05:00
										 |  |  |     def app_with_bundle_id(*ids) | 
					
						
							| 
									
										
										
										
											2017-10-05 21:47:26 +01:00
										 |  |  |       path = mdfind(*ids) | 
					
						
							|  |  |  |              .reject { |p| p.include?("/Backups.backupdb/") } | 
					
						
							|  |  |  |              .first | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |       Pathname.new(path) unless path.nil? || path.empty? | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-01 20:47:26 -05:00
										 |  |  |     def mdfind(*ids) | 
					
						
							|  |  |  |       (@mdfind ||= {}).fetch(ids) do | 
					
						
							| 
									
										
										
										
											2014-07-11 15:51:19 -05:00
										 |  |  |         @mdfind[ids] = Utils.popen_read("/usr/bin/mdfind", mdfind_query(*ids)).split("\n") | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-01 20:47:26 -05:00
										 |  |  |     def pkgutil_info(id) | 
					
						
							| 
									
										
										
										
											2014-04-01 20:49:11 -05:00
										 |  |  |       (@pkginfo ||= {}).fetch(id) do |key| | 
					
						
							| 
									
										
										
										
											2014-07-11 15:51:19 -05:00
										 |  |  |         @pkginfo[key] = Utils.popen_read("/usr/sbin/pkgutil", "--pkg-info", key).strip | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-04-01 20:47:26 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def mdfind_query(*ids) | 
					
						
							|  |  |  |       ids.map! { |id| "kMDItemCFBundleIdentifier == #{id}" }.join(" || ") | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-06-09 09:01:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def tcc_db | 
					
						
							|  |  |  |       @tcc_db ||= Pathname.new("/Library/Application Support/com.apple.TCC/TCC.db") | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def pre_mavericks_accessibility_dotfile | 
					
						
							|  |  |  |       @pre_mavericks_accessibility_dotfile ||= Pathname.new("/private/var/db/.AccessibilityAPIEnabled") | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end |