Skip to content

Commit

Permalink
fix: don't modify 'prefix' for relocatable pkg-config files (#977)
Browse files Browse the repository at this point in the history
A relocatable `.pc` file has a `prefix` key beginning with
`${pcfiledir}`. The `prefix` key is not modified if the `.pc`
file is relocatable.
  • Loading branch information
sergio-costas authored Jan 24, 2025
1 parent e663f97 commit b4a09d9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions craft_parts/packages/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ def fix_pkg_config(
- From snaps built locally: `<local-path-to-project>/stage`
- Built during the build stage: the install directory
But if the prefix begins with `pcfiledir` variable, it must be kept as-is,
because that variable refers to the current location of the .pc file. It
allows to create "relocatable" pkgconfig files, so no changes are required.
:param pkg_config_file: pkg-config (.pc) file to modify
:param prefix_prepend: directory to prepend to the prefix
:param prefix_trim: directory to remove from prefix
Expand All @@ -185,10 +189,17 @@ def fix_pkg_config(
f"^prefix=(?P<trim>{'|'.join(prefixes_to_trim)})(?P<prefix>.*)"
)
pattern = re.compile("^prefix=(?P<prefix>.*)")
pattern_pcfiledir = re.compile("^prefix *= *[$]{pcfiledir}.*")

# process .pc file
with fileinput.input(pkg_config_file, inplace=True) as input_file:
for line in input_file:
# If the prefix begins with ${pcfiledir} statement, this is
# a position-independent (thus, "relocatable") .pc file, so
# no changes are required.
if pattern_pcfiledir.search(line) is not None:
print(line, end="")
continue
match = pattern.search(line)
match_trim = pattern_trim.search(line)

Expand Down
1 change: 1 addition & 0 deletions docs/common/craft-parts/craft-parts.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ packahe
param
params
pc
pcfiledir
pkgconfig
pre
prepend
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Changelog
*********

X.Y.Z (2025-MM-DD)
------------------

Bug fixes:

- Preserve the ``pcfiledir`` tag in ``pkgconfig`` files.

2.4.0 (2025-01-23)
------------------
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/packages/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ def test_normalize_fix_pkg_config(
f"{tmpdir}/usr"
)

def test_normalize_fix_pkg_config_with_pcfiledir(
self, tmpdir, pkg_config_file, expected_pkg_config_content
):
"""Verify normalization fixes pkg-config files."""
pc_file = tmpdir / "my-file.pc"
pkg_config_file(pc_file, "${pcfiledir}/../../..")
normalize(tmpdir, repository=DummyRepository)

assert pc_file.read_text(encoding="utf-8") == expected_pkg_config_content(
"${pcfiledir}/../../.."
)

def test_fix_pkg_config_is_dir(self, tmpdir):
"""Verify directories ending in .pc do not raise an error."""
pc_file = tmpdir / "granite.pc"
Expand Down

0 comments on commit b4a09d9

Please sign in to comment.