nocompile.gni 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # This file is meant to be included into an target to create a unittest that
  5. # invokes a set of no-compile tests. A no-compile test is a test that asserts
  6. # a particular construct will not compile.
  7. #
  8. # Also see:
  9. # http://dev.chromium.org/developers/testing/no-compile-tests
  10. #
  11. # To use this, create a gyp target with the following form:
  12. #
  13. # import("//build/nocompile.gni")
  14. # nocompile_test("my_module_nc_unittests") {
  15. # sources = [
  16. # 'nc_testset_1.nc',
  17. # 'nc_testset_2.nc',
  18. # ]
  19. #
  20. # # optional extra include dirs:
  21. # include_dirs = [ ... ]
  22. # }
  23. #
  24. # The .nc files are C++ files that contain code we wish to assert will not
  25. # compile. Each individual test case in the file should be put in its own
  26. # #ifdef section. The expected output should be appended with a C++-style
  27. # comment that has a python list of regular expressions. This will likely
  28. # be greater than 80-characters. Giving a solid expected output test is
  29. # important so that random compile failures do not cause the test to pass.
  30. #
  31. # Example .nc file:
  32. #
  33. # #if defined(TEST_NEEDS_SEMICOLON) // [r"expected ',' or ';' at end of input"]
  34. #
  35. # int a = 1
  36. #
  37. # #elif defined(TEST_NEEDS_CAST) // [r"invalid conversion from 'void*' to 'char*'"]
  38. #
  39. # void* a = NULL;
  40. # char* b = a;
  41. #
  42. # #endif
  43. #
  44. # If we needed disable TEST_NEEDS_SEMICOLON, then change the define to:
  45. #
  46. # DISABLE_TEST_NEEDS_SEMICOLON
  47. # TEST_NEEDS_CAST
  48. #
  49. # The lines above are parsed by a regexp so avoid getting creative with the
  50. # formatting or ifdef logic; it will likely just not work.
  51. #
  52. # Implementation notes:
  53. # The .nc files are actually processed by a python script which executes the
  54. # compiler and generates a .cc file that is empty on success, or will have a
  55. # series of #error lines on failure, and a set of trivially passing gunit
  56. # TEST() functions on success. This allows us to fail at the compile step when
  57. # something goes wrong, and know during the unittest run that the test was at
  58. # least processed when things go right.
  59. import("//build/config/clang/clang.gni")
  60. import("//build/config/python.gni")
  61. import("//build/toolchain/toolchain.gni")
  62. import("//testing/test.gni")
  63. declare_args() {
  64. # TODO(crbug.com/105388): make sure no-compile test is not flaky.
  65. enable_nocompile_tests = (is_linux || is_chromeos || is_apple) && is_clang &&
  66. host_cpu == target_cpu
  67. }
  68. if (enable_nocompile_tests) {
  69. import("//build/config/c++/c++.gni")
  70. import("//build/config/sysroot.gni")
  71. template("nocompile_test") {
  72. nocompile_target = target_name + "_run_nocompile"
  73. action_foreach(nocompile_target) {
  74. testonly = true
  75. script = "//tools/nocompile_driver.py"
  76. sources = invoker.sources
  77. deps = invoker.deps
  78. if (defined(invoker.public_deps)) {
  79. public_deps = invoker.public_deps
  80. }
  81. result_path = "$target_gen_dir/{{source_name_part}}_nc.cc"
  82. depfile = "${result_path}.d"
  83. outputs = [ result_path ]
  84. args = [
  85. rebase_path("$clang_base_path/bin/clang++", root_build_dir),
  86. "4", # number of compilers to invoke in parallel.
  87. "{{source}}",
  88. rebase_path(result_path, root_build_dir),
  89. "--",
  90. "-nostdinc++",
  91. "-isystem" + rebase_path("$libcxx_prefix/include", root_build_dir),
  92. "-isystem" + rebase_path("$libcxxabi_prefix/include", root_build_dir),
  93. "-std=c++17",
  94. "-Wall",
  95. "-Werror",
  96. "-Wfatal-errors",
  97. "-Wthread-safety",
  98. "-I" + rebase_path("//", root_build_dir),
  99. "-I" + rebase_path("//third_party/abseil-cpp/", root_build_dir),
  100. "-I" + rebase_path("//buildtools/third_party/libc++/", root_build_dir),
  101. "-I" + rebase_path(root_gen_dir, root_build_dir),
  102. # TODO(https://crbug.com/989932): Track build/config/compiler/BUILD.gn
  103. "-Wno-implicit-int-float-conversion",
  104. ]
  105. if (is_apple && host_os != "mac") {
  106. args += [ "--target=x86_64-apple-macos" ]
  107. }
  108. # Iterate over any extra include dirs and append them to the command line.
  109. if (defined(invoker.include_dirs)) {
  110. foreach(include_dir, invoker.include_dirs) {
  111. args += [ "-I" + rebase_path(include_dir, root_build_dir) ]
  112. }
  113. }
  114. if (sysroot != "") {
  115. args += [
  116. "--sysroot",
  117. rebase_path(sysroot, root_build_dir),
  118. ]
  119. }
  120. if (!is_nacl) {
  121. args += [
  122. # TODO(crbug.com/1343975) Evaluate and possibly enable.
  123. "-Wno-deprecated-builtins",
  124. ]
  125. }
  126. }
  127. test(target_name) {
  128. deps = invoker.deps + [ ":$nocompile_target" ]
  129. sources = get_target_outputs(":$nocompile_target")
  130. }
  131. }
  132. }