| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 02:15:28 +02:00
										 |  |  | require "macos_version" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "os/mac/xcode" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-08-26 03:06:19 +02:00
										 |  |  |   # Helper module for querying system information on macOS. | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |   module Mac | 
					
						
							| 
									
										
										
										
											2016-04-25 17:53:43 +01:00
										 |  |  |     raise "Loaded OS::Mac on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |     # This check is the only acceptable or necessary one in this file. | 
					
						
							|  |  |  |     # rubocop:disable Homebrew/MoveToExtendOS | 
					
						
							|  |  |  |     raise "Loaded OS::Mac on Linux!" if OS.linux? | 
					
						
							|  |  |  |     # rubocop:enable Homebrew/MoveToExtendOS | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Provide MacOS alias for backwards compatibility and nicer APIs. | 
					
						
							|  |  |  |     ::MacOS = OS::Mac | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-30 04:25:24 +01:00
										 |  |  |     VERSION = ENV.fetch("HOMEBREW_MACOS_VERSION").chomp.freeze | 
					
						
							|  |  |  |     private_constant :VERSION | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     # This can be compared to numerics, strings, or symbols | 
					
						
							|  |  |  |     # using the standard Ruby Comparable methods. | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  |     # | 
					
						
							|  |  |  |     # @api internal | 
					
						
							| 
									
										
										
										
											2023-05-09 02:15:28 +02:00
										 |  |  |     sig { returns(MacOSVersion) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.version | 
					
						
							| 
									
										
										
										
											2021-02-26 19:41:04 +00:00
										 |  |  |       @version ||= full_version.strip_patch | 
					
						
							| 
									
										
										
										
											2015-10-16 16:41:14 +08:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # This can be compared to numerics, strings, or symbols | 
					
						
							|  |  |  |     # using the standard Ruby Comparable methods. | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  |     # | 
					
						
							|  |  |  |     # @api internal | 
					
						
							| 
									
										
										
										
											2023-05-09 02:15:28 +02:00
										 |  |  |     sig { returns(MacOSVersion) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.full_version | 
					
						
							| 
									
										
										
										
											2024-02-22 17:20:01 +00:00
										 |  |  |       @full_version ||= if (fake_macos = ENV.fetch("HOMEBREW_FAKE_MACOS", nil)) # for Portable Ruby building | 
					
						
							|  |  |  |         MacOSVersion.new(fake_macos) | 
					
						
							| 
									
										
										
										
											2022-05-31 16:32:23 +01:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2023-05-09 02:15:28 +02:00
										 |  |  |         MacOSVersion.new(VERSION) | 
					
						
							| 
									
										
										
										
											2022-05-31 16:32:23 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-12 17:06:29 -07:00
										 |  |  |     sig { params(version: String).void } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.full_version=(version) | 
					
						
							| 
									
										
										
										
											2023-05-09 02:15:28 +02:00
										 |  |  |       @full_version = MacOSVersion.new(version.chomp) | 
					
						
							| 
									
										
										
										
											2017-01-20 09:00:53 +01:00
										 |  |  |       @version = nil | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-23 10:14:18 +01:00
										 |  |  |     sig { returns(::Version) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.latest_sdk_version | 
					
						
							| 
									
										
										
										
											2017-11-17 19:53:38 +00:00
										 |  |  |       # TODO: bump version when new Xcode macOS SDK is released | 
					
						
							| 
									
										
										
										
											2021-10-11 20:25:55 +08:00
										 |  |  |       # NOTE: We only track the major version of the SDK. | 
					
						
							| 
									
										
										
										
											2024-09-16 16:33:29 +01:00
										 |  |  |       ::Version.new("15") | 
					
						
							| 
									
										
										
										
											2020-11-23 10:14:18 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 17:46:47 +01:00
										 |  |  |     sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.preferred_perl_version | 
					
						
							| 
									
										
										
										
											2023-06-05 18:36:03 +01:00
										 |  |  |       if version >= :sonoma | 
					
						
							|  |  |  |         "5.34" | 
					
						
							|  |  |  |       elsif version >= :big_sur | 
					
						
							| 
									
										
										
										
											2021-04-29 17:46:47 +01:00
										 |  |  |         "5.30" | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         "5.18" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  |     sig { returns(T::Array[String]) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.languages | 
					
						
							| 
									
										
										
										
											2020-07-08 11:03:56 +01:00
										 |  |  |       return @languages if @languages | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       os_langs = Utils.popen_read("defaults", "read", "-g", "AppleLanguages") | 
					
						
							|  |  |  |       if os_langs.blank? | 
					
						
							|  |  |  |         # User settings don't exist so check the system-wide one. | 
					
						
							|  |  |  |         os_langs = Utils.popen_read("defaults", "read", "/Library/Preferences/.GlobalPreferences", "AppleLanguages") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       os_langs = os_langs.scan(/[^ \n"(),]+/) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-21 21:28:58 +02:00
										 |  |  |       @languages = os_langs | 
					
						
							| 
									
										
										
										
											2016-09-10 04:24:55 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.language | 
					
						
							| 
									
										
										
										
											2016-09-10 04:24:55 +02:00
										 |  |  |       languages.first | 
					
						
							| 
									
										
										
										
											2016-09-09 00:11:43 +02:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 18:16:34 -07:00
										 |  |  |     sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.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-10-20 12:03:48 +02:00
										 |  |  |     sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.sdk_root_needed? | 
					
						
							| 
									
										
										
										
											2020-04-07 16:43:59 +01:00
										 |  |  |       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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.sdk_locator | 
					
						
							| 
									
										
										
										
											2020-07-01 16:02:29 +01:00
										 |  |  |       if CLT.installed? && CLT.provides_sdk? | 
					
						
							|  |  |  |         CLT.sdk_locator | 
					
						
							| 
									
										
										
										
											2020-03-08 20:09:06 +00:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2020-07-01 16:02:29 +01:00
										 |  |  |         Xcode.sdk_locator | 
					
						
							| 
									
										
										
										
											2018-06-12 14:55:31 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-01 16:02:29 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-06-12 14:55:31 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.sdk(version = nil) | 
					
						
							| 
									
										
										
										
											2023-03-07 23:42:00 +00:00
										 |  |  |       sdk_locator.sdk_if_applicable(version) | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.sdk_for_formula(formula, version = nil, check_only_runtime_requirements: false) | 
					
						
							| 
									
										
										
										
											2020-04-07 16:43:59 +01:00
										 |  |  |       # If the formula requires Xcode, don't return the CLT SDK | 
					
						
							| 
									
										
										
										
											2020-12-17 18:30:03 -05:00
										 |  |  |       # If check_only_runtime_requirements is true, don't necessarily return the | 
					
						
							| 
									
										
										
										
											2021-05-08 11:20:01 +10:00
										 |  |  |       # Xcode SDK if the XcodeRequirement is only a build or test requirement. | 
					
						
							| 
									
										
										
										
											2023-03-10 23:46:07 +00:00
										 |  |  |       return Xcode.sdk if formula.requirements.any? do |req| | 
					
						
							| 
									
										
										
										
											2020-12-17 18:30:03 -05:00
										 |  |  |         next false unless req.is_a? XcodeRequirement | 
					
						
							|  |  |  |         next false if check_only_runtime_requirements && req.build? && !req.test? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         true | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-04-07 16:43:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-07 23:42:00 +00:00
										 |  |  |       sdk(version) | 
					
						
							| 
									
										
										
										
											2020-04-07 16:43:59 +01:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |     # Returns the path to an SDK or nil, following the rules set by {sdk}. | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.sdk_path(version = nil) | 
					
						
							| 
									
										
										
										
											2023-03-07 23:42:00 +00:00
										 |  |  |       s = sdk(version) | 
					
						
							| 
									
										
										
										
											2017-09-24 19:24:46 +01:00
										 |  |  |       s&.path | 
					
						
							| 
									
										
										
										
											2015-12-04 13:58:22 -08:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.sdk_path_if_needed(version = 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-07 23:42:00 +00:00
										 |  |  |       sdk_path(version) | 
					
						
							| 
									
										
										
										
											2018-07-24 09:32:55 -07:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     # See these issues for some history: | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											2020-11-03 12:39:26 -05: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} | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.macports_or_fink | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |       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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 15:12:53 -07:00
										 |  |  |     sig { params(ids: String).returns(T.nilable(Pathname)) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.app_with_bundle_id(*ids) | 
					
						
							| 
									
										
										
										
											2024-11-08 20:33:40 +08:00
										 |  |  |       require "bundle_version" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       paths = mdfind(*ids).filter_map do |bundle_path| | 
					
						
							|  |  |  |         Pathname.new(bundle_path) if bundle_path.exclude?("/Backups.backupdb/") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       return paths.first unless paths.all? { |bp| (bp/"Contents/Info.plist").exist? } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       # Prefer newest one, if we can find it. | 
					
						
							|  |  |  |       paths.max_by { |bundle_path| Homebrew::BundleVersion.from_info_plist(bundle_path/"Contents/Info.plist") } | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 15:12:53 -07:00
										 |  |  |     sig { params(ids: String).returns(T::Array[String]) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.mdfind(*ids) | 
					
						
							| 
									
										
										
										
											2014-04-01 20:47:26 -05:00
										 |  |  |       (@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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 15:12:53 -07:00
										 |  |  |     sig { params(ids: String).returns(String) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.mdfind_query(*ids) | 
					
						
							| 
									
										
										
										
											2014-04-01 20:47:26 -05:00
										 |  |  |       ids.map! { |id| "kMDItemCFBundleIdentifier == #{id}" }.join(" || ") | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | end |