Skip to content

Commit d4f300b

Browse files
itamarozaniebclaude
committed
gh-119696: Add --enable-relocatable, for relative pkg-config prefixes
Add an `--enable-relocatable` configure option (off by default) for changes that make an installation easier to move but aren't right for every build. It is a work in progress: this is the first thing it does, and it doesn't make an installation fully relocatable yet. With the option, `make install` writes `prefix=${pcfiledir}/../..` in `python-3.X.pc` and `python-3.X-embed.pc` (with as many `..` as needed) instead of the absolute prefix, so the files stay correct when the installation is moved. It isn't the default because pkg-config doesn't recognise the resulting paths as system directories for an install under /usr. Without the option the installed files are unchanged. Co-authored-by: Zanie Blue <contact@zanie.dev> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent d8bc666 commit d4f300b

5 files changed

Lines changed: 88 additions & 4 deletions

File tree

‎Lib/test/test_sysconfig.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,28 @@ def test_makefile_overwrites_config_vars(self):
709709
self.assertNotEqual(data['prefix'], data['base_prefix'])
710710
self.assertNotEqual(data['exec_prefix'], data['base_exec_prefix'])
711711

712+
@unittest.skipIf(os.name != 'posix', 'pkg-config files are only installed on POSIX')
713+
@unittest.skipIf(is_wasi or is_android or is_apple_mobile,
714+
'no pkg-config files on this platform')
715+
def test_pkgconfig_prefix(self):
716+
if get_config_var('RELOCATABLE') != 'yes':
717+
self.skipTest('requires --enable-relocatable')
718+
if is_python_build():
719+
self.skipTest('pkg-config files are only installed by make install')
720+
libpc = os.path.join(os.path.dirname(sys._stdlib_dir), 'pkgconfig')
721+
ldversion = get_config_var('LDVERSION')
722+
for name in (f'python-{ldversion}.pc', f'python-{ldversion}-embed.pc'):
723+
path = os.path.join(libpc, name)
724+
if not os.path.exists(path):
725+
self.skipTest(f'{path} not found')
726+
with self.subTest(file=name), open(path, encoding='utf-8') as f:
727+
prefix = next(line.removeprefix('prefix=').rstrip('\n')
728+
for line in f if line.startswith('prefix='))
729+
self.assertStartsWith(prefix, '${pcfiledir}/')
730+
resolved = prefix.replace('${pcfiledir}', libpc)
731+
self.assertEqual(os.path.realpath(resolved),
732+
os.path.realpath(sys.base_prefix))
733+
712734
@unittest.skipIf(os.name != 'posix', '_sysconfig-vars JSON file is only available on POSIX')
713735
@unittest.skipIf(is_wasi, "_sysconfig-vars JSON file currently isn't available on WASI")
714736
@unittest.skipIf(is_android or is_apple_mobile, 'Android and iOS change the prefix')

‎Makefile.pre.in‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ DSYMUTIL= @DSYMUTIL@
6868
DSYMUTIL_PATH= @DSYMUTIL_PATH@
6969

7070
GNULD= @GNULD@
71+
RELOCATABLE= @RELOCATABLE@
7172

7273
# Shell used by make (some versions default to the login shell, which is bad)
7374
SHELL= /bin/sh -e
@@ -2940,6 +2941,24 @@ python-config: $(srcdir)/Misc/python-config.in Misc/python-config.sh
29402941
cp python-config.py python-config; \
29412942
fi
29422943

2944+
# With --enable-relocatable, the installed pkg-config files define prefix
2945+
# relative to their own location, when the pkgconfig directory is inside
2946+
# the prefix.
2947+
Misc/python-install.pc Misc/python-embed-install.pc: Misc/python.pc Misc/python-embed.pc
2948+
rel=; dir='$(LIBPC)'; \
2949+
if test "$(RELOCATABLE)" = yes; then \
2950+
while test "$$dir" != '$(prefix)' && test "$$dir" != /; do \
2951+
rel="$$rel/.."; dir=`dirname "$$dir"`; \
2952+
done; \
2953+
fi; \
2954+
for pc in python python-embed; do \
2955+
if test "$$dir" = '$(prefix)'; then \
2956+
sed "s|^prefix=.*|prefix=\$${pcfiledir}$$rel|" Misc/$$pc.pc > Misc/$$pc-install.pc; \
2957+
else \
2958+
cp Misc/$$pc.pc Misc/$$pc-install.pc; \
2959+
fi; \
2960+
done
2961+
29432962
# macOS' make seems to ignore a dependency on a
29442963
# "$(BUILD_SCRIPTS_DIR): $(MKDIR_P) $@" rule.
29452964
BUILD_SCRIPTS_DIR=build/scripts-$(VERSION)
@@ -3026,7 +3045,7 @@ LIBPL= @LIBPL@
30263045
LIBPC= $(LIBDIR)/pkgconfig
30273046

