• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/python_action.gni")
18import("$dir_pw_build/target_types.gni")
19import("$dir_pw_build/test_info.gni")
20import("$dir_pw_compilation_testing/negative_compilation_test.gni")
21import("$dir_pw_toolchain/generate_toolchain.gni")
22import("$dir_pw_toolchain/host_clang/toolchains.gni")
23
24declare_args() {
25  # The unit test framework implementation. Defaults to
26  # pw_unit_test:light, which implements a subset of GoogleTest safe to run on
27  # device. Set to //pw_unit_test:googletest when using GoogleTest.
28  #
29  # Type: string (GN path to a source set)
30  # Usage: toolchain-controlled only
31  pw_unit_test_BACKEND = "$dir_pw_unit_test:light"
32
33  # The GoogleTest library target. This is not a pw_unit_test backend (anymore).
34  # Use this to depend on the GoogleTest library directly *WITHOUT* using the
35  # pw_unit_test facade. The //pw_unit_test:googletest backend depends on this
36  # library target.
37  # Defaults to //third_party/googletest.
38  #
39  # Type: string (GN path to a source set)
40  # Usage: toolchain-controlled only
41  pw_unit_test_GOOGLETEST_BACKEND = "$dir_pw_third_party/googletest"
42
43  # Implementation of a main function for ``pw_test`` unit test binaries. Must
44  # be set to an appropriate target for the pw_unit_test backend.
45  #
46  # Type: string (GN path to a source set)
47  # Usage: toolchain-controlled only
48  pw_unit_test_MAIN = "$dir_pw_unit_test:simple_printing_main"
49
50  # Path to a test runner to automatically run unit tests after they are built.
51  #
52  # If set, a ``pw_test`` target's ``<target_name>.run`` action will invoke the
53  # test runner specified by this argument, passing the path to the unit test to
54  # run. If this is unset, the ``pw_test`` target's ``<target_name>.run`` step
55  # will do nothing.
56  #
57  # Targets that don't support parallelized execution of tests (e.g. a on-device
58  # test runner that must flash a device and run the test in serial) should
59  # set pw_unit_test_POOL_DEPTH to 1.
60  #
61  # Type: string (name of an executable on the PATH, or path to an executable)
62  # Usage: toolchain-controlled only
63  pw_unit_test_AUTOMATIC_RUNNER = ""
64
65  # Optional list of arguments to forward to the automatic runner.
66  #
67  # Type: list of strings (args to pass to pw_unit_test_AUTOMATIC_RUNNER)
68  # Usage: toolchain-controlled only
69  pw_unit_test_AUTOMATIC_RUNNER_ARGS = []
70
71  # Optional timeout to apply when running tests via the automatic runner.
72  # Timeout is in seconds. Defaults to empty which means no timeout.
73  pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT = ""
74
75  # The maximum number of unit tests that may be run concurrently for the
76  # current toolchain. Setting this to 0 disables usage of a pool, allowing
77  # unlimited parallelization.
78  #
79  # Note: A single target with two toolchain configurations (e.g. release/debug)
80  #       will use two separate test runner pools by default. Set
81  #       pw_unit_test_POOL_TOOLCHAIN to the same toolchain for both targets to
82  #       merge the pools and force serialization.
83  #
84  # Type: integer
85  # Usage: toolchain-controlled only
86  pw_unit_test_POOL_DEPTH = 0
87
88  # The toolchain to use when referring to the pw_unit_test runner pool. When
89  # this is disabled, the current toolchain is used. This means that every
90  # toolchain will use its own pool definition. If two toolchains should share
91  # the same pool, this argument should be by one of the toolchains to the GN
92  # path of the other toolchain.
93  #
94  # Type: string (GN path to a toolchain)
95  # Usage: toolchain-controlled only
96  pw_unit_test_POOL_TOOLCHAIN = ""
97
98  # The name of the GN target type used to build pw_unit_test executables.
99  #
100  # Type: string (name of a GN template)
101  # Usage: toolchain-controlled only
102  pw_unit_test_EXECUTABLE_TARGET_TYPE = "pw_executable"
103
104  # The path to the .gni file that defines pw_unit_test_EXECUTABLE_TARGET_TYPE.
105  #
106  # If pw_unit_test_EXECUTABLE_TARGET_TYPE is not the default of
107  # `pw_executable`, this .gni file is imported to provide the template
108  # definition.
109  #
110  # Type: string (path to a .gni file)
111  # Usage: toolchain-controlled only
112  pw_unit_test_EXECUTABLE_TARGET_TYPE_FILE = ""
113
114  # If true, the pw_unit_test target, pw_test targets, and pw_test_group targets
115  # will define `testonly = true`.  This is false by default for backwards
116  # compatibility.
117  pw_unit_test_TESTONLY = false
118}
119
120if (pw_unit_test_EXECUTABLE_TARGET_TYPE != "pw_executable" &&
121    pw_unit_test_EXECUTABLE_TARGET_TYPE_FILE != "") {
122  import(pw_unit_test_EXECUTABLE_TARGET_TYPE_FILE)
123}
124
125# Defines a target if enable_if is true. Otherwise, it defines that target as
126# <target_name>.DISABLED and creates an empty <target_name> group. This can be
127# used to conditionally create targets without having to conditionally add them
128# to groups. This results in simpler BUILD.gn files.
129template("pw_internal_disableable_target") {
130  assert(defined(invoker.enable_if),
131         "`enable_if` is required for pw_internal_disableable_target")
132  assert(defined(invoker.target_type),
133         "`target_type` is required for pw_internal_disableable_target")
134
135  if (invoker.enable_if) {
136    _actual_target_name = target_name
137  } else {
138    _actual_target_name = target_name + ".DISABLED"
139
140    # If the target is disabled, create an empty target in its place. Use an
141    # action with the original target's sources as inputs to ensure that
142    # the source files exist (even if they don't compile).
143    pw_python_action(target_name) {
144      script = "$dir_pw_build/py/pw_build/nop.py"
145      stamp = true
146
147      inputs = []
148      if (defined(invoker.sources)) {
149        inputs += invoker.sources
150      }
151      if (defined(invoker.public)) {
152        inputs += invoker.public
153      }
154
155      if (defined(invoker.source_gen_deps)) {
156        deps = invoker.source_gen_deps
157      }
158    }
159  }
160
161  target(invoker.target_type, _actual_target_name) {
162    sources = []
163    public_deps = []
164    deps = []
165    forward_variables_from(invoker,
166                           "*",
167                           [
168                             "enable_if",
169                             "negative_compilation_tests",
170                             "source_gen_deps",
171                             "target_type",
172                             "test_automatic_runner_args",
173                           ])
174
175    # Remove "" from dependencies. This allows disabling targets if a variable
176    # (e.g. a backend) is empty.
177    public_deps += [ "" ]
178    public_deps -= [ "" ]
179    deps += [ "" ]
180    deps -= [ "" ]
181    if (defined(invoker.source_gen_deps)) {
182      deps += invoker.source_gen_deps
183      foreach(source_gen_dep, invoker.source_gen_deps) {
184        sources += get_target_outputs(source_gen_dep)
185      }
186    }
187  }
188}
189
190# Creates a library and an executable target for a unit test with pw_unit_test.
191#
192# <target_name>.lib contains the provided test sources as a library, which can
193# then be linked into a test executable.
194# <target_name> is a standalone executable which contains only the test sources
195# specified in the pw_unit_test_template.
196#
197# If the pw_unit_test_AUTOMATIC_RUNNER variable is set, this template also
198# creates a "${test_name}.run" target which runs the unit test executable after
199# building it.
200#
201# Targets defined using this template will produce test metadata with a
202# `test_type` of "unit_test" and an additional `test_directory` value describing
203# the location of the test binary within the build output.
204#
205# Args:
206#   - enable_if: (optional) Conditionally enables or disables this test. The
207#         test target and *.run target do nothing when the test is disabled. The
208#         disabled test can still be built and run with the
209#         <target_name>.DISABLED and <target_name>.DISABLED.run targets.
210#         Defaults to true (enable_if).
211#   - envvars: (optional) A list of `var_name=value` strings to set as
212#         environment variables when running the target exectuable.
213#   - tags: (optional) List of strings to include in the test metadata. These
214#         have no effect on the build, but may be used by external tools to
215#         distinguish between tests. For example, a tool may want to skip tests
216#         tagged as "slow".
217#   - extra_metadata: (optional) Extra metadata to include in test group
218#         metadata output. This can be used to pass information about this test
219#         to later build tasks.
220#   - source_gen_deps: (optional) List of targets which generate `sources` for
221#         this test. These `deps` will be included even if the test is disabled.
222#         `get_target_outputs` is used to add the generated `sources` to the
223#         test's `sources` list, so these targets must appear earlier in the
224#         same build file.
225#   - All of the regular "executable" target args are accepted.
226#
227template("pw_test") {
228  # This is required in order to reference the pw_test template's target name
229  # within the test_metadata of the metadata group below. The group() definition
230  # creates a new scope where the "target_name" variable is set to its target,
231  # shadowing the one in this scope.
232  _test_target_name = target_name
233
234  _test_is_enabled = !defined(invoker.enable_if) || invoker.enable_if
235
236  if (pw_toolchain_COVERAGE_ENABLED) {
237    _profraw_path = "$target_out_dir/test/$_test_target_name.profraw"
238  }
239
240  # Always set the output_dir as pigweed is not compatible with shared
241  # bin directories for tests.
242  _test_output_dir = "${target_out_dir}/test"
243  if (defined(invoker.output_dir)) {
244    _test_output_dir = invoker.output_dir
245  }
246
247  _test_main = pw_unit_test_MAIN
248  if (defined(invoker.test_main)) {
249    _test_main = invoker.test_main
250  }
251
252  # The unit test code as a source_set.
253  pw_internal_disableable_target("$target_name.lib") {
254    target_type = "pw_source_set"
255    enable_if = _test_is_enabled
256    testonly = pw_unit_test_TESTONLY
257
258    # It is possible that the executable target type has been overriden by
259    # pw_unit_test_EXECUTABLE_TARGET_TYPE, which may allow for additional
260    # variables to be specified on the executable template. As such, we cannot
261    # forward all variables ("*") from the invoker to source_set library, as
262    # those additional variables would not be used and GN gen would error.
263    _source_set_relevant_variables = [
264      # GN source_set variables
265      # https://gn.googlesource.com/gn/+/main/docs/reference.md#target-declarations-source_set_declare-a-source-set-target-variables
266      "asmflags",
267      "cflags",
268      "cflags_c",
269      "cflags_cc",
270      "cflags_objc",
271      "cflags_objcc",
272      "defines",
273      "include_dirs",
274      "inputs",
275      "ldflags",
276      "lib_dirs",
277      "libs",
278      "precompiled_header",
279      "precompiled_source",
280      "rustenv",
281      "rustflags",
282      "swiftflags",
283      "testonly",
284      "assert_no_deps",
285      "data_deps",
286      "deps",
287      "public_deps",
288      "runtime_deps",
289      "write_runtime_deps",
290      "all_dependent_configs",
291      "public_configs",
292      "check_includes",
293      "configs",
294      "data",
295      "friend",
296      "inputs",
297      "metadata",
298      "output_extension",
299      "output_name",
300      "public",
301      "sources",
302      "source_gen_deps",
303      "visibility",
304
305      # pw_source_set variables
306      # https://pigweed.dev/pw_build/?highlight=pw_executable#target-types
307      "remove_configs",
308      "remove_public_deps",
309    ]
310    forward_variables_from(invoker, _source_set_relevant_variables)
311
312    if (!defined(deps)) {
313      deps = []
314    }
315    deps += [ dir_pw_unit_test ]
316
317    if (defined(invoker.negative_compilation_tests) &&
318        invoker.negative_compilation_tests) {
319      deps += [
320        ":$_test_target_name.nc_test",
321        "$dir_pw_compilation_testing:internal_pigweed_use_only",
322      ]
323    }
324  }
325
326  # Metadata for this test when used as part of a pw_test_group target.
327  _test_metadata = "${target_name}.metadata"
328  _extra_metadata = {
329    forward_variables_from(invoker, [ "extra_metadata" ])
330    test_directory = rebase_path(_test_output_dir, root_build_dir)
331  }
332  pw_test_info(_test_metadata) {
333    test_type = "unit_test"
334    test_name = _test_target_name
335    forward_variables_from(invoker, [ "tags" ])
336    extra_metadata = _extra_metadata
337  }
338
339  pw_internal_disableable_target(_test_target_name) {
340    target_type = pw_unit_test_EXECUTABLE_TARGET_TYPE
341    enable_if = _test_is_enabled
342    testonly = pw_unit_test_TESTONLY
343
344    # Include configs, deps, etc. from the pw_test in the executable as well as
345    # the library to ensure that linker flags propagate to the executable.
346    deps = []
347    forward_variables_from(invoker,
348                           "*",
349                           [
350                             "extra_metadata",
351                             "metadata",
352                             "sources",
353                             "source_gen_deps",
354                             "public",
355                           ])
356    deps += [
357      ":$_test_metadata",
358      ":$_test_target_name.lib",
359    ]
360    if (_test_main != "") {
361      deps += [ _test_main ]
362    }
363    output_dir = _test_output_dir
364
365    metadata = {
366      # N.B.: This is placed here instead of in $_test_target_name._run because
367      # pw_test_group only forwards the metadata from _test_target_name and not
368      # _test_target_name._run or _test_target_name.run.
369      if (pw_toolchain_COVERAGE_ENABLED) {
370        profraws = [
371          {
372            type = "profraw"
373            path = rebase_path(_profraw_path, root_build_dir)
374          },
375        ]
376      }
377
378      # Only collect test metadata for the test itself.
379      test_barrier = [ ":$_test_metadata" ]
380    }
381  }
382
383  if (defined(invoker.negative_compilation_tests) &&
384      invoker.negative_compilation_tests) {
385    pw_cc_negative_compilation_test("$target_name.nc_test") {
386      forward_variables_from(invoker, "*")
387      testonly = pw_unit_test_TESTONLY
388
389      # Add a dependency on pw_unit_test since it is implied for pw_unit_test
390      # targets.
391      if (!defined(deps)) {
392        deps = []
393      }
394      deps += [ dir_pw_unit_test ]
395    }
396  }
397
398  if (pw_unit_test_AUTOMATIC_RUNNER != "") {
399    # When the automatic runner is set, create an action which runs the unit
400    # test executable using the test runner script.
401    if (_test_is_enabled) {
402      _test_to_run = _test_target_name
403    } else {
404      # Create a run target for the .DISABLED version of the test.
405      _test_to_run = _test_target_name + ".DISABLED"
406
407      # Create a placeholder .run target for the regular version of the test.
408      group(_test_target_name + ".run") {
409        testonly = pw_unit_test_TESTONLY
410        deps = [ ":$_test_target_name" ]
411      }
412    }
413
414    _test_automatic_runner_args = pw_unit_test_AUTOMATIC_RUNNER_ARGS
415    if (defined(invoker.test_automatic_runner_args)) {
416      _test_automatic_runner_args = []
417      _test_automatic_runner_args += invoker.test_automatic_runner_args
418    }
419
420    pw_python_action(_test_to_run + "._run") {
421      # Optionally limit max test runner concurrency.
422      if (pw_unit_test_POOL_DEPTH != 0) {
423        _pool_toolchain = current_toolchain
424        if (pw_unit_test_POOL_TOOLCHAIN != "") {
425          _pool_toolchain = pw_unit_test_POOL_TOOLCHAIN
426        }
427        pool = "$dir_pw_unit_test:unit_test_pool($_pool_toolchain)"
428      }
429
430      deps = [ ":$_test_target_name" ]
431      inputs = [ pw_unit_test_AUTOMATIC_RUNNER ]
432      module = "pw_unit_test.test_runner"
433      python_deps = [
434        "$dir_pw_cli/py",
435        "$dir_pw_unit_test/py",
436      ]
437      args = [
438        "--runner",
439        rebase_path(pw_unit_test_AUTOMATIC_RUNNER, root_build_dir),
440        "--test",
441        "<TARGET_FILE(:$_test_to_run)>",
442      ]
443      if (defined(invoker.envvars)) {
444        foreach(envvars, envvars) {
445          args += [
446            "--env",
447            envvar,
448          ]
449        }
450      }
451      if (pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT != "") {
452        args += [
453          "--timeout",
454          pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT,
455        ]
456      }
457      if (pw_toolchain_COVERAGE_ENABLED) {
458        _llvm_profile_file = rebase_path(_profraw_path, root_build_dir)
459        args += [
460          "--env",
461          "LLVM_PROFILE_FILE=" + _llvm_profile_file,
462        ]
463      }
464
465      if (_test_automatic_runner_args != []) {
466        args += [ "--" ] + _test_automatic_runner_args
467      }
468
469      outputs = []
470      if (pw_toolchain_COVERAGE_ENABLED) {
471        outputs += [ _profraw_path ]
472      }
473      stamp = true
474    }
475
476    group(_test_to_run + ".run") {
477      testonly = pw_unit_test_TESTONLY
478      public_deps = [ ":$_test_to_run._run" ]
479    }
480  } else {
481    group(_test_target_name + ".run") {
482      testonly = pw_unit_test_TESTONLY
483      public_deps = [ ":$_test_target_name" ]
484    }
485  }
486}
487
488# Defines a related collection of unit tests.
489#
490# Targets defined using this template will produce test metadata with a
491# `test_type` of "test_group" and an additional `deps` list describing the tests
492# collected by this target.
493#
494# Args:
495#   - tests: List of pw_test targets for each of the tests in the group.
496#   - group_deps: (optional) pw_test_group targets on which this group depends.
497#   - enable_if: (optional) Conditionally enables or disables this test group.
498#         If false, an empty group is created. Defaults to true.
499#   - output_metadata: (optional) If true, generates a JSON file containing the
500#         test metadata for this group and all of its dependencies. Defaults to
501#         false.
502#
503template("pw_test_group") {
504  _group_target = target_name
505  if (defined(invoker.tests)) {
506    _deps = invoker.tests
507  } else {
508    _deps = []
509  }
510
511  # Allow empty pw_test_groups with no tests or group_deps.
512  if (!defined(invoker.tests) && !defined(invoker.group_deps)) {
513    not_needed("*")
514  }
515
516  _group_is_enabled = !defined(invoker.enable_if) || invoker.enable_if
517
518  if (_group_is_enabled) {
519    if (defined(invoker.group_deps)) {
520      _deps += invoker.group_deps
521    }
522
523    group(_group_target + ".lib") {
524      testonly = pw_unit_test_TESTONLY
525      deps = []
526      foreach(_target, _deps) {
527        _dep_target = get_label_info(_target, "label_no_toolchain")
528        _dep_toolchain = get_label_info(_target, "toolchain")
529        deps += [ "$_dep_target.lib($_dep_toolchain)" ]
530      }
531    }
532
533    # Create a manifest entry to indicate which tests are a part of this group.
534    _test_group_metadata = "${target_name}_pw_test_group_metadata"
535    _extra_metadata = {
536      forward_variables_from(invoker, [ "extra_metadata" ])
537      if (_deps != []) {
538        deps = []
539        foreach(dep, _deps) {
540          deps += [ get_label_info(dep, "label_no_toolchain") ]
541        }
542      }
543    }
544    pw_test_info(_test_group_metadata) {
545      testonly = pw_unit_test_TESTONLY
546      build_label = _group_target
547      test_type = "test_group"
548      test_name = rebase_path(get_label_info(_group_target, "dir"), "//")
549      extra_metadata = _extra_metadata
550      deps = _deps
551    }
552
553    if (defined(invoker.output_metadata) && invoker.output_metadata) {
554      generated_file(_group_target) {
555        testonly = pw_unit_test_TESTONLY
556        outputs = [ "$target_out_dir/$target_name.testinfo.json" ]
557        data_keys = [
558          "test_groups",
559          "unit_tests",
560          "action_tests",
561          "perf_tests",
562          "fuzz_tests",
563        ]
564        walk_keys = [ "test_barrier" ]
565        output_conversion = "json"
566        deps = [ ":$_test_group_metadata" ]
567      }
568    } else {
569      group(_group_target) {
570        testonly = pw_unit_test_TESTONLY
571        deps = [ ":$_test_group_metadata" ]
572      }
573    }
574
575    # If automatic test running is enabled, create a *.run group that collects
576    # all of the individual *.run targets and groups.
577    if (pw_unit_test_AUTOMATIC_RUNNER != "") {
578      group(_group_target + ".run") {
579        testonly = pw_unit_test_TESTONLY
580        deps = [ ":$_group_target" ]
581        foreach(_target, _deps) {
582          _dep_target = get_label_info(_target, "label_no_toolchain")
583          _dep_toolchain = get_label_info(_target, "toolchain")
584          deps += [ "$_dep_target.run($_dep_toolchain)" ]
585        }
586      }
587    }
588  } else {  # _group_is_enabled
589    # Create empty groups for the tests to avoid pulling in any dependencies.
590    group(_group_target) {
591    }
592    group(_group_target + ".lib") {
593    }
594
595    if (pw_unit_test_AUTOMATIC_RUNNER != "") {
596      group(_group_target + ".run") {
597      }
598    }
599
600    not_needed("*")
601    not_needed(invoker, "*")
602  }
603
604  # All of the tests in this group and its dependencies bundled into a single
605  # test binary.
606  pw_test(_group_target + ".bundle") {
607    deps = [ ":$_group_target.lib" ]
608    enable_if = _group_is_enabled
609  }
610}
611