Merge pull request #18305 from Homebrew/no-undefs

Use prepended modules instead of `undef` for OS-specific code
This commit is contained in:
Douglas Eichelberger 2024-09-19 14:03:31 -07:00 committed by GitHub
commit 3b649d1dec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 239 additions and 191 deletions

View File

@ -19,9 +19,6 @@ module Homebrew
sig { returns(T::Array[String]) } sig { returns(T::Array[String]) }
attr_reader :options_only, :flags_only attr_reader :options_only, :flags_only
# undefine tap to allow --tap argument
undef tap
sig { void } sig { void }
def initialize def initialize
require "cli/named_args" require "cli/named_args"

View File

@ -24,6 +24,9 @@ module Homebrew
}.freeze, T::Hash[Symbol, String]) }.freeze, T::Hash[Symbol, String])
private_constant :ArgType, :HIDDEN_DESC_PLACEHOLDER, :SYMBOL_TO_USAGE_MAPPING private_constant :ArgType, :HIDDEN_DESC_PLACEHOLDER, :SYMBOL_TO_USAGE_MAPPING
sig { returns(Args) }
attr_reader :args
sig { returns(Args::OptionsType) } sig { returns(Args::OptionsType) }
attr_reader :processed_options attr_reader :processed_options

View File

@ -1,23 +1,29 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew module OS
class Cleanup module Linux
undef use_system_ruby? module Cleanup
extend T::Helpers
sig { returns(T::Boolean) } requires_ancestor { Homebrew::Cleanup }
def use_system_ruby?
return false if Homebrew::EnvConfig.force_vendor_ruby?
rubies = [which("ruby"), which("ruby", ORIGINAL_PATHS)].compact sig { returns(T::Boolean) }
system_ruby = Pathname.new("/usr/bin/ruby") def use_system_ruby?
rubies << system_ruby if system_ruby.exist? return false if Homebrew::EnvConfig.force_vendor_ruby?
check_ruby_version = HOMEBREW_LIBRARY_PATH/"utils/ruby_check_version_script.rb" rubies = [which("ruby"), which("ruby", ORIGINAL_PATHS)].compact
rubies.uniq.any? do |ruby| system_ruby = Pathname.new("/usr/bin/ruby")
quiet_system ruby, "--enable-frozen-string-literal", "--disable=gems,did_you_mean,rubyopt", rubies << system_ruby if system_ruby.exist?
check_ruby_version, RUBY_VERSION
check_ruby_version = HOMEBREW_LIBRARY_PATH/"utils/ruby_check_version_script.rb"
rubies.uniq.any? do |ruby|
quiet_system ruby, "--enable-frozen-string-literal", "--disable=gems,did_you_mean,rubyopt",
check_ruby_version, RUBY_VERSION
end
end end
end end
end end
end end
Homebrew::Cleanup.prepend(OS::Linux::Cleanup)

View File

@ -0,0 +1,31 @@
# typed: strict
# frozen_string_literal: true
module OS
module Linux
module CLI
module Parser
extend T::Helpers
requires_ancestor { Homebrew::CLI::Parser }
sig { void }
def set_default_options
args["formula?"] = true if args.respond_to?(:formula?)
end
sig { void }
def validate_options
return unless args.respond_to?(:cask?)
return unless args.cask?
# NOTE: We don't raise an error here because we don't want
# to print the help page or a stack trace.
odie "Invalid `--cask` usage: Casks do not work on Linux"
end
end
end
end
end
Homebrew::CLI::Parser.prepend(OS::Linux::CLI::Parser)

View File

