-
-
Notifications
You must be signed in to change notification settings - Fork 247
POC: Add a relocatable build flag #861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| commit 6187c3916ac1e7e6eb2bff7ca5f976c99cbe273b | ||
| Author: Zanie Blue <[email protected]> | ||
| Date: Sat Nov 15 08:47:20 2025 -0600 | ||
|
|
||
| Add an `--enable-relocatable` configure flag | ||
|
|
||
| diff --git a/Makefile.pre.in b/Makefile.pre.in | ||
| index 19423c11545..6c9cba59e27 100644 | ||
| --- a/Makefile.pre.in | ||
| +++ b/Makefile.pre.in | ||
| @@ -60,6 +60,7 @@ DSYMUTIL= @DSYMUTIL@ | ||
| DSYMUTIL_PATH= @DSYMUTIL_PATH@ | ||
|
|
||
| GNULD= @GNULD@ | ||
| +RELOCATABLE= @RELOCATABLE@ | ||
|
|
||
| # Shell used by make (some versions default to the login shell, which is bad) | ||
| SHELL= /bin/sh -e | ||
| diff --git a/configure b/configure | ||
| index 9757b3419d3..38f2d4dfbf2 100755 | ||
| --- a/configure | ||
| +++ b/configure | ||
| @@ -949,6 +949,7 @@ LINK_PYTHON_DEPS | ||
| LIBRARY_DEPS | ||
| HOSTRUNNER | ||
| NODE | ||
| +RELOCATABLE | ||
| STATIC_LIBPYTHON | ||
| GNULD | ||
| EXPORTSFROM | ||
| @@ -1091,6 +1092,7 @@ with_suffix | ||
| enable_shared | ||
| with_static_libpython | ||
| enable_profiling | ||
| +enable_relocatable | ||
| enable_gil | ||
| with_pydebug | ||
| with_trace_refs | ||
| @@ -1826,6 +1828,8 @@ Optional Features: | ||
| no) | ||
| --enable-profiling enable C-level code profiling with gprof (default is | ||
| no) | ||
| + --enable-relocatable enable build of a relocatable Python distribution | ||
| + (default is no) | ||
| --disable-gil enable support for running without the GIL (default | ||
| is no) | ||
| --enable-pystats enable internal statistics gathering (default is no) | ||
| @@ -7673,6 +7677,31 @@ if test "x$enable_profiling" = xyes; then | ||
| LDFLAGS="-pg $LDFLAGS" | ||
| fi | ||
|
|
||
| +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for --enable-relocatable" >&5 | ||
| +printf %s "checking for --enable-relocatable... " >&6; } | ||
| +# Check whether --enable-relocatable was given. | ||
| +if test ${enable_relocatable+y} | ||
| +then : | ||
| + enableval=$enable_relocatable; | ||
| +fi | ||
| + | ||
| +if test -z "$enable_relocatable" | ||
| +then | ||
| + enable_relocatable="no" | ||
| +fi | ||
| +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $enable_relocatable" >&5 | ||
| +printf "%s\n" "$enable_relocatable" >&6; } | ||
| + | ||
| +if test "x$enable_relocatable" = xyes; then | ||
| + | ||
| +printf "%s\n" "#define Py_ENABLE_RELOCATABLE 1" >>confdefs.h | ||
| + | ||
| + RELOCATABLE=yes | ||
| +else | ||
| + RELOCATABLE=no | ||
| +fi | ||
| + | ||
| + | ||
| { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking LDLIBRARY" >&5 | ||
| printf %s "checking LDLIBRARY... " >&6; } | ||
|
|
||
| diff --git a/configure.ac b/configure.ac | ||
| index f244e0b71a6..b1c859071f4 100644 | ||
| --- a/configure.ac | ||
| +++ b/configure.ac | ||
| @@ -1523,6 +1523,23 @@ if test "x$enable_profiling" = xyes; then | ||
| LDFLAGS="-pg $LDFLAGS" | ||
| fi | ||
|
|
||
| +AC_MSG_CHECKING([for --enable-relocatable]) | ||
| +AC_ARG_ENABLE([relocatable], | ||
| + AS_HELP_STRING([--enable-relocatable], [enable build of a relocatable Python distribution (default is no)])) | ||
| +if test -z "$enable_relocatable" | ||
| +then | ||
| + enable_relocatable="no" | ||
| +fi | ||
| +AC_MSG_RESULT([$enable_relocatable]) | ||
| + | ||
| +if test "x$enable_relocatable" = xyes; then | ||
| + AC_DEFINE([Py_ENABLE_RELOCATABLE], [1], [Define if --enable-relocatable is used]) | ||
| + RELOCATABLE=yes | ||
| +else | ||
| + RELOCATABLE=no | ||
| +fi | ||
| +AC_SUBST([RELOCATABLE]) | ||
| + | ||
| AC_MSG_CHECKING([LDLIBRARY]) | ||
|
|
||
| # Apple framework builds need more magic. LDLIBRARY is the dynamic | ||
| diff --git a/pyconfig.h.in b/pyconfig.h.in | ||
| index 72870411bc0..76fe6a9b52c 100644 | ||
| --- a/pyconfig.h.in | ||
| +++ b/pyconfig.h.in | ||
| @@ -1733,6 +1733,9 @@ | ||
| /* Define if you want to build an interpreter with many run-time checks. */ | ||
| #undef Py_DEBUG | ||
|
|
||
| +/* Define if --enable-relocatable is used */ | ||
| +#undef Py_ENABLE_RELOCATABLE | ||
| + | ||
| /* Defined if Python is built as a shared library. */ | ||
| #undef Py_ENABLE_SHARED | ||
|
|
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||
| commit 34915211b42a64082e0470ebb9a8c4f2ebb20512 | ||||||
| Author: Zanie Blue <[email protected]> | ||||||
| Date: Fri Nov 21 15:44:42 2025 -0600 | ||||||
|
|
||||||
| Set linker arguments when `--enabled-relocatable` is used | ||||||
|
|
||||||
| diff --git a/Makefile.pre.in b/Makefile.pre.in | ||||||
| index 6c9cba59e27..6c89690c35e 100644 | ||||||
| --- a/Makefile.pre.in | ||||||
| +++ b/Makefile.pre.in | ||||||
| @@ -1025,10 +1025,19 @@ libpython$(LDVERSION).so: $(LIBRARY_OBJS) $(DTRACE_OBJS) | ||||||
| fi | ||||||
|
|
||||||
| libpython3.so: libpython$(LDVERSION).so | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to patch the |
||||||
| - $(BLDSHARED) $(NO_AS_NEEDED) -o $@ -Wl,-h$@ $^ | ||||||
| + if [ "$(RELOCATABLE)" = "yes" ]; then \ | ||||||
| + $(BLDSHARED) $(NO_AS_NEEDED) -o $@ -Wl,-h$@ -Wl,-rpath,'$$ORIGIN' $^; \ | ||||||
| + else \ | ||||||
| + $(BLDSHARED) $(NO_AS_NEEDED) -o $@ -Wl,-h$@ $^; \ | ||||||
| + fi | ||||||
|
|
||||||
| libpython$(LDVERSION).dylib: $(LIBRARY_OBJS) | ||||||
| - $(CC) -dynamiclib $(PY_CORE_LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$(prefix)/lib/libpython$(LDVERSION).dylib -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(DTRACE_OBJS) $(SHLIBS) $(LIBC) $(LIBM); \ | ||||||
| + if [ "$(RELOCATABLE)" = "yes" ]; then \ | ||||||
| + install_name="@executable_path/../lib/libpython$(LDVERSION).dylib"; \ | ||||||
| + else \ | ||||||
| + install_name="$(prefix)/lib/libpython$(LDVERSION).dylib"; \ | ||||||
| + fi; \ | ||||||
| + $(CC) -dynamiclib $(PY_CORE_LDFLAGS) -undefined dynamic_lookup -Wl,-install_name,$$install_name -Wl,-compatibility_version,$(VERSION) -Wl,-current_version,$(VERSION) -o $@ $(LIBRARY_OBJS) $(DTRACE_OBJS) $(MODLIBS) $(SHLIBS) $(LIBC) $(LIBM); | ||||||
|
|
||||||
|
|
||||||
| libpython$(VERSION).sl: $(LIBRARY_OBJS) | ||||||
| diff --git a/configure b/configure | ||||||
| index 38f2d4dfbf2..9991932680e 100755 | ||||||
| --- a/configure | ||||||
| +++ b/configure | ||||||
| @@ -13749,7 +13749,13 @@ then | ||||||
| LINKFORSHARED="-Wl,-E -Wl,+s";; | ||||||
| # LINKFORSHARED="-Wl,-E -Wl,+s -Wl,+b\$(BINLIBDEST)/lib-dynload";; | ||||||
| Linux-android*) LINKFORSHARED="-pie -Xlinker -export-dynamic";; | ||||||
| - Linux*|GNU*) LINKFORSHARED="-Xlinker -export-dynamic";; | ||||||
| + Linux*|GNU*) | ||||||
| + LINKFORSHARED="-Xlinker -export-dynamic" | ||||||
| + # Add rpath for relocatable builds | ||||||
| + if test "x$enable_relocatable" = xyes; then | ||||||
| + LINKFORSHARED="$LINKFORSHARED -Wl,-rpath=\$ORIGIN/../lib" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| + fi | ||||||
| + ;; | ||||||
| # -u libsys_s pulls in all symbols in libsys | ||||||
| Darwin/*|iOS/*) | ||||||
| LINKFORSHARED="$extra_undefs -framework CoreFoundation" | ||||||
| diff --git a/configure.ac b/configure.ac | ||||||
| index b1c859071f4..f75f41441e2 100644 | ||||||
| --- a/configure.ac | ||||||
| +++ b/configure.ac | ||||||
| @@ -3613,7 +3613,13 @@ then | ||||||
| LINKFORSHARED="-Wl,-E -Wl,+s";; | ||||||
| # LINKFORSHARED="-Wl,-E -Wl,+s -Wl,+b\$(BINLIBDEST)/lib-dynload";; | ||||||
| Linux-android*) LINKFORSHARED="-pie -Xlinker -export-dynamic";; | ||||||
| - Linux*|GNU*) LINKFORSHARED="-Xlinker -export-dynamic";; | ||||||
| + Linux*|GNU*) | ||||||
| + LINKFORSHARED="-Xlinker -export-dynamic" | ||||||
| + # Add rpath for relocatable builds | ||||||
| + if test "x$enable_relocatable" = xyes; then | ||||||
| + LINKFORSHARED="$LINKFORSHARED -Wl,-rpath=\$ORIGIN/../lib" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rpath here is not escaped correctly resulting in the the RPATH in the binary being set to This causes among other issues,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| + fi | ||||||
| + ;; | ||||||
| # -u libsys_s pulls in all symbols in libsys | ||||||
| Darwin/*|iOS/*) | ||||||
| LINKFORSHARED="$extra_undefs -framework CoreFoundation" | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.