comparison cmake/TargetArch.cmake @ 0:a4671277546c tip

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