From 304662234dc65961a2b01628e1d4c7a612da9bf5 Mon Sep 17 00:00:00 2001 From: WANG Xuerui Date: Mon, 16 Jun 2025 16:16:55 +0800 Subject: [PATCH] RISC-V: [gprofng] Allow building gprofng without asm/hwprobe.h The code is actually able to gracefully fallback if the syscall number of riscv_hwprobe is not available at build time, but it still depended on the header unconditionally. In certain environments such as one of crosstool-NG's Canadian Cross build step (binutils for host), or one with very outdated kernel headers, the header will not be present, causing the build to fail. While the relevant projects/environments should be fixed nevertheless, a configure-time check for is helpful for fixing gprofng builds with released versions of ct-ng etc. Signed-off-by: WANG Xuerui --- gprofng/common/config.h.in | 3 +++ gprofng/common/cpuid.c | 6 ++++-- gprofng/configure | 15 +++++++++++++++ gprofng/configure.ac | 4 ++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/gprofng/common/config.h.in b/gprofng/common/config.h.in index aabb043957a..5f2c1278c95 100644 --- a/gprofng/common/config.h.in +++ b/gprofng/common/config.h.in @@ -9,6 +9,9 @@ /* Enable java profiling */ #undef GPROFNG_JAVA_PROFILING +/* Define to 1 if you have the header file. */ +#undef HAVE_ASM_HWPROBE_H + /* Define to 1 if you have the `clock_gettime' function. */ #undef HAVE_CLOCK_GETTIME diff --git a/gprofng/common/cpuid.c b/gprofng/common/cpuid.c index e40404c645a..f9f90462839 100644 --- a/gprofng/common/cpuid.c +++ b/gprofng/common/cpuid.c @@ -48,8 +48,10 @@ __get_cpuid (unsigned int op ATTRIBUTE_UNUSED, unsigned int *eax, #include #include #include +#ifdef HAVE_ASM_HWPROBE_H #include #endif +#endif /* * Various routines to handle identification @@ -188,7 +190,7 @@ get_cpuid_info () break; } #elif defined(__riscv) - #ifndef __riscv_hwprobe + #if !defined(__riscv_hwprobe) || !defined(HAVE_ASM_HWPROBE_H) cpi->cpi_vendor = 0; cpi->cpi_family = 0; cpi->cpi_model = 0; @@ -208,7 +210,7 @@ get_cpuid_info () cpi->cpi_vendor = res.value; cpi->cpi_family = 0; cpi->cpi_model = 0; - #endif + #endif #endif return cpi; } diff --git a/gprofng/configure b/gprofng/configure index 2edfff107dd..2f4e18c47f6 100755 --- a/gprofng/configure +++ b/gprofng/configure @@ -17165,6 +17165,21 @@ fi done +# For riscv builds inside incomplete environments such as during intermediate +# steps of cross toolchain building, or with outdated Linux headers. +for ac_header in asm/hwprobe.h +do : + ac_fn_c_check_header_mongrel "$LINENO" "asm/hwprobe.h" "ac_cv_header_asm_hwprobe_h" "$ac_includes_default" +if test "x$ac_cv_header_asm_hwprobe_h" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_ASM_HWPROBE_H 1 +_ACEOF + +fi + +done + + clock_gettime_link= # At least for glibc, clock_gettime is in librt. But don't # pull that in if it still doesn't give us the function we want. This diff --git a/gprofng/configure.ac b/gprofng/configure.ac index 1f473b9b583..deb3ed586da 100644 --- a/gprofng/configure.ac +++ b/gprofng/configure.ac @@ -245,6 +245,10 @@ AC_SUBST(GPROFNG_CPPFLAGS, [${gprofng_cppflags}]) AC_CHECK_DECLS([basename]) AC_CHECK_FUNCS(clock_gettime strsignal) +# For riscv builds inside incomplete environments such as during intermediate +# steps of cross toolchain building, or with outdated Linux headers. +AC_CHECK_HEADERS(asm/hwprobe.h) + clock_gettime_link= # At least for glibc, clock_gettime is in librt. But don't # pull that in if it still doesn't give us the function we want. This -- 2.47.2