Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use prepended modules instead of undef for OS-specific code #18305

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Library/Homebrew/cli/args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class Args < OpenStruct
sig { returns(T::Array[String]) }
attr_reader :options_only, :flags_only

# undefine tap to allow --tap argument
undef tap
dduugg marked this conversation as resolved.
Show resolved Hide resolved

sig { void }
def initialize
require "cli/named_args"
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Parser
}.freeze, T::Hash[Symbol, String])
private_constant :ArgType, :HIDDEN_DESC_PLACEHOLDER, :SYMBOL_TO_USAGE_MAPPING

sig { returns(Args) }
attr_reader :args
dduugg marked this conversation as resolved.
Show resolved Hide resolved

sig { returns(Args::OptionsType) }
attr_reader :processed_options

Expand Down
32 changes: 19 additions & 13 deletions Library/Homebrew/extend/os/linux/cleanup.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
# typed: strict
# frozen_string_literal: true

module Homebrew
class Cleanup
undef use_system_ruby?
module OS
module Linux
module Cleanup
dduugg marked this conversation as resolved.
Show resolved Hide resolved
extend T::Helpers

sig { returns(T::Boolean) }
def use_system_ruby?
return false if Homebrew::EnvConfig.force_vendor_ruby?
requires_ancestor { Homebrew::Cleanup }

rubies = [which("ruby"), which("ruby", ORIGINAL_PATHS)].compact
system_ruby = Pathname.new("/usr/bin/ruby")
rubies << system_ruby if system_ruby.exist?
sig { returns(T::Boolean) }
def use_system_ruby?
return false if Homebrew::EnvConfig.force_vendor_ruby?

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
rubies = [which("ruby"), which("ruby", ORIGINAL_PATHS)].compact
system_ruby = Pathname.new("/usr/bin/ruby")

Check warning on line 16 in Library/Homebrew/extend/os/linux/cleanup.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/linux/cleanup.rb#L15-L16

Added lines #L15 - L16 were not covered by tests
rubies << system_ruby if system_ruby.exist?

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 warning on line 21 in Library/Homebrew/extend/os/linux/cleanup.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/linux/cleanup.rb#L19-L21

Added lines #L19 - L21 were not covered by tests
check_ruby_version, RUBY_VERSION
end
end
end
end
end

Homebrew::Cleanup.prepend(OS::Linux::Cleanup)
31 changes: 31 additions & 0 deletions Library/Homebrew/extend/os/linux/cli/parser.rb
Original file line number Diff line number Diff line change
@@ -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)
92 changes: 48 additions & 44 deletions Library/Homebrew/extend/os/linux/formula.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
# typed: true # rubocop:disable Sorbet/StrictSigil
# frozen_string_literal: true

class Formula
undef shared_library
undef loader_path
undef deuniversalize_machos
undef add_global_deps_to_spec
undef valid_platform?

sig { params(name: String, version: T.nilable(T.any(String, Integer))).returns(String) }
def shared_library(name, version = nil)
suffix = if version == "*" || (name == "*" && version.blank?)
"{,.*}"
elsif version.present?
".#{version}"
end
"#{name}.so#{suffix}"
end
module OS
module Linux
module Formula
extend T::Helpers

sig { returns(String) }
def loader_path
"$ORIGIN"
end
requires_ancestor { ::Formula }

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 { params(name: String, version: T.nilable(T.any(String, Integer))).returns(String) }
def shared_library(name, version = nil)
suffix = if version == "*" || (name == "*" && version.blank?)
"{,.*}"
elsif version.present?
".#{version}"
end
"#{name}.so#{suffix}"
end

sig { returns(String) }
def loader_path
"$ORIGIN"

Check warning on line 23 in Library/Homebrew/extend/os/linux/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/linux/formula.rb#L23

Added line #L23 was not covered by tests
end

sig { returns(T::Boolean) }
def valid_platform?
requirements.none?(MacOSRequirement)
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)
24 changes: 0 additions & 24 deletions Library/Homebrew/extend/os/linux/parser.rb

This file was deleted.

18 changes: 11 additions & 7 deletions Library/Homebrew/extend/os/mac/cleaner.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# typed: strict
# frozen_string_literal: true

class Cleaner
private
module OS
module Mac
module Cleaner
private

