Compiler projects using llvm
# This file defines a template for adding a unittest binary.
#
# It's a thin wrapper around GN's built-in executable() target type and
# accepts the same parameters, and in addition this paramater:
#
#   has_custom_main (optional)
#       [bool] If set, link against gtest instead of UnitTestMain; for tests
#              that define their own main() function.
#
# Example use:
#
#   unittest("FormatTest") {
#     sources = [ ... ]
#     ...
#   }

template("unittest") {
  executable(target_name) {
    has_custom_main = false  # Default value.

    # Foward everything (has_custom_main if set; configs, sources, deps, ...).
    forward_variables_from(invoker, "*")
    assert(!defined(invoker.output_dir), "cannot set unittest output_dir")
    assert(!defined(invoker.testonly), "cannot set unittest testonly")

    # Common settings for all unit tests.
    # Unit test binaries shouldn't go right in out/gn/bin, for two reasons:
    # 1. That's where production binaries go.
    # 2. The CMake build doesn't put the unit tests of all projects (clang,
    #    lld,...) in one directory, so it's not guaranteed that there won't
    #    be name collisions between test binaries from separate projects.
    # Each lit suite takes an foo_obj_root parameter and puts temporary files
    # for lit tests at foo_obj_root/test and looks for unit test binaries
    # below foo_obj_root/unittests. As long as the BUILD.gn files processing
    # the lit.site.cfg.py.in files match the output dir here, it doesn't
    # matter all that much where the unit test binaries go, with the weak
    # constraints that test binaries of different projects should go in
    # different folders, and that it's not too difficult to manually
    # run the unit test binary if necessary. Using target_out_dir here
    # means that //clang/unittests/Format gets its binary in
    # out/gn/obj/clang/unittests/Format/FormatTests, which seems fine.
    #
    # If you change output_dir here, look through
    # `git grep target_out_dir '*/unittests/*'` and update those too.
    output_dir = target_out_dir

    if (has_custom_main) {
      deps += [ "//llvm/utils/unittest:gtest" ]
    } else {
      deps += [ "//llvm/utils/unittest/UnitTestMain" ]
    }
    testonly = true
  }
}

set_defaults("unittest") {
  configs = shared_binary_target_configs
}