Skip to content

Commit b45a7a2

Browse files
committed
cleaner: rewrite pkg-config file Cellar paths and keg-only
1 parent 1f9bd2d commit b45a7a2

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Library/Homebrew/cleaner.rb

+38
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def clean
5959
observe_file_removal info_dir_file
6060
end
6161

62+
# TODO: rewrite_config_scripts
63+
rewrite_pkgconfig
6264
rewrite_shebangs
6365
clean_python_metadata
6466

@@ -153,6 +155,42 @@ def clean_dir(directory)
153155
end
154156
end
155157

158+
sig { void }
159+
def rewrite_pkgconfig
160+
basepath = @formula.prefix.realpath
161+
pc_files = %w[lib share].flat_map do |subdir|
162+
next [] unless (pc_dir = basepath/subdir/"pkgconfig").exist?
163+
next [] if @formula.skip_clean?(basepath/subdir) || @formula.skip_clean?(pc_dir)
164+
165+
pc_dir.glob("*.pc").reject { |pc_file| @formula.skip_clean?(pc_file) }
166+
end
167+
return if pc_files.empty?
168+
169+
keg_only_pc_files = @formula.deps
170+
.reject { |d| d.build? || d.test? }
171+
.map(&:to_formula)
172+
.select(&:keg_only?)
173+
.flat_map { |f| f.prefix.glob("{lib,share}/pkgconfig/*.pc") }
174+
.to_h { |pc_file| [pc_file.basename(".pc").to_s, pc_file.to_s] }
175+
keg_only_modules_pattern = keg_only_pc_files.keys.map { |mod| Regexp.escape(mod) }.join("|")
176+
177+
pc_files.each do |pc_file|
178+
any_modification = T.let(false, T::Boolean)
179+
lines = pc_file.each_line.map do |line|
180+
any_modification ||= line.gsub!(@formula.prefix.realpath.to_s, @formula.opt_prefix.to_s).present?
181+
next line if keg_only_pc_files.empty? || !line.start_with?(/Requires(?:\.private)?:/)
182+
183+
line.gsub(/(?<=[:,\s])(#{keg_only_modules_pattern})(?=[<=>!,\s])/) do |match|
184+
any_modification ||= keg_only_pc_files.include?(match)
185+
keg_only_pc_files[match] || match
186+
end
187+
end
188+
next unless any_modification
189+
190+
pc_file.atomic_write(lines.join)
191+
end
192+
end
193+
156194
sig { void }
157195
def rewrite_shebangs
158196
require "language/node"

0 commit comments

Comments
 (0)