[PATCH] binutils: Use AC_TRY_COMPILE to check target clang/gcc

Alan Modra amodra@gmail.com
Thu Oct 2 08:18:30 GMT 2025


On Wed, Oct 01, 2025 at 09:02:43PM +0100, Sam James wrote:
> "H.J. Lu" <hjl.tools@gmail.com> writes:
> 
> > Use AC_TRY_COMPILE to check for the working target clang and gcc when
> > configuring for cross tools.
> >
> > PR binutils/33503
> > * configure: Regenerated.
> >
> > config/
> >
> > PR binutils/33503
> > * clang-plugin.m4 (CLANG_PLUGIN_FILE_FOR_TARGET): Use
> > AC_TRY_COMPILE to check the target clang and replace
> > clang_cv_is_clang with clang_target_cv_working.
> > * gcc-plugin.m4 (GCC_PLUGIN_OPTION_FOR_TARGET): Use
> > AC_TRY_COMPILE to check the target gcc.
> 
> Can you sync the GCC side?

Also, can someone tell me why we need to invoke llvm-config?  Are
there versions or installations of clang where
  clang --print-file-name LLVMgold.so
does not find/print the path to LLVMgold.so, but
  `llvm-config --libdir`/LLVMgold.so
will?  If it is only old versions or broken installations of clang,
then should we bother?

Besides the llvm-config issue, I believe we need some more fixes.
- It is possible for plugin_file to be non-NULL when LLVMgold.so does
  not exist.
- configure output is messy, with results not printed against their
  "checking.." line, eg.
checking for clang... (cached) yes
checking for clang plugin file... checking for x86_64-pc-linux-gnu-ar... (cached) ar --plugin /usr/lib/llvm-20/lib/clang/20/../../LLVMgold.so
/usr/lib/llvm-20/lib/clang/20/../../LLVMgold.so

Fixed with the following, plus regenerating everything.

diff --git a/config/clang-plugin.m4 b/config/clang-plugin.m4
index b6b28ab8d21..b5fb241178b 100644
--- a/config/clang-plugin.m4
+++ b/config/clang-plugin.m4
@@ -26,37 +26,30 @@ AC_DEFUN([CLANG_PLUGIN_FILE],[dnl
     plugin_names="LLVMgold.so"
     for plugin in $plugin_names; do
       plugin_file=`${CC} ${CFLAGS} --print-file-name $plugin`
-      if test x$plugin_file = x$plugin; then
-	AC_CHECK_TOOL(LLVM_CONFIG, llvm-config)
-	if test "$?" != 0; then
-	  AC_MSG_ERROR([Required tool 'llvm-config' not found on PATH.])
-	fi
-	clang_lib_dir=`$LLVM_CONFIG --libdir`
-	if test -f $clang_lib_dir/$plugin; then
-	  plugin_file=$clang_lib_dir/$plugin
-	fi
-	if test x$plugin_file != x$plugin; then
-	  break;
-	fi
+      if test x$plugin_file != x$plugin; then
+	break;
       fi
+      plugin_file=
     done
     if test -z $plugin_file; then
-      AC_MSG_ERROR([Couldn't find clang plugin file for $CC.])
-    fi
-    dnl Check if ${AR} $plugin_option rc works.
-    AC_CHECK_TOOL(AR, ar)
-    if test "${AR}" = "" ; then
-      AC_MSG_ERROR([Required archive tool 'ar' not found on PATH.])
-    fi
-    plugin_option="--plugin $plugin_file"
-    touch conftest.c
-    ${AR} $plugin_option rc conftest.a conftest.c
-    if test "$?" != 0; then
-      AC_MSG_WARN([Failed: $AR $plugin_option rc])
-      plugin_file=
+      AC_MSG_RESULT([no])
+    else
+      AC_MSG_RESULT($plugin_file)
+      dnl Check if ${AR} $plugin_option rc works.
+      AC_CHECK_TOOL(AR, ar)
+      if test "${AR}" = "" ; then
+	AC_MSG_ERROR([Required archive tool 'ar' not found on PATH.])
+      else
+	plugin_option="--plugin $plugin_file"
+	touch conftest.c
+	${AR} $plugin_option rc conftest.a conftest.c
+	if test "$?" != 0; then
+	  AC_MSG_WARN([Failed: $AR $plugin_option rc])
+	  plugin_file=
+	fi
+	rm -f conftest.*
+      fi
     fi
-    rm -f conftest.*
-    AC_MSG_RESULT($plugin_file)
   fi
   $1="$plugin_file"
 ])
@@ -86,28 +79,17 @@ AC_DEFUN([CLANG_PLUGIN_FILE_FOR_TARGET],[dnl
   if test $clang_target_cv_working = yes; then
     AC_MSG_CHECKING([for clang plugin file for target])
     plugin_names="LLVMgold.so"
-    dnl Check if the host compiler is used.
     for plugin in $plugin_names; do
       plugin_file=`${COMPILER_FOR_TARGET} ${CFLAGS_FOR_TARGET} --print-file-name $plugin`
-      if test x$plugin_file = x$plugin; then
-	GCC_TARGET_TOOL(llvm-config, LLVM_CONFIG_FOR_TARGET, LLVM_CONFIG)
-	if test "$?" != 0; then
-	  AC_MSG_ERROR([Required target tool 'llvm-config' not found.])
-	fi
-	clang_lib_dir=`$LLVM_CONFIG_FOR_TARGET --libdir`
-	if test -f $clang_lib_dir/$plugin; then
-	  plugin_file=$clang_lib_dir/$plugin
-	fi
-      fi
       if test x$plugin_file != x$plugin; then
 	break;
       fi
       plugin_file=
     done
-    if test -n $plugin_file; then
-      AC_MSG_RESULT($plugin_file)
-    else
+    if test -z $plugin_file; then
       AC_MSG_RESULT([no])
+    else
+      AC_MSG_RESULT($plugin_file)
     fi
   fi
   $1="$plugin_file"
diff --git a/binutils/testsuite/lib/binutils-common.exp b/binutils/testsuite/lib/binutils-common.exp
index fb12607303f..4a8367568e6 100644
--- a/binutils/testsuite/lib/binutils-common.exp
+++ b/binutils/testsuite/lib/binutils-common.exp
@@ -1824,21 +1824,10 @@ if { [isnative] } then {
 	    set CLANG_FOR_TARGET clang
 	}
     }
-    if [info exists CLANG_FOR_TARGET] then {
-	set llvm_plug_so [string trim [exec $CLANG_FOR_TARGET -print-file-name=LLVMgold.so]]
-	if { $llvm_plug_so ne "LLVMgold.so" } then {
-	    set llvm_plug_opt "--plugin $llvm_plug_so"
-	}
-
-	if { $llvm_plug_opt eq "" } then {
-	    # If it is still blank, try llvm-config --libdir.  Clang
-	    # searches CLANG_INSTALL_LIBDIR_BASENAME which corresponds
-	    # to this.
-	    catch "exec llvm-config --libdir" got
-	    if {[file isdirectory $got] \
-		&& [file isfile $got/LLVMgold.so]} then {
-		set llvm_plug_opt "--plugin $got/LLVMgold.so"
-	    }
-	}
+}
+if [info exists CLANG_FOR_TARGET] then {
+    set llvm_plug_so [string trim [exec $CLANG_FOR_TARGET -print-file-name=LLVMgold.so]]
+    if { $llvm_plug_so ne "LLVMgold.so" } then {
+	set llvm_plug_opt "--plugin $llvm_plug_so"
     }
 }


-- 
Alan Modra


More information about the Binutils mailing list