From 60c26877123e63373d0ef2bdedd9671c013feb3a Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Wed, 22 May 2024 13:22:34 +0200 Subject: [PATCH 1/2] Fix removal of optimization cflags in debug builds (#9647) Discard known '-O' flags, including just '-O', but do not remove only '-O' in '-Ounknown' --- configure.ac | 10 ++++++---- scripts/phpize.m4 | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index db355bbd6c5..ad1f42cfbed 100644 --- a/configure.ac +++ b/configure.ac @@ -846,8 +846,9 @@ if test "$PHP_GCOV" = "yes"; then dnl Remove all optimization flags from CFLAGS. changequote({,}) - CFLAGS=`echo "$CFLAGS" | "${SED}" -e 's/-O[0-9s]*//g'` - CXXFLAGS=`echo "$CXXFLAGS" | "${SED}" -e 's/-O[0-9s]*//g'` + dnl Discard known '-O...' flags, including just '-O', but do not remove only '-O' in '-Ounknown' + CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` + CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` changequote([,]) dnl Add the special gcc flags. @@ -866,8 +867,9 @@ if test "$PHP_DEBUG" = "yes"; then PHP_DEBUG=1 ZEND_DEBUG=yes changequote({,}) - CFLAGS=`echo "$CFLAGS" | "${SED}" -e 's/-O[0-9s]*//g'` - CXXFLAGS=`echo "$CXXFLAGS" | "${SED}" -e 's/-O[0-9s]*//g'` + dnl Discard known '-O...' flags, including just '-O', but do not remove only '-O' in '-Ounknown' + CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` + CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` changequote([,]) dnl Add -O0 only if GCC or ICC is used. if test "$GCC" = "yes" || test "$ICC" = "yes"; then diff --git a/scripts/phpize.m4 b/scripts/phpize.m4 index 616d16420a5..14bcde45f03 100644 --- a/scripts/phpize.m4 +++ b/scripts/phpize.m4 @@ -116,8 +116,9 @@ if test "$PHP_DEBUG" = "yes"; then PHP_DEBUG=1 ZEND_DEBUG=yes changequote({,}) - CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9s]*//g'` - CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O[0-9s]*//g'` + dnl Discard known '-O...' flags, including just '-O', but do not remove only '-O' in '-Ounknown' + CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` + CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` changequote([,]) dnl Add -O0 only if GCC or ICC is used. if test "$GCC" = "yes" || test "$ICC" = "yes"; then From c639614346ec08c0a2b62fb62e2db129ea4d6e39 Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Tue, 10 Sep 2024 18:40:49 +0200 Subject: [PATCH 2/2] Do not remove -O0 in the middle of a flag Fixes GH-15826 Closes GH-15828 Co-authored-by: Peter Kokot --- build/php.m4 | 12 ++++++++++++ configure.ac | 12 ++---------- scripts/phpize.m4 | 6 +----- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/build/php.m4 b/build/php.m4 index 32487e4b389..e13fc3367ea 100644 --- a/build/php.m4 +++ b/build/php.m4 @@ -2735,3 +2735,15 @@ AC_DEFUN([PHP_PATCH_CONFIG_HEADERS], [ $SED -e 's/^#undef PACKAGE_[^ ]*/\/\* & \*\//g' < $srcdir/$1 \ > $srcdir/$1.tmp && mv $srcdir/$1.tmp $srcdir/$1 ]) + +dnl +dnl PHP_REMOVE_OPTIMIZATION_FLAGS +dnl +dnl Removes known compiler optimization flags like -O, -O0, -O1, ..., -Ofast +dnl from CFLAGS and CXXFLAGS. +dnl +AC_DEFUN([PHP_REMOVE_OPTIMIZATION_FLAGS], [ + sed_script='s/\([[\t ]]\|^\)-O\([[0-9gsz]]\|fast\|\)\([[\t ]]\|$\)/\1/g' + CFLAGS=$(echo "$CFLAGS" | $SED -e "$sed_script") + CXXFLAGS=$(echo "$CXXFLAGS" | $SED -e "$sed_script") +]) diff --git a/configure.ac b/configure.ac index ad1f42cfbed..9c71f650331 100644 --- a/configure.ac +++ b/configure.ac @@ -845,11 +845,7 @@ if test "$PHP_GCOV" = "yes"; then PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/build/Makefile.gcov, $abs_srcdir) dnl Remove all optimization flags from CFLAGS. - changequote({,}) - dnl Discard known '-O...' flags, including just '-O', but do not remove only '-O' in '-Ounknown' - CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` - CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` - changequote([,]) + PHP_REMOVE_OPTIMIZATION_FLAGS dnl Add the special gcc flags. CFLAGS="$CFLAGS -O0 -fprofile-arcs -ftest-coverage" @@ -866,11 +862,7 @@ PHP_ARG_ENABLE([debug], if test "$PHP_DEBUG" = "yes"; then PHP_DEBUG=1 ZEND_DEBUG=yes - changequote({,}) - dnl Discard known '-O...' flags, including just '-O', but do not remove only '-O' in '-Ounknown' - CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` - CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` - changequote([,]) + PHP_REMOVE_OPTIMIZATION_FLAGS dnl Add -O0 only if GCC or ICC is used. if test "$GCC" = "yes" || test "$ICC" = "yes"; then CFLAGS="$CFLAGS -O0" diff --git a/scripts/phpize.m4 b/scripts/phpize.m4 index 14bcde45f03..b713dcc6505 100644 --- a/scripts/phpize.m4 +++ b/scripts/phpize.m4 @@ -115,11 +115,7 @@ dnl Discard optimization flags when debugging is enabled. if test "$PHP_DEBUG" = "yes"; then PHP_DEBUG=1 ZEND_DEBUG=yes - changequote({,}) - dnl Discard known '-O...' flags, including just '-O', but do not remove only '-O' in '-Ounknown' - CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` - CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)//g'` - changequote([,]) + PHP_REMOVE_OPTIMIZATION_FLAGS dnl Add -O0 only if GCC or ICC is used. if test "$GCC" = "yes" || test "$ICC" = "yes"; then CFLAGS="$CFLAGS -O0"