xref: /aosp_15_r20/external/angle/build/nocompile.gni (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker# Copyright 2011 The Chromium Authors
2*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
3*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
4*8975f5c5SAndroid Build Coastguard Worker
5*8975f5c5SAndroid Build Coastguard Worker# This file is meant to be included into an target to create a unittest that
6*8975f5c5SAndroid Build Coastguard Worker# invokes a set of no-compile tests.  A no-compile test is a test that asserts
7*8975f5c5SAndroid Build Coastguard Worker# a particular construct will not compile.
8*8975f5c5SAndroid Build Coastguard Worker#
9*8975f5c5SAndroid Build Coastguard Worker# Usage:
10*8975f5c5SAndroid Build Coastguard Worker#
11*8975f5c5SAndroid Build Coastguard Worker# 1. Create a GN target:
12*8975f5c5SAndroid Build Coastguard Worker#
13*8975f5c5SAndroid Build Coastguard Worker#    import("//build/nocompile.gni")
14*8975f5c5SAndroid Build Coastguard Worker#
15*8975f5c5SAndroid Build Coastguard Worker#    nocompile_source_set("base_nocompile_tests") {
16*8975f5c5SAndroid Build Coastguard Worker#      sources = [
17*8975f5c5SAndroid Build Coastguard Worker#        "functional/one_not_equal_two_nocompile.nc",
18*8975f5c5SAndroid Build Coastguard Worker#      ]
19*8975f5c5SAndroid Build Coastguard Worker#      deps = [
20*8975f5c5SAndroid Build Coastguard Worker#        ":base"
21*8975f5c5SAndroid Build Coastguard Worker#      ]
22*8975f5c5SAndroid Build Coastguard Worker#    }
23*8975f5c5SAndroid Build Coastguard Worker#
24*8975f5c5SAndroid Build Coastguard Worker#    Note that by convention, nocompile tests use the `.nc` extension rather
25*8975f5c5SAndroid Build Coastguard Worker#    than the standard `.cc` extension: this is because the expectation lines
26*8975f5c5SAndroid Build Coastguard Worker#    often exceed 80 characters, which would make clang-format unhappy.
27*8975f5c5SAndroid Build Coastguard Worker#
28*8975f5c5SAndroid Build Coastguard Worker# 2. Add a dep from a related test binary to the nocompile source set:
29*8975f5c5SAndroid Build Coastguard Worker#
30*8975f5c5SAndroid Build Coastguard Worker#    test("base_unittests") {
31*8975f5c5SAndroid Build Coastguard Worker#      ...
32*8975f5c5SAndroid Build Coastguard Worker#      deps += [ ":base_nocompile_tests" ]
33*8975f5c5SAndroid Build Coastguard Worker#    }
34*8975f5c5SAndroid Build Coastguard Worker#
35*8975f5c5SAndroid Build Coastguard Worker# 3. Populate the .nc file with test cases. Expected compile failures should be
36*8975f5c5SAndroid Build Coastguard Worker#    annotated with a comment of the form:
37*8975f5c5SAndroid Build Coastguard Worker#
38*8975f5c5SAndroid Build Coastguard Worker#    // expected-error {{<expected error string here>}}
39*8975f5c5SAndroid Build Coastguard Worker#
40*8975f5c5SAndroid Build Coastguard Worker#    For example:
41*8975f5c5SAndroid Build Coastguard Worker#
42*8975f5c5SAndroid Build Coastguard Worker#    void OneDoesNotEqualTwo() {
43*8975f5c5SAndroid Build Coastguard Worker#      static_assert(1 == 2);  // expected-error {{static assertion failed due to requirement '1 == 2'}}
44*8975f5c5SAndroid Build Coastguard Worker#    }
45*8975f5c5SAndroid Build Coastguard Worker#
46*8975f5c5SAndroid Build Coastguard Worker#    The verification logic is built as part of clang; full documentation is at
47*8975f5c5SAndroid Build Coastguard Worker#    https://clang.llvm.org/docs/InternalsManual.html#specifying-diagnostics.
48*8975f5c5SAndroid Build Coastguard Worker#
49*8975f5c5SAndroid Build Coastguard Worker# Also see:
50*8975f5c5SAndroid Build Coastguard Worker#   http://dev.chromium.org/developers/testing/no-compile-tests
51*8975f5c5SAndroid Build Coastguard Worker#
52*8975f5c5SAndroid Build Coastguard Workerimport("//build/config/clang/clang.gni")
53*8975f5c5SAndroid Build Coastguard Workerif (is_win) {
54*8975f5c5SAndroid Build Coastguard Worker  import("//build/toolchain/win/win_toolchain_data.gni")
55*8975f5c5SAndroid Build Coastguard Worker}
56*8975f5c5SAndroid Build Coastguard Worker
57*8975f5c5SAndroid Build Coastguard Workerdeclare_args() {
58*8975f5c5SAndroid Build Coastguard Worker  enable_nocompile_tests = is_clang && !is_nacl
59*8975f5c5SAndroid Build Coastguard Worker}
60*8975f5c5SAndroid Build Coastguard Worker
61*8975f5c5SAndroid Build Coastguard Workerif (enable_nocompile_tests) {
62*8975f5c5SAndroid Build Coastguard Worker  template("nocompile_source_set") {
63*8975f5c5SAndroid Build Coastguard Worker    action_foreach(target_name) {
64*8975f5c5SAndroid Build Coastguard Worker      testonly = true
65*8975f5c5SAndroid Build Coastguard Worker
66*8975f5c5SAndroid Build Coastguard Worker      script = "//tools/nocompile/wrapper.py"
67*8975f5c5SAndroid Build Coastguard Worker      sources = invoker.sources
68*8975f5c5SAndroid Build Coastguard Worker      if (defined(invoker.deps)) {
69*8975f5c5SAndroid Build Coastguard Worker        deps = invoker.deps
70*8975f5c5SAndroid Build Coastguard Worker      }
71*8975f5c5SAndroid Build Coastguard Worker
72*8975f5c5SAndroid Build Coastguard Worker      # An action is not a compiler, so configs is empty until it is explicitly
73*8975f5c5SAndroid Build Coastguard Worker      # set.
74*8975f5c5SAndroid Build Coastguard Worker      configs = default_compiler_configs
75*8975f5c5SAndroid Build Coastguard Worker      if (defined(invoker.configs)) {
76*8975f5c5SAndroid Build Coastguard Worker        configs += invoker.configs
77*8975f5c5SAndroid Build Coastguard Worker      }
78*8975f5c5SAndroid Build Coastguard Worker
79*8975f5c5SAndroid Build Coastguard Worker      # Disable the checks that the Chrome style plugin normally enforces to
80*8975f5c5SAndroid Build Coastguard Worker      # reduce the amount of boilerplate needed in nocompile tests.
81*8975f5c5SAndroid Build Coastguard Worker      configs -= [ "//build/config/clang:find_bad_constructs" ]
82*8975f5c5SAndroid Build Coastguard Worker
83*8975f5c5SAndroid Build Coastguard Worker      if (is_win) {
84*8975f5c5SAndroid Build Coastguard Worker        result_path =
85*8975f5c5SAndroid Build Coastguard Worker            "$target_out_dir/$target_name/{{source_name_part}}_placeholder.obj"
86*8975f5c5SAndroid Build Coastguard Worker      } else {
87*8975f5c5SAndroid Build Coastguard Worker        result_path =
88*8975f5c5SAndroid Build Coastguard Worker            "$target_out_dir/$target_name/{{source_name_part}}_placeholder.o"
89*8975f5c5SAndroid Build Coastguard Worker      }
90*8975f5c5SAndroid Build Coastguard Worker      rebased_obj_path = rebase_path(result_path, root_build_dir)
91*8975f5c5SAndroid Build Coastguard Worker
92*8975f5c5SAndroid Build Coastguard Worker      depfile = "${result_path}.d"
93*8975f5c5SAndroid Build Coastguard Worker      rebased_depfile_path = rebase_path(depfile, root_build_dir)
94*8975f5c5SAndroid Build Coastguard Worker      outputs = [ result_path ]
95*8975f5c5SAndroid Build Coastguard Worker
96*8975f5c5SAndroid Build Coastguard Worker      if (is_win) {
97*8975f5c5SAndroid Build Coastguard Worker        if (host_os == "win") {
98*8975f5c5SAndroid Build Coastguard Worker          cxx = "clang-cl.exe"
99*8975f5c5SAndroid Build Coastguard Worker        } else {
100*8975f5c5SAndroid Build Coastguard Worker          cxx = "clang-cl"
101*8975f5c5SAndroid Build Coastguard Worker        }
102*8975f5c5SAndroid Build Coastguard Worker      } else {
103*8975f5c5SAndroid Build Coastguard Worker        cxx = "clang++"
104*8975f5c5SAndroid Build Coastguard Worker      }
105*8975f5c5SAndroid Build Coastguard Worker
106*8975f5c5SAndroid Build Coastguard Worker      args = []
107*8975f5c5SAndroid Build Coastguard Worker
108*8975f5c5SAndroid Build Coastguard Worker      if (is_win) {
109*8975f5c5SAndroid Build Coastguard Worker        # ninja normally parses /showIncludes output, but the depsformat
110*8975f5c5SAndroid Build Coastguard Worker        # variable can only be set in compiler tools, not for custom actions.
111*8975f5c5SAndroid Build Coastguard Worker        # Unfortunately, this means the clang wrapper needs to generate the
112*8975f5c5SAndroid Build Coastguard Worker        # depfile itself.
113*8975f5c5SAndroid Build Coastguard Worker        args += [ "--generate-depfile" ]
114*8975f5c5SAndroid Build Coastguard Worker      }
115*8975f5c5SAndroid Build Coastguard Worker
116*8975f5c5SAndroid Build Coastguard Worker      args += [
117*8975f5c5SAndroid Build Coastguard Worker        rebase_path("$clang_base_path/bin/$cxx", root_build_dir),
118*8975f5c5SAndroid Build Coastguard Worker        "{{source}}",
119*8975f5c5SAndroid Build Coastguard Worker        rebased_obj_path,
120*8975f5c5SAndroid Build Coastguard Worker        rebased_depfile_path,
121*8975f5c5SAndroid Build Coastguard Worker        "--",
122*8975f5c5SAndroid Build Coastguard Worker        "{{cflags}}",
123*8975f5c5SAndroid Build Coastguard Worker        "{{cflags_cc}}",
124*8975f5c5SAndroid Build Coastguard Worker        "{{defines}}",
125*8975f5c5SAndroid Build Coastguard Worker        "{{include_dirs}}",
126*8975f5c5SAndroid Build Coastguard Worker
127*8975f5c5SAndroid Build Coastguard Worker        # No need to generate an object file for nocompile tests.
128*8975f5c5SAndroid Build Coastguard Worker        "-Xclang",
129*8975f5c5SAndroid Build Coastguard Worker        "-fsyntax-only",
130*8975f5c5SAndroid Build Coastguard Worker
131*8975f5c5SAndroid Build Coastguard Worker        # Enable clang's VerifyDiagnosticConsumer:
132*8975f5c5SAndroid Build Coastguard Worker        # https://clang.llvm.org/doxygen/classclang_1_1VerifyDiagnosticConsumer.html
133*8975f5c5SAndroid Build Coastguard Worker        "-Xclang",
134*8975f5c5SAndroid Build Coastguard Worker        "-verify",
135*8975f5c5SAndroid Build Coastguard Worker
136*8975f5c5SAndroid Build Coastguard Worker        # But don't require expected-note comments since that is not the
137*8975f5c5SAndroid Build Coastguard Worker        # primary point of the nocompile tests.
138*8975f5c5SAndroid Build Coastguard Worker        "-Xclang",
139*8975f5c5SAndroid Build Coastguard Worker        "-verify-ignore-unexpected=note",
140*8975f5c5SAndroid Build Coastguard Worker
141*8975f5c5SAndroid Build Coastguard Worker        # Disable the error limit so that nocompile tests do not need to be
142*8975f5c5SAndroid Build Coastguard Worker        # arbitrarily split up when they hit the default error limit.
143*8975f5c5SAndroid Build Coastguard Worker        "-ferror-limit=0",
144*8975f5c5SAndroid Build Coastguard Worker
145*8975f5c5SAndroid Build Coastguard Worker        # So funny characters don't show up in error messages.
146*8975f5c5SAndroid Build Coastguard Worker        "-fno-color-diagnostics",
147*8975f5c5SAndroid Build Coastguard Worker
148*8975f5c5SAndroid Build Coastguard Worker        # Always treat warnings as errors.
149*8975f5c5SAndroid Build Coastguard Worker        "-Werror",
150*8975f5c5SAndroid Build Coastguard Worker      ]
151*8975f5c5SAndroid Build Coastguard Worker
152*8975f5c5SAndroid Build Coastguard Worker      if (!is_win) {
153*8975f5c5SAndroid Build Coastguard Worker        args += [
154*8975f5c5SAndroid Build Coastguard Worker          # On non-Windows platforms, clang can generate the depfile.
155*8975f5c5SAndroid Build Coastguard Worker          "-MMD",
156*8975f5c5SAndroid Build Coastguard Worker          "-MF",
157*8975f5c5SAndroid Build Coastguard Worker          rebased_depfile_path,
158*8975f5c5SAndroid Build Coastguard Worker          "-MT",
159*8975f5c5SAndroid Build Coastguard Worker          rebased_obj_path,
160*8975f5c5SAndroid Build Coastguard Worker
161*8975f5c5SAndroid Build Coastguard Worker          # Non-Windows clang uses file extensions to determine how to treat
162*8975f5c5SAndroid Build Coastguard Worker          # various inputs, so explicitly tell it to treat all inputs (even
163*8975f5c5SAndroid Build Coastguard Worker          # those with weird extensions like .nc) as C++ source files.
164*8975f5c5SAndroid Build Coastguard Worker          "-x",
165*8975f5c5SAndroid Build Coastguard Worker          "c++",
166*8975f5c5SAndroid Build Coastguard Worker        ]
167*8975f5c5SAndroid Build Coastguard Worker      } else {
168*8975f5c5SAndroid Build Coastguard Worker        # For some reason, the Windows includes are not part of the default
169*8975f5c5SAndroid Build Coastguard Worker        # compiler configs. Set it explicitly here, since things like libc++
170*8975f5c5SAndroid Build Coastguard Worker        # depend on the VC runtime.
171*8975f5c5SAndroid Build Coastguard Worker        if (target_cpu == "x86") {
172*8975f5c5SAndroid Build Coastguard Worker          win_toolchain_data = win_toolchain_data_x86
173*8975f5c5SAndroid Build Coastguard Worker        } else if (target_cpu == "x64") {
174*8975f5c5SAndroid Build Coastguard Worker          win_toolchain_data = win_toolchain_data_x64
175*8975f5c5SAndroid Build Coastguard Worker        } else if (target_cpu == "arm64") {
176*8975f5c5SAndroid Build Coastguard Worker          win_toolchain_data = win_toolchain_data_arm64
177*8975f5c5SAndroid Build Coastguard Worker        } else {
178*8975f5c5SAndroid Build Coastguard Worker          error("Unsupported target_cpu, add it to win_toolchain_data.gni")
179*8975f5c5SAndroid Build Coastguard Worker        }
180*8975f5c5SAndroid Build Coastguard Worker        args += win_toolchain_data.include_flags_imsvc_list
181*8975f5c5SAndroid Build Coastguard Worker        args += [ "/showIncludes:user" ]
182*8975f5c5SAndroid Build Coastguard Worker      }
183*8975f5c5SAndroid Build Coastguard Worker
184*8975f5c5SAndroid Build Coastguard Worker      # Note: for all platforms, the depfile only lists user includes, and not
185*8975f5c5SAndroid Build Coastguard Worker      # system includes. If system includes change, the compiler flags are
186*8975f5c5SAndroid Build Coastguard Worker      # expected to artificially change in some way to invalidate and force the
187*8975f5c5SAndroid Build Coastguard Worker      # nocompile tests to run again.
188*8975f5c5SAndroid Build Coastguard Worker    }
189*8975f5c5SAndroid Build Coastguard Worker  }
190*8975f5c5SAndroid Build Coastguard Worker}
191