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