Skip to content

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

@rueteh

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

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