Skip to content

Commit

Permalink
Add archdetect as separate cmake include file.
Browse files Browse the repository at this point in the history
Simplify architecture checking by setting BASEARCH in addition to ARCH.
  • Loading branch information
Dead2 committed Sep 21, 2019
1 parent 852dad9 commit eb8cb58
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ foo.gz
CMakeCache.txt
CMakeFiles
Testing
*.cmake
/*.cmake
*.stackdump
*._h
zconf.h
Expand Down
107 changes: 46 additions & 61 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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
#
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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})
Expand All @@ -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()

Expand Down
95 changes: 95 additions & 0 deletions cmake/archdetect.c
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit eb8cb58

Please sign in to comment.