Skip to content

Commit 22c061e

Browse files
itamaroclaude
andcommitted
gh-119696: Install build-details.json with relative paths
`make install` writes `build-details.json` (PEP 739) with absolute paths, so the file is wrong once the installation is moved. Fixing that is one of the steps towards relocatable CPython installations. PEP 739 allows the paths to be relative to `base_prefix`, and `base_prefix` to be relative to the file's directory; `generate-build-details.py` already implements that behind `--relative-paths --config-file-path`. Pass those flags from the Makefile. The installed-file tests now resolve the paths like a consumer would, instead of skipping on a relocated installation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 2e14c83 commit 22c061e

4 files changed

Lines changed: 41 additions & 20 deletions

File tree

‎Doc/whatsnew/3.16.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,12 @@ Build changes
15081508

15091509
(Contributed by Stefano Rivera in :gh:`131372`.)
15101510

1511+
* The paths in the installed ``build-details.json`` file (:pep:`739`) are
1512+
now relative to the file's location instead of absolute, so the file stays
1513+
correct when the installation is moved.
1514+
1515+
(Contributed by Itamar Oren in :gh:`119696`.)
1516+
15111517
* Add the :option:`--with-curses` :program:`configure` option to select the
15121518
curses backend for the :mod:`curses` and :mod:`curses.panel` modules.
15131519
In addition to ``ncursesw`` and ``ncurses``, it can now build against the

‎Lib/test/test_build_details.py‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,32 @@ def contents(self):
138138
with open(self.location, 'r', encoding='utf-8') as f:
139139
return f.read()
140140

141+
def resolve(self, value):
142+
"""Resolve a path from the file, as a PEP 739 consumer would."""
143+
base_prefix = os.path.join(os.path.dirname(self.location),
144+
self.key('base_prefix'))
145+
return os.path.normpath(os.path.join(base_prefix, value))
146+
141147
@needs_installed_python
142148
def test_location(self):
143149
self.assertTrue(os.path.isfile(self.location))
144150

151+
@needs_installed_python
152+
def test_paths_are_relative(self):
153+
self.assertFalse(os.path.isabs(self.key('base_prefix')))
154+
self.assertEqual(os.path.realpath(self.resolve('.')),
155+
os.path.realpath(sys.base_prefix))
156+
157+
for name in ('base_interpreter', 'libpython.dynamic',
158+
'libpython.dynamic_stableabi', 'libpython.static',
159+
'c_api.headers', 'c_api.pkgconfig_path'):
160+
with self.subTest(key=name):
161+
try:
162+
value = self.key(name)
163+
except KeyError:
164+
continue
165+
self.assertFalse(os.path.isabs(value))
166+
145167
# Override generic format tests with tests for our specific implemenation.
146168

147169
@needs_installed_python
@@ -150,11 +172,7 @@ def test_location(self):
150172
'Android and iOS run tests via a custom testbed method that changes sys.executable'
151173
)
152174
def test_base_interpreter(self):
153-
value = self.key('base_interpreter')
154-
155-
# Skip check if installation is relocated
156-
if sysconfig._installation_is_relocated():
157-
self.skipTest("Installation is relocated")
175+
value = self.resolve(self.key('base_interpreter'))
158176

159177
self.assertEqual(os.path.realpath(value), os.path.realpath(sys.executable))
160178

@@ -166,13 +184,11 @@ def test_base_interpreter(self):
166184
def test_c_api(self):
167185
value = self.key('c_api')
168186

169-
# Skip check if installation is relocated
170-
if sysconfig._installation_is_relocated():
171-
self.skipTest("Installation is relocated")
172-
173-
self.assertTrue(os.path.exists(os.path.join(value['headers'], 'Python.h')))
187+
headers = self.resolve(value['headers'])
188+
self.assertTrue(os.path.exists(os.path.join(headers, 'Python.h')))
174189
version = sysconfig.get_config_var('VERSION')
175-
self.assertTrue(os.path.exists(os.path.join(value['pkgconfig_path'], f'python-{version}.pc')))
190+
pkgconfig_path = self.resolve(value['pkgconfig_path'])
191+
self.assertTrue(os.path.exists(os.path.join(pkgconfig_path, f'python-{version}.pc')))
176192

177193
@needs_installed_python
178194
@unittest.skipIf(
@@ -184,11 +200,8 @@ def test_libpython_static(self):
184200
if 'static' not in value:
185201
self.skipTest("No static libpython")
186202

187-
if sysconfig._installation_is_relocated():
188-
self.skipTest("Installation is relocated")
189-
190203
# The path must name an installed static library (an 'ar' archive).
191-
with open(value['static'], 'rb') as f:
204+
with open(self.resolve(value['static']), 'rb') as f:
192205
self.assertEqual(f.read(8), b'!<arch>\n')
193206

194207
@needs_installed_python
@@ -206,10 +219,7 @@ def test_libpython_dynamic(self):
206219
if 'dynamic' not in value:
207220
self.skipTest("No dynamic libpython")
208221

209-
if sysconfig._installation_is_relocated():
210-
self.skipTest("Installation is relocated")
211-
212-
self.assertTrue(os.path.isfile(value['dynamic']))
222+
self.assertTrue(os.path.isfile(self.resolve(value['dynamic'])))
213223

214224

215225
@unittest.skipIf(

‎Makefile.pre.in‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,9 @@ pybuilddir.txt: $(PYTHON_FOR_BUILD_DEPS)
10331033
fi
10341034

10351035
$(BUILD_DETAILS): pybuilddir.txt
1036-
$(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py `cat pybuilddir.txt`/$(BUILD_DETAILS)
1036+
$(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py \
1037+
--relative-paths --config-file-path "$(LIBDEST)/$(BUILD_DETAILS)" \
1038+
`cat pybuilddir.txt`/$(BUILD_DETAILS)
10371039

10381040
# Build static library
10391041
$(LIBRARY): $(LIBRARY_OBJS)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The paths in the installed ``build-details.json`` file (:pep:`739`) are now
2+
relative to the file's location instead of absolute, so the file stays
3+
correct when the installation is moved.

0 commit comments

Comments
 (0)