1# This file defines a template for adding a unittest binary.
2#
3# It's a thin wrapper around GN's built-in executable() target type and
4# accepts the same parameters, and in addition this paramater:
5#
6#   has_custom_main (optional)
7#       [bool] If set, link against gtest instead of UnitTestMain; for tests
8#              that define their own main() function.
9#
10# Example use:
11#
12#   unittest("FormatTest") {
13#     sources = [ ... ]
14#     ...
15#   }
16
17template("unittest") {
18  executable(target_name) {
19    has_custom_main = false  # Default value.
20
21    # Foward everything (has_custom_main if set; configs, sources, deps, ...).
22    forward_variables_from(invoker, "*")
23    assert(!defined(invoker.output_dir), "cannot set unittest output_dir")
24    assert(!defined(invoker.testonly), "cannot set unittest testonly")
25
26    # Common settings for all unit tests.
27    # Unit test binaries shouldn't go right in out/gn/bin, for two reasons:
28    # 1. That's where production binaries go.
29    # 2. The CMake build doesn't put the unit tests of all projects (clang,
30    #    lld,...) in one directory, so it's not guaranteed that there won't
31    #    be name collisions between test binaries from separate projects.
32    # Each lit suite takes an foo_obj_root parameter and puts temporary files
33    # for lit tests at foo_obj_root/test and looks for unit test binaries
34    # below foo_obj_root/unittests. As long as the BUILD.gn files processing
35    # the lit.site.cfg.py.in files match the output dir here, it doesn't
36    # matter all that much where the unit test binaries go, with the weak
37    # constraints that test binaries of different projects should go in
38    # different folders, and that it's not too difficult to manually
39    # run the unit test binary if necessary. Using target_out_dir here
40    # means that //clang/unittests/Format gets its binary in
41    # out/gn/obj/clang/unittests/Format/FormatTests, which seems fine.
42    #
43    # If you change output_dir here, look through
44    # `git grep target_out_dir '*/unittests/*'` and update those too.
45    output_dir = target_out_dir
46
47    if (has_custom_main) {
48      deps += [ "//llvm/utils/unittest:gtest" ]
49    } else {
50      deps += [ "//llvm/utils/unittest/UnitTestMain" ]
51    }
52    testonly = true
53  }
54}
55
56set_defaults("unittest") {
57  configs = shared_binary_target_configs
58}
59