]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-136677: Introduce executable specific linker flags to configure (#137296)
authorrueteh <60346006+rueteh@users.noreply.github.com>
Tue, 24 Feb 2026 22:52:02 +0000 (09:52 +1100)
committerGitHub <noreply@github.com>
Tue, 24 Feb 2026 22:52:02 +0000 (22:52 +0000)
* introduce executable specific linker flags

Add PY_CORE_EXE_LDFLAGS and EXE_LDFLAGS which stores executable specific
LDFLAGS, replacing PY_CORE_LDFLAGS for building
executable targets.

If PY_CORE_EXE_LDFLAGS / EXE_LDFLAGS is not provided, then it defaults
to the value of PY_CORE_LDFLAGS which is the existing behaviour.

If both flags are supplied, and there is a need
to distinguish between executable and shared specific LDFLAGS,
in particular, PY_CORE_LDFLAGS should contain the shared specific LDFLAGS.

* documentation for new linker flags

* update Misc folder documentation

* Update Makefile.pre.in

Co-authored-by: Victor Stinner <vstinner@python.org>
---------

Co-authored-by: Filipe Laíns <filipe.lains@gmail.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Filipe Laíns <lains@riseup.net>
Doc/using/configure.rst
Lib/_osx_support.py
Lib/test/pythoninfo.py
Lib/test/test__osx_support.py
Makefile.pre.in
Misc/ACKS
Misc/NEWS.d/next/Build/2025-07-21-00-33-38.gh-issue-136677.Y1_3ec.rst [new file with mode: 0644]
configure
configure.ac

index 50812358a690c322c09b77ad97af1632fb392245..813127663ed8feb91bbce83e9716b9809f7a1c90 100644 (file)
@@ -1650,6 +1650,9 @@ Linker flags
    value to be able to build extension modules using the
    directories specified in the environment variables.
 
+   Please consider using ``EXE_LDFLAGS`` if the supplied linker flags are
+   executable specific, e.g. GCC's ``-pie`` flag.
+
 .. envvar:: LIBS
 
    Linker flags to pass libraries to the linker when linking the Python
@@ -1685,6 +1688,30 @@ Linker flags
 
    .. versionadded:: 3.8
 
+.. envvar:: EXE_LDFLAGS
+
+   Linker flags used for building executable targets such as the
+   interpreter. If supplied, :envvar:`PY_CORE_EXE_LDFLAGS`
+   will be used in replacement of :envvar:`PY_CORE_LDFLAGS`.
+
+   .. versionadded:: 3.15
+
+.. envvar:: CONFIGURE_EXE_LDFLAGS
+
+   Value of :envvar:`EXE_LDFLAGS` variable passed to the ``./configure``
+   script.
+
+   .. versionadded:: 3.15
+
+.. envvar:: PY_CORE_EXE_LDFLAGS
+
+   Linker flags used for building the interpreter and
+   executable targets.
+
+   Default: ``$(PY_CORE_LDFLAGS)``
+
+   .. versionadded:: 3.15
+
 
 .. rubric:: Footnotes
 
index 0cb064fcd791be0104242f9f2070b38b53e32dc0..29b89e311cb1fe7877c5539789a74995adf4dd6f 100644 (file)
@@ -17,7 +17,8 @@ __all__ = [
 _UNIVERSAL_CONFIG_VARS = ('CFLAGS', 'LDFLAGS', 'CPPFLAGS', 'BASECFLAGS',
                             'BLDSHARED', 'LDSHARED', 'CC', 'CXX',
                             'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS',
-                            'PY_CORE_CFLAGS', 'PY_CORE_LDFLAGS')
+                            'PY_CORE_CFLAGS', 'PY_CORE_LDFLAGS',
+                            'PY_CORE_EXE_LDFLAGS')
 
 # configuration variables that may contain compiler calls
 _COMPILER_CONFIG_VARS = ('BLDSHARED', 'LDSHARED', 'CC', 'CXX')
index 3befc0f2e633cad42759567f75e6e422ddaafe2b..fdd8989f9cb3f82060c3c34256cead0b19ec63b8 100644 (file)
@@ -528,6 +528,7 @@ def collect_sysconfig(info_add):
         'PY_CFLAGS',
         'PY_CFLAGS_NODIST',
         'PY_CORE_LDFLAGS',
+        'PY_CORE_EXE_LDFLAGS',
         'PY_LDFLAGS',
         'PY_LDFLAGS_NODIST',
         'PY_STDMODULE_CFLAGS',
index 0813c4804c1cdca8846aaba9a57d11622850ca68..b92ce6796b50fb81c4ca99086218ab711731a1d9 100644 (file)
@@ -25,7 +25,7 @@ class Test_OSXSupport(unittest.TestCase):
             'CFLAGS', 'LDFLAGS', 'CPPFLAGS',
             'BASECFLAGS', 'BLDSHARED', 'LDSHARED', 'CC',
             'CXX', 'PY_CFLAGS', 'PY_LDFLAGS', 'PY_CPPFLAGS',
-            'PY_CORE_CFLAGS', 'PY_CORE_LDFLAGS'
+            'PY_CORE_CFLAGS', 'PY_CORE_LDFLAGS', 'PY_CORE_EXE_LDFLAGS'
         )
 
     def add_expected_saved_initial_values(self, config_vars, expected_vars):
