Skip to content

Commit 91f343f

Browse files
authored
Correctly set install_name for macOS libpython (#914)
Previously the `install_name` for libpython on macOS was just the prefix `/install/lib/...` which was knowingly invalid. The post-build scripts fixed this for `python3` itself, but don't fix the dylib itself for the case where downstream users of the toolchain try to link libpython. Now libpython has the standard install_name relative to rpaths, and downstream binaries need to add a rpath to the toolchain's lib directory to load it. This is also now the same behavior as the linux toolchain
1 parent 06b7216 commit 91f343f

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

cpython-unix/build-cpython.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,16 +704,19 @@ if [ "${PYBUILD_SHARED}" = "1" ]; then
704704
LIBPYTHON_SHARED_LIBRARY_BASENAME=libpython${PYTHON_MAJMIN_VERSION}${PYTHON_BINARY_SUFFIX}.dylib
705705
LIBPYTHON_SHARED_LIBRARY=${ROOT}/out/python/install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}
706706

707+
# Fix the Python binary to reference libpython via @rpath and add
708+
# an rpath entry so it can find the library.
707709
install_name_tool \
708-
-change /install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} @executable_path/../lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} \
710+
-change /install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} @rpath/${LIBPYTHON_SHARED_LIBRARY_BASENAME} \
711+
-add_rpath @executable_path/../lib \
709712
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}
710713

711714
# Python's build system doesn't make this file writable.
712-
# TODO(geofft): @executable_path/ is a weird choice here, who is
713-
# relying on it? Should probably be @loader_path.
714715
chmod 755 ${ROOT}/out/python/install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}
716+
# Set libpython's install name to @rpath so binaries linking against it
717+
# can locate it via their own rpath entries.
715718
install_name_tool \
716-
-change /install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} @executable_path/${LIBPYTHON_SHARED_LIBRARY_BASENAME} \
719+
-id @rpath/${LIBPYTHON_SHARED_LIBRARY_BASENAME} \
717720
${ROOT}/out/python/install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME}
718721

719722
# We also normalize /tools/deps/lib/libz.1.dylib to the system location.
@@ -726,7 +729,8 @@ if [ "${PYBUILD_SHARED}" = "1" ]; then
726729

727730
if [ -n "${PYTHON_BINARY_SUFFIX}" ]; then
728731
install_name_tool \
729-
-change /install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} @executable_path/../lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} \
732+
-change /install/lib/${LIBPYTHON_SHARED_LIBRARY_BASENAME} @rpath/${LIBPYTHON_SHARED_LIBRARY_BASENAME} \
733+
-add_rpath @executable_path/../lib \
730734
${ROOT}/out/python/install/bin/python${PYTHON_MAJMIN_VERSION}${PYTHON_BINARY_SUFFIX}
731735
fi
732736

