-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathcompiler.m4
215 lines (202 loc) · 6.52 KB
/
compiler.m4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# compiler.m4 - Configure compiler capabilities
#
# SPDX-FileCopyrightText: NVIDIA CORPORATION & AFFILIATES
# Copyright (c) 2001-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: GPL-2.0-only or BSD-3-Clause
# See file LICENSE for terms.
#
# Check compiler specific attributes
# Usage: CHECK_COMPILER_ATTRIBUTE([attribute], [program], [definition])
# Note:
# - [definition] can be omitted if it is equal to attribute
#
AC_DEFUN([CHECK_COMPILER_ATTRIBUTE], [
AC_CACHE_VAL(vma_cv_attribute_[$1], [
#
# Try to compile using the C compiler
#
AC_TRY_COMPILE([$2],[],
[vma_cv_attribute_$1=yes],
[vma_cv_attribute_$1=no])
AS_IF([test "x$vma_cv_attribute_$1" = "xyes"], [
AC_LANG_PUSH(C++)
AC_TRY_COMPILE([extern "C" {
$2
}],[],
[vma_cv_attribute_$1=yes],
[vma_cv_attribute_$1=no])
AC_LANG_POP(C++)
])
])
AC_MSG_CHECKING([for attribute $1])
AC_MSG_RESULT([$vma_cv_attribute_$1])
AS_IF([test "x$vma_cv_attribute_$1" = "xyes"], [
AS_IF([test "x$3" = "x"],
[AC_DEFINE_UNQUOTED([HAVE_$1], [1], [Define to 1 if attribute $1 is supported])],
[AC_DEFINE_UNQUOTED([HAVE_$3], [1], [Define to 1 if attribute $1 is supported])]
)
])
])
# Check compiler for the specified version of the C++ standard
# Usage: CHECK_COMPILER_CXX([standard], [option], [definition])
# Note:
# - [definition] can be omitted if it is equal to attribute
#
AC_DEFUN([CHECK_COMPILER_CXX], [
case "$1" in
11)
m4_define([_vma_cv_compiler_body_11], [[
#ifndef __cplusplus
#error This is not a C++ compiler
#elif __cplusplus < 201103L
#error This is not a C++11 compiler
#else
#include <iostream>
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
/* decltype */
int a = 5;
decltype(a) b = a;
return (b - a);
}
#endif // __cplusplus >= 201103L
]])
;;
14)
m4_define([_vma_cv_compiler_body_14], [[
#ifndef __cplusplus
#error This is not a C++ compiler
#elif __cplusplus < 201402L
#error This is not a C++14 compiler
#else
#include <iostream>
int main(int argc, char** argv)
{
(void)argc;
(void)argv;
/* Binary integer literals */
constexpr auto i = 0b0000000000101010;
static_assert(i == 42, "wrong value");
return 0;
}
#endif // __cplusplus >= 201402L
]])
;;
*)
AC_MSG_ERROR([invalid first argument as [$1] to [$0]])
;;
esac
case "$2" in
std)
vma_cv_option=-std=c++$1
;;
gnu)
vma_cv_option=-std=gnu++$1
;;
*)
AC_MSG_ERROR([invalid first argument as [$2] to [$0]])
;;
esac
AC_CACHE_VAL(vma_cv_compiler_cxx_[$1], [
vma_cv_compiler_save_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$vma_cv_option $CXXFLAGS"
#
# Try to compile using the C++ compiler
#
AC_LANG_PUSH(C++)
AC_COMPILE_IFELSE([AC_LANG_SOURCE(_vma_cv_compiler_body_[$1])],
[vma_cv_compiler_cxx_$1=yes],
[vma_cv_compiler_cxx_$1=no])
AC_LANG_POP(C++)
CXXFLAGS="$vma_cv_compiler_save_CXXFLAGS"
])
AC_MSG_CHECKING([for compiler c++ [$1]])
AC_MSG_RESULT([$vma_cv_compiler_cxx_$1])
AS_IF([test "x$vma_cv_compiler_cxx_[$1]" = "xyes"],
[CXXFLAGS="$vma_cv_option $CXXFLAGS"],
[AC_MSG_ERROR([A compiler with support for C++[$1] language features is required])]
)
])
##########################
# Configure compiler capabilities
#
AC_DEFUN([COMPILER_CAPABILITY_SETUP],
[
AC_MSG_CHECKING([for compiler])
CFLAGS="-D_GNU_SOURCE -fPIC $CFLAGS"
CXXFLAGS="-D_GNU_SOURCE -fPIC $CXXFLAGS"
case $CC in
gcc*|g++*)
AC_MSG_RESULT([gcc])
CFLAGS="$CFLAGS -Wall -Wextra -Werror -Wundef \
-ffunction-sections -fdata-sections -Wsequence-point -pipe -Winit-self -Wmissing-include-dirs \
-Wno-free-nonheap-object "
CXXFLAGS="$CXXFLAGS -Wshadow -Wall -Wextra -Werror -Wundef \
-ffunction-sections -fdata-sections -Wsequence-point -pipe -Winit-self -Wmissing-include-dirs \
-Wno-free-nonheap-object -Wno-overloaded-virtual "
;;
icc*|icpc*)
AC_MSG_RESULT([icc])
CFLAGS="$CFLAGS -Wall -Werror"
CXXFLAGS="$CXXFLAGS -Wall -Werror"
;;
clang*|clang++*)
AC_MSG_RESULT([clang])
CFLAGS="$CFLAGS -Wall -Werror -Wno-self-assign"
CXXFLAGS="$CXXFLAGS -Wall -Werror -Wno-overloaded-virtual"
# workaround for clang w/o -Wnon-c-typedef-for-linkage
CXXFLAGS="$CXXFLAGS -Wno-unknown-warning-option -Wno-non-c-typedef-for-linkage"
;;
*)
AC_MSG_RESULT([unknown])
;;
esac
# Control debug support
#
AC_ARG_ENABLE([debug],
AC_HELP_STRING([--enable-debug],
[Enable debug mode (default=no)]), [], [enable_debug=no])
AC_MSG_CHECKING(
[for debug support])
if test "x$enable_debug" = xyes; then
AC_MSG_RESULT([yes])
CFLAGS="$CFLAGS -g -D_DEBUG"
CXXFLAGS="$CXXFLAGS -g -D_DEBUG"
else
AC_MSG_RESULT([no])
CFLAGS="$CFLAGS -g -O3 -DNDEBUG"
CXXFLAGS="$CXXFLAGS -g -O3 -DNDEBUG"
fi
# Control symbols visibility
#
AC_ARG_ENABLE([symbol_visibility],
AC_HELP_STRING([--enable-symbol-visibility],
[Enable symbols visibility (default=no)]), [], [enable_symbol_visibility=no])
if test "x$enable_symbol_visibility" = xno; then
CHECK_COMPILER_ATTRIBUTE([visibility],
[extern __attribute__((__visibility__("hidden"))) int hiddenvar;
extern __attribute__((__visibility__("default"))) int exportedvar;
extern __attribute__((__visibility__("hidden"))) int hiddenfunc (void);
extern __attribute__((__visibility__("default"))) int exportedfunc (void);
void dummyfunc (void) {}],
[ATTRIBUTE_VISIBILITY])
AC_MSG_CHECKING([for symbols visibility])
AS_IF([test "x$vma_cv_attribute_visibility" = "xyes"],
[CXXFLAGS="$CXXFLAGS -fvisibility=hidden"
CFLAGS="$CFLAGS -fvisibility=hidden"
AC_DEFINE_UNQUOTED([DEFINED_EXPORT_SYMBOL], [1], [Define to 1 to hide symbols])
AC_MSG_RESULT([no])
],
[
AC_MSG_RESULT([yes])
AC_MSG_WARN([A compiler with -fvisibility option is required for this feature])
]
)
else
AC_MSG_CHECKING([for symbols visibility])
AC_MSG_RESULT([yes])
fi
CHECK_COMPILER_CXX([11], [std], [])
])