llvm/llvm/utils/gn/secondary/lld/test/BUILD.gn

import("//llvm/lib/DebugInfo/PDB/enable_dia.gni")
import("//llvm/triples.gni")
import("//llvm/utils/gn/build/libs/xml/enable.gni")
import("//llvm/utils/gn/build/libs/zlib/enable.gni")
import("//llvm/utils/gn/build/libs/zstd/enable.gni")
import("//llvm/utils/gn/build/write_cmake_config.gni")
import("//llvm/utils/llvm-lit/lit_path_function.gni")
import("lld_lit_site_cfg_files.gni")

# The bits common to writing lit.site.cfg.py.in and Unit/lit.site.cfg.py.in.
template("write_lit_cfg") {
  write_cmake_config(target_name) {
    input = invoker.input
    output = invoker.output
    dir = get_path_info(output, "dir")
    values = [
      "LIT_SITE_CFG_IN_HEADER=" +
          "## Autogenerated from $input, do not edit\n\n" + lit_path_function,
      "LLD_BINARY_DIR=" +
          rebase_path(get_label_info("//lld", "target_out_dir"), dir),
      "LLD_SOURCE_DIR=" + rebase_path("//lld", dir),
    ]
    if (host_os == "win") {
      # See comment for Windows solink in llvm/utils/gn/build/toolchain/BUILD.gn
      values += [ "SHLIBDIR=" + rebase_path("$root_out_dir/bin", dir) ]
    } else {
      values += [ "SHLIBDIR=" + rebase_path("$root_out_dir/lib", dir) ]
    }
    values += invoker.extra_values
  }
}

write_lit_cfg("lit_site_cfg") {
  # Fully-qualified instead of relative for LIT_SITE_CFG_IN_HEADER.
  input = "//lld/test/lit.site.cfg.py.in"
  output = lld_lit_site_cfg_file
  dir = get_path_info(output, "dir")

  extra_values = [
    "CURRENT_LIBS_DIR=",  # FIXME: for shared builds only (?)
    "CURRENT_TOOLS_DIR=" + rebase_path("$root_out_dir/bin", dir),
    "ENABLE_BACKTRACES=1",
    "LLVM_BINARY_DIR=" +
        rebase_path(get_label_info("//llvm", "target_out_dir"), dir),
    "LLVM_HOST_TRIPLE=$llvm_current_triple",
    "LLVM_LIBS_DIR=",  # needed only for shared builds
    "LLVM_LIT_TOOLS_DIR=",  # Intentionally empty, matches cmake build.
    "LLVM_SOURCE_DIR=" + rebase_path("//llvm", dir),
    "LLVM_TOOLS_DIR=" + rebase_path("$root_out_dir/bin", dir),
    "Python3_EXECUTABLE=$python_path",
    "LLVM_TARGET_TRIPLE=$llvm_target_triple",

    "LLD_DEFAULT_LD_LLD_IS_MINGW=0",
    "LLVM_BUILD_EXAMPLES=0",
    "LLVM_BYE_LINK_INTO_TOOLS=0",
  ]

  if (host_os == "win") {
    extra_values += [
      "LLVM_LIT_ERRC_MESSAGES=no such file or directory;is a directory;" +
          "invalid argument;permission denied",
      "LLVM_ENABLE_PLUGINS=0",
    ]
  } else {
    extra_values += [
      "LLVM_LIT_ERRC_MESSAGES=",
      "LLVM_ENABLE_PLUGINS=1",
    ]
  }

  if (host_os == "mac") {
    extra_values += [ "SHLIBEXT=.dylib" ]
  } else if (host_os == "win") {
    extra_values += [ "SHLIBEXT=.dll" ]
  } else {
    extra_values += [ "SHLIBEXT=.so" ]
  }

  if (llvm_enable_dia_sdk) {
    extra_values += [ "LLVM_ENABLE_DIA_SDK=1" ]
  } else {
    extra_values += [ "LLVM_ENABLE_DIA_SDK=0" ]  # Must be 0.
  }

  if (llvm_enable_libxml2) {
    extra_values += [ "LLVM_ENABLE_LIBXML2=1" ]
  } else {
    extra_values += [ "LLVM_ENABLE_LIBXML2=0" ]  # Must be 0.
  }

  if (llvm_enable_zlib) {
    extra_values += [ "LLVM_ENABLE_ZLIB=1" ]
  } else {
    extra_values += [ "LLVM_ENABLE_ZLIB=0" ]  # Must be 0.
  }

  if (llvm_enable_zstd) {
    extra_values += [ "LLVM_ENABLE_ZSTD=1" ]
  } else {
    extra_values += [ "LLVM_ENABLE_ZSTD=0" ]  # Must be 0.
  }

  if (current_cpu == "x64" || current_cpu == "arm64" ||
      current_cpu == "ppc64") {
    extra_values += [ "CMAKE_SIZEOF_VOID_P=8" ]
  } else {
    extra_values += [ "CMAKE_SIZEOF_VOID_P=4" ]
  }
}

