tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # This file is meant to be included into an target to create a unittest that |
| 6 | # invokes a set of no-compile tests. A no-compile test is a test that asserts |
| 7 | # a particular construct will not compile. |
| 8 | # |
| 9 | # Also see: |
| 10 | # http://dev.chromium.org/developers/testing/no-compile-tests |
| 11 | # |
| 12 | # To use this, create a gyp target with the following form: |
| 13 | # |
| 14 | # import("//build/nocompile.gni") |
| 15 | # nocompile_test("my_module_nc_unittests") { |
| 16 | # sources = [ |
| 17 | # 'nc_testset_1.nc', |
| 18 | # 'nc_testset_2.nc', |
| 19 | # ] |
Jan Wilken Dörrie | 1f5ea04 | 2020-08-13 18:29:49 | [diff] [blame] | 20 | # |
| 21 | # # optional extra include dirs: |
| 22 | # include_dirs = [ ... ] |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 23 | # } |
| 24 | # |
| 25 | # The .nc files are C++ files that contain code we wish to assert will not |
| 26 | # compile. Each individual test case in the file should be put in its own |
| 27 | # #ifdef section. The expected output should be appended with a C++-style |
| 28 | # comment that has a python list of regular expressions. This will likely |
| 29 | # be greater than 80-characters. Giving a solid expected output test is |
| 30 | # important so that random compile failures do not cause the test to pass. |
| 31 | # |
| 32 | # Example .nc file: |
| 33 | # |
Lei Zhang | f84e2728 | 2018-04-27 02:24:00 | [diff] [blame] | 34 | # #if defined(TEST_NEEDS_SEMICOLON) // [r"expected ',' or ';' at end of input"] |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 35 | # |
| 36 | # int a = 1 |
| 37 | # |
Lei Zhang | f84e2728 | 2018-04-27 02:24:00 | [diff] [blame] | 38 | # #elif defined(TEST_NEEDS_CAST) // [r"invalid conversion from 'void*' to 'char*'"] |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 39 | # |
| 40 | # void* a = NULL; |
| 41 | # char* b = a; |
| 42 | # |
| 43 | # #endif |
| 44 | # |
Lei Zhang | f84e2728 | 2018-04-27 02:24:00 | [diff] [blame] | 45 | # If we needed disable TEST_NEEDS_SEMICOLON, then change the define to: |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 46 | # |
Lei Zhang | f84e2728 | 2018-04-27 02:24:00 | [diff] [blame] | 47 | # DISABLE_TEST_NEEDS_SEMICOLON |
| 48 | # TEST_NEEDS_CAST |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 49 | # |
| 50 | # The lines above are parsed by a regexp so avoid getting creative with the |
| 51 | # formatting or ifdef logic; it will likely just not work. |
| 52 | # |
| 53 | # Implementation notes: |
| 54 | # The .nc files are actually processed by a python script which executes the |
| 55 | # compiler and generates a .cc file that is empty on success, or will have a |
| 56 | # series of #error lines on failure, and a set of trivially passing gunit |
| 57 | # TEST() functions on success. This allows us to fail at the compile step when |
| 58 | # something goes wrong, and know during the unittest run that the test was at |
| 59 | # least processed when things go right. |
| 60 | |
Hans Wennborg | 8261b168 | 2019-01-14 13:40:16 | [diff] [blame] | 61 | import("//build/config/clang/clang.gni") |
Dirk Pranke | ca35ab20 | 2020-11-04 23:56:53 | [diff] [blame] | 62 | import("//build/config/python.gni") |
Nico Weber | 8c6f296 | 2019-08-01 15:30:22 | [diff] [blame] | 63 | import("//build/toolchain/toolchain.gni") |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 64 | import("//testing/test.gni") |
| 65 | |
| 66 | declare_args() { |
wychen | 4f7b28c | 2017-01-04 08:04:29 | [diff] [blame] | 67 | # TODO(crbug.com/105388): make sure no-compile test is not flaky. |
Jan Wilken Dörrie | 1f5ea04 | 2020-08-13 18:29:49 | [diff] [blame] | 68 | enable_nocompile_tests = (is_linux || is_chromeos || is_apple) && is_clang && |
| 69 | host_cpu == target_cpu |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (enable_nocompile_tests) { |
tzik | cffd16b | 2017-08-01 00:23:32 | [diff] [blame] | 73 | import("//build/config/c++/c++.gni") |
Hans Wennborg | 8261b168 | 2019-01-14 13:40:16 | [diff] [blame] | 74 | import("//build/config/sysroot.gni") |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 75 | template("nocompile_test") { |
| 76 | nocompile_target = target_name + "_run_nocompile" |
| 77 | |
Dirk Pranke | ca35ab20 | 2020-11-04 23:56:53 | [diff] [blame] | 78 | # TODO(crbug.com/1112471): Get this to run cleanly under Python 3. |
| 79 | python2_action_foreach(nocompile_target) { |
Eric Seckler | fb2a899d | 2018-09-10 15:25:30 | [diff] [blame] | 80 | testonly = true |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 81 | script = "//tools/nocompile_driver.py" |
| 82 | sources = invoker.sources |
Eric Seckler | fb2a899d | 2018-09-10 15:25:30 | [diff] [blame] | 83 | deps = invoker.deps |
| 84 | if (defined(invoker.public_deps)) { |
| 85 | public_deps = invoker.public_deps |
| 86 | } |
tzik | b5f8f4b | 2016-02-16 02:34:45 | [diff] [blame] | 87 | |
| 88 | result_path = "$target_gen_dir/{{source_name_part}}_nc.cc" |
| 89 | depfile = "${result_path}.d" |
Nico Weber | 4782d7368 | 2020-01-14 04:52:17 | [diff] [blame] | 90 | outputs = [ result_path ] |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 91 | args = [ |
Hans Wennborg | 8261b168 | 2019-01-14 13:40:16 | [diff] [blame] | 92 | rebase_path("$clang_base_path/bin/clang++", root_build_dir), |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 93 | "4", # number of compilers to invoke in parallel. |
| 94 | "{{source}}", |
brettw | bb57bea1 | 2017-01-18 22:11:05 | [diff] [blame] | 95 | rebase_path(result_path, root_build_dir), |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 96 | "--", |
tzik | cffd16b | 2017-08-01 00:23:32 | [diff] [blame] | 97 | "-nostdinc++", |
Tom Anderson | 0412593 | 2017-11-28 03:13:13 | [diff] [blame] | 98 | "-isystem" + rebase_path("$libcxx_prefix/include", root_build_dir), |
| 99 | "-isystem" + rebase_path("$libcxxabi_prefix/include", root_build_dir), |
tzik | cffd16b | 2017-08-01 00:23:32 | [diff] [blame] | 100 | "-std=c++14", |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 101 | "-Wall", |
| 102 | "-Werror", |
| 103 | "-Wfatal-errors", |
Victor Costan | 63cd2c2d | 2018-02-16 02:29:07 | [diff] [blame] | 104 | "-Wthread-safety", |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 105 | "-I" + rebase_path("//", root_build_dir), |
Eric Seckler | 03d0d42 | 2018-08-29 20:06:36 | [diff] [blame] | 106 | "-I" + rebase_path(root_gen_dir, root_build_dir), |
Hans Wennborg | 18026ae | 2019-08-22 19:07:46 | [diff] [blame] | 107 | |
Jamie Madill | 90cdc25 | 2019-08-15 23:12:47 | [diff] [blame] | 108 | # TODO(https://crbug.com/989932): Track build/config/compiler/BUILD.gn |
Hans Wennborg | 18026ae | 2019-08-22 19:07:46 | [diff] [blame] | 109 | "-Wno-implicit-int-float-conversion", |
| 110 | ] |
Jan Wilken Dörrie | 1f5ea04 | 2020-08-13 18:29:49 | [diff] [blame] | 111 | |
Nico Weber | aced470b | 2020-11-26 22:00:46 | [diff] [blame] | 112 | if (is_apple && host_os != "mac") { |
| 113 | args += [ "--target=x86_64-apple-macos" ] |
| 114 | } |
| 115 | |
Jan Wilken Dörrie | 1f5ea04 | 2020-08-13 18:29:49 | [diff] [blame] | 116 | # Iterate over any extra include dirs and append them to the command line. |
| 117 | if (defined(invoker.include_dirs)) { |
| 118 | foreach(include_dir, invoker.include_dirs) { |
| 119 | args += [ "-I" + rebase_path(include_dir, root_build_dir) ] |
| 120 | } |
| 121 | } |
| 122 | |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 123 | if (sysroot != "") { |
| 124 | args += [ |
| 125 | "--sysroot", |
| 126 | rebase_path(sysroot, root_build_dir), |
| 127 | ] |
| 128 | } |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | test(target_name) { |
| 132 | deps = invoker.deps + [ ":$nocompile_target" ] |
| 133 | sources = get_target_outputs(":$nocompile_target") |
| 134 | } |
| 135 | } |
| 136 | } |