diff --git a/craft_parts/packages/normalize.py b/craft_parts/packages/normalize.py index 4a33a6de7..8b2186a29 100644 --- a/craft_parts/packages/normalize.py +++ b/craft_parts/packages/normalize.py @@ -173,6 +173,10 @@ def fix_pkg_config( - From snaps built locally: `/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 @@ -185,10 +189,17 @@ def fix_pkg_config( f"^prefix=(?P{'|'.join(prefixes_to_trim)})(?P.*)" ) pattern = re.compile("^prefix=(?P.*)") + 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) diff --git a/docs/common/craft-parts/craft-parts.wordlist.txt b/docs/common/craft-parts/craft-parts.wordlist.txt index d9057de31..12867458e 100644 --- a/docs/common/craft-parts/craft-parts.wordlist.txt +++ b/docs/common/craft-parts/craft-parts.wordlist.txt @@ -408,6 +408,7 @@ packahe param params pc +pcfiledir pkgconfig pre prepend diff --git a/docs/reference/changelog.rst b/docs/reference/changelog.rst index 98355f773..d6428fa1f 100644 --- a/docs/reference/changelog.rst +++ b/docs/reference/changelog.rst @@ -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) ------------------ diff --git a/tests/unit/packages/test_normalize.py b/tests/unit/packages/test_normalize.py index 3a469ee25..44a2ef401 100644 --- a/tests/unit/packages/test_normalize.py +++ b/tests/unit/packages/test_normalize.py @@ -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"