From b45a7a29925e28525a765dbe4a26025a128c4502 Mon Sep 17 00:00:00 2001 From: Michael Cho Date: Sun, 1 Sep 2024 22:17:26 -0400 Subject: [PATCH] cleaner: rewrite pkg-config file Cellar paths and keg-only --- Library/Homebrew/cleaner.rb | 38 +++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Library/Homebrew/cleaner.rb b/Library/Homebrew/cleaner.rb index 5df0e39b158656..7c4bf26719413a 100644 --- a/Library/Homebrew/cleaner.rb +++ b/Library/Homebrew/cleaner.rb @@ -59,6 +59,8 @@ def clean observe_file_removal info_dir_file end + # TODO: rewrite_config_scripts + rewrite_pkgconfig rewrite_shebangs clean_python_metadata @@ -153,6 +155,42 @@ def clean_dir(directory) end end + sig { void } + def rewrite_pkgconfig + basepath = @formula.prefix.realpath + pc_files = %w[lib share].flat_map do |subdir| + next [] unless (pc_dir = basepath/subdir/"pkgconfig").exist? + next [] if @formula.skip_clean?(basepath/subdir) || @formula.skip_clean?(pc_dir) + + pc_dir.glob("*.pc").reject { |pc_file| @formula.skip_clean?(pc_file) } + end + return if pc_files.empty? + + keg_only_pc_files = @formula.deps + .reject { |d| d.build? || d.test? } + .map(&:to_formula) + .select(&:keg_only?) + .flat_map { |f| f.prefix.glob("{lib,share}/pkgconfig/*.pc") } + .to_h { |pc_file| [pc_file.basename(".pc").to_s, pc_file.to_s] } + keg_only_modules_pattern = keg_only_pc_files.keys.map { |mod| Regexp.escape(mod) }.join("|") + + pc_files.each do |pc_file| + any_modification = T.let(false, T::Boolean) + lines = pc_file.each_line.map do |line| + any_modification ||= line.gsub!(@formula.prefix.realpath.to_s, @formula.opt_prefix.to_s).present? + next line if keg_only_pc_files.empty? || !line.start_with?(/Requires(?:\.private)?:/) + + line.gsub(/(?<=[:,\s])(#{keg_only_modules_pattern})(?=[<=>!,\s])/) do |match| + any_modification ||= keg_only_pc_files.include?(match) + keg_only_pc_files[match] || match + end + end + next unless any_modification + + pc_file.atomic_write(lines.join) + end + end + sig { void } def rewrite_shebangs require "language/node"