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