summaryrefslogtreecommitdiff
path: root/cmake
diff options
context:
space:
mode:
authorHans Kristian Rosbach <hk-git@circlestorm.org>2019-09-20 20:35:44 +0200
committerHans Kristian Rosbach <hk-github@circlestorm.org>2019-09-21 22:10:46 +0200
commiteb8cb583638395db8d6435c2756ec016d3b74d6c (patch)
tree5fdd24d986f96d867ea08e0117815bdf8c3bdb02 /cmake
parent852dad9de9c77251d6623785ed9e1974a3e79e8f (diff)
Add archdetect as separate cmake include file.
Simplify architecture checking by setting BASEARCH in addition to ARCH.
Diffstat (limited to 'cmake')
-rw-r--r--cmake/archdetect.c95
-rw-r--r--cmake/archdetect.cmake92
2 files changed, 187 insertions, 0 deletions
diff --git a/cmake/archdetect.c b/cmake/archdetect.c
new file mode 100644
index 0000000..f8feb88
--- /dev/null
+++ b/cmake/archdetect.c
@@ -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
index 0000000..9f8d1c1
--- /dev/null
+++ b/cmake/archdetect.cmake
@@ -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}'")