Skip to content

Commit

Permalink
fix: don't modify 'prefix' for relocatable pkg-config files (#5157)
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.

Co-authored-by: Callahan <[email protected]>
  • Loading branch information
sergio-costas and mr-cal authored Jan 27, 2025
1 parent 359a364 commit 0a7f753
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion requirements-devel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ craft-grammar==2.0.1
# via
# craft-application
# snapcraft (setup.py)
craft-parts==2.3.0
craft-parts==2.4.1
# via
# craft-application
# snapcraft (setup.py)
Expand Down
2 changes: 1 addition & 1 deletion requirements-docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ craft-grammar==2.0.1
# via
# craft-application
# snapcraft (setup.py)
craft-parts==2.3.0
craft-parts==2.4.1
# via
# craft-application
# snapcraft (setup.py)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ craft-grammar==2.0.1
# via
# craft-application
# snapcraft (setup.py)
craft-parts==2.3.0
craft-parts==2.4.1
# via
# craft-application
# snapcraft (setup.py)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def recursive_data_files(directory, install_directory):
"craft-archives~=2.0",
"craft-cli>=2.15.0",
"craft-grammar>=2.0.1,<3.0.0",
"craft-parts==2.3.0",
"craft-parts==2.4.1",
"craft-platforms~=0.4",
"craft-providers>=2.0.4,<3.0.0",
"craft-store>=3.0.2,<4.0.0",
Expand Down
11 changes: 11 additions & 0 deletions snapcraft_legacy/internal/repo/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,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 @@ -358,10 +362,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
16 changes: 16 additions & 0 deletions tests/legacy/unit/repo/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,22 @@ def test_fix_pkg_config_trim_prefix(
)


def test_fix_pkg_config_with_pcfiledir(
tmpdir,
pkg_config_file,
expected_pkg_config_content,
):
"""Verify prefixes that begin with ${pcfiledir} aren't modified."""
pc_file = tmpdir / "my-file.pc"
pkg_config_file(pc_file, "${pcfiledir}/../../..")

fix_pkg_config(tmpdir, pc_file)

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


def test_normalize_fix_pkg_config(tmpdir, pkg_config_file, expected_pkg_config_content):
"""Verify normalization fixes pkg-config files."""
pc_file = tmpdir / "my-file.pc"
Expand Down

0 comments on commit 0a7f753

Please sign in to comment.