blob: 897f99a39a3c31b2683e6992a13ab50b04221f71 [file] [log] [blame]
Andrey Churbanov17cce422015-01-13 14:35:23 +00001#
2#//===----------------------------------------------------------------------===//
3#//
Chandler Carruth57b08b02019-01-19 10:56:40 +00004#// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5#// See https://llvm.org/LICENSE.txt for license information.
6#// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrey Churbanov17cce422015-01-13 14:35:23 +00007#//
8#//===----------------------------------------------------------------------===//
9#
10
11# Determine the architecture from predefined compiler macros
12# The architecture name can only contain alphanumeric characters and underscores (i.e., C identifier)
13
14# void get_architecture(string* return_arch)
15# - Returns the architecture in return_arch
Jonathan Peyton2e013352015-07-15 16:05:30 +000016function(libomp_get_architecture return_arch)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000017 set(detect_arch_src_txt "
Andrey Churbanov17cce422015-01-13 14:35:23 +000018 #if defined(__KNC__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000019 #error ARCHITECTURE=mic
Andrey Churbanov17cce422015-01-13 14:35:23 +000020 #elif defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000021 #error ARCHITECTURE=x86_64
Andrey Churbanov17cce422015-01-13 14:35:23 +000022 #elif defined(__i386) || defined(__i386__) || defined(__IA32__) || defined(_M_I86) || defined(_M_IX86) || defined(__X86__) || defined(_X86_)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000023 #error ARCHITECTURE=i386
Andrey Churbanov17cce422015-01-13 14:35:23 +000024 #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 +000025 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000026 #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 +000027 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000028 #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 +000029 #error ARCHITECTURE=arm
Jonathan Peyton2e013352015-07-15 16:05:30 +000030 #elif defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000031 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000032 #elif defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000033 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000034 #elif defined(__ARM_ARCH_2__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000035 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000036 #elif defined(__arm__) || defined(_M_ARM) || defined(_ARM)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000037 #error ARCHITECTURE=arm
Andrey Churbanov17cce422015-01-13 14:35:23 +000038 #elif defined(__aarch64__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000039 #error ARCHITECTURE=aarch64
Andrey Churbanov17cce422015-01-13 14:35:23 +000040 #elif defined(__powerpc64__) && defined(__LITTLE_ENDIAN__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000041 #error ARCHITECTURE=ppc64le
Andrey Churbanov17cce422015-01-13 14:35:23 +000042 #elif defined(__powerpc64__)
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000043 #error ARCHITECTURE=ppc64
Sylvestre Ledrucd9d3742016-12-08 09:22:24 +000044 #elif defined(__mips__) && defined(__mips64)
45 #error ARCHITECTURE=mips64
46 #elif defined(__mips__) && !defined(__mips64)
47 #error ARCHITECTURE=mips
Jonas Hahnfeld2488ae92019-07-25 14:36:20 +000048 #elif defined(__riscv) && __riscv_xlen == 64
49 #error ARCHITECTURE=riscv64
Andrey Churbanov17cce422015-01-13 14:35:23 +000050 #else
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000051 #error ARCHITECTURE=UnknownArchitecture
Andrey Churbanov17cce422015-01-13 14:35:23 +000052 #endif
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000053 ")
54 # Write out ${detect_arch_src_txt} to a file within the cmake/ subdirectory
55 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/libomp_detect_arch.c" ${detect_arch_src_txt})
Andrey Churbanov17cce422015-01-13 14:35:23 +000056
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000057 # Try to compile using the C Compiler. It will always error out with an #error directive, so store error output to ${local_architecture}
58 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 +000059
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000060 # Match the important architecture line and store only that matching string in ${local_architecture}
61 string(REGEX MATCH "ARCHITECTURE=([a-zA-Z0-9_]+)" local_architecture "${local_architecture}")
Andrey Churbanov17cce422015-01-13 14:35:23 +000062
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000063 # Get rid of the ARCHITECTURE= part of the string
64 string(REPLACE "ARCHITECTURE=" "" local_architecture "${local_architecture}")
Andrey Churbanov17cce422015-01-13 14:35:23 +000065
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000066 # set the return value to the architecture detected (e.g., 32e, 32, arm, ppc64, etc.)
67 set(${return_arch} "${local_architecture}" PARENT_SCOPE)
Andrey Churbanov17cce422015-01-13 14:35:23 +000068
Jonathan Peyton5b4acbd2015-07-15 16:57:19 +000069 # Remove ${detect_arch_src_txt} from cmake/ subdirectory
70 file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/libomp_detect_arch.c")
Andrey Churbanov17cce422015-01-13 14:35:23 +000071endfunction()