Repository navigation
Description
Feature or enhancement
Proposal:
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).
Proposal
Introduce a new ldflag that can split the current usage of PY_CORE_LDFLAGS into two components, one used to build shared target and another to build executable targets. The following is a small snippet which I've been experimenting to illustrate the idea:
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
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Activity
FFY00 commentedon Dec 10, 2025
@zanieb, @geofft, do you have any similar needs in PBS, that we can tackle too when implementing this?
jjhelmus commentedon Dec 10, 2025
We do not use separate linker flags in PBS for the executable and shared library. This is something I could see using if available (-pie would be much easier to adopt).
We do apply a patch that allows us to build the shared library and a statically linked executable from the same object files. I do not think this would conflicts with splitting LDFLAGS as the executable and library remain separate build targets
FFY00 commentedon Dec 10, 2025
Thanks for the feedback! In that case, I'll proceed with the current scope of the feature request.
gh-136677: Introduce executable specific linker flags to configure (#…
pythongh-136677: Introduce executable specific linker flags to config…
vstinner commentedon Mar 19, 2026
The commit 4e45c9c introduced
PY_CORE_EXE_LDFLAGSandEXE_LDFLAGSvariables in Python v3.15.0a7. I close the issue.pythongh-136677: Introduce executable specific linker flags to config…