index f4119abf324fca4dcc5ed83ac42e2773caab79ef..122957dec29b6fc34a7acc61de6a2e289d618cac 100644 (file)
@@ -123,7 +123,11 @@ 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
+# In particular, EXE_LDFLAGS is an extra flag to provide fine grain distinction between
+# LDFLAGS used to build executables and shared targets.
 PY_CORE_LDFLAGS=$(PY_LDFLAGS) $(PY_LDFLAGS_NODIST)
+CONFIGURE_EXE_LDFLAGS=@EXE_LDFLAGS@
+PY_CORE_EXE_LDFLAGS:= $(if $(CONFIGURE_EXE_LDFLAGS), $(CONFIGURE_EXE_LDFLAGS) $(PY_LDFLAGS_NODIST), $(PY_CORE_LDFLAGS))
 # Strict or non-strict aliasing flags used to compile dtoa.c, see above
 CFLAGS_ALIASING=@CFLAGS_ALIASING@
 
@@ -986,7 +990,7 @@ clinic-tests: check-clean-src $(srcdir)/Lib/test/clinic.test.c
 
 # Build the interpreter
 $(BUILDPYTHON):        Programs/python.o $(LINK_PYTHON_DEPS)
-       $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
+       $(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/python.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
 
 platform: $(PYTHON_FOR_BUILD_DEPS) 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
@@ -1676,7 +1680,7 @@ regen-re: $(BUILDPYTHON)
        $(RUNSHARED) ./$(BUILDPYTHON) $(srcdir)/Tools/build/generate_re_casefix.py $(srcdir)/Lib/re/_casefix.py
 
 Programs/_testembed: Programs/_testembed.o $(LINK_PYTHON_DEPS)
-       $(LINKCC) $(PY_CORE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/_testembed.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
+       $(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/_testembed.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
 
 ############################################################################
 # "Bootstrap Python" used to run Programs/_freeze_module.py
index 3464bd95bac791daf7436789e1bbce0622e25690..e38bda37bfa92d9130d7a64231d06ff1fa46ec47 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1916,6 +1916,7 @@ Guo Ci Teo
 Mikhail Terekhov
 Victor Terrón
 Pablo Galindo
+Rue Ching Teh
 Richard M. Tew
 Srinivas Reddy Thatiparthy
 Tobias Thelen
diff --git a/Misc/NEWS.d/next/Build/2025-07-21-00-33-38.gh-issue-136677.Y1_3ec.rst b/Misc/NEWS.d/next/Build/2025-07-21-00-33-38.gh-issue-136677.Y1_3ec.rst
new file mode 100644 (file)
index 0000000..30addc4
--- /dev/null
@@ -0,0 +1 @@
+Introduce executable specific linker flags to ``./configure``.
index 98b4af86858673703afc7f1470d41a6282b856a8..dd62fd90e38ee1e9182983625349ecf61e6c2e51 100755 (executable)
--- a/configure
+++ b/configure
@@ -915,6 +915,7 @@ UNIVERSAL_ARCH_FLAGS
 WASM_STDLIB
 WASM_ASSETS_DIR
 LDFLAGS_NOLTO
+EXE_LDFLAGS
 LDFLAGS_NODIST
 CFLAGS_NODIST
 BASECFLAGS
@@ -9745,6 +9746,7 @@ esac
 
 
 
+
 # The -arch flags for universal builds on macOS
 UNIVERSAL_ARCH_FLAGS=
 
index 34318769fcc29f45ba1473ba176ff822422cc032..8b90d8ca896f0c9386f8611699d9049350085f5f 100644 (file)
@@ -2422,6 +2422,7 @@ AS_CASE([$enable_wasm_dynamic_linking],
 AC_SUBST([BASECFLAGS])
 AC_SUBST([CFLAGS_NODIST])
 AC_SUBST([LDFLAGS_NODIST])
+AC_SUBST([EXE_LDFLAGS])
 AC_SUBST([LDFLAGS_NOLTO])
 AC_SUBST([WASM_ASSETS_DIR])
 AC_SUBST([WASM_STDLIB])