forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnocompile.gni
407 lines (370 loc) · 13 KB
/
nocompile.gni
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# Copyright 2011 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This file is meant to be included into an target to create a unittest that
# invokes a set of no-compile tests. A no-compile test is a test that asserts
# a particular construct will not compile.
#
# Usage:
#
# 1. Create a GN target:
#
# import("//build/nocompile.gni")
#
# nocompile_source_set("base_nocompile") {
# sources = [
# "functional/one_not_equal_two_nocompile.nc",
# ]
# deps = [
# ":base"
# ]
# }
#
# Note that by convention, nocompile tests use the `.nc` extension rather
# than the standard `.cc` extension: this is because the expectation lines
# often exceed 80 characters, which would make clang-format unhappy.
#
# 2. Add a dep from a related test binary to the nocompile source set:
#
# test("base_unittests") {
# ...
# deps += [ ":base_nocompile_tests" ]
# }
#
# 3. Populate the .nc file with test cases. Expected compile failures should be
# annotated with a comment of the form:
#
# // expected-error {{<expected error string here>}}
#
# For example:
#
# void OneDoesNotEqualTwo() {
# static_assert(1 == 2); // expected-error {{static assertion failed due to requirement '1 == 2'}}
# }
#
# The verification logic is built as part of clang; full documentation is at
# https://clang.llvm.org/doxygen/classclang_1_1VerifyDiagnosticConsumer.html.
#
# Also see:
# http://dev.chromium.org/developers/testing/no-compile-tests
#
import("//build/config/clang/clang.gni")
if (is_win) {
import("//build/toolchain/win/win_toolchain_data.gni")
}
declare_args() {
enable_nocompile_tests = (is_linux || is_chromeos || is_apple || is_win) &&
is_clang && host_cpu == target_cpu
enable_nocompile_tests_new = is_clang && !is_nacl
}
if (enable_nocompile_tests_new) {
template("nocompile_source_set") {
action_foreach(target_name) {
testonly = true
script = "//tools/nocompile/wrapper.py"
sources = invoker.sources
deps = invoker.deps
# An action is not a compiler, so configs is empty until it is explicitly set.
configs = default_compiler_configs
# Disable the checks that the Chrome style plugin normally enforces to
# reduce the amount of boilerplate needed in nocompile tests.
configs -= [ "//build/config/clang:find_bad_constructs" ]
if (is_win) {
result_path =
"$target_out_dir/$target_name/{{source_name_part}}_placeholder.obj"
} else {
result_path =
"$target_out_dir/$target_name/{{source_name_part}}_placeholder.o"
}
rebased_obj_path = rebase_path(result_path, root_build_dir)
depfile = "${result_path}.d"
rebased_depfile_path = rebase_path(depfile, root_build_dir)
outputs = [ result_path ]
if (is_win) {
if (host_os == "win") {
cxx = "clang-cl.exe"
} else {
cxx = "clang-cl"
}
} else {
cxx = "clang++"
}
args = []
if (is_win) {
# ninja normally parses /showIncludes output, but the depsformat
# variable can only be set in compiler tools, not for custom actions.
# Unfortunately, this means the clang wrapper needs to generate the
# depfile itself.
args += [ "--generate-depfile" ]
}
args += [
rebase_path("$clang_base_path/bin/$cxx", root_build_dir),
"{{source}}",
rebased_obj_path,
rebased_depfile_path,
"--",
"{{cflags}}",
"{{cflags_cc}}",
"{{defines}}",
"{{include_dirs}}",
# No need to generate an object file for nocompile tests.
"-Xclang",
"-fsyntax-only",
# Enable clang's VerifyDiagnosticConsumer:
# https://clang.llvm.org/doxygen/classclang_1_1VerifyDiagnosticConsumer.html
"-Xclang",
"-verify",
# But don't require expected-note comments since that is not the
# primary point of the nocompile tests.
"-Xclang",
"-verify-ignore-unexpected=note",
# Disable the error limit so that nocompile tests do not need to be
# arbitrarily split up when they hit the default error limit.
"-ferror-limit=0",
# So funny characters don't show up in error messages.
"-fno-color-diagnostics",
# Always treat warnings as errors.
"-Werror",
]
if (!is_win) {
args += [
# On non-Windows platforms, clang can generate the depfile.
"-MMD",
"-MF",
rebased_depfile_path,
"-MT",
rebased_obj_path,
# Non-Windows clang uses file extensions to determine how to treat
# various inputs, so explicitly tell it to treat all inputs (even
# those with weird extensions like .nc) as C++ source files.
"-x",
"c++",
]
} else {
# For some reason, the Windows includes are not part of the default
# compiler configs. Set it explicitly here, since things like libc++
# depend on the VC runtime.
if (target_cpu == "x86") {
win_toolchain_data = win_toolchain_data_x86
} else if (target_cpu == "x64") {
win_toolchain_data = win_toolchain_data_x64
} else if (target_cpu == "arm64") {
win_toolchain_data = win_toolchain_data_arm64
} else {
error("Unsupported target_cpu, add it to win_toolchain_data.gni")
}
args += win_toolchain_data.include_flags_imsvc_list
args += [ "/showIncludes:user" ]
}
# Note: for all platforms, the depfile only lists user includes, and not
# system includes. If system includes change, the compiler flags are
# expected to artificially change in some way to invalidate and force the
# nocompile tests to run again.
}
}
}
# TODO(https://crbug.com/1480969): this section remains for legacy
# documentation. However, nocompile tests using these legacy templates are
# migrated to the new-style tests. Please do not add more old-style tests.
#
# To use this, create a GN target with the following form:
#
# import("//build/nocompile.gni")
# nocompile_test("my_module_nc_unittests") {
# sources = [
# 'nc_testset_1.nc',
# 'nc_testset_2.nc',
# ]
#
# # optional extra include dirs:
# include_dirs = [ ... ]
# }
#
# The tests are invoked by building the target named in the nocompile_test()
# macro, for example:
#
# ninja -C out/Default my_module_nc_unittests
#
# The .nc files are C++ files that contain code we wish to assert will not
# compile. Each individual test case in the file should be put in its own
# #if defined(...) section specifying an unique preprocessor symbol beginning
# with NCTEST which names the test. The set of tests in a file is automatically
# determined by scanning the file for these #if blocks and no other explicit
# definition of the symbol is required to register a test.
#
# The expected complier error message should be appended with a C++-style
# comment that has a python list of regular expressions. This will likely be
# greater than 80-characters. Giving a solid expected output test is important
# so that random compile failures do not cause the test to pass.
#
# Example .nc file:
#
# #if defined(NCTEST_NEEDS_SEMICOLON) // [r"expected ',' or ';' at end of input"]
#
# int a = 1
#
# #elif defined(NCTEST_NEEDS_CAST) // [r"invalid conversion from 'void*' to 'char*'"]
#
# void* a = NULL;
# char* b = a;
#
# #endif
#
# If we need to disable NCTEST_NEEDS_SEMICOLON, then change the #if to:
#
# #if defined(DISABLED_NCTEST_NEEDS_SEMICOLON)
# ...
# #elif defined(NCTEST_NEEDS_CAST)
# ...
#
# The lines above are parsed by a regexp so avoid getting creative with the
# formatting or ifdef logic; it will likely just not work.
#
# Implementation notes:
# The .nc files are actually processed by a python script which executes the
# compiler and generates a .cc file that is empty on success, or will have a
# series of #error lines on failure, and a set of trivially passing gunit
# TEST() functions on success. This allows us to fail at the compile step when
# something goes wrong, and know during the unittest run that the test was at
# least processed when things go right.
if (enable_nocompile_tests) {
import("//build/config/c++/c++.gni")
import("//build/config/sysroot.gni")
import("//testing/test.gni")
if (is_mac) {
import("//build/config/mac/mac_sdk.gni")
}
template("nocompile_test") {
nocompile_target = target_name + "_run_nocompile"
action_foreach(nocompile_target) {
testonly = true
script = "//tools/nocompile/driver.py"
sources = invoker.sources
deps = invoker.deps
if (defined(invoker.public_deps)) {
public_deps = invoker.public_deps
}
result_path = "$target_gen_dir/{{source_name_part}}_nc.cc"
outputs = [ result_path ]
rebased_result_path = rebase_path(result_path, root_build_dir)
if (is_win) {
if (host_os == "win") {
cxx = "clang-cl.exe"
nulldevice = "nul"
} else {
cxx = "clang-cl"
# Unfortunately, clang-cl always wants to suffix the output file name
# with ".obj", and /dev/null.obj is not a valid file. As a bit of a
# hack, simply use the path to the generated .cc file, knowing:
# - that clang-cl will append ".obj" to the filename, so it will never
# clash.
# - except when the nocompile test unexpectedly passes, the output
# file will never actually be written.
nulldevice = rebased_result_path
}
} else {
cxx = "clang++"
}
depfile = "${result_path}.d"
args = []
if (is_win) {
args += [
"--depfile",
rebased_result_path + ".d",
]
}
args += [
rebase_path("$clang_base_path/bin/$cxx", root_build_dir),
"4", # number of compilers to invoke in parallel.
"{{source}}",
rebased_result_path,
"--",
"-Werror",
"-Wfatal-errors",
"-Wthread-safety",
"-I" + rebase_path("//", root_build_dir),
"-I" + rebase_path("//third_party/abseil-cpp/", root_build_dir),
"-I" + rebase_path("//buildtools/third_party/libc++/", root_build_dir),
"-I" + rebase_path(root_gen_dir, root_build_dir),
# TODO(https://crbug.com/989932): Track build/config/compiler/BUILD.gn
"-Wno-implicit-int-float-conversion",
]
if (is_win) {
# On Windows we fall back to using system headers from a sysroot from
# depot_tools. This is negotiated by python scripts and the result is
# available in //build/toolchain/win/win_toolchain_data.gni. From there
# we get the `include_flags_imsvc` which point to the system headers.
if (host_cpu == "x86") {
win_toolchain_data = win_toolchain_data_x86
} else if (host_cpu == "x64") {
win_toolchain_data = win_toolchain_data_x64
} else if (host_cpu == "arm64") {
win_toolchain_data = win_toolchain_data_arm64
} else {
error("Unsupported host_cpu, add it to win_toolchain_data.gni")
}
args += win_toolchain_data.include_flags_imsvc_list
args += [
"/W4",
"-Wno-unused-parameter",
"-I" + rebase_path("$libcxx_prefix/include", root_build_dir),
"/std:c++20",
"/showIncludes",
"/Fo" + nulldevice,
"/c",
"/Tp",
]
} else {
args += [
"-Wall",
"-nostdinc++",
"-isystem" + rebase_path("$libcxx_prefix/include", root_build_dir),
"-isystem" + rebase_path("$libcxxabi_prefix/include", root_build_dir),
"-std=c++20",
"-MMD",
"-MF",
rebased_result_path + ".d",
"-MT",
rebased_result_path,
"-o",
"/dev/null",
"-c",
"-x",
"c++",
]
}
args += [ "{{source}}" ]
if (is_mac && host_os != "mac") {
args += [
"--target=x86_64-apple-macos",
"-mmacos-version-min=$mac_deployment_target",
]
}
# Iterate over any extra include dirs and append them to the command line.
if (defined(invoker.include_dirs)) {
foreach(include_dir, invoker.include_dirs) {
args += [ "-I" + rebase_path(include_dir, root_build_dir) ]
}
}
if (sysroot != "") {
assert(!is_win)
sysroot_path = rebase_path(sysroot, root_build_dir)
args += [
"--sysroot",
sysroot_path,
]
}
if (!is_nacl) {
args += [
# TODO(crbug.com/1343975) Evaluate and possibly enable.
"-Wno-deprecated-builtins",
]
}
}
test(target_name) {
deps = invoker.deps + [ ":$nocompile_target" ]
sources = get_target_outputs(":$nocompile_target")
forward_variables_from(invoker, [ "bundle_deps" ])
}
}
}