Skip to content

Commit d7266a1

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

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Library/Homebrew/cleaner.rb

+36
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,40 @@ def clean_dir(directory)
153155
end
154156
end
155157

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

0 commit comments

Comments
 (0)