From 67d4c27dcffecef9e45fe10070943d4c9d3f8106 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 fb59a81700f4..e1635570040e 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 616d16420a5a..14bcde45f03a 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 e624350ace758188d1a3e2a3efda97afb99cac9a 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 --- build/php.m4 | 14 ++++++++++++++ configure.ac | 12 ++---------- scripts/phpize.m4 | 6 +----- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/build/php.m4 b/build/php.m4 index 32487e4b389e..6e261a197594 100644 --- a/build/php.m4 +++ b/build/php.m4 @@ -2735,3 +2735,17 @@ 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], [ + changequote({,}) + sed_script='s/\([\t ]\|^\)-O\([0-9gsz]\|fast\|\)\([\t ]\|$\)/\1/g' + changequote([,]) + CFLAGS=`echo "$CFLAGS" | sed -e "$sed_script"` + CXXFLAGS=`echo "$CFLAGS" | sed -e "$sed_script"` +]) diff --git a/configure.ac b/configure.ac index e1635570040e..6b3650558a34 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 14bcde45f03a..b713dcc65059 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"