1+ # Modified from https://github.com/axr/solar-cmake/blob/73cfea0db0284c5e2010aca23989046e5bda95c9/Solar.cmake
2+ # Based on the Qt 5 processor detection code, so should be very accurate
3+ # https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/global/qprocessordetection.h
4+ # Currently handles arm (v5, v6, v7), x86 (32/64), ia64, and ppc (32/64)
5+
6+ # Regarding POWER/PowerPC, just as is noted in the Qt source,
7+ # "There are many more known variants/revisions that we do not handle/detect."
8+
9+ set (archdetect_c_code "
10+ #if defined(__arm__) || defined(__TARGET_ARCH_ARM) || defined(_M_ARM) || defined(_M_ARM64) || defined(__aarch64__) || defined(__ARM64__)
11+ #if defined(__ARM_ARCH_8__) || defined(__ARM_ARCH_8) \\
12+ || defined(__ARM_ARCH_8A__) || defined(__ARM_ARCH_8A) \\
13+ || defined(__ARM_ARCH_8R__) || defined(__ARM_ARCH_8R) \\
14+ || defined(__ARM_ARCH_8M__) || defined(__ARM_ARCH_8M) \\
15+ || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 8)
16+ #error cmake_ARCH armv8
17+ #elif defined(__ARM_ARCH_7__) \\
18+ || defined(__ARM_ARCH_7A__) \\
19+ || defined(__ARM_ARCH_7R__) \\
20+ || defined(__ARM_ARCH_7M__) \\
21+ || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
22+ #error cmake_ARCH armv7
23+ #elif defined(__ARM_ARCH_6__) \\
24+ || defined(__ARM_ARCH_6J__) \\
25+ || defined(__ARM_ARCH_6T2__) \\
26+ || defined(__ARM_ARCH_6Z__) \\
27+ || defined(__ARM_ARCH_6K__) \\
28+ || defined(__ARM_ARCH_6ZK__) \\
29+ || defined(__ARM_ARCH_6M__) \\
30+ || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
31+ #error cmake_ARCH armv6
32+ #elif defined(__ARM_ARCH_5TEJ__) \\
33+ || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
34+ #error cmake_ARCH armv5
35+ #else
36+ #error cmake_ARCH arm
37+ #endif
38+ #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
39+ #error cmake_ARCH i386
40+ #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
41+ #error cmake_ARCH x86_64
42+ #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
43+ #error cmake_ARCH ia64
44+ #elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \\
45+ || defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) \\
46+ || defined(_M_MPPC) || defined(_M_PPC)
47+ #if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__)
48+ #error cmake_ARCH ppc64
49+ #else
50+ #error cmake_ARCH ppc
51+ #endif
52+ #endif
53+
54+ #error cmake_ARCH unknown
55+ " )
56+
57+
58+ # Set ppc_support to TRUE before including this file or ppc and ppc64
59+ # will be treated as invalid architectures since they are no longer supported by Apple
60+
61+ function (target_architecture output_var)
62+ if (APPLE AND CMAKE_OSX_ARCHITECTURES)
63+ # On OS X we use CMAKE_OSX_ARCHITECTURES *if* it was set
64+ # First let's normalize the order of the values
65+
66+ # Note that it's not possible to compile PowerPC applications if you are using
67+ # the OS X SDK version 10.6 or later - you'll need 10.4/10.5 for that, so we
68+ # disable it by default
69+ # See this page for more information:
70+ # http://stackoverflow.com/questions/5333490/how-can-we-restore-ppc-ppc64-as-well-as-full-10-4-10-5-sdk-support-to-xcode-4
71+
72+ # Architecture defaults to i386 or ppc on OS X 10.5 and earlier, depending on the CPU type detected at runtime.
73+ # On OS X 10.6+ the default is x86_64 if the CPU supports it, i386 otherwise.
74+
75+ foreach (osx_arch ${CMAKE_OSX_ARCHITECTURES} )
76+ if ("${osx_arch} " STREQUAL "ppc" AND ppc_support)
77+ set (osx_arch_ppc TRUE )
78+ elseif ("${osx_arch} " STREQUAL "i386" )
79+ set (osx_arch_i386 TRUE )
80+ elseif ("${osx_arch} " STREQUAL "x86_64" )
81+ set (osx_arch_x86_64 TRUE )
82+ elseif ("${osx_arch} " STREQUAL "ppc64" AND ppc_support)
83+ set (osx_arch_ppc64 TRUE )
84+ else ()
85+ message (FATAL_ERROR "Invalid OS X arch name: ${osx_arch} " )
86+ endif ()
87+ endforeach ()
88+
89+ # Now add all the architectures in our normalized order
90+ if (osx_arch_ppc)
91+ list (APPEND ARCH ppc)
92+ endif ()
93+
94+ if (osx_arch_i386)
95+ list (APPEND ARCH i386)
96+ endif ()
97+
98+ if (osx_arch_x86_64)
99+ list (APPEND ARCH x86_64)
100+ endif ()
101+
102+ if (osx_arch_ppc64)
103+ list (APPEND ARCH ppc64)
104+ endif ()
105+ else ()
106+ file (WRITE "${CMAKE_BINARY_DIR} /arch.c" "${archdetect_c_code} " )
107+
108+ enable_language (C)
109+
110+ # Detect the architecture in a rather creative way...
111+ # This compiles a small C program which is a series of ifdefs that selects a
112+ # particular #error preprocessor directive whose message string contains the
113+ # target architecture. The program will always fail to compile (both because
114+ # file is not a valid C program, and obviously because of the presence of the
115+ # #error preprocessor directives... but by exploiting the preprocessor in this
116+ # way, we can detect the correct target architecture even when cross-compiling,
117+ # since the program itself never needs to be run (only the compiler/preprocessor)
118+ try_run (
119+ run_result_unused
120+ compile_result_unused
121+ "${CMAKE_BINARY_DIR} "
122+ "${CMAKE_BINARY_DIR} /arch.c"
123+ COMPILE_OUTPUT_VARIABLE ARCH
124+ CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
125+ )
126+
127+ # Parse the architecture name from the compiler output
128+ string (REGEX MATCH "cmake_ARCH ([a-zA-Z0-9_]+)" ARCH "${ARCH} " )
129+
130+ # Get rid of the value marker leaving just the architecture name
131+ string (REPLACE "cmake_ARCH " "" ARCH "${ARCH} " )
132+
133+ # If we are compiling with an unknown architecture this variable should
134+ # already be set to "unknown" but in the case that it's empty (i.e. due
135+ # to a typo in the code), then set it to unknown
136+ if (NOT ARCH)
137+ set (ARCH unknown)
138+ endif ()
139+ endif ()
140+
141+ set (${output_var} "${ARCH} " PARENT_SCOPE)
142+ endfunction ()
0 commit comments