30283047
.PHONY: libainstall
3029-
libainstall: all scripts
3048+
libainstall: all scripts Misc/python-install.pc Misc/python-embed-install.pc
30303049
@for i in $(LIBDIR) $(LIBPL) $(LIBPC) $(BINDIR); \
30313050
do \
30323051
if test ! -d $(DESTDIR)$$i; then \
@@ -3059,8 +3078,8 @@ libainstall: all scripts
30593078
$(INSTALL_DATA) Modules/Setup.bootstrap $(DESTDIR)$(LIBPL)/Setup.bootstrap
30603079
$(INSTALL_DATA) Modules/Setup.stdlib $(DESTDIR)$(LIBPL)/Setup.stdlib
30613080
$(INSTALL_DATA) Modules/Setup.local $(DESTDIR)$(LIBPL)/Setup.local
3062-
$(INSTALL_DATA) Misc/python.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION).pc
3063-
$(INSTALL_DATA) Misc/python-embed.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION)-embed.pc
3081+
$(INSTALL_DATA) Misc/python-install.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION).pc
3082+
$(INSTALL_DATA) Misc/python-embed-install.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION)-embed.pc
30643083
$(INSTALL_SCRIPT) $(srcdir)/Modules/makesetup $(DESTDIR)$(LIBPL)/makesetup
30653084
$(INSTALL_SCRIPT) $(srcdir)/install-sh $(DESTDIR)$(LIBPL)/install-sh
30663085
$(INSTALL_SCRIPT) python-config.py $(DESTDIR)$(LIBPL)/python-config.py
@@ -3426,7 +3445,8 @@ distclean: clobber docclean
34263445
-rm -f core Makefile Makefile.pre config.status Modules/Setup.local \
34273446
Modules/Setup.bootstrap Modules/Setup.stdlib \
34283447
Modules/ld_so_aix Modules/python.exp Misc/python.pc \
3429-
Misc/python-embed.pc Misc/python-config.sh
3448+
Misc/python-embed.pc Misc/python-install.pc \
3449+
Misc/python-embed-install.pc Misc/python-config.sh
34303450
-rm -f python*-gdb.py
34313451
# Issue #28258: set LC_ALL to avoid issues with Estonian locale.
34323452
# Expansion is performed here by shell (spawned by make) itself before
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add the ``--enable-relocatable`` :program:`configure` option. It is a work
2+
in progress and does not yet make the installation fully relocatable: for
3+
now, it makes the installed pkg-config files define ``prefix`` relative to
4+
their own location.

‎configure‎

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure.ac‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,18 @@ if test "x$enable_profiling" = xyes; then
17591759
LDFLAGS="-pg $LDFLAGS"
17601760
fi
17611761

1762+
AC_MSG_CHECKING([for --enable-relocatable])
1763+
AC_ARG_ENABLE([relocatable],
1764+
[AS_HELP_STRING([--enable-relocatable],
1765+
[make the installation easier to relocate
1766+
(work in progress; default is no)])],
1767+
[], [enable_relocatable=no])
1768+
AS_CASE([$enable_relocatable],
1769+
[yes|no], [],
1770+
[AC_MSG_ERROR([--enable-relocatable must be yes or no])])
1771+
AC_MSG_RESULT([$enable_relocatable])
1772+
AC_SUBST([RELOCATABLE], [$enable_relocatable])
1773+
17621774
AC_MSG_CHECKING([LDLIBRARY])
17631775

17641776
# Apple framework builds need more magic. LDLIBRARY is the dynamic

0 commit comments

Comments
 (0)