]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Detect hwcap flags needed for runtime detection on ARM Linux
authorJosh Triplett <josh@joshtriplett.org>
Sun, 25 Apr 2021 22:21:54 +0000 (15:21 -0700)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Thu, 29 Apr 2021 13:29:55 +0000 (15:29 +0200)
This allows us to provide useful warning messages from cmake or
configure if the system headers don't provide the necessary flags to do
runtime detection.

CMakeLists.txt
arch/arm/armfeature.c
configure

index 56b4a839c5c7afabd2fa0dd294d1af3a4cac6785..dd174a3dff83fdd25ab4baff58c27621672f9474 100644 (file)
@@ -649,6 +649,34 @@ endif()
 if(WITH_OPTIM)
     if(BASEARCH_ARM_FOUND)
         add_definitions(-DARM_FEATURES)
+        if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
+            check_c_source_compiles(
+                "#include <sys/auxv.h>
+                int main() {
+                    return (getauxval(AT_HWCAP2) & HWCAP2_CRC32);
+                }"
+                ARM_AUXV_HAS_CRC32
+            )
+            if(ARM_AUXV_HAS_CRC32)
+                add_definitions(-DARM_AUXV_HAS_CRC32)
+            else()
+                message(STATUS "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime.")
+            endif()
+            if(NOT "${ARCH}" MATCHES "aarch64")
+                check_c_source_compiles(
+                    "#include <sys/auxv.h>
+                    int main() {
+                      return (getauxval(AT_HWCAP) & HWCAP_NEON);
+                    }"
+                    ARM_AUXV_HAS_NEON
+                )
+                if(ARM_AUXV_HAS_NEON)
+                    add_definitions(-DARM_AUXV_HAS_NEON)
+                else()
+                    message(STATUS "HWCAP_NEON not present in sys/auxv.h; cannot detect support at runtime.")
+                endif()
+            endif()
+        endif()
         list(APPEND ZLIB_ARCH_HDRS ${ARCHDIR}/arm.h)
         list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/armfeature.c)
         if(WITH_ACLE AND NOT MSVC)
index cea19e614e36cfc34671ed2fcc956bc97a277189..e8fcc1e5ea7956da1d40ed2953856a1351cb38c4 100644 (file)
@@ -14,7 +14,7 @@
 #endif
 
 static int arm_has_crc32() {
-#if defined(__linux__) && defined(HWCAP2_CRC32)
+#if defined(__linux__) && defined(ARM_AUXV_HAS_CRC32)
     return (getauxval(AT_HWCAP2) & HWCAP2_CRC32) != 0 ? 1 : 0;
 #elif defined(__FreeBSD__) && defined(__aarch64__)
     return getenv("QEMU_EMULATING") == NULL
@@ -34,7 +34,7 @@ static int arm_has_crc32() {
 /* AArch64 has neon. */
 #if !defined(__aarch64__) && !defined(_M_ARM64)
 static inline int arm_has_neon() {
-#if defined(__linux__) && defined(HWCAP_NEON)
+#if defined(__linux__) && defined(ARM_AUXV_HAS_NEON)
     return (getauxval(AT_HWCAP) & HWCAP_NEON) != 0 ? 1 : 0;
 #elif defined(__APPLE__)
     int hasneon;
index aafaa06ac5df6ae78ef20ec5353fd226d05ed764..89388be0cd043c6e298d43361345ee76d560516c 100755 (executable)
--- a/configure
+++ b/configure
@@ -524,6 +524,14 @@ else
   esac
 fi
 
+# Simplify some later conditionals
+case "$uname" in
+Linux* | linux*)
+  LINUX=1 ;;
+*)
+  LINUX=0 ;;
+esac
+
 # destination names for shared library if not defined above
 SHAREDLIB=${SHAREDLIB-"${LIBNAME}$shared_ext"}
 SHAREDLIBV=${SHAREDLIBV-"${LIBNAME}$shared_ext.$VER"}
@@ -1265,6 +1273,34 @@ case "${ARCH}" in
             SFLAGS="${SFLAGS} -DARM_FEATURES"
             ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} armfeature.o"
             ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} armfeature.lo"
+
+            if test $LINUX -eq 1; then
+                cat > $test.c <<EOF
+#include <sys/auxv.h>
+int main() {
+    return (getauxval(AT_HWCAP2) & HWCAP2_CRC32);
+}
+EOF
+                if try $CC -c $CFLAGS $test.c; then
+                    CFLAGS="${CFLAGS} -DARM_AUXV_HAS_CRC32"
+                    SFLAGS="${SFLAGS} -DARM_AUXV_HAS_CRC32"
+                else
+                    echo "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log
+                fi
+
+                cat > $test.c <<EOF
+#include <sys/auxv.h>
+int main() {
+    return (getauxval(AT_HWCAP) & HWCAP_NEON);
+}
+EOF
+                if try $CC -c $CFLAGS $test.c; then
+                    CFLAGS="${CFLAGS} -DARM_AUXV_HAS_NEON"
+                    SFLAGS="${SFLAGS} -DARM_AUXV_HAS_NEON"
+                else
+                    echo "HWCAP_NEON not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log
+                fi
+            fi
         fi
 
         cat > $test.c << EOF
@@ -1405,6 +1441,21 @@ EOF
             ARCH_STATIC_OBJS="${ARCH_STATIC_OBJS} armfeature.o"
             ARCH_SHARED_OBJS="${ARCH_SHARED_OBJS} armfeature.lo"
 
+            if test $LINUX -eq 1; then
+                cat > $test.c <<EOF
+#include <sys/auxv.h>
+int main() {
+    return (getauxval(AT_HWCAP2) & HWCAP2_CRC32);
+}
+EOF
+                if try $CC -c $CFLAGS $test.c; then
+                    CFLAGS="${CFLAGS} -DARM_AUXV_HAS_CRC32"
+                    SFLAGS="${SFLAGS} -DARM_AUXV_HAS_CRC32"
+                else
+                    echo "HWCAP2_CRC32 not present in sys/auxv.h; cannot detect support at runtime." | tee -a configure.log
+                fi
+            fi
+
             if test $buildacle -eq 1; then
                 if test $native -eq 0; then
                     ARCH="${ARCH}+crc"