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