Repository navigation
Expand file tree
/
Copy pathHIPOBJClangCompilerOptions.cmake
More file actions
333 lines (295 loc) · 9.14 KB
/
Copy pathHIPOBJClangCompilerOptions.cmake
File metadata and controls
333 lines (295 loc) · 9.14 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
#
# SPDX-License-Identifier: MIT
# Warning flags for llvm/clang
#
# https://clang.llvm.org/docs/DiagnosticsReference.html
include_guard(GLOBAL)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)
include(HIPOBJFortifySource)
# Check whether calls to the C library's fortified functions trip
# -Wused-but-marked-unused
#
# With _FORTIFY_SOURCE, glibc 2.40 and later give clang printf-family
# wrappers that are declared static __attribute__((__unused__)), so
# every call to fprintf, snprintf, and the like warns. The result is
# cached, so the check only runs once.
function(hipobj_check_fortify_used_but_marked_unused outvar)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_FLAGS
"-O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -Werror=used-but-marked-unused")
check_cxx_source_compiles("
#include <cstdio>
int main()
{
char buf[8];
std::snprintf(buf, sizeof(buf), \"%d\", 1);
std::fprintf(stderr, \"%s\", buf);
return 0;
}" HIPOBJ_FORTIFY_ALLOWS_USED_BUT_MARKED_UNUSED)
cmake_pop_check_state()
set(${outvar} ${HIPOBJ_FORTIFY_ALLOWS_USED_BUT_MARKED_UNUSED} PARENT_SCOPE)
endfunction()
function(hipobj_get_clang_warning_flags outvar compiler_version)
# Warning flags for clang 17 and earlier
set(flags
# Basic "high" warning levels
-Wall
-Wextra
# Avoid non-standard C/C++ behavior
-pedantic
# Suggestions from the Effective C++ book
-Weffc++
# Build with this until it becomes a problem
# -Wnvcc-compat requires clang 19 and is handled below
-Wcuda-compat
# Don't bake gcc-isms into the code. clang will usually deal with
# this but it's not a good idea.
-Wgnu
# Don't use deprecated constructs
-Wdeprecated
# Aggressively warn about thread-safety issues
# Requires markup: https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
-Wthread-safety
-Wthread-safety-beta
-Wthread-safety-negative
-Wthread-safety-verbose
# Turn this on later (generates a lot of noise now)
-Wdocumentation
# Avoid things that will cause problems on Windows
-Wmicrosoft
# Do not warn about incompatibility with earlier versions
# of C++. The pedantic versions suppress a larger set of
# warnings than the non-pedantic ones.
-Wno-c++98-compat-pedantic
-Wno-c++11-compat-pedantic
-Wno-c++14-compat-pedantic
-Wno-pre-c++17-compat-pedantic
# Turn on stack protection options
-fstack-clash-protection
-fstack-protector-strong # Consider -all instead of -strong
# Turn on strict flex arrays (helps ASAN, _FORTIFY_SOURCE, etc.)
-fstrict-flex-arrays=3
# Initialize local variables with a pattern
-ftrivial-auto-var-init=pattern
# Zero the registers a function used when it returns, so their
# contents don't outlive the call or serve as ROP gadgets
-fzero-call-used-regs=used-gpr
# Keep null pointer checks the optimizer could prove redundant, and
# don't assume that pointers to different types never alias
-fno-delete-null-pointer-checks
-fno-strict-aliasing
# Check the preconditions of libstdc++ calls (bounds, etc.)
-D_GLIBCXX_ASSERTIONS
# Misc warnings
#
# This includes most warnings that are not enabled by default
# with the exception of C++ 11/14/etc. and clang extension
# warnings since we only support one compiler with a defined
# language level
#
# This list is not set in stone and we should consider removing
# diagnostics that return a lot of false positives.
#
# Anything commented out with no comment isn't supported w/ clang
# versions in common use. We'll want to update this scheme with
# one that better respects versions in the future.
-Warc-repeated-use-of-weak
-Warray-bounds-pointer-arithmetic
-Wassign-enum
-Watomic-implicit-seq-cst
-Watomic-properties
-Wbad-function-cast
-Wbinary-literal
-Wcast-align
-Wcast-function-type
-Wcast-qual
-Wcomma
-Wconditional-uninitialized
-Wconsumed
-Wconversion
#-Wcovered-switch-default (flags default labels where we handle all enum values)
-Wcstring-format-directive
-Wctad-maybe-unsupported
-Wdate-time
-Wdirect-ivar-access
-Wdisabled-macro-expansion
-Wdouble-promotion
-Wdtor-name
-Wduplicate-enum
-Wduplicate-method-arg
-Wduplicate-method-match
-Wendif-labels
-Wexit-time-destructors
-Wexpansion-to-defined
-Wexplicit-ownership-type
-Wextra-semi
-Wextra-semi-stmt
-Wfloat-equal
-Wformat=2
-Wformat-non-iso
-Wformat-pedantic
-Wformat-security
-Wformat-type-confusion
-Wfour-char-constants
-Wfuse-ld-path
-Wglobal-constructors
-Wheader-hygiene
-Widiomatic-parentheses
-Wimplicit-fallthrough
-Wimplicit-retain-self
-Wincompatible-function-pointer-types-strict
-Wincomplete-module
-Winconsistent-missing-destructor-override
-Winvalid-or-nonexistent-directory
-Wlocal-type-template-args
-Wloop-analysis
-Wmain
-Wmain-return-type
-Wmax-tokens
-Wmethod-signatures
-Wmissing-include-dirs
-Wmissing-noreturn
-Wmissing-prototypes
-Wmissing-variable-declarations
-Wnarrowing
-Wnewline-eof
-Wnoexcept-type
-Wnon-gcc
-Wnon-virtual-dtor
-Wnonportable-system-include-path
-Wnullable-to-nonnull-conversion
-Wold-style-cast
-Wopenmp
-Wover-aligned
-Woverriding-method-mismatch
-Wpacked
#-Wpadded # Generates a LOT of unfixable noise
-Wpartial-availability
-Wpointer-arith
-Wpoison-system-directories
-Wpragmas
-Wquoted-include-in-framework-header
-Wreceiver-forward-class
-Wredundant-parens
-Wreserved-identifier
-Wsequence-point
-Wshadow-all
-Wshift-sign-overflow
-Wsigned-enum-bitfield
-Wsource-uses-openmp
-Wspir-compat
-Wstatic-in-inline
-Wstrict-potentially-direct-selector
-Wstrict-prototypes
-Wstrict-selector-match
-Wsuggest-destructor-override
-Wsuggest-override
-Wsuper-class-method-mismatch
-Wswitch-default
-Wswitch-enum
-Wtautological-constant-in-range-compare
-Wtype-limits
-Wunaligned-access
-Wundeclared-selector
-Wundef
-Wundef-prefix
-Wundefined-func-template
-Wundefined-reinterpret-cast
-Wunguarded-availability
-Wunnamed-type-template-args
-Wunreachable-code-aggressive
-Wused-but-marked-unused
-Wvariadic-macros
-Wvector-conversion
-Wvla
-Wno-weak-vtables # Lots of noise in gtest code, so turn off for now
-Wzero-as-null-pointer-constant
# -Wlocal-type-template-args enables
# -Wc++98-compat-local-type-template-args.
-Wno-c++98-compat-local-type-template-args
)
if(compiler_version VERSION_GREATER_EQUAL 18.1.6)
set(flags
# Misc warnings
-Wnonportable-private-system-apinotes-path
-Wopenacc
-Wsource-uses-openacc
${flags}
)
endif()
if(compiler_version VERSION_GREATER_EQUAL 19.1)
set(flags
# Build with this until it becomes a problem
# -Wcuda-compat is handled in the general flags, above
-Wnvcc-compat
# Misc warnings
-Wformat-signedness
${flags}
)
endif()
if(compiler_version VERSION_GREATER_EQUAL 20.1)
set(flags
# Misc warnings
-Wdecls-in-multiple-modules
-Wvariadic-macro-arguments-omitted
${flags}
)
endif()
if(compiler_version VERSION_GREATER_EQUAL 21.0)
set(flags
# Aggressively warn about thread-safety issues
# Requires markup: https://clang.llvm.org/docs/ThreadSafetyAnalysis.html
-Wthread-safety-pointer
# Misc warnings
-Wignored-base-class-qualifiers
-Wshift-bool
-Wunique-object-duplication
${flags}
)
endif()
if(compiler_version VERSION_GREATER_EQUAL 22.1)
set(flags
# Misc warnings
-Walloc-size
-Wenum-compare-typo
-Wshadow-header
${flags}
)
endif()
if(compiler_version VERSION_GREATER_EQUAL 23.1)
set(flags
# Don't assume a bool loaded from memory holds 0 or 1, so a
# corrupted bool can't steer the optimized code
-fno-strict-bool
# Misc warnings
-Wattribute-alias
-Wnonportable-include-path-separator
${flags}
)
endif()
# Only use _FORTIFY_SOURCE if the optimization level is -O2, -O3, or -Os
hipobj_get_fortify_flags(fortify_flags)
# Drop -Wused-but-marked-unused when the fortified C library functions
# trip it, since there's nothing to fix in hipObject's code. With a
# multi-config generator, this drops it from every configuration.
if(fortify_flags)
hipobj_check_fortify_used_but_marked_unused(fortify_allows_warning)
if(NOT fortify_allows_warning)
list(REMOVE_ITEM flags -Wused-but-marked-unused)
endif()
endif()
set(flags
${fortify_flags}
${flags}
)
# Control-flow protection (Intel CET) only exists on x86
check_cxx_compiler_flag(-fcf-protection=full
HIPOBJ_COMPILER_SUPPORTS_CF_PROTECTION)
if(HIPOBJ_COMPILER_SUPPORTS_CF_PROTECTION)
list(APPEND flags -fcf-protection=full)
endif()
set(${outvar} ${flags} PARENT_SCOPE)
endfunction()