@ -1,52 +1,56 @@
# typed: true # rubocop:disable Sorbet/StrictSigil # typed: true # rubocop:disable Sorbet/StrictSigil
# frozen_string_literal: true # frozen_string_literal: true
class Formula module OS
undef shared_library module Linux
undef loader_path module Formula
undef deuniversalize_machos extend T::Helpers
undef add_global_deps_to_spec
undef valid_platform?
sig { params(name: String, version: T.nilable(T.any(String, Integer))).returns(String) } requires_ancestor { ::Formula }
def shared_library(name, version = nil)
suffix = if version == "*" || (name == "*" && version.blank?) sig { params(name: String, version: T.nilable(T.any(String, Integer))).returns(String) }
"{,.*}" def shared_library(name, version = nil)
elsif version.present? suffix = if version == "*" || (name == "*" && version.blank?)
".#{version}" "{,.*}"
elsif version.present?
".#{version}"
end
"#{name}.so#{suffix}"
end
sig { returns(String) }
def loader_path
"$ORIGIN"
end
sig { params(targets: T.nilable(T.any(Pathname, String))).void }
def deuniversalize_machos(*targets); end
sig { params(spec: SoftwareSpec).void }
def add_global_deps_to_spec(spec)
return unless DevelopmentTools.needs_build_formulae?
@global_deps ||= begin
dependency_collector = spec.dependency_collector
related_formula_names = Set.new([
name,
*aliases,
*versioned_formulae_names,
])
[
dependency_collector.gcc_dep_if_needed(related_formula_names),
dependency_collector.glibc_dep_if_needed(related_formula_names),
].compact.freeze
end
@global_deps.each { |dep| spec.dependency_collector.add(dep) }
end
sig { returns(T::Boolean) }
def valid_platform?
requirements.none?(MacOSRequirement)
end
end end
"#{name}.so#{suffix}"
end
sig { returns(String) }
def loader_path
"$ORIGIN"
end
sig { params(targets: T.nilable(T.any(Pathname, String))).void }
def deuniversalize_machos(*targets); end
sig { params(spec: SoftwareSpec).void }
def add_global_deps_to_spec(spec)
return unless DevelopmentTools.needs_build_formulae?
@global_deps ||= begin
dependency_collector = spec.dependency_collector
related_formula_names = Set.new([
name,
*aliases,
*versioned_formulae_names,
])
[
dependency_collector.gcc_dep_if_needed(related_formula_names),
dependency_collector.glibc_dep_if_needed(related_formula_names),
].compact.freeze
end
@global_deps.each { |dep| spec.dependency_collector.add(dep) }
end
sig { returns(T::Boolean) }
def valid_platform?
requirements.none?(MacOSRequirement)
end end
end end
Formula.prepend(OS::Linux::Formula)

View File

@ -1,24 +0,0 @@
# typed: strict
# frozen_string_literal: true
module Homebrew
module CLI
class Parser
undef set_default_options
undef validate_options
def set_default_options
@args["formula?"] = true if @args.respond_to?(:formula?)
end
def validate_options
return unless @args.respond_to?(:cask?)
return unless @args.cask?
# NOTE: We don't raise an error here because we don't want
# to print the help page or a stack trace.
odie "Invalid `--cask` usage: Casks do not work on Linux"
end
end
end
end

View File

@ -1,13 +1,17 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
class Cleaner module OS
private module Mac
module Cleaner
private
undef executable_path? sig { params(path: Pathname).returns(T::Boolean) }
def executable_path?(path)
sig { params(path: Pathname).returns(T::Boolean) } path.mach_o_executable? || path.text_executable?
def executable_path?(path) end
path.mach_o_executable? || path.text_executable? end
end end
end end
Cleaner.prepend(OS::Mac::Cleaner)

View File

@ -1,15 +1,17 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew module OS
class Cleanup module Mac
undef use_system_ruby? module Cleanup
sig { returns(T::Boolean) }
def use_system_ruby?
return false if Homebrew::EnvConfig.force_vendor_ruby?
sig { returns(T::Boolean) } Homebrew::EnvConfig.developer? && ENV["HOMEBREW_USE_RUBY_FROM_PATH"].present?
def use_system_ruby? end
return false if Homebrew::EnvConfig.force_vendor_ruby?
Homebrew::EnvConfig.developer? && ENV["HOMEBREW_USE_RUBY_FROM_PATH"].present?
end end
end end
end end
Homebrew::Cleanup.prepend(OS::Mac::Cleanup)

View File

