From ea1f2098ace015de8556fac9b8333667db0ea33c Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Thu, 28 Jul 2022 14:52:19 -0400 Subject: [PATCH] Move `HOMEBREW_SIMULATE_MACOS_ON_LINUX` handling to `SimulateSystem` --- Library/Homebrew/default_prefix.rb | 4 +++- Library/Homebrew/extend/on_system.rb | 5 ---- .../Homebrew/extend/os/linux/keg_relocate.rb | 12 ---------- Library/Homebrew/formula_auditor.rb | 2 +- Library/Homebrew/keg_relocate.rb | 9 ++++++- Library/Homebrew/resource_auditor.rb | 2 +- Library/Homebrew/simulate_system.rb | 19 ++++++++++----- Library/Homebrew/software_spec.rb | 16 +++++++------ Library/Homebrew/test/simulate_system_spec.rb | 24 +++++++++++++++++++ Library/Homebrew/test/software_spec_spec.rb | 14 +++++++++++ 10 files changed, 73 insertions(+), 34 deletions(-) diff --git a/Library/Homebrew/default_prefix.rb b/Library/Homebrew/default_prefix.rb index b22f98268d..70e493c233 100644 --- a/Library/Homebrew/default_prefix.rb +++ b/Library/Homebrew/default_prefix.rb @@ -1,10 +1,12 @@ # typed: true # frozen_string_literal: true +require "simulate_system" + module Homebrew DEFAULT_PREFIX, DEFAULT_REPOSITORY = if OS.mac? && Hardware::CPU.arm? [HOMEBREW_MACOS_ARM_DEFAULT_PREFIX, HOMEBREW_MACOS_ARM_DEFAULT_REPOSITORY] - elsif OS.linux? && !EnvConfig.simulate_macos_on_linux? + elsif Homebrew::SimulateSystem.simulating_or_running_on_linux? [HOMEBREW_LINUX_DEFAULT_PREFIX, HOMEBREW_LINUX_DEFAULT_REPOSITORY] else [HOMEBREW_DEFAULT_PREFIX, HOMEBREW_DEFAULT_REPOSITORY] diff --git a/Library/Homebrew/extend/on_system.rb b/Library/Homebrew/extend/on_system.rb index a7f7b3744f..6b3ec69ecc 100644 --- a/Library/Homebrew/extend/on_system.rb +++ b/Library/Homebrew/extend/on_system.rb @@ -20,11 +20,6 @@ module OnSystem sig { params(os_name: Symbol, or_condition: T.nilable(Symbol)).returns(T::Boolean) } def os_condition_met?(os_name, or_condition = nil) - if Homebrew::EnvConfig.simulate_macos_on_linux? - return false if os_name == :linux - return true if [:macos, *MacOSVersions::SYMBOLS.keys].include?(os_name) - end - return Homebrew::SimulateSystem.send("simulating_or_running_on_#{os_name}?") if BASE_OS_OPTIONS.include?(os_name) raise ArgumentError, "Invalid OS condition: #{os_name.inspect}" unless MacOSVersions::SYMBOLS.key?(os_name) diff --git a/Library/Homebrew/extend/os/linux/keg_relocate.rb b/Library/Homebrew/extend/os/linux/keg_relocate.rb index ee1537910c..2fb382916d 100644 --- a/Library/Homebrew/extend/os/linux/keg_relocate.rb +++ b/Library/Homebrew/extend/os/linux/keg_relocate.rb @@ -80,16 +80,4 @@ class Keg end elf_files end - - def self.bottle_dependencies - @bottle_dependencies ||= begin - formulae = [] - gcc = Formulary.factory(CompilerSelector.preferred_gcc) - if !Homebrew::EnvConfig.simulate_macos_on_linux? && - DevelopmentTools.non_apple_gcc_version("gcc") < gcc.version.to_i - formulae << gcc - end - formulae - end - end end diff --git a/Library/Homebrew/formula_auditor.rb b/Library/Homebrew/formula_auditor.rb index a06941104d..218cd91412 100644 --- a/Library/Homebrew/formula_auditor.rb +++ b/Library/Homebrew/formula_auditor.rb @@ -329,7 +329,7 @@ module Homebrew # The number of conflicts on Linux is absurd. # TODO: remove this and check these there too. - return if OS.linux? && !Homebrew::EnvConfig.simulate_macos_on_linux? + return if Homebrew::SimulateSystem.simulating_or_running_on_linux? recursive_runtime_formulae = formula.runtime_formula_dependencies(undeclared: false) version_hash = {} diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index 1df8ee86a4..a396f43205 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -368,7 +368,14 @@ class Keg end def self.bottle_dependencies - [] + return [] unless Homebrew::SimulateSystem.simulating_or_running_on_linux? + + @bottle_dependencies ||= begin + formulae = [] + gcc = Formulary.factory(CompilerSelector.preferred_gcc) + formulae << gcc if DevelopmentTools.non_apple_gcc_version("gcc") < gcc.version.to_i + formulae + end end end diff --git a/Library/Homebrew/resource_auditor.rb b/Library/Homebrew/resource_auditor.rb index 8155d311c0..624f2b8e07 100644 --- a/Library/Homebrew/resource_auditor.rb +++ b/Library/Homebrew/resource_auditor.rb @@ -105,7 +105,7 @@ module Homebrew # Ideally `ca-certificates` would not be excluded here, but sourcing a HTTP mirror was tricky. # Instead, we have logic elsewhere to pass `--insecure` to curl when downloading the certs. # TODO: try remove the OS/env conditional - if (OS.mac? || Homebrew::EnvConfig.simulate_macos_on_linux?) && spec_name == :stable && + if Homebrew::SimulateSystem.simulating_or_running_on_macos? && spec_name == :stable && owner.name != "ca-certificates" && curl_dep && !urls.find { |u| u.start_with?("http://") } problem "should always include at least one HTTP mirror" end diff --git a/Library/Homebrew/simulate_system.rb b/Library/Homebrew/simulate_system.rb index de61eabef7..206e9a460f 100644 --- a/Library/Homebrew/simulate_system.rb +++ b/Library/Homebrew/simulate_system.rb @@ -9,7 +9,14 @@ module Homebrew class << self extend T::Sig - attr_reader :os, :arch + attr_reader :arch + + sig { returns(T.nilable(Symbol)) } + def os + return :macos if @os.blank? && !OS.mac? && Homebrew::EnvConfig.simulate_macos_on_linux? + + @os + end sig { params(new_os: Symbol).void } def os=(new_os) @@ -33,16 +40,16 @@ module Homebrew sig { returns(T::Boolean) } def simulating_or_running_on_macos? - return OS.mac? if @os.blank? + return OS.mac? if os.blank? - [:macos, *MacOSVersions::SYMBOLS.keys].include?(@os) + [:macos, *MacOSVersions::SYMBOLS.keys].include?(os) end sig { returns(T::Boolean) } def simulating_or_running_on_linux? - return OS.linux? if @os.blank? + return OS.linux? if os.blank? - @os == :linux + os == :linux end sig { returns(Symbol) } @@ -52,7 +59,7 @@ module Homebrew sig { returns(Symbol) } def current_os - return @os if @os.present? + return T.must(os) if os.present? return :linux if OS.linux? MacOS.version.to_sym diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb index 30e9856d83..0a246d3469 100644 --- a/Library/Homebrew/software_spec.rb +++ b/Library/Homebrew/software_spec.rb @@ -170,14 +170,16 @@ class SoftwareSpec @uses_from_macos_elements << deps - # Linux simulating macOS. Assume oldest macOS version. - return if Homebrew::EnvConfig.simulate_macos_on_linux? && !bounds.key?(:since) - - # macOS new enough for dependency to not be required. + # Check whether macOS is new enough for dependency to not be required. if Homebrew::SimulateSystem.simulating_or_running_on_macos? - current_os = MacOS::Version.from_symbol(Homebrew::SimulateSystem.current_os) - since_os = MacOS::Version.from_symbol(bounds[:since]) if bounds.key?(:since) - return if current_os >= since_os + # Assume the oldest macOS version when simulating a generic macOS version + return if Homebrew::SimulateSystem.current_os == :macos && !bounds.key?(:since) + + if Homebrew::SimulateSystem.current_os != :macos + current_os = MacOS::Version.from_symbol(Homebrew::SimulateSystem.current_os) + since_os = MacOS::Version.from_symbol(bounds[:since]) if bounds.key?(:since) + return if current_os >= since_os + end end depends_on deps diff --git a/Library/Homebrew/test/simulate_system_spec.rb b/Library/Homebrew/test/simulate_system_spec.rb index 0b72700d9f..3923680064 100644 --- a/Library/Homebrew/test/simulate_system_spec.rb +++ b/Library/Homebrew/test/simulate_system_spec.rb @@ -36,6 +36,12 @@ describe Homebrew::SimulateSystem do described_class.os = :monterey expect(described_class.simulating_or_running_on_macos?).to be true end + + it "returns true on Linux with HOMEBREW_SIMULATE_MACOS_ON_LINUX", :needs_linux do + described_class.clear + ENV["HOMEBREW_SIMULATE_MACOS_ON_LINUX"] = "1" + expect(described_class.simulating_or_running_on_macos?).to be true + end end describe "::simulating_or_running_on_linux?" do @@ -66,6 +72,12 @@ describe Homebrew::SimulateSystem do described_class.os = :monterey expect(described_class.simulating_or_running_on_linux?).to be false end + + it "returns false on Linux with HOMEBREW_SIMULATE_MACOS_ON_LINUX", :needs_linux do + described_class.clear + ENV["HOMEBREW_SIMULATE_MACOS_ON_LINUX"] = "1" + expect(described_class.simulating_or_running_on_linux?).to be false + end end describe "::current_arch" do @@ -114,5 +126,17 @@ describe Homebrew::SimulateSystem do described_class.os = :monterey expect(described_class.current_os).to eq :monterey end + + it "returns the current macOS version on macOS with HOMEBREW_SIMULATE_MACOS_ON_LINUX", :needs_macos do + described_class.clear + ENV["HOMEBREW_SIMULATE_MACOS_ON_LINUX"] = "1" + expect(described_class.current_os).to eq MacOS.version.to_sym + end + + it "returns `:macos` on Linux with HOMEBREW_SIMULATE_MACOS_ON_LINUX", :needs_linux do + described_class.clear + ENV["HOMEBREW_SIMULATE_MACOS_ON_LINUX"] = "1" + expect(described_class.current_os).to eq :macos + end end end diff --git a/Library/Homebrew/test/software_spec_spec.rb b/Library/Homebrew/test/software_spec_spec.rb index a737088803..8271b35681 100644 --- a/Library/Homebrew/test/software_spec_spec.rb +++ b/Library/Homebrew/test/software_spec_spec.rb @@ -150,6 +150,20 @@ describe SoftwareSpec do expect(spec.deps.first.tags).to include(:build) end + it "ignores dependencies with HOMEBREW_SIMULATE_MACOS_ON_LINUX" do + ENV["HOMEBREW_SIMULATE_MACOS_ON_LINUX"] = "1" + spec.uses_from_macos("foo") + + expect(spec.deps).to be_empty + end + + it "ignores dependencies with tags with HOMEBREW_SIMULATE_MACOS_ON_LINUX" do + ENV["HOMEBREW_SIMULATE_MACOS_ON_LINUX"] = "1" + spec.uses_from_macos("foo" => :build) + + expect(spec.deps).to be_empty + end + it "ignores OS version specifications" do spec.uses_from_macos("foo", since: :mojave) spec.uses_from_macos("bar" => :build, :since => :mojave)