Debian Codegraph @

crt/aws-c-common/cmake/AwsFeatureTests.cmake

raw
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0.
3
4 include(CheckCSourceRuns)
5 include(AwsCFlags)
6
7 option(USE_CPU_EXTENSIONS "Whenever possible, use functions optimized for CPUs with specific extensions (ex: SSE, AVX)." ON)
8
9 # In the current (11/2/21) state of mingw64, the packaged gcc is not capable of emitting properly aligned avx2 instructions under certain circumstances.
10 # This leads to crashes for windows builds using mingw64 when invoking the avx2-enabled versions of certain functions. Until we can find a better
11 # work-around, disable avx2 (and all other extensions) in mingw builds.
12 #
13 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54412
14 #
15 if(MINGW)
16 message(STATUS "MINGW detected! Disabling avx2 and other CPU extensions")
17 set(USE_CPU_EXTENSIONS OFF)
18 endif()
19
20 if (USE_CPU_EXTENSIONS)
21 set(AWS_USE_CPU_EXTENSIONS ON)
22 endif()
23
24 if(NOT CMAKE_CROSSCOMPILING)
25 check_c_source_runs("
26 #include <stdbool.h>
27 bool foo(int a, int b, int *c) {
28 return __builtin_mul_overflow(a, b, c);
29 }
30
31 int main() {
32 int out;
33 if (foo(1, 2, &out)) {
34 return 0;
35 }
36
37 return 0;
38 }" AWS_HAVE_GCC_OVERFLOW_MATH_EXTENSIONS)
39 endif()
40
41 check_c_source_compiles("
42 #include <windows.h>
43 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
44 int main() {
45 return 0;
46 }
47 #else
48 it's not windows desktop
49 #endif
50 " AWS_HAVE_WINAPI_DESKTOP)
51
52 check_c_source_compiles("
53 int main() {
54 #if !(defined(__x86_64__) || defined(__i386__) || defined(_M_X64) || defined(_M_IX86))
55 # error \"not intel\"
56 #endif
57 return 0;
58 }
59 " AWS_ARCH_INTEL)
60
61 check_c_source_compiles("
62 int main() {
63 #if !(defined(__x86_64__) || defined(_M_X64))
64 # error \"not intel\"
65 #endif
66 return 0;
67 }
68 " AWS_ARCH_INTEL_X64)
69
70 check_c_source_compiles("
71 int main() {
72 #if !(defined(__aarch64__) || defined(_M_ARM64))
73 # error \"not arm64\"
74 #endif
75 return 0;
76 }
77 " AWS_ARCH_ARM64)
78
79 check_c_source_compiles("
80 int main() {
81 #if !(defined(__arm__) || defined(_M_ARM))
82 # error \"not arm\"
83 #endif
84 return 0;
85 }
86 " AWS_ARCH_ARM32)
87
88 check_c_source_compiles("
89 int main() {
90 int foo = 42, bar = 24;
91 __asm__ __volatile__(\"\":\"=r\"(foo):\"r\"(bar):\"memory\");
92 }" AWS_HAVE_GCC_INLINE_ASM)
93
94 check_c_source_compiles("
95 #include <sys/auxv.h>
96 int main() {
97 #ifdef __linux__
98 getauxval(AT_HWCAP);
99 getauxval(AT_HWCAP2);
100 #endif
101 return 0;
102 }" AWS_HAVE_AUXV)
103
104 string(REGEX MATCH "^(aarch64|arm)" ARM_CPU "${CMAKE_SYSTEM_PROCESSOR}")
105
106 if(NOT LEGACY_COMPILER_SUPPORT OR ARM_CPU)
107 check_c_source_compiles("
108 #include <execinfo.h>
109 #include <stdlib.h>
110 int main() {
111 backtrace(NULL, 0);
112 return 0;
113 }" AWS_HAVE_EXECINFO)
114 endif()
115
116 check_c_source_compiles("
117 #include <linux/if_link.h>
118 int main() {
119 return 1;
120 }" AWS_HAVE_LINUX_IF_LINK_H)
121
122 if(MSVC)
123 check_c_source_compiles("
124 #include <intrin.h>
125 int main() {
126 unsigned __int64 a = 0x0fffffffffffffffI64;
127 unsigned __int64 b = 0xf0000000I64;
128 unsigned __int64 c, d;
129 d = _umul128(a, b, &c);
130 return 0;
131 }" AWS_HAVE_MSVC_INTRINSICS_X64)
132 endif()
133
134 # This does a lot to detect when intrinsics are available and has to set cflags to do so.
135 # leave it in its own file for ease of managing it.
136 include(AwsSIMD)