Uses different pass count for different parallel queue test cases
[libcds.git] / build / cmake / TargetArch.cmake
1 # Source: https://github.com/axr/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)
11     #if defined(__ARM_ARCH_7__) \\
12         || defined(__ARM_ARCH_7A__) \\
13         || defined(__ARM_ARCH_7R__) \\
14         || defined(__ARM_ARCH_7M__) \\
15         || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
16         #error cmake_ARCH armv7
17     #elif defined(__ARM_ARCH_6__) \\
18         || defined(__ARM_ARCH_6J__) \\
19         || defined(__ARM_ARCH_6T2__) \\
20         || defined(__ARM_ARCH_6Z__) \\
21         || defined(__ARM_ARCH_6K__) \\
22         || defined(__ARM_ARCH_6ZK__) \\
23         || defined(__ARM_ARCH_6M__) \\
24         || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
25         #error cmake_ARCH armv6
26     #elif defined(__ARM_ARCH_5TEJ__) \\
27         || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
28         #error cmake_ARCH armv5
29     #else
30         #error cmake_ARCH arm
31     #endif
32 #elif defined(__aarch64__)
33     #if defined(__ARM_ARCH) && __ARM_ARCH == 8
34         #error cmake_ARCH armv8
35     #else
36         #error cmake_ARCH arm64
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 # Set ppc_support to TRUE before including this file or ppc and ppc64
58 # will be treated as invalid architectures since they are no longer supported by Apple
59
60 function(target_architecture output_var)
61     if(APPLE AND CMAKE_OSX_ARCHITECTURES)
62         # On OS X we use CMAKE_OSX_ARCHITECTURES *if* it was set
63         # First let's normalize the order of the values
64
65         # Note that it's not possible to compile PowerPC applications if you are using
66         # the OS X SDK version 10.6 or later - you'll need 10.4/10.5 for that, so we
67         # disable it by default
68         # See this page for more information:
69         # http://stackoverflow.com/questions/5333490/how-can-we-restore-ppc-ppc64-as-well-as-full-10-4-10-5-sdk-support-to-xcode-4
70
71         # Architecture defaults to i386 or ppc on OS X 10.5 and earlier, depending on the CPU type detected at runtime.
72         # On OS X 10.6+ the default is x86_64 if the CPU supports it, i386 otherwise.
73
74         foreach(osx_arch ${CMAKE_OSX_ARCHITECTURES})
75             if("${osx_arch}" STREQUAL "ppc" AND ppc_support)
76                 set(osx_arch_ppc TRUE)
77             elseif("${osx_arch}" STREQUAL "i386")
78                 set(osx_arch_i386 TRUE)
79             elseif("${osx_arch}" STREQUAL "x86_64")
80                 set(osx_arch_x86_64 TRUE)
81             elseif("${osx_arch}" STREQUAL "ppc64" AND ppc_support)
82                 set(osx_arch_ppc64 TRUE)
83             else()
84                 message(FATAL_ERROR "Invalid OS X arch name: ${osx_arch}")
85             endif()
86         endforeach()
87
88         # Now add all the architectures in our normalized order
89         if(osx_arch_ppc)
90             list(APPEND ARCH ppc)
91         endif()
92
93         if(osx_arch_i386)
94             list(APPEND ARCH i386)
95         endif()
96
97         if(osx_arch_x86_64)
98             list(APPEND ARCH x86_64)
99         endif()
100
101         if(osx_arch_ppc64)
102             list(APPEND ARCH ppc64)
103         endif()
104     else()
105         file(WRITE "${CMAKE_BINARY_DIR}/arch.c" "${archdetect_c_code}")
106
107         enable_language(C)
108
109         # Detect the architecture in a rather creative way...
110         # This compiles a small C program which is a series of ifdefs that selects a
111         # particular #error preprocessor directive whose message string contains the
112         # target architecture. The program will always fail to compile (both because
113         # file is not a valid C program, and obviously because of the presence of the
114         # #error preprocessor directives... but by exploiting the preprocessor in this
115         # way, we can detect the correct target architecture even when cross-compiling,
116         # since the program itself never needs to be run (only the compiler/preprocessor)
117         try_run(
118             run_result_unused
119             compile_result_unused
120             "${CMAKE_BINARY_DIR}"
121             "${CMAKE_BINARY_DIR}/arch.c"
122             COMPILE_OUTPUT_VARIABLE ARCH
123             CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
124         )
125
126         # Parse the architecture name from the compiler output
127         string(REGEX MATCH "cmake_ARCH ([a-zA-Z0-9_]+)" ARCH "${ARCH}")
128
129         # Get rid of the value marker leaving just the architecture name
130         string(REPLACE "cmake_ARCH " "" ARCH "${ARCH}")
131
132         # If we are compiling with an unknown architecture this variable should
133         # already be set to "unknown" but in the case that it's empty (i.e. due
134         # to a typo in the code), then set it to unknown
135         if (NOT ARCH)
136             set(ARCH unknown)
137         endif()
138     endif()
139
140     set(${output_var} "${ARCH}" PARENT_SCOPE)
141 endfunction()