From: Nathan Moinvaziri Date: Sun, 18 Jan 2026 03:12:44 +0000 (-0800) Subject: Convert arch detection from preprocessor errors to runtime with header X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ec2f43e910be013720da5d699669bd6b7298efac;p=thirdparty%2Fzlib-ng.git Convert arch detection from preprocessor errors to runtime with header --- diff --git a/arch/arm/adler32_neon.c b/arch/arm/adler32_neon.c index b09594736..2ef02398e 100644 --- a/arch/arm/adler32_neon.c +++ b/arch/arm/adler32_neon.c @@ -9,8 +9,8 @@ #ifdef ARM_NEON #include "zbuild.h" -#include "adler32_p.h" #include "neon_intrins.h" +#include "adler32_p.h" static const uint16_t ALIGNED_(64) taps[64] = { 64, 63, 62, 61, 60, 59, 58, 57, diff --git a/arch/arm/crc32_armv8.c b/arch/arm/crc32_armv8.c index 7d5d1b939..d42f5cc13 100644 --- a/arch/arm/crc32_armv8.c +++ b/arch/arm/crc32_armv8.c @@ -7,8 +7,8 @@ #ifdef ARM_CRC32 #include "zbuild.h" -#include "crc32.h" #include "acle_intrins.h" +#include "crc32.h" Z_INTERNAL Z_TARGET_CRC uint32_t crc32_armv8(uint32_t crc, const uint8_t *buf, size_t len) { uint32_t c = ~crc; diff --git a/arch/arm/slide_hash_armv6.c b/arch/arm/slide_hash_armv6.c index da22a993e..b241e6c5e 100644 --- a/arch/arm/slide_hash_armv6.c +++ b/arch/arm/slide_hash_armv6.c @@ -6,8 +6,8 @@ #ifdef ARM_SIMD #include "zbuild.h" -#include "deflate.h" #include "acle_intrins.h" +#include "deflate.h" /* SIMD version of hash_chain rebase */ static inline void slide_hash_chain(Pos *table, uint32_t entries, uint16_t wsize) { diff --git a/arch/arm/slide_hash_neon.c b/arch/arm/slide_hash_neon.c index 3b954d17e..e5a636bae 100644 --- a/arch/arm/slide_hash_neon.c +++ b/arch/arm/slide_hash_neon.c @@ -11,8 +11,8 @@ #ifdef ARM_NEON #include "zbuild.h" -#include "deflate.h" #include "neon_intrins.h" +#include "deflate.h" /* SIMD version of hash_chain rebase */ static inline void slide_hash_chain(Pos *table, uint32_t entries, uint16_t wsize) { diff --git a/cmake/detect-arch.c b/cmake/detect-arch.c index 6a33f8bb9..d9bc7c1ec 100644 --- a/cmake/detect-arch.c +++ b/cmake/detect-arch.c @@ -1,116 +1,14 @@ -// 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 +/* detect-arch.c -- Detect compiler architecture and print to stderr + * 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 +#include -// x86 -#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ - defined(__i386) || defined(_M_IX86) - #error archfound i686 +#include "../zarch.h" -// ARM -#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || defined(_M_ARM64EC) - #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__) - #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - #error archfound powerpc64le - #else - #error archfound powerpc64 - #endif - #else - #error archfound powerpc - #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 - -// RISC-V -#elif defined(__riscv) - #if __riscv_xlen == 64 - #error archfound riscv64 - #elif __riscv_xlen == 32 - #error archfound riscv32 - #endif - -// LOONGARCH -#elif defined(__loongarch_lp64) - #error archfound loongarch64 - -// Emscripten (WebAssembly) -#elif defined(__EMSCRIPTEN__) - #error archfound wasm32 - -// return 'unrecognized' if we do not know what architecture this is -#else - #error archfound unrecognized -#endif +int main(void) { + fprintf(stderr, "archfound " ARCH_NAME "\n"); + return 0; +} diff --git a/cmake/detect-arch.cmake b/cmake/detect-arch.cmake index f7ca6f1b8..bebb26f58 100644 --- a/cmake/detect-arch.cmake +++ b/cmake/detect-arch.cmake @@ -11,17 +11,17 @@ elseif(EMSCRIPTEN) elseif(CMAKE_CROSSCOMPILING) set(ARCH ${CMAKE_C_COMPILER_TARGET}) else() - # Let preprocessor parse archdetect.c and raise an error containing the arch identifier + # Compile and run detect-arch.c which prints architecture name to stderr try_run( run_result_unused compile_result_unused ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/detect-arch.c - COMPILE_OUTPUT_VARIABLE RAWOUTPUT CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} + RUN_OUTPUT_VARIABLE RAWOUTPUT ) - # Find basearch tag, and extract the arch word into BASEARCH variable + # Find archfound tag, and extract the arch word into ARCH variable string(REGEX REPLACE ".*archfound ([a-zA-Z0-9_]+).*" "\\1" ARCH "${RAWOUTPUT}") if(NOT ARCH) set(ARCH unknown) diff --git a/zarch.h b/zarch.h new file mode 100644 index 000000000..f53b26b34 --- /dev/null +++ b/zarch.h @@ -0,0 +1,195 @@ +/* zarch.h -- Detect compiler architecture and define ARCH_* macros + * Copyright (C) 2019 Hans Kristian Rosbach + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#ifndef ZARCH_H +#define ZARCH_H + +/* x86_64 */ +#if defined(__x86_64__) || defined(_M_X64) +# define ARCH_X86 +# define ARCH_64BIT +# define ARCH_NAME "x86_64" + +/* x86 (32-bit) */ +#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || \ + defined(__i386) || defined(_M_IX86) +# define ARCH_X86 +# define ARCH_32BIT +# define ARCH_NAME "i686" + +/* ARM 64-bit */ +#elif defined(__aarch64__) || defined(__arm64__) || defined(_M_ARM64) || defined(_M_ARM64EC) +# define ARCH_ARM +# define ARCH_64BIT +# define ARCH_NAME "aarch64" + +/* ARM 32-bit */ +#elif defined(__arm__) || defined(__arm) || defined(_M_ARM) || defined(__TARGET_ARCH_ARM) +# define ARCH_ARM +# define ARCH_32BIT +# if defined(__ARM64_ARCH_8__) || defined(__ARM_ARCH_8A__) || defined(__ARMv8__) || defined(__ARMv8_A__) +# define ARCH_NAME "armv8" +# elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) +# define ARCH_NAME "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__) +# define ARCH_NAME "armv6" +# elif defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__) +# define ARCH_NAME "armv5" +# elif defined(__ARM_ARCH_4T__) +# define ARCH_NAME "armv4" +# elif defined(__ARM_ARCH_3__) +# define ARCH_NAME "armv3" +# elif defined(__ARM_ARCH_2__) +# define ARCH_NAME "armv2" +# else +# define ARCH_NAME "arm" +# endif + +/* PowerPC */ +#elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) +# define ARCH_POWER +# if defined(__64BIT__) || defined(__powerpc64__) || defined(__ppc64__) +# define ARCH_64BIT +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define ARCH_NAME "powerpc64le" +# else +# define ARCH_NAME "powerpc64" +# endif +# else +# define ARCH_32BIT +# define ARCH_NAME "powerpc" +# endif + +/* --------------- Less common architectures alphabetically below --------------- */ + +/* ALPHA */ +#elif defined(__alpha__) || defined(__alpha) +# define ARCH_ALPHA +# define ARCH_64BIT +# define ARCH_NAME "alpha" + +/* Blackfin */ +#elif defined(__BFIN__) +# define ARCH_BLACKFIN +# define ARCH_32BIT +# define ARCH_NAME "blackfin" + +/* Itanium */ +#elif defined(__ia64) || defined(_M_IA64) +# define ARCH_IA64 +# define ARCH_64BIT +# define ARCH_NAME "ia64" + +/* MIPS */ +#elif defined(__mips__) || defined(__mips) +# define ARCH_MIPS +# if defined(__mips64) || defined(__mips64__) +# define ARCH_64BIT +# define ARCH_NAME "mips64" +# else +# define ARCH_32BIT +# define ARCH_NAME "mips" +# endif + +/* Motorola 68000-series */ +#elif defined(__m68k__) +# define ARCH_M68K +# define ARCH_32BIT +# define ARCH_NAME "m68k" + +/* SuperH */ +#elif defined(__sh__) +# define ARCH_SH +# define ARCH_32BIT +# define ARCH_NAME "sh" + +/* SPARC */ +#elif defined(__sparc__) || defined(__sparc) +# define ARCH_SPARC +# if defined(__sparcv9) || defined(__sparc_v9__) || defined(__sparc64__) +# define ARCH_64BIT +# define ARCH_NAME "sparc9" +# elif defined(__sparcv8) || defined(__sparc_v8__) +# define ARCH_32BIT +# define ARCH_NAME "sparc8" +# else +# define ARCH_32BIT +# define ARCH_NAME "sparc" +# endif + +/* SystemZ / S390 */ +#elif defined(__s390x) || defined(__zarch__) +# define ARCH_S390 +# define ARCH_64BIT +# define ARCH_NAME "s390x" +#elif defined(__s390__) +# define ARCH_S390 +# define ARCH_32BIT +# define ARCH_NAME "s390" +#elif defined(__370__) +# define ARCH_S390 +# define ARCH_32BIT +# define ARCH_NAME "s370" + +/* PARISC */ +#elif defined(__hppa__) +# define ARCH_PARISC +# if defined(__hppa64__) || defined(__LP64__) +# define ARCH_64BIT +# define ARCH_NAME "parisc64" +# else +# define ARCH_32BIT +# define ARCH_NAME "parisc" +# endif + +/* RS-6000 */ +#elif defined(__THW_RS6000) +# define ARCH_RS6000 +# define ARCH_32BIT +# define ARCH_NAME "rs6000" + +/* RISC-V */ +#elif defined(__riscv) +# define ARCH_RISCV +# if __riscv_xlen == 64 +# define ARCH_64BIT +# define ARCH_NAME "riscv64" +# elif __riscv_xlen == 32 +# define ARCH_32BIT +# define ARCH_NAME "riscv32" +# else +# define ARCH_NAME "riscv" +# endif + +/* LOONGARCH */ +#elif defined(__loongarch_lp64) +# define ARCH_LOONGARCH +# define ARCH_64BIT +# define ARCH_NAME "loongarch64" + +/* Emscripten (WebAssembly) */ +#elif defined(__EMSCRIPTEN__) +# define ARCH_WASM +# if defined(__wasm64__) +# define ARCH_64BIT +# define ARCH_NAME "wasm64" +# else +# define ARCH_32BIT +# define ARCH_NAME "wasm32" +# endif + +/* Unrecognized architecture */ +#else +# if defined(__LP64__) || defined(_LP64) || defined(_WIN64) +# define ARCH_64BIT +# else +# define ARCH_32BIT +# endif +# define ARCH_NAME "unrecognized" +#endif + +#endif /* ZARCH_H */