[PATCH 03/11] Rewrite find_cxx_header config configure.ac

Fangrui Song maskray@google.com
Sat Oct 29 04:03:51 GMT 2022


On 2022-10-28, Adhemerval Zanella via Libc-alpha wrote:
>clang does not support -MP option to create phony target for each
>dependency other than the main file.  Use -fsyntax-only with a
>more comprensible regex to get the the cxx header.

Clang has supported -MP for many years.
The problem was that when the main file is <stdin>, the first non-main-file dependency is not listed
in the -MP output.

     echo "#include <cstdlib>" | fclang++ -M -MP -x c++ - 2>/dev/null | sed -n "\,cstdlib:,{s/:\$//;p}"

I just fixed this in https://github.com/llvm/llvm-project/commit/ff9576f74514b836e1ba0268409a2ecb919d7118
which shall be included in Clang 16.



>---
> configure    | 4 ++--
> configure.ac | 4 ++--
> 2 files changed, 4 insertions(+), 4 deletions(-)
>
>diff --git a/configure b/configure
>index ff2c406b3b..26bd8200dd 100755
>--- a/configure
>+++ b/configure
>@@ -5579,8 +5579,8 @@ fi
> # copy of those headers in Makerules.
> if test -n "$CXX"; then
>   find_cxx_header () {
>-    echo "#include <$1>" | $CXX -M -MP -x c++ - 2>/dev/null \
>-	 | sed -n "\,$1:,{s/:\$//;p}"
>+    echo "#include <$1>" | $CXX -H -fsyntax-only -x c++ - 2>&1 \
>+	 | sed -rn "\,^\.? .,{s/^\.*\. //p}"

That said, the patch looks good to support more Clang versions.

For portability, prefer -E to -r and add `;` before `}` (FreeBSD sed doesn't allow `p}`).

Actually no ERE is needed and   sed -n "\,^\. .,{s/^\. //;p;}"  should work.
Alternatively, use      awk '$1 == "."{print $2}'

>   }
>   CXX_CSTDLIB_HEADER="$(find_cxx_header cstdlib)"
>   CXX_CMATH_HEADER="$(find_cxx_header cmath)"
>diff --git a/configure.ac b/configure.ac
>index eb5bc6a131..a009e7a17f 100644
>--- a/configure.ac
>+++ b/configure.ac
>@@ -1136,8 +1136,8 @@ AC_SUBST(CXX_SYSINCLUDES)
> # copy of those headers in Makerules.
> if test -n "$CXX"; then
>   find_cxx_header () {
>-    echo "#include <$1>" | $CXX -M -MP -x c++ - 2>/dev/null \
>-	 | sed -n "\,$1:,{s/:\$//;p}"
>+    echo "#include <$1>" | $CXX -H -fsyntax-only -x c++ - 2>&1 \
>+	 | sed -rn "\,^\.? .,{s/[^\.]*\. //p}"
>   }
>   CXX_CSTDLIB_HEADER="$(find_cxx_header cstdlib)"
>   CXX_CMATH_HEADER="$(find_cxx_header cmath)"
>-- 
>2.34.1
>


More information about the Libc-alpha mailing list