In the Makefile, PY_CORE_LDFLAGS is used to build both the interpreter as well as shared libraries.
This makes it tricky to integrate it with build systems like Bazel (using rules_foreign_cc as a wrapper) which has a clear distinction between executable and shared libraries specific linker flags.
For example, the gcc -pie
flag is only applicable for executables, and not shared libraries. In MacOS, the clang -dynamiclib
flag only applies to shared libraries (and currently the shared lib in macOS is a seperate build target).
Edit: Updated with an experimental patch
A small snippet which I’ve been experimenting with is to split out PY_CORE_LDFLAGS
which serves two purpose as mentioned above into PY_CORE_LDFLAGS (which with the following diff is used to build shared targets, and PY_CORE_EXE_LDFLAGS
which is used to build the executable targets only:
diff --git a/Makefile.pre.in b/Makefile.pre.in
index a276d535c7f..e3847eb5d31 100644
---
Makefile.pre.in | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git Python-3.9.20.bak/Makefile.pre.in Python-3.9.20/Makefile.pre.in
index a276d53..e3847eb 100644
--- Python-3.9.20.bak/Makefile.pre.in
+++ Python-3.9.20/Makefile.pre.in
@@ -115,6 +115,7 @@ PY_STDMODULE_CFLAGS= $(PY_CFLAGS) $(PY_CFLAGS_NODIST) $(PY_CPPFLAGS) $(CFLAGSFOR
PY_BUILTIN_MODULE_CFLAGS= $(PY_STDMODULE_CFLAGS) -DPy_BUILD_CORE_BUILTIN
PY_CORE_CFLAGS= $(PY_STDMODULE_CFLAGS) -DPy_BUILD_CORE
# Linker flags used for building the interpreter object files
+PY_CORE_EXE_LDFLAGS=@PY_CORE_EXE_LDFLAGS@ $(PY_LDFLAGS_NODIST)
PY_CORE_LDFLAGS=$(PY_LDFLAGS) $(PY_LDFLAGS_NODIST)
# Strict or non-strict aliasing flags used to compile dtoa.c, see above
CFLAGS_ALIASING=@CFLAGS_ALIASING@
@@ -589,7 +590,7 @@ clinic: check-clean-src $(srcdir)/Modules/_blake2/blake2s_impl.c
# Build the interpreter
$(BUILDPYTHON): Programs/python.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY) $(EXPORTSYMS)
- $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS)
+ $(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS)
platform: $(BUILDPYTHON) pybuilddir.txt
$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print("%s-%d.%d" % (get_platform(), *sys.version_info[:2]))' >platform
@@ -724,7 +725,7 @@ Makefile Modules/config.c: Makefile.pre \
Programs/_testembed: Programs/_testembed.o $(LIBRARY) $(LDLIBRARY) $(PY3LIBRARY) $(EXPORTSYMS)
- $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/_testembed.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS)
+ $(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/_testembed.o $(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS)
############################################################################
# Importlib
--
2.34.1
This is with consideration that all ld flags inherit from CONFIGURE_LDFLAGS
with a majority being shared targets. I understand that there’s a lot of history and usage for these flags especially with how they’re used to build python packages, and I’m keen for any inputs and feedback!
Thank you so much for your time!