From 7b74730f9d55dcaaaeeac944de1c7ab8bd9a2221 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Tue, 20 Jul 2021 12:10:49 +0800 Subject: [PATCH 1/7] formula_cellar_checks: fix universal binary handling The `check_binary_arches` audit will fail any formula that produces universal binaries. We have a handful of formulae in Homebrew/core that do this (see any formula that does `ENV.permit_arch_flags`, for example). Moreover, some third party taps may have their own formulae that build universal binaries. I've updated the check so that it ignores a formula that produces universal binaries whenever the formula is in the appropriate allowlist. We'll need to create one in Homebrew/core for the handful of formulae that do (expectedly) build universal binaries. If we don't want to maintain an allowlist, we can easily modify this to pass over any formulae that builds compatible universal binaries. I've also fixed the spacing of the error this audit produces whenever there is more than one file that fails the audit. --- Library/Homebrew/formula_cellar_checks.rb | 33 ++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index cff0baa647..7ef33945b6 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -323,13 +323,34 @@ module FormulaCellarChecks mismatches = keg.binary_executable_or_library_files.reject do |file| file.arch == Hardware::CPU.arch end - return if mismatches.empty? - <<~EOS - Binaries built for a non-native architecture were installed into #{formula}'s prefix. - The offending files are: - #{mismatches * "\n "} - EOS + compatible_universal_binaries = mismatches.select do |file| + file.arch == :universal && file.archs.include?(Hardware::CPU.arch) + end + mismatches -= compatible_universal_binaries + + return if mismatches.empty? && compatible_universal_binaries.empty? + return if mismatches.empty? && tap_audit_exception(:universal_binary_allowlist, formula.name) + + s = "" + + unless mismatches.empty? + s += <<~EOS + Binaries built for an incompatible architecture were installed into #{formula}'s prefix. + The offending files are: + #{mismatches * "\n "} + EOS + end + + unless compatible_universal_binaries.empty? + s += <<~EOS + Unexpected universal binaries were found. + The offending files are: + #{compatible_universal_binaries * "\n "} + EOS + end + + s end def audit_installed From 1678a3785e1e04a6976e723a9e176defd2ab41f4 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Tue, 20 Jul 2021 12:35:09 +0800 Subject: [PATCH 2/7] Fix logic in `check_binary_arches` --- Library/Homebrew/formula_cellar_checks.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 7ef33945b6..16f69fbbc0 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -323,14 +323,15 @@ module FormulaCellarChecks mismatches = keg.binary_executable_or_library_files.reject do |file| file.arch == Hardware::CPU.arch end + return if mismatches.empty? compatible_universal_binaries = mismatches.select do |file| file.arch == :universal && file.archs.include?(Hardware::CPU.arch) end mismatches -= compatible_universal_binaries - return if mismatches.empty? && compatible_universal_binaries.empty? - return if mismatches.empty? && tap_audit_exception(:universal_binary_allowlist, formula.name) + universal_binaries_expected = tap_audit_exception(:universal_binary_allowlist, formula.name) + return if mismatches.empty? && universal_binaries_expected s = "" @@ -342,7 +343,7 @@ module FormulaCellarChecks EOS end - unless compatible_universal_binaries.empty? + if !compatible_universal_binaries.empty? && !universal_binaries_expected s += <<~EOS Unexpected universal binaries were found. The offending files are: From 55cc1eb8b098283cd280cf91974890fadcf808f4 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Tue, 20 Jul 2021 13:08:15 +0800 Subject: [PATCH 3/7] Check `tap_audit_exception` only if tap is present --- Library/Homebrew/formula_cellar_checks.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 16f69fbbc0..080adbbe5b 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -330,7 +330,8 @@ module FormulaCellarChecks end mismatches -= compatible_universal_binaries - universal_binaries_expected = tap_audit_exception(:universal_binary_allowlist, formula.name) + universal_binaries_expected = + formula.tap.present? && tap_audit_exception(:universal_binary_allowlist, formula.name) return if mismatches.empty? && universal_binaries_expected s = "" From 239fd06728131c0b82e0788970d6481d23ed2af0 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Tue, 20 Jul 2021 18:40:19 +0800 Subject: [PATCH 4/7] Enforce universal binary check only for Homebrew/core Also, prefer `if` to `unless`. --- Library/Homebrew/formula_cellar_checks.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 080adbbe5b..f7f20f4933 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -330,13 +330,15 @@ module FormulaCellarChecks end mismatches -= compatible_universal_binaries - universal_binaries_expected = - formula.tap.present? && tap_audit_exception(:universal_binary_allowlist, formula.name) + universal_binaries_expected = true + universal_binaries_expected = if formula.tap.present? && formula.tap.core_tap? + tap_audit_exception(:universal_binary_allowlist, formula.name) + end return if mismatches.empty? && universal_binaries_expected s = "" - unless mismatches.empty? + if mismatches.any? s += <<~EOS Binaries built for an incompatible architecture were installed into #{formula}'s prefix. The offending files are: @@ -344,7 +346,7 @@ module FormulaCellarChecks EOS end - if !compatible_universal_binaries.empty? && !universal_binaries_expected + if compatible_universal_binaries.any? && !universal_binaries_expected s += <<~EOS Unexpected universal binaries were found. The offending files are: From 663aee0a674f1873f44655853700baa807e7cfa7 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Tue, 20 Jul 2021 18:48:40 +0800 Subject: [PATCH 5/7] Fix style The linter complained about useless assignment to `universal_binaries_expected`. It wasn't useless, but ok. --- Library/Homebrew/formula_cellar_checks.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index f7f20f4933..627d15c492 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -330,9 +330,10 @@ module FormulaCellarChecks end mismatches -= compatible_universal_binaries - universal_binaries_expected = true universal_binaries_expected = if formula.tap.present? && formula.tap.core_tap? tap_audit_exception(:universal_binary_allowlist, formula.name) + else + true end return if mismatches.empty? && universal_binaries_expected From 699051b3c320fb9c5e40d63b27ef3b9473ad61eb Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Tue, 20 Jul 2021 18:53:53 +0800 Subject: [PATCH 6/7] Use `#present?` instead of `#any?` --- Library/Homebrew/formula_cellar_checks.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 627d15c492..16439b2c52 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -339,7 +339,7 @@ module FormulaCellarChecks s = "" - if mismatches.any? + if mismatches.present? s += <<~EOS Binaries built for an incompatible architecture were installed into #{formula}'s prefix. The offending files are: @@ -347,7 +347,7 @@ module FormulaCellarChecks EOS end - if compatible_universal_binaries.any? && !universal_binaries_expected + if compatible_universal_binaries.present? && !universal_binaries_expected s += <<~EOS Unexpected universal binaries were found. The offending files are: From a29098c81301beeea5a9ef8bc5c993ce8765ebe2 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 21 Jul 2021 12:05:54 +0800 Subject: [PATCH 7/7] Use `#partition` to simplify extracting `compatible_universal_binaries` --- Library/Homebrew/formula_cellar_checks.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 16439b2c52..dde65b9769 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -325,10 +325,9 @@ module FormulaCellarChecks end return if mismatches.empty? - compatible_universal_binaries = mismatches.select do |file| + compatible_universal_binaries, mismatches = mismatches.partition do |file| file.arch == :universal && file.archs.include?(Hardware::CPU.arch) end - mismatches -= compatible_universal_binaries universal_binaries_expected = if formula.tap.present? && formula.tap.core_tap? tap_audit_exception(:universal_binary_allowlist, formula.name)