Skip to content

Commit 2bcfe11

Browse files
itamarozanieb
andauthored
gh-119696: Support relative pkg-config prefixes behind --enable-relocatable flag (#162185)
`make install` writes `python-3.X.pc` and `python-3.X-embed.pc` with an absolute `prefix=`, so they are wrong once the installation is relocated. Using `prefix=${pcfiledir}/../..` fixes that, but it isn't a viable default: pkg-config doesn't treat the resulting paths as system directories when the installation is under `/usr` (python/prebuilt-cpython#45). So this also introduces an `--enable-relocatable` configure option as an opt-in mechanism for this and future build-time tweaks that are needed to support relocatability but shouldn't be made the default. Since relocatability is work-in-progress, and specifying the option doesn't yet produce a fully functional relocatable build, it is left out of the configure options docs and What's New for now. Co-authored-by: Zanie Blue <contact@zanie.dev>
1 parent a3a8f75 commit 2bcfe11

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+
@unittest.skipIf(get_config_var('RELOCATABLE') != 'yes',
716+
'requires --enable-relocatable')
717+
@unittest.skipIf(is_python_build(),
718+
'pkg-config files are only installed by make install')
719+
def test_pkgconfig_prefix(self):
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
@@ -2943,6 +2944,24 @@ python-config: $(srcdir)/Misc/python-config.in Misc/python-config.sh
29432944
cp python-config.py python-config; \
29442945
fi
29452946

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

30313050
.PHONY: libainstall
3032-
libainstall: all scripts
3051+
libainstall: all scripts Misc/python-install.pc Misc/python-embed-install.pc
30333052
@for i in $(LIBDIR) $(LIBPL) $(LIBPC) $(BINDIR); \
30343053
do \
30353054
if test ! -d $(DESTDIR)$$i; then \
@@ -3062,8 +3081,8 @@ libainstall: all scripts
30623081
$(INSTALL_DATA) Modules/Setup.bootstrap $(DESTDIR)$(LIBPL)/Setup.bootstrap
30633082
$(INSTALL_DATA) Modules/Setup.stdlib $(DESTDIR)$(LIBPL)/Setup.stdlib
30643083
$(INSTALL_DATA) Modules/Setup.local $(DESTDIR)$(LIBPL)/Setup.local
3065-
$(INSTALL_DATA) Misc/python.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION).pc
3066-
$(INSTALL_DATA) Misc/python-embed.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION)-embed.pc
3084+
$(INSTALL_DATA) Misc/python-install.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION).pc
3085+
$(INSTALL_DATA) Misc/python-embed-install.pc $(DESTDIR)$(LIBPC)/python-$(LDVERSION)-embed.pc
30673086
$(INSTALL_SCRIPT) $(srcdir)/Modules/makesetup $(DESTDIR)$(LIBPL)/makesetup
30683087
$(INSTALL_SCRIPT) $(srcdir)/install-sh $(DESTDIR)$(LIBPL)/install-sh
30693088
$(INSTALL_SCRIPT) python-config.py $(DESTDIR)$(LIBPL)/python-config.py
@@ -3429,7 +3448,8 @@ distclean: clobber docclean
34293448
-rm -f core Makefile Makefile.pre config.status Modules/Setup.local \
34303449
Modules/Setup.bootstrap Modules/Setup.stdlib \
34313450
Modules/ld_so_aix Modules/python.exp Misc/python.pc \
3432-
Misc/python-embed.pc Misc/python-config.sh
3451+
Misc/python-embed.pc Misc/python-install.pc \
3452+
Misc/python-embed-install.pc Misc/python-config.sh
34333453
-rm -f python*-gdb.py
34343454
# Issue #28258: set LC_ALL to avoid issues with Estonian locale.
34353455
# 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
@@ -1770,6 +1770,18 @@ if test "x$enable_profiling" = xyes; then
17701770
LDFLAGS="-pg $LDFLAGS"
17711771
fi
17721772

1773+
AC_MSG_CHECKING([for --enable-relocatable])
1774+
AC_ARG_ENABLE([relocatable],
1775+
[AS_HELP_STRING([--enable-relocatable],
1776+
[make the installation easier to relocate
1777+
(work in progress; default is no)])],
1778+
[], [enable_relocatable=no])
1779+
AS_CASE([$enable_relocatable],
1780+
[yes|no], [],
1781+
[AC_MSG_ERROR([--enable-relocatable must be yes or no])])
1782+
AC_MSG_RESULT([$enable_relocatable])
1783+
AC_SUBST([RELOCATABLE], [$enable_relocatable])
1784+
17731785
AC_MSG_CHECKING([LDLIBRARY])
17741786

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

0 commit comments

Comments
 (0)