Skip to content

Introduce seperate LDFLAGS for executable and shared library build targets #136677

Closed

Description

@rueteh

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

Links to previous discussion of this feature:

https://discuss.python.org/t/introduce-seperate-ldflags-for-executable-and-shared-library-build-targets/94999/1

Linked PRs

Activity

  1. FFY00 commented on Dec 10, 2025

    @FFY00
    Member

    @zanieb, @geofft, do you have any similar needs in PBS, that we can tackle too when implementing this?

  2. jjhelmus commented on Dec 10, 2025

    @jjhelmus
    Contributor

    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

  3. FFY00 commented on Dec 10, 2025

    @FFY00
    Member

    Thanks for the feedback! In that case, I'll proceed with the current scope of the feature request.

  4. added a commit that references this issue on Feb 24, 2026
  5. added a commit that references this issue on Feb 28, 2026
  6. vstinner commented on Mar 19, 2026

    @vstinner
    Member

    The commit 4e45c9c introduced PY_CORE_EXE_LDFLAGS and EXE_LDFLAGS variables in Python v3.15.0a7. I close the issue.

  7. added a commit that references this issue on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buildThe build process and cross-buildtype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions