]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Add archdetect as separate cmake include file.
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Fri, 20 Sep 2019 18:35:44 +0000 (20:35 +0200)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Sat, 21 Sep 2019 20:10:46 +0000 (22:10 +0200)
Simplify architecture checking by setting BASEARCH in addition to ARCH.

.gitignore
CMakeLists.txt
cmake/archdetect.c [new file with mode: 0644]
cmake/archdetect.cmake [new file with mode: 0644]

index 1687788c37aba8814fc141d1316136e748a9312a..9d8dcfaa6e2ae66dbfc891807189d56ecb6a8bae 100644 (file)
@@ -47,7 +47,7 @@ foo.gz
 CMakeCache.txt
 CMakeFiles
 Testing
-*.cmake
+/*.cmake
 *.stackdump
 *._h
 zconf.h
index 55102d44ac5ce8827f83703634a9c37fb1e2d7c7..3cbc4e2a93f1b6b16479ec2db39e208acfa88c46 100644 (file)
@@ -4,6 +4,7 @@ if(CMAKE_VERSION VERSION_LESS 3.12)
 else()
   cmake_policy(VERSION 3.5.1...3.13.2)
 endif()
+message(STATUS "Using CMake version ${CMAKE_VERSION}")
 
 set(CMAKE_MACOSX_RPATH 1)
 
@@ -52,6 +53,12 @@ include(CheckCSourceRuns)
 include(CMakeDependentOption)
 include(FeatureSummary)
 
+include(cmake/archdetect.cmake)
+
+if(CMAKE_TOOLCHAIN_FILE)
+    message(STATUS "Using CMake toolchain: ${CMAKE_TOOLCHAIN_FILE}")
+endif()
+
 # Make sure we use an appropriate BUILD_TYPE by default, "Release" to be exact
 # this should select the maximum generic optimisation on the current platform (i.e. -O3 for gcc/clang)
 if(NOT CMAKE_BUILD_TYPE)
@@ -68,26 +75,6 @@ check_include_file(stdint.h    HAVE_STDINT_H)
 check_include_file(stddef.h    HAVE_STDDEF_H)
 check_include_file(sys/sdt.h   HAVE_SYS_SDT_H)
 
-if(CMAKE_OSX_ARCHITECTURES)
-    # If multiple architectures are requested (universal build), pick only the first
-    list(GET CMAKE_OSX_ARCHITECTURES 0 ARCH)
-else()
-    if (MSVC)
-        if("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM" OR "${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARMV7")
-            set(ARCH "arm")
-        elseif ("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM64")
-            set(ARCH "aarch64")
-        endif()
-    endif()
-    if(NOT ARCH)
-        set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
-    endif()
-endif()
-message(STATUS "Architecture: ${ARCH}")
-if(CMAKE_TOOLCHAIN_FILE)
-    message(STATUS "Using cmake toolchain: ${CMAKE_TOOLCHAIN_FILE}")
-endif()
-
 #
 # Options parsing
 #
@@ -101,10 +88,10 @@ option(WITH_OPTIM "Build with optimisation" ON)
 option(WITH_NEW_STRATEGIES "Use new strategies" ON)
 option(WITH_NATIVE_INSTRUCTIONS
     "Instruct the compiler to use the full instruction set on this host (gcc/clang -march=native)" OFF)
-if("${ARCH}" MATCHES "arm" OR "${ARCH}" MATCHES "aarch64")
+if(BASEARCH_ARM_FOUND)
     option(WITH_ACLE "Build with ACLE CRC" ON)
     option(WITH_NEON "Build with NEON intrinsics" ON)
-elseif("${ARCH}" MATCHES "s390x")
+elseif(BASEARCH_S360_FOUND AND "${ARCH}" MATCHES "s390x")
     option(WITH_DFLTCC_DEFLATE "Use DEFLATE CONVERSION CALL instruction for compression on IBM Z" OFF)
     option(WITH_DFLTCC_INFLATE "Use DEFLATE CONVERSION CALL instruction for decompression on IBM Z" OFF)
 endif()
@@ -116,7 +103,7 @@ add_feature_info(WITH_SANITIZERS WITH_SANITIZERS "Build with address sanitizer a
 add_feature_info(WITH_MSAN WITH_MSAN "Build with memory sanitizer")
 add_feature_info(WITH_FUZZERS WITH_FUZZERS "Build test/fuzz")
 add_feature_info(WITH_NEW_STRATEGIES WITH_NEW_STRATEGIES "Use new strategies")
-if("${ARCH}" MATCHES "arm" OR "${ARCH}" MATCHES "aarch64")
+if(BASEARCH_ARM_FOUND)
     add_feature_info(WITH_ACLE WITH_ACLE "Build with ACLE CRC")
     add_feature_info(WITH_NEON WITH_NEON "Build with NEON intrinsics")
 endif()
@@ -161,10 +148,11 @@ elseif(MSVC)
     # (who'd use cmake from an IDE...) but checking for ICC before checking for MSVC should
     # avoid mistakes.
     # /Oi ?
-    if(NOT ${ARCH} MATCHES "AMD64")
-        set(SSE2FLAG "/arch:SSE2")
-    endif()
-    if("${ARCH}" MATCHES "arm")
+    if(BASEARCH_X86_FOUND)
+        if(NOT ${ARCH} MATCHES "x86_64")
+            set(SSE2FLAG "/arch:SSE2")
+        endif()
+    elseif(BASEARCH_ARM_FOUND)
         add_definitions(-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE)
         set(NEONFLAG "/arch:VFPv4")
     endif()
@@ -188,7 +176,7 @@ else()
         set(WARNFLAGS "-Wall -Wno-implicit-fallthrough")
     endif()
     # Check support for ARM floating point
-    if("${ARCH}" MATCHES "arm")
+    if(BASEARCH_ARM_FOUND)
         if (__GNUC__)
             execute_process(COMMAND ${CMAKE_C_COMPILER} "-dumpmachine"
                             OUTPUT_VARIABLE GCC_MACHINE)
@@ -211,36 +199,34 @@ else()
         endif()
     endif()
     if(NOT NATIVEFLAG)
-        if(NOT SSE2FLAG)
-            if(__GNUC__)
-                set(SSE2FLAG "-msse2")
-            endif()
-        endif()
-        if(NOT SSE4FLAG)
-            if(__GNUC__)
-                set(SSE4FLAG "-msse4")
+        if(__GNUC__)
+            if(BASEARCH_X86_FOUND)
+                if(NOT SSE2FLAG)
+                    set(SSE2FLAG "-msse2")
+                endif()
+                if(NOT SSE4FLAG)
+                    set(SSE4FLAG "-msse4")
+                endif()
+                if(NOT PCLMULFLAG)
+                    set(PCLMULFLAG "-mpclmul")
+                endif()
             endif()
-        endif()
-        if(NOT PCLMULFLAG)
-            if(__GNUC__)
-                set(PCLMULFLAG "-mpclmul")
+            elseif(BASEARCH_ARM_FOUND)
+                set(ACLEFLAG "-march=armv8-a+crc")
+                if("${ARCH}" MATCHES "aarch64")
+                    set(NEONFLAG "-march=armv8-a+crc+simd")
+                endif()
             endif()
         endif()
-        if("${ARCH}" MATCHES "arm")
-            set(ACLEFLAG "-march=armv8-a+crc")
-        elseif("${ARCH}" MATCHES "aarch64")
-            set(ACLEFLAG "-march=armv8-a+crc")
-            set(NEONFLAG "-march=armv8-a+crc+simd")
-        endif()
     else()
         set(SSE2FLAG ${NATIVEFLAG})
         set(SSE4FLAG ${NATIVEFLAG})
         set(PCLMULFLAG ${NATIVEFLAG})
-        if("${ARCH}" MATCHES "arm")
-            set(ACLEFLAG "${NATIVEFLAG}")
-        elseif("${ARCH}" MATCHES "aarch64")
+        if(BASEARCH_ARM_FOUND)
             set(ACLEFLAG "${NATIVEFLAG}")
-            set(NEONFLAG "${NATIVEFLAG}")
+            if("${ARCH}" MATCHES "aarch64")
+                set(NEONFLAG "${NATIVEFLAG}")
+            endif()
         endif()
     endif()
 endif()
@@ -543,23 +529,22 @@ endmacro()
 
 set(ZLIB_ARCH_SRCS)
 set(ARCHDIR "arch/generic")
-if("${ARCH}" MATCHES "x86_64" OR "${ARCH}" MATCHES "AMD64" OR "${ARCH}" MATCHES "i[3-6]86")
+if(BASEARCH_X86_FOUND)
     set(ARCHDIR "arch/x86")
     add_definitions(-DUNALIGNED_OK)
     add_feature_info(SSE2 1 "Support the SSE2 instruction set, using \"${SSE2FLAG}\"")
-elseif("${ARCH}" MATCHES "arm" OR "${ARCH}" MATCHES "aarch64")
+elseif(BASEARCH_ARM_FOUND)
     set(ARCHDIR "arch/arm")
     add_definitions(-DUNALIGNED_OK)
-elseif("${ARCH}" MATCHES "s390x")
+elseif(BASEARCH_S360_FOUND AND "${ARCH}" MATCHES "s390x")
     set(ARCHDIR "arch/s390")
 else()
     message(STATUS "No optimized architecture: using ${ARCHDIR}")
 endif()
-if("${ARCH}" MATCHES "arm" OR "${ARCH}" MATCHES "aarch64")
-    list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/armfeature.c ${ARCHDIR}/fill_window_arm.c)
-endif()
+
 if(WITH_OPTIM)
-    if("${ARCH}" MATCHES "arm" OR "${ARCH}" MATCHES "aarch64")
+    if(BASEARCH_ARM_FOUND)
+        list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/armfeature.c ${ARCHDIR}/fill_window_arm.c)
         if(WITH_NEON)
             list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/adler32_neon.c)
             add_definitions(-DARM_NEON_ADLER32)
@@ -578,7 +563,7 @@ if(WITH_OPTIM)
             endif()
             add_feature_info(ACLE_CRC 1 "Support CRC hash generation using the ACLE instruction set, using \"${ACLEFLAG}\"")
         endif()
-    elseif("${ARCHDIR}" MATCHES "arch/x86")
+    elseif(BASEARCH_X86_FOUND)
         add_definitions("-DX86_CPUID")
         list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/x86.c)
         if(HAVE_SSE42CRC_INLINE_ASM OR HAVE_SSE42CRC_INTRIN)
@@ -616,7 +601,7 @@ if(WITH_OPTIM)
                 add_feature_info(PCLMUL_CRC 1 "Support CRC hash generation using PCLMULQDQ, using \"${PCLMULFLAG} ${SSE4FLAG}\"")
             endif()
         endif()
-    elseif("${ARCH}" MATCHES "s390x")
+    elseif(BASEARCH_S360_FOUND AND "${ARCH}" MATCHES "s390x")
         if(WITH_DFLTCC_DEFLATE OR WITH_DFLTCC_INFLATE)
             list(APPEND ZLIB_ARCH_SRCS ${ARCHDIR}/dfltcc_common.c)
         endif()
@@ -750,7 +735,7 @@ if(MINGW OR MSYS)
     set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
 endif()
 
-set(ZLIB_ALL_SRCS ${ZLIB_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_DLL_SRCS} 
+set(ZLIB_ALL_SRCS ${ZLIB_SRCS} ${ZLIB_ARCH_SRCS} ${ZLIB_DLL_SRCS}
     ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
 if(WITH_GZFILEOP)
     list(APPEND ZLIB_ALL_SRCS ${ZLIB_GZFILE_SRCS})
@@ -768,7 +753,7 @@ else()
 endif()
 
 foreach(ZLIB_INSTALL_LIBRARY ${ZLIB_INSTALL_LIBRARIES})
-    target_include_directories(${ZLIB_INSTALL_LIBRARY} PUBLIC 
+    target_include_directories(${ZLIB_INSTALL_LIBRARY} PUBLIC
         ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
 endforeach()
 
diff --git a/cmake/archdetect.c b/cmake/archdetect.c
new file mode 100644 (file)
index 0000000..f8feb88
--- /dev/null
@@ -0,0 +1,95 @@
+// archdetect.c -- Detect compiler architecture and raise preprocessor error
+//                 containing a simple arch identifier.
+// Copyright (C) 2019 Hans Kristian Rosbach
+// Licensed under the Zlib license, see LICENSE.md for details
+
+// x86_64
+#if defined(__x86_64__) || defined(_M_X64)
+    #error archfound x86_64
+
+// x86
+#elif defined(__i386) || defined(_M_IX86)
+    #error archfound i686
+
+// ARM
+#elif defined(__aarch64__) || defined(_M_ARM64)
+    #error archfound aarch64
+#elif defined(__arm__) || defined(__arm) || defined(_M_ARM) || defined(__TARGET_ARCH_ARM)
+    #if defined(__ARM64_ARCH_8__) || defined(__ARMv8__) || defined(__ARMv8_A__)
+        #error archfound armv8
+    #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__))
+        #error archfound armv7
+    #elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) || defined(__ARM_ARCH_6M__)
+        #error archfound armv6
+    #elif defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__)
+        #error archfound armv5
+    #elif defined(__ARM_ARCH_4T__) || defined(__TARGET_ARCH_5E__)
+        #error archfound armv4
+    #elif defined(__ARM_ARCH_3__) || defined(__TARGET_ARCH_3M__)
+        #error archfound armv3
+    #elif defined(__ARM_ARCH_2__)
+        #error archfound armv2
+    #endif
+
+// PowerPC
+#elif defined(__powerpc__) || defined(_ppc__) || defined(__PPC__)
+    #if defined(__64BIT__) || defined(__powerpc64__) || defined(__ppc64__)
+        #error archfound ppc64
+    #else
+        #error archfound ppc
+    #endif
+
+// --------------- Less common architectures alphabetically below ---------------
+
+// ALPHA
+#elif defined(__alpha__) || defined(__alpha)
+    #error archfound alpha
+
+// Blackfin
+#elif defined(__BFIN__)
+    #error archfound blackfin
+
+// Itanium
+#elif defined(__ia64) || defined(_M_IA64)
+    #error archfound ia64
+
+// MIPS
+#elif defined(__mips__) || defined(__mips)
+    #error archfound mips
+
+// Motorola 68000-series
+#elif defined(__m68k__)
+    #error archfound m68k
+
+// SuperH
+#elif defined(__sh__)
+    #error archfound sh
+
+// SPARC
+#elif defined(__sparc__) || defined(__sparc)
+    #if defined(__sparcv9) || defined(__sparc_v9__)
+        #error archfound sparc9
+    #elif defined(__sparcv8) || defined(__sparc_v8__)
+        #error archfound sparc8
+    #endif
+
+// SystemZ
+#elif defined(__370__)
+    #error archfound s370
+#elif defined(__s390__)
+    #error archfound s390
+#elif defined(__s390x) || defined(__zarch__)
+    #error archfound s390x
+
+// PARISC
+#elif defined(__hppa__)
+    #error archfound parisc
+
+// RS-6000
+#elif defined(__THW_RS6000)
+    #error archfound rs6000
+
+// return 'unrecognized' if we do not know what architecture this is
+#else
+    #error archfound unrecognized
+#endif
diff --git a/cmake/archdetect.cmake b/cmake/archdetect.cmake
new file mode 100644 (file)
index 0000000..9f8d1c1
--- /dev/null
@@ -0,0 +1,92 @@
+# archdetect.cmake -- Detect compiler architecture and set ARCH and BASEARCH
+# Copyright (C) 2019 Hans Kristian Rosbach
+# Licensed under the Zlib license, see LICENSE.md for details
+set(ARCHDETECT_FOUND TRUE)
+
+if(CMAKE_OSX_ARCHITECTURES)
+    # If multiple architectures are requested (universal build), pick only the first
+    list(GET CMAKE_OSX_ARCHITECTURES 0 ARCH)
+elseif(MSVC)
+    if("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "X86")
+        set(ARCH "i686")
+    elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "x64")
+        set(ARCH "x86_64")
+    elseif("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM" OR "${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARMV7")
+        set(ARCH "arm")
+    elseif ("${MSVC_C_ARCHITECTURE_ID}" STREQUAL "ARM64")
+        set(ARCH "aarch64")
+    endif()
+else()
+    # Let preprocessor parse archdetect.c and raise an error containing the arch identifier
+    enable_language(C)
+    try_run(
+        run_result_unused
+        compile_result_unused
+        ${CMAKE_CURRENT_SOURCE_DIR}
+        ${CMAKE_CURRENT_SOURCE_DIR}/cmake/archdetect.c
+        COMPILE_OUTPUT_VARIABLE RAWOUTPUT
+        CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
+    )
+
+    # Find basearch tag, and extract the arch word into BASEARCH variable
+    string(REGEX REPLACE ".*archfound ([a-zA-Z0-9_]+).*" "\\1" ARCH "${RAWOUTPUT}")
+    if (NOT ARCH)
+        set(ARCH unknown)
+    endif()
+endif()
+
+# Make sure we have ARCH set
+if(NOT ARCH OR ARCH STREQUAL "unknown")
+    set(ARCH ${CMAKE_SYSTEM_PROCESSOR})
+    message(STATUS "Arch not recognized, falling back to cmake arch: '${ARCH}'")
+else()
+    message(STATUS "Arch detected: '${ARCH}'")
+endif()
+
+
+# Base arch detection
+if("${ARCH}" MATCHES "(x86_64|AMD64|i[3-6]86)")
+    set(BASEARCH "x86")
+    set(BASEARCH_X86_FOUND TRUE)
+elseif("${ARCH}" MATCHES "(arm(v[0-9])?|aarch64)")
+    set(BASEARCH "arm")
+    set(BASEARCH_ARM_FOUND TRUE)
+elseif("${ARCH}" MATCHES "ppc(64)?")
+    set(BASEARCH "ppc")
+    set(BASEARCH_PPC_FOUND TRUE)
+elseif("${ARCH}" MATCHES "alpha")
+    set(BASEARCH "alpha")
+    set(BASEARCH_ALPHA_FOUND TRUE)
+elseif("${ARCH}" MATCHES "blackfin")
+    set(BASEARCH "blackfin")
+    set(BASEARCH_BLACKFIN_FOUND TRUE)
+elseif("${ARCH}" MATCHES "ia64")
+    set(BASEARCH "ia64")
+    set(BASEARCH_IA64_FOUND TRUE)
+elseif("${ARCH}" MATCHES "mips")
+    set(BASEARCH "mips")
+    set(BASEARCH_MIPS_FOUND TRUE)
+elseif("${ARCH}" MATCHES "m68k")
+    set(BASEARCH "m68k")
+    set(BASEARCH_M68K_FOUND TRUE)
+elseif("${ARCH}" MATCHES "sh")
+    set(BASEARCH "sh")
+    set(BASEARCH_SH_FOUND TRUE)
+elseif("${ARCH}" MATCHES "sparc[89]?")
+    set(BASEARCH "sparc")
+    set(BASEARCH_SPARC_FOUND TRUE)
+elseif("${ARCH}" MATCHES "s3[679]0x?")
+    set(BASEARCH "s360")
+    set(BASEARCH_S360_FOUND TRUE)
+elseif("${ARCH}" MATCHES "parisc")
+    set(BASEARCH "parisc")
+    set(BASEARCH_PARISC_FOUND TRUE)
+elseif("${ARCH}" MATCHES "rs6000")
+    set(BASEARCH "rs6000")
+    set(BASEARCH_RS6000_FOUND TRUE)
+else()
+    set(BASEARCH "x86")
+    set(BASEARCH_X86_FOUND TRUE)
+    message(STATUS "Basearch '${ARCH}' not recognized, defaulting to 'x86'.")
+endif()
+message(STATUS "Basearch of '${ARCH}' has been detected as: '${BASEARCH}'")