1 # This file defines a template for adding a unittest binary.
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:
6 # has_custom_main (optional)
7 # [bool] If set, link against gtest instead of UnitTestMain; for tests
8 # that define their own main() function.
12 # unittest("FormatTest") {
17 template("unittest") {
18 executable(target_name) {
19 has_custom_main = false # Default value.
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")
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.
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
47 if (has_custom_main) {
48 deps += [ "//third-party/unittest:gtest" ]
50 deps += [ "//third-party/unittest/UnitTestMain" ]
56 set_defaults("unittest") {
57 configs = shared_binary_target_configs