@ -1,26 +1,26 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew module OS
module DevCmd module Mac
class Bottle < AbstractCommand module DevCmd
undef tar_args module Bottle
sig { returns(T::Array[String]) }
sig { returns(T::Array[String]) } def tar_args
def tar_args if MacOS.version >= :catalina
if MacOS.version >= :catalina ["--no-mac-metadata", "--no-acls", "--no-xattrs"].freeze
["--no-mac-metadata", "--no-acls", "--no-xattrs"].freeze else
else [].freeze
[].freeze end
end end
end
undef gnu_tar sig { params(gnu_tar_formula: Formula).returns(String) }
def gnu_tar(gnu_tar_formula)
sig { params(gnu_tar_formula: Formula).returns(String) } "#{gnu_tar_formula.opt_bin}/gtar"
def gnu_tar(gnu_tar_formula) end
"#{gnu_tar_formula.opt_bin}/gtar"
end end
end end
end end
end end
Homebrew::DevCmd::Bottle.prepend(OS::Mac::DevCmd::Bottle)

View File

@ -1,33 +1,39 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
class Formula module OS
undef valid_platform? module Mac
undef std_cmake_args module Formula
extend T::Helpers
sig { returns(T::Boolean) } requires_ancestor { ::Formula }
def valid_platform?
requirements.none?(LinuxRequirement)
end
sig { sig { returns(T::Boolean) }
params( def valid_platform?
install_prefix: T.any(String, Pathname), requirements.none?(LinuxRequirement)
install_libdir: T.any(String, Pathname), end
find_framework: String,
).returns(T::Array[String])
}
def std_cmake_args(install_prefix: prefix, install_libdir: "lib", find_framework: "LAST")
args = generic_std_cmake_args(install_prefix:, install_libdir:,
find_framework:)
# Avoid false positives for clock_gettime support on 10.11. sig {
# CMake cache entries for other weak symbols may be added here as needed. params(
args << "-DHAVE_CLOCK_GETTIME:INTERNAL=0" if MacOS.version == "10.11" && MacOS::Xcode.version >= "8.0" install_prefix: T.any(String, Pathname),
install_libdir: T.any(String, Pathname),
find_framework: String,
).returns(T::Array[String])
}
def std_cmake_args(install_prefix: prefix, install_libdir: "lib", find_framework: "LAST")
args = generic_std_cmake_args(install_prefix:, install_libdir:, find_framework:)
# Ensure CMake is using the same SDK we are using. # Avoid false positives for clock_gettime support on 10.11.
args << "-DCMAKE_OSX_SYSROOT=#{MacOS.sdk_for_formula(self).path}" if MacOS.sdk_root_needed? # CMake cache entries for other weak symbols may be added here as needed.
args << "-DHAVE_CLOCK_GETTIME:INTERNAL=0" if MacOS.version == "10.11" && MacOS::Xcode.version >= "8.0"
args # Ensure CMake is using the same SDK we are using.
args << "-DCMAKE_OSX_SYSROOT=#{MacOS.sdk_for_formula(self).path}" if MacOS.sdk_root_needed?
args
end
end
end end
end end
Formula.prepend(OS::Mac::Formula)

View File

@ -1,12 +1,20 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
class FormulaInstaller module OS
undef fresh_install? module Mac
module FormulaInstaller
extend T::Helpers
sig { params(formula: Formula).returns(T.nilable(T::Boolean)) } requires_ancestor { ::FormulaInstaller }
def fresh_install?(formula)
!Homebrew::EnvConfig.developer? && !OS::Mac.version.outdated_release? && sig { params(formula: Formula).returns(T.nilable(T::Boolean)) }
(!installed_as_dependency? || !formula.any_version_installed?) def fresh_install?(formula)
!Homebrew::EnvConfig.developer? && !OS::Mac.version.outdated_release? &&
(!installed_as_dependency? || !formula.any_version_installed?)
end
end
end end
end end
FormulaInstaller.prepend(OS::Mac::FormulaInstaller)

View File

