1# Original file location https://github.com/petroules/solar-cmake/blob/master/TargetArch.cmake
2#Copyright (c) 2012 Petroules Corporation. All rights reserved.
3#Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4#
5# 1    Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6# 2    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
8#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9# Based on the Qt 5 processor detection code, so should be very accurate
10# https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/global/qprocessordetection.h
11# Currently handles arm (v5, v6, v7), x86 (32/64), ia64, and ppc (32/64)
12
13# Regarding POWER/PowerPC, just as is noted in the Qt source,
14# "There are many more known variants/revisions that we do not handle/detect."
15
16set(archdetect_c_code "
17#if defined(__arm__) || defined(__TARGET_ARCH_ARM)
18    #if defined(__ARM_ARCH_7__) \\
19        || defined(__ARM_ARCH_7A__) \\
20        || defined(__ARM_ARCH_7R__) \\
21        || defined(__ARM_ARCH_7M__) \\
22        || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
23        #error cmake_ARCH armv7
24    #elif defined(__ARM_ARCH_6__) \\
25        || defined(__ARM_ARCH_6J__) \\
26        || defined(__ARM_ARCH_6T2__) \\
27        || defined(__ARM_ARCH_6Z__) \\
28        || defined(__ARM_ARCH_6K__) \\
29        || defined(__ARM_ARCH_6ZK__) \\
30        || defined(__ARM_ARCH_6M__) \\
31        || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
32        #error cmake_ARCH armv6
33    #elif defined(__ARM_ARCH_5TEJ__) \\
34        || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
35        #error cmake_ARCH armv5
36    #else
37        #error cmake_ARCH arm
38    #endif
39#elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
40    #error cmake_ARCH i386
41#elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
42    #error cmake_ARCH x86_64
43#elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
44    #error cmake_ARCH ia64
45#elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \\
46      || defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC)  \\
47      || defined(_M_MPPC) || defined(_M_PPC)
48    #if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__)
49        #error cmake_ARCH ppc64
50    #else
51        #error cmake_ARCH ppc
52    #endif
53#endif
54
55#error cmake_ARCH unknown
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
61function(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)
142endfunction()
143