Skip to content

Commit a3a8f75

Browse files
authored
gh-119696: Install build-details.json with relative paths (#161907)
`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](https://discuss.python.org/t/making-the-default-cpython-build-more-relocatable/109937). 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`. The Makefile now passes `--relative-paths` with a new `--installed-location` flag, which takes the file's installed path from `sysconfig` instead of from `$(LIBDEST)`, so that `make install prefix=...` (which the Android build uses) doesn't change the result. The installed-file tests now resolve the paths like a consumer would, instead of skipping on a relocated installation.
1 parent 9fe52e9 commit a3a8f75

5 files changed

Lines changed: 51 additions & 20 deletions

File tree

‎Doc/whatsnew/3.16.rst‎

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

15261526
(Contributed by Stefano Rivera in :gh:`131372`.)
15271527

1528+
* The paths in the installed ``build-details.json`` file (:pep:`739`) are
1529+
now relative to the file's location instead of absolute, so the file stays
1530+
correct when the installation is moved.
1531+
1532+
(Contributed by Itamar Oren in :gh:`119696`.)
1533+
15281534
* Add the :option:`--with-curses` :program:`configure` option to select the
15291535
curses backend for the :mod:`curses` and :mod:`curses.panel` modules.
15301536
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
@@ -139,10 +139,32 @@ def contents(self):
139139
with open(self.location, 'r', encoding='utf-8') as f:
140140
return f.read()
141141

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

152+
@needs_installed_python
153+
def test_paths_are_relative(self):
154+
self.assertFalse(os.path.isabs(self.key('base_prefix')))
155+
self.assertEqual(os.path.realpath(self.resolve('.')),
156+
os.path.realpath(sys.base_prefix))
157+
158+
for name in ('base_interpreter', 'libpython.dynamic',
159+
'libpython.dynamic_stableabi', 'libpython.static',
160+
'c_api.headers', 'c_api.pkgconfig_path'):
161+
with self.subTest(key=name):
162+
try:
163+
value = self.key(name)
164+
except KeyError:
165+
continue
166+
self.assertFalse(os.path.isabs(value))
167+
146168
def test_abi(self):
147169
self.assertEqual(self.key('abi.flags'), list(sys.abiflags))
148170
self.assertEqual(self.key('abi.extension_suffix'),
@@ -162,11 +184,7 @@ def test_extension_suffixes(self):
162184
'Android and iOS run tests via a custom testbed method that changes sys.executable'
163185
)
164186
def test_base_interpreter(self):
165-
value = self.key('base_interpreter')
166-
167-
# Skip check if installation is relocated
168-
if sysconfig._installation_is_relocated():
169-
self.skipTest("Installation is relocated")
187+
value = self.resolve(self.key('base_interpreter'))
170188

171189
self.assertEqual(os.path.realpath(value), os.path.realpath(sys.executable))
172190

@@ -178,13 +196,11 @@ def test_base_interpreter(self):
178196
def test_c_api(self):
179197
value = self.key('c_api')
180198

181-
# Skip check if installation is relocated
182-
if sysconfig._installation_is_relocated():
183-
self.skipTest("Installation is relocated")
184-
185-
self.assertTrue(os.path.exists(os.path.join(value['headers'], 'Python.h')))
199+
headers = self.resolve(value['headers'])
200+
self.assertTrue(os.path.exists(os.path.join(headers, 'Python.h')))
186201
version = sysconfig.get_config_var('VERSION')
187-
self.assertTrue(os.path.exists(os.path.join(value['pkgconfig_path'], f'python-{version}.pc')))
202+
pkgconfig_path = self.resolve(value['pkgconfig_path'])
203+
self.assertTrue(os.path.exists(os.path.join(pkgconfig_path, f'python-{version}.pc')))
188204

189205
@needs_installed_python
190206
@unittest.skipIf(
@@ -196,11 +212,8 @@ def test_libpython_static(self):
196212
if 'static' not in value:
197213
self.skipTest("No static libpython")
198214

199-
if sysconfig._installation_is_relocated():
200-
self.skipTest("Installation is relocated")
201-
202215
# The path must name an installed static library (an 'ar' archive).
203-
with open(value['static'], 'rb') as f:
216+
with open(self.resolve(value['static']), 'rb') as f:
204217
self.assertEqual(f.read(8), b'!<arch>\n')
205218

206219
@needs_installed_python
@@ -218,10 +231,7 @@ def test_libpython_dynamic(self):
218231
if 'dynamic' not in value:
219232
self.skipTest("No dynamic libpython")
220233

221-
if sysconfig._installation_is_relocated():
222-
self.skipTest("Installation is relocated")
223-
224-
self.assertTrue(os.path.isfile(value['dynamic']))
234+
self.assertTrue(os.path.isfile(self.resolve(value['dynamic'])))
225235

226236

227237
@unittest.skipIf(

‎Makefile.pre.in‎

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

10361036
$(BUILD_DETAILS): pybuilddir.txt
1037-
$(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py `cat pybuilddir.txt`/$(BUILD_DETAILS)
1037+
$(RUNSHARED_FOR_BUILD) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/build/generate-build-details.py \
1038+
--relative-paths --installed-location \
1039+
`cat pybuilddir.txt`/$(BUILD_DETAILS)
10381040

10391041
# Build static library
10401042
$(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.

‎Tools/build/generate-build-details.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,20 @@ def main() -> None:
305305
default=None,
306306
help='If specified, ``base_prefix`` will be set as a relative path to the given config file path.',
307307
)
308+
parser.add_argument(
309+
'--installed-location',
310+
action='store_true',
311+
help='Use the path the file is installed to as ``--config-file-path``.',
312+
)
308313

309314
args = parser.parse_args()
310315

311316
data = generate_data(args.schema_version)
317+
if args.installed_location:
318+
# Not taken from the Makefile: `make install prefix=...` changes
319+
# LIBDEST, but not the prefix that sysconfig reports.
320+
args.config_file_path = os.path.join(
321+
sysconfig.get_path('stdlib'), 'build-details.json')
312322
if args.relative_paths:
313323
make_paths_relative(data, args.config_file_path)
314324

0 commit comments

Comments
 (0)