undef executable_path?

sig { params(path: Pathname).returns(T::Boolean) }
def executable_path?(path)
path.mach_o_executable? || path.text_executable?
sig { params(path: Pathname).returns(T::Boolean) }
def executable_path?(path)
path.mach_o_executable? || path.text_executable?
end
end
end
end

Cleaner.prepend(OS::Mac::Cleaner)
18 changes: 10 additions & 8 deletions Library/Homebrew/extend/os/mac/cleanup.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# typed: strict
# frozen_string_literal: true

module Homebrew
class Cleanup
undef use_system_ruby?
module OS
module Mac
module Cleanup
sig { returns(T::Boolean) }
def use_system_ruby?
return false if Homebrew::EnvConfig.force_vendor_ruby?

sig { returns(T::Boolean) }
def use_system_ruby?
return false if Homebrew::EnvConfig.force_vendor_ruby?

Homebrew::EnvConfig.developer? && ENV["HOMEBREW_USE_RUBY_FROM_PATH"].present?
Homebrew::EnvConfig.developer? && ENV["HOMEBREW_USE_RUBY_FROM_PATH"].present?

Check warning on line 11 in Library/Homebrew/extend/os/mac/cleanup.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/mac/cleanup.rb#L11

Added line #L11 was not covered by tests
end
end
end
end

Homebrew::Cleanup.prepend(OS::Mac::Cleanup)
34 changes: 17 additions & 17 deletions Library/Homebrew/extend/os/mac/dev-cmd/bottle.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# typed: strict
# frozen_string_literal: true

module Homebrew
module DevCmd
class Bottle < AbstractCommand
undef tar_args

sig { returns(T::Array[String]) }
def tar_args
if MacOS.version >= :catalina
["--no-mac-metadata", "--no-acls", "--no-xattrs"].freeze
else
[].freeze
module OS
module Mac
module DevCmd
module Bottle
sig { returns(T::Array[String]) }
def tar_args
if MacOS.version >= :catalina

Check warning on line 10 in Library/Homebrew/extend/os/mac/dev-cmd/bottle.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/mac/dev-cmd/bottle.rb#L10

Added line #L10 was not covered by tests
["--no-mac-metadata", "--no-acls", "--no-xattrs"].freeze
else
[].freeze
end
end
end

undef gnu_tar

sig { params(gnu_tar_formula: Formula).returns(String) }
def gnu_tar(gnu_tar_formula)
"#{gnu_tar_formula.opt_bin}/gtar"
sig { params(gnu_tar_formula: Formula).returns(String) }
def gnu_tar(gnu_tar_formula)
"#{gnu_tar_formula.opt_bin}/gtar"

Check warning on line 19 in Library/Homebrew/extend/os/mac/dev-cmd/bottle.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/mac/dev-cmd/bottle.rb#L19

Added line #L19 was not covered by tests
end
end
end
end
end

Homebrew::DevCmd::Bottle.prepend(OS::Mac::DevCmd::Bottle)
58 changes: 32 additions & 26 deletions Library/Homebrew/extend/os/mac/formula.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
# typed: strict
# frozen_string_literal: true

class Formula
undef valid_platform?
undef std_cmake_args
module OS
module Mac
module Formula
extend T::Helpers

sig { returns(T::Boolean) }
def valid_platform?
requirements.none?(LinuxRequirement)
end
requires_ancestor { ::Formula }

sig { returns(T::Boolean) }
def valid_platform?
requirements.none?(LinuxRequirement)
end

sig {
params(

Check warning on line 17 in Library/Homebrew/extend/os/mac/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/mac/formula.rb#L17

Added line #L17 was not covered by tests
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:)

Check warning on line 24 in Library/Homebrew/extend/os/mac/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/mac/formula.rb#L24

Added line #L24 was not covered by tests

sig {
params(
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:)

# Avoid false positives for clock_gettime support on 10.11.
# 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"

# 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
# Avoid false positives for clock_gettime support on 10.11.
# 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"

# 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

Check warning on line 33 in Library/Homebrew/extend/os/mac/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/extend/os/mac/formula.rb#L33

Added line #L33 was not covered by tests
end
end
end
end

Formula.prepend(OS::Mac::Formula)
Loading