Compiler projects using llvm
import("//clang/lib/ARCMigrate/enable.gni")
import("//clang/lib/StaticAnalyzer/Frontend/enable.gni")
import("//llvm/lib/Target/targets.gni")
import("//llvm/triples.gni")
import("//llvm/utils/gn/build/libs/pthread/enable.gni")
import("//llvm/utils/gn/build/libs/zlib/enable.gni")
import("//llvm/utils/gn/build/write_cmake_config.gni")
import("//llvm/utils/llvm-lit/lit_path_function.gni")
import("clang_lit_site_cfg_files.gni")

template("write_lit_config") {
  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,
      "CLANG_BINARY_DIR=" +
          rebase_path(get_label_info("//clang", "target_out_dir"), dir),
      "CLANG_SOURCE_DIR=" + rebase_path("//clang", dir),
      "ENABLE_SHARED=0",
      "LLVM_BINARY_DIR=" +
          rebase_path(get_label_info("//llvm", "target_out_dir"), dir),
      "LLVM_LIBS_DIR=",  # needed only for shared builds
      "LLVM_SOURCE_DIR=" + rebase_path("//llvm", dir),
      "LLVM_TOOLS_DIR=" + rebase_path("$root_out_dir/bin", dir),
      "LLVM_TARGET_TRIPLE=$llvm_target_triple",
    ]
    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_config("lit_site_cfg") {
  # Fully-qualified instead of relative for LIT_SITE_CFG_IN_HEADER.
  input = "//clang/test/lit.site.cfg.py.in"
  output = clang_lit_site_cfg_file
  dir = get_path_info(output, "dir")
  extra_values = [
    "LLVM_WITH_Z3=0",
    "CLANG_BUILD_EXAMPLES=0",
    "CLANG_DEFAULT_CXX_STDLIB=",  # Empty string means "default value" here.
    "CLANG_DEFAULT_PIE_ON_LINUX=0",
    "CLANG_ENABLE_OPAQUE_POINTERS_INTERNAL=1",
    "CLANG_PLUGIN_SUPPORT=0",  # FIXME: Analysis/plugins need global -fPIC
    "CURRENT_TOOLS_DIR=" + rebase_path("$root_out_dir/bin", dir),
    "CLANG_VENDOR_UTI=org.llvm.clang",

    # This is only used if LLVM_USE_SANITIZER includes lsan and the host
    # OS is macOS. Since the GN build currently never uses LLVM_USE_SANITIZER,
    # this is never read.  If it's ever needed,
    # utils/gn/build/toolchain/BUILD.gn should get the compiler from a variable
    # that's also read here -- but that should happen after multi-toolchain
    # builds exist, to make sure it's a toolchain var.
    "CMAKE_CXX_COMPILER=c++",
    "CMAKE_C_COMPILER=cc",
    "ENABLE_BACKTRACES=1",
    "LLVM_EXTERNAL_LIT=",
    "LLVM_HOST_TRIPLE=$llvm_current_triple",
    "LLVM_LIT_TOOLS_DIR=",  # Intentionally empty, matches cmake build.
    "LLVM_USE_SANITIZER=",
    "Python3_EXECUTABLE=$python_path",
    "USE_Z3_SOLVER=",
  ]

  if (clang_enable_arcmt) {
    extra_values += [ "CLANG_ENABLE_ARCMT=1" ]
  } else {
    extra_values += [ "CLANG_ENABLE_ARCMT=0" ]
  }

  if (clang_enable_static_analyzer) {
    extra_values += [ "CLANG_ENABLE_STATIC_ANALYZER=1" ]
  } else {
    extra_values += [ "CLANG_ENABLE_STATIC_ANALYZER=0" ]
  }

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

  if (host_cpu == "x64") {
    extra_values += [ "HOST_ARCH=x86_64" ]
  } else if (host_cpu == "arm64") {
    extra_values += [ "HOST_ARCH=arm64" ]
  } else if (host_cpu == "ppc64") {
    extra_values += [ "HOST_ARCH=powerpc64le" ]
  } else {
    assert(false, "unimplemented host_cpu " + host_cpu)
  }

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

  if (host_os == "win") {
    extra_values += [
      # See comment for Windows solink in llvm/utils/gn/build/toolchain/BUILD.gn
      "CMAKE_LIBRARY_OUTPUT_DIRECTORY=" + rebase_path("$root_out_dir/bin", dir),
      "LLVM_LIT_ERRC_MESSAGES=no such file or directory;is a directory;" +
          "invalid argument;permission denied",
    ]
  } else {
    extra_values += [
      "CMAKE_LIBRARY_OUTPUT_DIRECTORY=" + rebase_path("$root_out_dir/lib", dir),
      "LLVM_LIT_ERRC_MESSAGES=",
    ]
  }

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

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

# This target should contain all dependencies of check-clang.
# //:default depends on it, so that ninja's default target builds all
# prerequisites for check-clang but doesn't run check-clang itself.
group("test") {
  deps = [
    ":lit_site_cfg",
    ":lit_unit_site_cfg",
    "//clang/lib/Headers",
    "//clang/tools/apinotes-test",
    "//clang/tools/c-index-test",
    "//clang/tools/clang-diff",
    "//clang/tools/clang-format",
    "//clang/tools/clang-fuzzer/dictionary:clang-fuzzer-dictionary",
    "//clang/tools/clang-import-test",
    "//clang/tools/clang-offload-bundler",
    "//clang/tools/clang-refactor",
    "//clang/tools/clang-rename",
    "//clang/tools/clang-repl",
    "//clang/tools/clang-scan-deps",
    "//clang/tools/diagtool",
    "//clang/tools/driver:symlinks",
    "//clang/unittests",
    "//clang/utils/TableGen:clang-tblgen",
    "//clang/utils/hmaptool",
    "//llvm/tools/llc",
    "//llvm/tools/llvm-ar:symlinks",
    "//llvm/tools/llvm-as",
    "//llvm/tools/llvm-bcanalyzer",
    "//llvm/tools/llvm-cat",
    "//llvm/tools/llvm-config",
    "//llvm/tools/llvm-cxxfilt",
    "//llvm/tools/llvm-dis",
    "//llvm/tools/llvm-dwarfdump",
    "//llvm/tools/llvm-ifs",
    "//llvm/tools/llvm-lto",
    "//llvm/tools/llvm-lto2",
    "//llvm/tools/llvm-modextract",
    "//llvm/tools/llvm-nm:symlinks",
    "//llvm/tools/llvm-objcopy:symlinks",
    "//llvm/tools/llvm-objdump:symlinks",
    "//llvm/tools/llvm-profdata",
    "//llvm/tools/llvm-rc:symlinks",
    "//llvm/tools/llvm-readobj:symlinks",
    "//llvm/tools/llvm-symbolizer:symlinks",
    "//llvm/tools/opt",
    "//llvm/tools/split-file",
    "//llvm/tools/yaml2obj",
    "//llvm/utils/FileCheck",
    "//llvm/utils/count",
    "//llvm/utils/llvm-lit",
    "//llvm/utils/not",
  ]
  if (clang_enable_arcmt) {
    deps += [
      "//clang/tools/arcmt-test",
      "//clang/tools/c-arcmt-test",
    ]
  }
  if (clang_enable_static_analyzer) {
    deps += [
      "//clang/tools/clang-check",
      "//clang/tools/clang-extdef-mapping",
    ]
  }

  # FIXME: clang_build_examples
  testonly = true
}

action("check-clang") {
  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-clang is always dirty, //:default doesn't depend on it so that
  # it's not part of the default ninja target.  Hence, check-clang 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"
}