Skip to content

Commit 95d7307

Browse files
committed
gh-115028: Add support for relocatable installations using $ORIGIN in RPATH
This patch adds a new configure option --with-relative-rpath that, when enabled, adds $ORIGIN-based RPATHs to binaries when building Python on Linux. This makes Python installations relocatable so they can be moved to different directories without breaking dynamic library dependencies. Three new variables are introduced to set RPATHs for different components: - PY_RPATH_EXEC: For the Python interpreter binary - PY_RPATH_LIB: For the shared libpython library - PY_RPATH_MOD: For Python extension modules This is a step toward solving the relocatability issues discussed in packaging forums and helps ena
1 parent 7c3692f commit 95d7307

4 files changed

Lines changed: 88 additions & 4 deletions

File tree

Makefile.pre.in

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Top-level Makefile for Python
1+
# Top-level Makeile for Python
22
#
33
# As distributed, this file is called Makefile.pre.in; it is processed
44
# into the real Makefile by running the script ./configure, which
@@ -58,6 +58,9 @@ DTRACE_HEADERS= @DTRACE_HEADERS@
5858
DTRACE_OBJS= @DTRACE_OBJS@
5959
DSYMUTIL= @DSYMUTIL@
6060
DSYMUTIL_PATH= @DSYMUTIL_PATH@
61+
PY_RPATH_EXEC= @PY_RPATH_EXEC@
62+
PY_RPATH_LIB= @PY_RPATH_LIB@
63+
PY_RPATH_MOD= @PY_RPATH_MOD@
6164

6265
GNULD= @GNULD@
6366

@@ -915,7 +918,8 @@ clinic-tests: check-clean-src $(srcdir)/Lib/test/clinic.test.c
915918

916919
# Build the interpreter
917920
$(BUILDPYTHON): Programs/python.o $(LINK_PYTHON_DEPS)
918-
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
921+
$(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) $(PY_RPATH_EXEC) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
922+
919923

920924
platform: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt
921925
$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform
@@ -945,7 +949,7 @@ $(LIBRARY): $(LIBRARY_OBJS)
945949
$(AR) $(ARFLAGS) $@ $(LIBRARY_OBJS)
946950

947951
libpython$(LDVERSION).so: $(LIBRARY_OBJS) $(DTRACE_OBJS)
948-
$(BLDSHARED) -Wl,-h$(INSTSONAME) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM)
952+
$(BLDSHARED) -Wl,-h$(INSTSONAME) $(PY_RPATH_LIB) -o $(INSTSONAME) $(LIBRARY_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM)
949953
if test $(INSTSONAME) != $@; then \
950954
$(LN) -f $(INSTSONAME) $@; \
951955
fi

Modules/makesetup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ sed -e 's/[ ]*#.*//' -e '/^[ ]*$/d' |
274274
;;
275275
esac
276276
rule="$file: $objs"
277-
rule="$rule; \$(BLDSHARED) $objs $libs \$(LIBPYTHON) -o $file"
277+
rule="$rule; \$(BLDSHARED) \$(PY_RPATH_MOD) $objs $libs \$(LIBPYTHON) -o $file"
278278
echo "$rule" >>$rulesf
279279
done
280280
done

configure

Lines changed: 48 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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,6 +3356,38 @@ AC_MSG_RESULT([no])
33563356
with_tsan="no"
33573357
])
33583358

3359+
# Check for --with-relative-rpath
3360+
AC_MSG_CHECKING([for --with-relative-rpath])
3361+
AC_ARG_WITH([relative-rpath],
3362+
[AS_HELP_STRING([--with-relative-rpath],
3363+
[use relative rpath with $ORIGIN for binaries (Linux only)])],
3364+
[], [with_relative_rpath=no])
3365+
3366+
# Initialize empty RPATH variables
3367+
PY_RPATH_EXEC=""
3368+
PY_RPATH_LIB=""
3369+
PY_RPATH_MOD=""
3370+
3371+
if test "$with_relative_rpath" = "yes"; then
3372+
if test "$ac_sys_system" = "Linux"; then
3373+
# Define the RPATH settings for each binary type
3374+
PY_RPATH_EXEC="-Wl,-rpath,\\\$\$ORIGIN/../\$(PLATLIBDIR)"
3375+
PY_RPATH_LIB="-Wl,-rpath,\\\$\$ORIGIN"
3376+
PY_RPATH_MOD="-Wl,-rpath,\\\$\$ORIGIN/../../"
3377+
AC_MSG_RESULT([yes])
3378+
else
3379+
AC_MSG_RESULT([no - only supported on Linux])
3380+
with_relative_rpath=no
3381+
fi
3382+
else
3383+
AC_MSG_RESULT([no])
3384+
fi
3385+
3386+
# Export these variables for the Makefile
3387+
AC_SUBST([PY_RPATH_EXEC])
3388+
AC_SUBST([PY_RPATH_LIB])
3389+
AC_SUBST([PY_RPATH_MOD])
3390+
33593391
# Set info about shared libraries.
33603392
AC_SUBST([SHLIB_SUFFIX])
33613393
AC_SUBST([LDSHARED])

0 commit comments

Comments
 (0)