Compiler projects using llvm
import("//llvm/lib/DebugInfo/PDB/enable_dia.gni")
import("//llvm/lib/Target/targets_string.gni")
import("//llvm/triples.gni")
import("//llvm/utils/gn/build/buildflags.gni")
import("//llvm/utils/gn/build/libs/curl/enable.gni")
import("//llvm/utils/gn/build/libs/pthread/enable.gni")
import("//llvm/utils/gn/build/libs/xar/enable.gni")
import("//llvm/utils/gn/build/libs/xml/enable.gni")
import("//llvm/utils/gn/build/libs/zlib/enable.gni")
import("//llvm/utils/gn/build/mac_sdk.gni")
import("//llvm/utils/gn/build/write_cmake_config.gni")
import("//llvm/utils/llvm-lit/lit_path_function.gni")
import("//llvm/version.gni")
import("llvm_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,
      "ENABLE_SHARED=0",
      "LLVM_BINARY_DIR=" +
          rebase_path(get_label_info("//llvm", "target_out_dir"), dir),
      "LLVM_SOURCE_DIR=" + rebase_path("//llvm", dir),
      "LLVM_TOOLS_DIR=" + rebase_path("$root_out_dir/bin", 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_config("lit_site_cfg") {
  input = "//llvm/test/lit.site.cfg.py.in"
  output = llvm_lit_site_cfg_file
  dir = get_path_info(output, "dir")
  extra_values = [
    "BUILD_SHARED_LIBS=0",

    # Only used by the Go bindings tests, or if LLVM_USE_SANITIZER includes
    # asan and the host OS is macOS. The GN build currently never uses
    # LLVM_USE_SANITIZER.  (See also CMAKE_CXX_COMPILER in clang/test/BUILD.gn.)
    "HOST_CC=cc",

    # Only used by the Go bindings tests, or if LLVM_USE_SANITIZER includes
    # lsan and the host OS is macOS. The GN build currently never uses
    # LLVM_USE_SANITIZER.  (See also CMAKE_CXX_COMPILER in clang/test/BUILD.gn.)
    "HOST_CXX=c++",

    # Only used by the Go bindings tests, and for detecting a 32-bit build
    # and in a belt-and-suspenders check for detecting 32-bit host builds.
    # (That check also checks LLVM_HOST_TRIPLE.)
    "HOST_LDFLAGS=",

    "LLVM_ENABLE_FFI=0",
    "LLVM_ENABLE_HTTPLIB=0",
    "LLVM_ENABLE_ZSTD=0",
    "LLVM_HAVE_OPT_VIEWER_MODULES=0",
    "LLVM_HOST_TRIPLE=$llvm_current_triple",
    "LLVM_INCLUDE_DXIL_TESTS=0",
    "LLVM_LIBS_DIR=" + rebase_path("$root_out_dir/lib", dir),
    "LLVM_LINK_LLVM_DYLIB=0",
    "LLVM_LIT_TOOLS_DIR=",  # Intentionally empty, matches cmake build.
    "LLVM_NATIVE_ARCH=$native_target",
    "LLVM_TOOL_LLVM_DRIVER_BUILD=0",  # FIXME: Add actual support for this.
    "LLVM_USE_INTEL_JITEVENTS=0",
    "LLVM_USE_SANITIZER=",
    "LLVM_VERSION_MAJOR=$llvm_version_major",
    "LLVM_VERSION_SUFFIX=git",
    "Python3_EXECUTABLE=$python_path",
    "TARGETS_TO_BUILD=$llvm_targets_to_build_string",
    "LLVM_TARGET_TRIPLE=$llvm_target_triple",

    "LLVM_LIBCXX_USED=0",

    # No bindings are implemented in the GN build.
    "LLVM_BINDINGS=",
    "LLVM_BYE_LINK_INTO_TOOLS=0",

    "GO_EXECUTABLE=",
    "LLVM_INCLUDE_GO_TESTS=0",

    "HAVE_OCAMLOPT=0",
    "OCAMLFIND=OCAMLFIND-NOTFOUND",
    "OCAMLFLAGS=",
    "LLVM_BUILD_EXAMPLES=0",
    "LLVM_HAVE_TF_AOT=0",
    "LLVM_HAVE_TF_API=0",
    "LLVM_INLINER_MODEL_AUTOGENERATED=0",
    "LLVM_RAEVICT_MODEL_AUTOGENERATED=0",
    "PXTAS_EXECUTABLE=",
  ]

  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 == "win") {
    extra_values += [
      "EXEEXT=.exe",
      "LLVM_ENABLE_PLUGINS=0",
      "LLVM_LIT_ERRC_MESSAGES=no such file or directory;is a directory;" +
          "invalid argument;permission denied",
    ]
  } else {
    extra_values += [
      "EXEEXT=",
      "LLVM_ENABLE_PLUGINS=1",
      "LLVM_LIT_ERRC_MESSAGES=",
    ]
  }

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

  if (host_os == "freebsd") {
    extra_values += [ "HOST_OS=FreeBSD" ]
  } else if (host_os == "mac") {
    extra_values += [ "HOST_OS=Darwin" ]
  } else if (host_os == "linux") {
    extra_values += [ "HOST_OS=Linux" ]
  } else if (host_os == "win") {
    extra_values += [ "HOST_OS=Windows" ]
  } else {
    assert(false, "unsupported host_os " + host_os)
  }
  if (host_os == "linux") {
    # lit.cfg.py's have_ld_plugin_support() checks for "gold" in --version,
    # so just claim that ld is gold on Linux.  The function also checks if
    # LLVMgold.so exists, but since that target isn't hooked up yet in the GN
    # build the LLVMgold.so tests currently don't run anywhere in the GN build.
    extra_values += [ "GOLD_EXECUTABLE=ld" ]
  } else {
    extra_values += [ "GOLD_EXECUTABLE=" ]
  }
  if (host_os == "mac") {
    extra_values += [
      "CMAKE_OSX_SYSROOT=" + rebase_path(mac_sdk_path, dir),
      "LD64_EXECUTABLE=ld",
    ]
  } else {
    extra_values += [
      "CMAKE_OSX_SYSROOT=",
      "LD64_EXECUTABLE=",
    ]
  }

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

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

  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_expensive_checks) {
    extra_values += [ "LLVM_ENABLE_EXPENSIVE_CHECKS=1" ]
  } else {
    extra_values += [ "LLVM_ENABLE_EXPENSIVE_CHECKS=0" ]  # Must be 0.
  }

  if (llvm_enable_threads) {
    extra_values += [ "LLVM_ENABLE_THREADS=1" ]
  } else {
    extra_values += [ "LLVM_ENABLE_THREADS=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_libcurl) {
    extra_values += [ "LLVM_ENABLE_CURL=1" ]
  } else {
    extra_values += [ "LLVM_ENABLE_CURL=0" ]  # Must be 0.
  }
}

write_lit_config("lit_unit_site_cfg") {
  input = "//llvm/test/Unit/lit.site.cfg.py.in"
  output = llvm_lit_unit_site_cfg_file
  extra_values = [ "LLVM_BUILD_MODE=." ]
}

# This target should contain all dependencies of check-llvm.
# //:default depends on it, so that ninja's default target builds all
# prerequisites for check-llvm but doesn't run check-llvm itself.
group("test") {
  deps = [
    ":lit_site_cfg",
    ":lit_unit_site_cfg",

    # Because llvm/tools/llvm-config/BUILD.gn calls llvm-build to generate
    # LibraryDependencies.inc, llvm-config expects these libraries to exist
    # even though nothing but unittests depends on them.  Add explicit
    # dependencies to make sure the libaries exist on disk when llvm-config's
    # lit tests run.
    "//llvm/lib/LineEditor",
    "//llvm/lib/Testing/Support",
    "//llvm/tools/bugpoint",
    "//llvm/tools/dsymutil",
    "//llvm/tools/llc",
    "//llvm/tools/lli",
    "//llvm/tools/lli/ChildTarget:lli-child-target",
    "//llvm/tools/llvm-ar:symlinks",
    "//llvm/tools/llvm-as",
    "//llvm/tools/llvm-bcanalyzer",
    "//llvm/tools/llvm-c-test",
    "//llvm/tools/llvm-cat",
    "//llvm/tools/llvm-cfi-verify",
    "//llvm/tools/llvm-cov",
    "//llvm/tools/llvm-cvtres",
    "//llvm/tools/llvm-cxxdump",
    "//llvm/tools/llvm-cxxfilt",
    "//llvm/tools/llvm-cxxmap",
    "//llvm/tools/llvm-debuginfod:symlinks",
    "//llvm/tools/llvm-debuginfod-find:symlinks",
    "//llvm/tools/llvm-diff",
    "//llvm/tools/llvm-dis",
    "//llvm/tools/llvm-dwarfdump",
    "//llvm/tools/llvm-dwarfutil",
    "//llvm/tools/llvm-dwp",
    "//llvm/tools/llvm-exegesis",
    "//llvm/tools/llvm-extract",
    "//llvm/tools/llvm-gsymutil:llvm-gsymutil",
    "//llvm/tools/llvm-ifs",
    "//llvm/tools/llvm-isel-fuzzer",
    "//llvm/tools/llvm-jitlink",
    "//llvm/tools/llvm-jitlink/llvm-jitlink-executor",
    "//llvm/tools/llvm-libtool-darwin:symlinks",
    "//llvm/tools/llvm-link",
    "//llvm/tools/llvm-lipo:symlinks",
    "//llvm/tools/llvm-lto",
    "//llvm/tools/llvm-lto2",
    "//llvm/tools/llvm-mc",
    "//llvm/tools/llvm-mca",
    "//llvm/tools/llvm-ml",
    "//llvm/tools/llvm-modextract",
    "//llvm/tools/llvm-mt",
    "//llvm/tools/llvm-nm",
    "//llvm/tools/llvm-objcopy:symlinks",
    "//llvm/tools/llvm-objdump:symlinks",
    "//llvm/tools/llvm-opt-fuzzer",
    "//llvm/tools/llvm-opt-report",
    "//llvm/tools/llvm-pdbutil",
    "//llvm/tools/llvm-profdata",
    "//llvm/tools/llvm-profgen",
    "//llvm/tools/llvm-rc:symlinks",
    "//llvm/tools/llvm-readobj:symlinks",
    "//llvm/tools/llvm-reduce",
    "//llvm/tools/llvm-remark-size-diff",
    "//llvm/tools/llvm-rtdyld",
    "//llvm/tools/llvm-sim",
    "//llvm/tools/llvm-size",
    "//llvm/tools/llvm-split",
    "//llvm/tools/llvm-stress",
    "//llvm/tools/llvm-strings",
    "//llvm/tools/llvm-symbolizer:symlinks",
    "//llvm/tools/llvm-tapi-diff",
    "//llvm/tools/llvm-tli-checker",
    "//llvm/tools/llvm-undname",
    "//llvm/tools/llvm-xray",
    "//llvm/tools/lto",
    "//llvm/tools/obj2yaml",
    "//llvm/tools/opt",
    "//llvm/tools/sancov",
    "//llvm/tools/sanstats",
    "//llvm/tools/split-file",
    "//llvm/tools/verify-uselistorder",
    "//llvm/tools/yaml2obj",
    "//llvm/unittests",
    "//llvm/utils/FileCheck",
    "//llvm/utils/TableGen:llvm-tblgen",
    "//llvm/utils/count",
    "//llvm/utils/llvm-lit",
    "//llvm/utils/llvm-locstats",
    "//llvm/utils/not",

    # llvm-config wants libgtest_main.a to exist at runtime when run as in
    # its tests, but nothing in the tree depends on them.
    "//llvm/utils/unittest/UnitTestMain:gtest_main",
    "//llvm/utils/yaml-bench",
  ]
  if (host_os != "win") {
    # loadable_modules don't work on Windows.
    # FIXME: In the CMake build, ENABLE_SHARED makes them work somehow
    # (but they're off by default there too).
    deps += [
      "//llvm/lib/Transforms/Hello",
      "//llvm/tools/bugpoint-passes",
    ]
  }

  # FIXME: llvm_build_examples
  testonly = true
}

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