blob: 536b488405a9629ed16fccc562110d67ac56a18c [file] [log] [blame]
Andrey Churbanov17cce422015-01-13 14:35:23 +00001#
2#//===----------------------------------------------------------------------===//
3#//
4#// The LLVM Compiler Infrastructure
5#//
6#// This file is dual licensed under the MIT and the University of Illinois Open
7#// Source Licenses. See LICENSE.txt for details.
8#//
9#//===----------------------------------------------------------------------===//
10#
11
12# Determine the architecture from predefined compiler macros
13# The architecture name can only contain alphanumeric characters and underscores (i.e., C identifier)
14
15# void get_architecture(string* return_arch)
16# - Returns the architecture in return_arch
Jonathan Peyton2e013352015-07-15 16:05:30 +000017function(libomp_get_architecture return_arch)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000018 set(detect_arch_src_txt "
Andrey Churbanov17cce422015-01-13 14:35:23 +000019 #if defined(__KNC__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000020 #error ARCHITECTURE=mic
Andrey Churbanov17cce422015-01-13 14:35:23 +000021 #elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000022 #error ARCHITECTURE=x86_64
Andrey Churbanov17cce422015-01-13 14:35:23 +000023 #elif defined(__i386) || defined(__i386__) || defined(__IA32__) || defined(_M_I86) || defined(_M_IX86) || defined(__X86__) || defined(_X86_)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000024 #error ARCHITECTURE=i386
Andrey Churbanov17cce422015-01-13 14:35:23 +000025 #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000026 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000027 #elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6ZK__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000028 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000029 #elif defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__) || defined(__ARM_ARCH_5TEJ__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000030 #error ARCHITECTURE=arm
Jonathan Peyton2e013352015-07-15 16:05:30 +000031 #elif defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000032 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000033 #elif defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000034 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000035 #elif defined(__ARM_ARCH_2__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000036 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000037 #elif defined(__arm__) || defined(_M_ARM) || defined(_ARM)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000038 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000039 #elif defined(__aarch64__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000040 #error ARCHITECTURE=aarch64
Andrey Churbanov17cce422015-01-13 14:35:23 +000041 #elif defined(__powerpc64__) && defined(__LITTLE_ENDIAN__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000042 #error ARCHITECTURE=ppc64le
Andrey Churbanov17cce422015-01-13 14:35:23 +000043 #elif defined(__powerpc64__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000044 #error ARCHITECTURE=ppc64
Sylvestre Ledrucd9d3742016-12-08 09:22:24 +000045 #elif defined(__mips__) && defined(__mips64)
46 #error ARCHITECTURE=mips64
47 #elif defined(__mips__) && !defined(__mips64)
48 #error ARCHITECTURE=mips
Andrey Churbanov17cce422015-01-13 14:35:23 +000049 #else
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000050 #error ARCHITECTURE=UnknownArchitecture
Andrey Churbanov17cce422015-01-13 14:35:23 +000051 #endif
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000052 ")
53 # Write out ${detect_arch_src_txt} to a file within the cmake/ subdirectory
54 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/libomp_detect_arch.c" ${detect_arch_src_txt})
Andrey Churbanov17cce422015-01-13 14:35:23 +000055
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000056 # Try to compile using the C Compiler. It will always error out with an #error directive, so store error output to ${local_architecture}
57 try_run(run_dummy compile_dummy "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/libomp_detect_arch.c" COMPILE_OUTPUT_VARIABLE local_architecture)
Andrey Churbanov17cce422015-01-13 14:35:23 +000058
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000059 # Match the important architecture line and store only that matching string in ${local_architecture}
60 string(REGEX MATCH "ARCHITECTURE=([a-zA-Z0-9_]+)" local_architecture "${local_architecture}")
Andrey Churbanov17cce422015-01-13 14:35:23 +000061
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000062 # Get rid of the ARCHITECTURE= part of the string
63 string(REPLACE "ARCHITECTURE=" "" local_architecture "${local_architecture}")
Andrey Churbanov17cce422015-01-13 14:35:23 +000064
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000065 # set the return value to the architecture detected (e.g., 32e, 32, arm, ppc64, etc.)
66 set(${return_arch} "${local_architecture}" PARENT_SCOPE)
Andrey Churbanov17cce422015-01-13 14:35:23 +000067
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000068 # Remove ${detect_arch_src_txt} from cmake/ subdirectory
69 file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/libomp_detect_arch.c")
Andrey Churbanov17cce422015-01-13 14:35:23 +000070endfunction()