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 | # ] |
| 20 | # } |
| 21 | # |
| 22 | # The .nc files are C++ files that contain code we wish to assert will not |
| 23 | # compile. Each individual test case in the file should be put in its own |
| 24 | # #ifdef section. The expected output should be appended with a C++-style |
| 25 | # comment that has a python list of regular expressions. This will likely |
| 26 | # be greater than 80-characters. Giving a solid expected output test is |
| 27 | # important so that random compile failures do not cause the test to pass. |
| 28 | # |
| 29 | # Example .nc file: |
| 30 | # |
| 31 | # #if defined(TEST_NEEDS_SEMICOLON) // [r"expected ',' or ';' at end of input"] |
| 32 | # |
| 33 | # int a = 1 |
| 34 | # |
| 35 | # #elif defined(TEST_NEEDS_CAST) // [r"invalid conversion from 'void*' to 'char*'"] |
| 36 | # |
| 37 | # void* a = NULL; |
| 38 | # char* b = a; |
| 39 | # |
| 40 | # #endif |
| 41 | # |
| 42 | # If we needed disable TEST_NEEDS_SEMICOLON, then change the define to: |
| 43 | # |
| 44 | # DISABLE_TEST_NEEDS_SEMICOLON |
| 45 | # TEST_NEEDS_CAST |
| 46 | # |
| 47 | # The lines above are parsed by a regexp so avoid getting creative with the |
| 48 | # formatting or ifdef logic; it will likely just not work. |
| 49 | # |
| 50 | # Implementation notes: |
| 51 | # The .nc files are actually processed by a python script which executes the |
| 52 | # compiler and generates a .cc file that is empty on success, or will have a |
| 53 | # series of #error lines on failure, and a set of trivially passing gunit |
| 54 | # TEST() functions on success. This allows us to fail at the compile step when |
| 55 | # something goes wrong, and know during the unittest run that the test was at |
| 56 | # least processed when things go right. |
| 57 | |
| 58 | import("//testing/test.gni") |
| 59 | |
| 60 | declare_args() { |
wychen | 4f7b28c | 2017-01-04 08:04:29 | [diff] [blame] | 61 | # TODO(crbug.com/105388): make sure no-compile test is not flaky. |
wychen | 09ac3a2 | 2017-02-27 20:19:26 | [diff] [blame] | 62 | enable_nocompile_tests = |
| 63 | (is_linux || is_mac || is_ios) && is_clang && host_cpu == target_cpu |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (enable_nocompile_tests) { |
wychen | 296d75b | 2017-01-04 06:51:50 | [diff] [blame] | 67 | import("//build/config/sysroot.gni") |
tzik | cffd16b | 2017-08-01 00:23:32 | [diff] [blame] | 68 | import("//build/config/c++/c++.gni") |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 69 | template("nocompile_test") { |
| 70 | nocompile_target = target_name + "_run_nocompile" |
| 71 | |
| 72 | action_foreach(nocompile_target) { |
| 73 | script = "//tools/nocompile_driver.py" |
| 74 | sources = invoker.sources |
tzik | b5f8f4b | 2016-02-16 02:34:45 | [diff] [blame] | 75 | |
| 76 | result_path = "$target_gen_dir/{{source_name_part}}_nc.cc" |
| 77 | depfile = "${result_path}.d" |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 78 | outputs = [ |
tzik | b5f8f4b | 2016-02-16 02:34:45 | [diff] [blame] | 79 | result_path, |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 80 | ] |
| 81 | args = [ |
| 82 | "4", # number of compilers to invoke in parallel. |
| 83 | "{{source}}", |
brettw | bb57bea1 | 2017-01-18 22:11:05 | [diff] [blame] | 84 | rebase_path(result_path, root_build_dir), |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 85 | "--", |
tzik | cffd16b | 2017-08-01 00:23:32 | [diff] [blame] | 86 | "-nostdinc++", |
Tom Anderson | 0412593 | 2017-11-28 03:13:13 | [diff] [blame] | 87 | "-isystem" + rebase_path("$libcxx_prefix/include", root_build_dir), |
| 88 | "-isystem" + rebase_path("$libcxxabi_prefix/include", root_build_dir), |
tzik | cffd16b | 2017-08-01 00:23:32 | [diff] [blame] | 89 | "-std=c++14", |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 90 | "-Wall", |
| 91 | "-Werror", |
| 92 | "-Wfatal-errors", |
Victor Costan | 63cd2c2d | 2018-02-16 02:29:07 | [diff] [blame^] | 93 | "-Wthread-safety", |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 94 | "-I" + rebase_path("//", root_build_dir), |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 95 | ] |
Trent Apted | 71169bb | 2017-07-28 00:12:10 | [diff] [blame] | 96 | if (sysroot != "") { |
| 97 | args += [ |
| 98 | "--sysroot", |
| 99 | rebase_path(sysroot, root_build_dir), |
| 100 | ] |
| 101 | } |
tzik | 0137dc5d4 | 2015-12-17 05:29:22 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | test(target_name) { |
| 105 | deps = invoker.deps + [ ":$nocompile_target" ] |
| 106 | sources = get_target_outputs(":$nocompile_target") |
| 107 | } |
| 108 | } |
| 109 | } |