src/validation.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -297,92 +297,92 @@ static ELF_ALLOWED_LIBRARIES_BY_MODULE: Lazy<HashMap<&'static str, Vec<&'static
297297
static DARWIN_ALLOWED_DYLIBS: Lazy<Vec<MachOAllowedDylib>> = Lazy::new(|| {
298298
[
299299
MachOAllowedDylib {
300-
name: "@executable_path/../lib/libpython3.10.dylib".to_string(),
300+
name: "@rpath/libpython3.10.dylib".to_string(),
301301
max_compatibility_version: "3.10.0".try_into().unwrap(),
302302
required: false,
303303
},
304304
MachOAllowedDylib {
305-
name: "@executable_path/../lib/libpython3.10d.dylib".to_string(),
305+
name: "@rpath/libpython3.10d.dylib".to_string(),
306306
max_compatibility_version: "3.10.0".try_into().unwrap(),
307307
required: false,
308308
},
309309
MachOAllowedDylib {
310-
name: "@executable_path/../lib/libpython3.11.dylib".to_string(),
310+
name: "@rpath/libpython3.11.dylib".to_string(),
311311
max_compatibility_version: "3.11.0".try_into().unwrap(),
312312
required: false,
313313
},
314314
MachOAllowedDylib {
315-
name: "@executable_path/../lib/libpython3.11d.dylib".to_string(),
315+
name: "@rpath/libpython3.11d.dylib".to_string(),
316316
max_compatibility_version: "3.11.0".try_into().unwrap(),
317317
required: false,
318318
},
319319
MachOAllowedDylib {
320-
name: "@executable_path/../lib/libpython3.12.dylib".to_string(),
320+
name: "@rpath/libpython3.12.dylib".to_string(),
321321
max_compatibility_version: "3.12.0".try_into().unwrap(),
322322
required: false,
323323
},
324324
MachOAllowedDylib {
325-
name: "@executable_path/../lib/libpython3.12d.dylib".to_string(),
325+
name: "@rpath/libpython3.12d.dylib".to_string(),
326326
max_compatibility_version: "3.12.0".try_into().unwrap(),
327327
required: false,
328328
},
329329
MachOAllowedDylib {
330-
name: "@executable_path/../lib/libpython3.13.dylib".to_string(),
330+
name: "@rpath/libpython3.13.dylib".to_string(),
331331
max_compatibility_version: "3.13.0".try_into().unwrap(),
332332
required: false,
333333
},
334334
MachOAllowedDylib {
335-
name: "@executable_path/../lib/libpython3.13d.dylib".to_string(),
335+
name: "@rpath/libpython3.13d.dylib".to_string(),
336336
max_compatibility_version: "3.13.0".try_into().unwrap(),
337337
required: false,
338338
},
339339
MachOAllowedDylib {
340-
name: "@executable_path/../lib/libpython3.13t.dylib".to_string(),
340+
name: "@rpath/libpython3.13t.dylib".to_string(),
341341
max_compatibility_version: "3.13.0".try_into().unwrap(),
342342
required: false,
343343
},
344344
MachOAllowedDylib {
345-
name: "@executable_path/../lib/libpython3.13td.dylib".to_string(),
345+
name: "@rpath/libpython3.13td.dylib".to_string(),
346346
max_compatibility_version: "3.13.0".try_into().unwrap(),
347347
required: false,
348348
},
349349
MachOAllowedDylib {
350-
name: "@executable_path/../lib/libpython3.14.dylib".to_string(),
350+
name: "@rpath/libpython3.14.dylib".to_string(),
351351
max_compatibility_version: "3.14.0".try_into().unwrap(),
352352
required: false,
353353
},
354354
MachOAllowedDylib {
355-
name: "@executable_path/../lib/libpython3.14d.dylib".to_string(),
355+
name: "@rpath/libpython3.14d.dylib".to_string(),
356356
max_compatibility_version: "3.14.0".try_into().unwrap(),
357357
required: false,
358358
},
359359
MachOAllowedDylib {
360-
name: "@executable_path/../lib/libpython3.14t.dylib".to_string(),
360+
name: "@rpath/libpython3.14t.dylib".to_string(),
361361
max_compatibility_version: "3.14.0".try_into().unwrap(),
362362
required: false,
363363
},
364364
MachOAllowedDylib {
365-
name: "@executable_path/../lib/libpython3.14td.dylib".to_string(),
365+
name: "@rpath/libpython3.14td.dylib".to_string(),
366366
max_compatibility_version: "3.14.0".try_into().unwrap(),
367367
required: false,
368368
},
369369
MachOAllowedDylib {
370-
name: "@executable_path/../lib/libpython3.15.dylib".to_string(),
370+
name: "@rpath/libpython3.15.dylib".to_string(),
371371
max_compatibility_version: "3.15.0".try_into().unwrap(),
372372
required: false,
373373
},
374374
MachOAllowedDylib {
375-
name: "@executable_path/../lib/libpython3.15d.dylib".to_string(),
375+
name: "@rpath/libpython3.15d.dylib".to_string(),
376376
max_compatibility_version: "3.15.0".try_into().unwrap(),
377377
required: false,
378378
},
379379
MachOAllowedDylib {
380-
name: "@executable_path/../lib/libpython3.15t.dylib".to_string(),
380+
name: "@rpath/libpython3.15t.dylib".to_string(),
381381
max_compatibility_version: "3.15.0".try_into().unwrap(),
382382
required: false,
383383
},
384384
MachOAllowedDylib {
385-
name: "@executable_path/../lib/libpython3.15td.dylib".to_string(),
385+
name: "@rpath/libpython3.15td.dylib".to_string(),
386386
max_compatibility_version: "3.15.0".try_into().unwrap(),
387387
required: false,
388388
},

0 commit comments

Comments
 (0)