@ -1,14 +1,18 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
class LinkageChecker module OS
undef system_libraries_exist_in_cache? module Mac
module LinkageChecker
private
private sig { returns(T::Boolean) }
def system_libraries_exist_in_cache?
sig { returns(T::Boolean) } # In macOS Big Sur and later, system libraries do not exist on-disk and instead exist in a cache.
def system_libraries_exist_in_cache? MacOS.version >= :big_sur
# In macOS Big Sur and later, system libraries do not exist on-disk and instead exist in a cache. end
MacOS.version >= :big_sur end
end end
end end
LinkageChecker.prepend(OS::Mac::LinkageChecker)

View File

@ -1,39 +1,46 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module Readall module OS
class << self module Mac
undef valid_casks? module Readall
extend T::Helpers
def valid_casks?(tap, os_name: nil, arch: Hardware::CPU.type) requires_ancestor { Kernel }
return true if os_name == :linux
current_macos_version = if os_name.is_a?(Symbol) sig { params(tap: Tap, os_name: T.nilable(Symbol), arch: T.nilable(Symbol)).returns(T::Boolean) }
MacOSVersion.from_symbol(os_name) def valid_casks?(tap, os_name: nil, arch: Hardware::CPU.type)
else return true if os_name == :linux
MacOS.version
end
success = T.let(true, T::Boolean) current_macos_version = if os_name.is_a?(Symbol)
tap.cask_files.each do |file| MacOSVersion.from_symbol(os_name)
cask = Cask::CaskLoader.load(file) else
MacOS.version
# Fine to have missing URLs for unsupported macOS
macos_req = cask.depends_on.macos
next if macos_req&.version && Array(macos_req.version).none? do |macos_version|
current_macos_version.compare(macos_req.comparator, macos_version)
end end
raise "Missing URL" if cask.url.nil? success = T.let(true, T::Boolean)
rescue Interrupt tap.cask_files.each do |file|
raise cask = Cask::CaskLoader.load(file)
rescue Exception => e # rubocop:disable Lint/RescueException
os_and_arch = "macOS #{current_macos_version} on #{arch}" # Fine to have missing URLs for unsupported macOS
onoe "Invalid cask (#{os_and_arch}): #{file}" macos_req = cask.depends_on.macos
$stderr.puts e next if macos_req&.version && Array(macos_req.version).none? do |macos_version|
success = false current_macos_version.compare(macos_req.comparator, macos_version)
end
raise "Missing URL" if cask.url.nil?
rescue Interrupt
raise
rescue Exception => e # rubocop:disable Lint/RescueException
os_and_arch = "macOS #{current_macos_version} on #{arch}"
onoe "Invalid cask (#{os_and_arch}): #{file}"
$stderr.puts e
success = false
end
success
end end
success
end end
end end
end end
Readall.singleton_class.prepend(OS::Mac::Readall)

View File

@ -1,21 +1,22 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew module OS
class SimulateSystem module Mac
class << self module SimulateSystem
undef simulating_or_running_on_macos?
undef current_os
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def simulating_or_running_on_macos? def simulating_or_running_on_macos?
os.blank? || [:macos, *MacOSVersion::SYMBOLS.keys].include?(os) return true if Homebrew::SimulateSystem.os.blank?
[:macos, *MacOSVersion::SYMBOLS.keys].include?(Homebrew::SimulateSystem.os)
end end
sig { returns(Symbol) } sig { returns(Symbol) }
def current_os def current_os
os || MacOS.version.to_sym Homebrew::SimulateSystem.os || MacOS.version.to_sym
end end
end end
end end
end end
Homebrew::SimulateSystem.singleton_class.prepend(OS::Mac::SimulateSystem)

View File

@ -1,4 +1,4 @@
# typed: strict # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "extend/os/linux/parser" if OS.linux? require "extend/os/linux/cli/parser" if OS.linux?

View File

@ -6,5 +6,4 @@
--ignore=/test/.gem --ignore=/test/.gem
--ignore=Formula --ignore=Formula
--ignore=Casks --ignore=Casks
--suppress-error-code=3008
--suppress-error-code=7019 --suppress-error-code=7019