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

cleaner: rewrite pkg-config file Cellar paths and keg-only #18229

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
43 changes: 43 additions & 0 deletions Library/Homebrew/cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
observe_file_removal info_dir_file
end

rewrite_pkgconfig
rewrite_shebangs
clean_python_metadata

Expand Down Expand Up @@ -153,6 +154,48 @@
end
end

sig { void }
def rewrite_pkgconfig
basepath = @formula.prefix.realpath
pc_files = %w[lib share].flat_map do |subdir|
pc_dir = basepath/subdir/"pkgconfig"
next [] if !pc_dir.exist? || @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?

# TODO: Add support for `brew unlink`-ed formulae and check on recursive dependencies
deps_pc_files = @formula.deps
.filter_map { |dep| dep.to_formula if !dep.build? && !dep.test? }
.select(&:keg_only?)
.flat_map { |f| f.opt_prefix.glob("{lib,share}/pkgconfig/*.pc") }
.to_h { |pc_file| [pc_file.basename(".pc").to_s.downcase, pc_file.to_s] }

Check warning on line 173 in Library/Homebrew/cleaner.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cleaner.rb#L172-L173

Added lines #L172 - L173 were not covered by tests
Copy link
Member Author

@cho-m cho-m Oct 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to figure out if there is a way to deal with whitespaces in HOMEBREW_PREFIX as this will break pkgconfig file. If I can't figure out a way, can just skip the modification if we detect whitespaces.

There is also an odd case of pkgconfig file backslash handling, e.g.

Cflags: -I/this/\
is/a/single/path

results in:

PKG_CONFIG_PATH=. pkg-config test --cflags
-I/this/is/a/single/path

May just ignore this for now until we find a scenario that needs it.


EDIT: This is actually more annoying due to possibility of installing relocatable bottle on system with whitespace in HOMEBREW_PREFIX.

deps_pc_modules_pattern = deps_pc_files.keys.map { |mod| Regexp.escape(mod) }.join("|")

pc_files.each do |pc_file|
modified_lines = pc_file.each_line.map do |line|
rewrote_prefix = line.gsub!(@formula.prefix.realpath.to_s, @formula.opt_prefix.to_s).present?
next [line, rewrote_prefix] if deps_pc_files.empty? || !line.start_with?(/Requires(?:\.private)?:/)

# pkgconf's pc.5 man page defines dependency list ABNF syntax as:
#
# > package-list = *WSP *( package-spec *( package-sep ) )
# > package-sep = WSP / ","
# > package-spec = package-key [ ver-op package-version ]
# > ver-op = "<" / "<=" / "=" / "!=" / ">=" / ">"
#
# A simplified regular expression is used to lookahead/lookbehind for common
# separator characters to extract the modules used in Requires/Requires.private
rewrote_module = line.gsub!(/(?<=[:,\s])(#{deps_pc_modules_pattern})(?=[<=>!,\s])/io, deps_pc_files).present?
[line, rewrote_prefix || rewrote_module]

Check warning on line 191 in Library/Homebrew/cleaner.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/cleaner.rb#L190-L191

Added lines #L190 - L191 were not covered by tests
end
next if modified_lines.none?(&:second)

pc_file.atomic_write(modified_lines.map(&:first).join)
end
end

sig { void }
def rewrite_shebangs
require "language/node"
Expand Down
44 changes: 44 additions & 0 deletions Library/Homebrew/test/cleaner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,50 @@

expect(file.read).to eq "brew\n"
end

it "modifies Cellar prefix in pkg-config files" do
file = f.lib/"pkgconfig/test.pc"
file.dirname.mkpath
file.write <<~EOS
prefix=#{f.prefix}
includedir=#{f.include}
libdir=#{f.lib}
Name: test
Description: test module
Version: 1.2.3
Cflags: -I${includedir}
EOS

cleaner.clean

expect(file.read).to eq <<~EOS
prefix=#{f.opt_prefix}
includedir=#{f.opt_include}
libdir=#{f.opt_lib}
Name: test
Description: test module
Version: 1.2.3
Cflags: -I${includedir}
EOS
end

it "does not modify pkg-config files with no rewrites" do
file = f.lib/"pkgconfig/test.pc"
file.dirname.mkpath
file.write <<~EOS
prefix=#{f.opt_prefix}
includedir=${prefix}/include
Name: test
Description: test module
Version: 1.2.3
Cflags: -I${includedir}
EOS
start_mtime = file.mtime

cleaner.clean

expect(file.mtime).to eq start_mtime
end
end

describe "::skip_clean" do
Expand Down
Loading