From d1f35aab457862162c5f89fad85eaefdbb63ffc1 Mon Sep 17 00:00:00 2001 From: Rue Ching Teh Date: Sun, 20 Jul 2025 23:32:46 +0000 Subject: [PATCH 1/3] 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. --- Lib/_osx_support.py | 3 ++- Lib/test/pythoninfo.py | 1 + Lib/test/test__osx_support.py | 2 +- Makefile.pre.in | 8 ++++++-- configure | 2 ++ configure.ac | 1 + 6 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Lib/_osx_support.py b/Lib/_osx_support.py index 0cb064fcd791be..29b89e311cb1fe 100644 --- a/Lib/_osx_support.py +++ b/Lib/_osx_support.py @@ -17,7 +17,8 @@ _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') diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py index 80a262c18a5ad2..b8c32666ae7a4b 100644 --- a/Lib/test/pythoninfo.py +++ b/Lib/test/pythoninfo.py @@ -524,6 +524,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', diff --git a/Lib/test/test__osx_support.py b/Lib/test/test__osx_support.py index 0813c4804c1cdc..b92ce6796b50fb 100644 --- a/Lib/test/test__osx_support.py +++ b/Lib/test/test__osx_support.py @@ -25,7 +25,7 @@ def setUp(self): '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): diff --git a/Makefile.pre.in b/Makefile.pre.in index 7fea799c3912dd..e26828e79c3531 100644 --- a/Makefile.pre.in +++ b/Makefile.pre.in @@ -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@ @@ -982,7 +986,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 @@ -1628,7 +1632,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 diff --git a/configure b/configure index 3570eecdcef94f..0d21ffafcda044 100755 --- a/configure +++ b/configure @@ -910,6 +910,7 @@ UNIVERSAL_ARCH_FLAGS WASM_STDLIB WASM_ASSETS_DIR LDFLAGS_NOLTO +EXE_LDFLAGS LDFLAGS_NODIST CFLAGS_NODIST BASECFLAGS @@ -9699,6 +9700,7 @@ esac + # The -arch flags for universal builds on macOS UNIVERSAL_ARCH_FLAGS= diff --git a/configure.ac b/configure.ac index 3566c4b9038c2b..e81a4c8b86dc37 100644 --- a/configure.ac +++ b/configure.ac @@ -2401,6 +2401,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]) From 9551faedf41723ea1a5ed5fa95862ba61069160d Mon Sep 17 00:00:00 2001 From: Rue Ching Teh Date: Sun, 20 Jul 2025 23:34:47 +0000 Subject: [PATCH 2/3] documentation for new linker flags --- Doc/using/configure.rst | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Doc/using/configure.rst b/Doc/using/configure.rst index e5fe3c72b1b26e..3734181a4c2581 100644 --- a/Doc/using/configure.rst +++ b/Doc/using/configure.rst @@ -1496,6 +1496,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 @@ -1531,6 +1534,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 From 2e12dea0a66ffb8a64c0a8cead416c3df433693c Mon Sep 17 00:00:00 2001 From: Rue Ching Teh Date: Mon, 21 Jul 2025 00:36:56 +0000 Subject: [PATCH 3/3] update Misc folder documentation --- Misc/ACKS | 1 + .../next/Build/2025-07-21-00-33-38.gh-issue-136677.Y1_3ec.rst | 1 + 2 files changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Build/2025-07-21-00-33-38.gh-issue-136677.Y1_3ec.rst diff --git a/Misc/ACKS b/Misc/ACKS index fabd79b9f74210..c67d243d96e656 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1891,6 +1891,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 index 00000000000000..30addc4bf64d4b --- /dev/null +++ b/Misc/NEWS.d/next/Build/2025-07-21-00-33-38.gh-issue-136677.Y1_3ec.rst @@ -0,0 +1 @@ +Introduce executable specific linker flags to ``./configure``.