write_lit_cfg("lit_unit_site_cfg") {
  # Fully-qualified instead of relative for LIT_SITE_CFG_IN_HEADER.
  input = "//lld/test/Unit/lit.site.cfg.py.in"
  output = lld_lit_unit_site_cfg_file
  extra_values = [ "LLVM_BUILD_MODE=." ]
}

# This target should contain all dependencies of check-lld.
# //:default depends on it, so that ninja's default target builds all
# prerequisites for check-lld but doesn't run check-lld itself.
group("test") {
  deps = [
    ":lit_site_cfg",
    ":lit_unit_site_cfg",
    "//lld/tools/lld:symlinks",
    "//lld/unittests",
    "//llvm/tools/dsymutil",
    "//llvm/tools/llc",
    "//llvm/tools/llvm-ar:symlinks",
    "//llvm/tools/llvm-as",
    "//llvm/tools/llvm-bcanalyzer",
    "//llvm/tools/llvm-cgdata",
    "//llvm/tools/llvm-cvtres",
    "//llvm/tools/llvm-dis",
    "//llvm/tools/llvm-dwarfdump",
    "//llvm/tools/llvm-lipo:symlinks",
    "//llvm/tools/llvm-mc",
    "//llvm/tools/llvm-nm:symlinks",
    "//llvm/tools/llvm-objcopy:symlinks",
    "//llvm/tools/llvm-objdump:symlinks",
    "//llvm/tools/llvm-pdbutil",
    "//llvm/tools/llvm-profdata",
    "//llvm/tools/llvm-readobj:symlinks",
    "//llvm/tools/llvm-strings:symlinks",
    "//llvm/tools/llvm-symbolizer:symlinks",
    "//llvm/tools/obj2yaml",
    "//llvm/tools/opt",
    "//llvm/tools/yaml2obj",
    "//llvm/utils/FileCheck",
    "//llvm/utils/count",
    "//llvm/utils/llvm-lit",
    "//llvm/utils/not",
    "//llvm/utils/split-file",
  ]
  testonly = true
}

# This is the action that runs all of lld's tests, check-lld.
action("check-lld") {
  script = "$root_out_dir/bin/llvm-lit"
  if (host_os == "win") {
    script += ".py"
  }
  args = [
    "-sv",
    rebase_path(".", root_out_dir),
  ]
  outputs = [ "$target_gen_dir/run-lit" ]  # Non-existing, so that ninja runs it
                                           # each time.

  # Since check-lld is always dirty, //:default doesn't depend on it so that
  # it's not part of the default ninja target.  Hence, check-lld shouldn't
  # have any deps except :test, so that the default target is sure to build
  # all the deps.
  deps = [ ":test" ]
  testonly = true

  pool = "//:console"
}