set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${ROOT_DIR}/cmake/modules")
include(VariableUtils)

find_program(ninjaExe ninja 
  PATHS ${TOOLS_DIR}/ninja/bin
  REQUIRED)

# Ensure we're building for the native system.
set_cache(CMAKE_SYSTEM_NAME "${CMAKE_HOST_SYSTEM_NAME}")

# Include the Compiler-RT cache values.
include(${CMAKE_CURRENT_LIST_DIR}/compiler-rt-cache.cmake)
unset(CMAKE_C_COMPILER CACHE)
unset(CMAKE_CXX_COMPILER CACHE)
unset(CMAKE_ASM_COMPILER CACHE)
unset(LLVM_CONFIG_PATH CACHE)

# Set Clang's default values based on if we're reconfiguring.
if (BOOTSTRAP_RECONFIGURING)
  set_cache(CLANG_DEFAULT_CXX_STDLIB libc++)
  set_cache(CLANG_DEFAULT_RTLIB compiler-rt)
  set_cache(CLANG_DEFAULT_UNWINDLIB libunwind)
else()
  unset(CLANG_DEFAULT_CXX_STDLIB CACHE)
  unset(CLANG_DEFAULT_RTLIB CACHE)
  unset(CLANG_DEFAULT_UNWINDLIB CACHE)
endif()

set_cache(CMAKE_BUILD_TYPE Release)
set_cache(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/bootstrap-llvm" PATH)
set_cache(CMAKE_GENERATOR Ninja)
set_cache(CMAKE_MAKE_PROGRAM ${ninjaExe} FILEPATH)

disable_cache_variables(
  CLANG_ENABLE_ARCMT
  CLANG_ENABLE_STATIC_ANALYZER
  CLANG_INSTALL_SCANBUILD
  CLANG_INSTALL_SCANVIEW
  CLANG_PLUGIN_SUPPORT
  CLANG_TOOL_CLANG_CHECK_BUILD
  CLANG_TOOL_CLANG_DIFF_BUILD
  CLANG_TOOL_CLANG_FORMAT_BUILD
  CLANG_TOOL_CLANG_FORMAT_VS_BUILD
  CLANG_TOOL_CLANG_FUZZER_BUILD
  CLANG_TOOL_CLANG_IMPORT_TEST_BUILD
  CLANG_TOOL_CLANG_OFFLOAD_BUNDLER_BUILD
  CLANG_TOOL_CLANG_REFACTOR_BUILD
  CLANG_TOOL_CLANG_RENAME_BUILD
  CLANG_TOOL_CLANG_SCAN_DEPS_BUILD
  CLANG_TOOL_CLANG_SHLIB_BUILD
  CLANG_TOOL_C_ARCMT_TEST_BUILD
  CLANG_TOOL_C_INDEX_TEST_BUILD
  CLANG_TOOL_DIAGTOOL_BUILD

  LLVM_ENABLE_BACKTRACES
  LLVM_ENABLE_UNWIND_TABLES
  LLVM_ENABLE_FFI
  LLVM_ENABLE_TERMINFO
  LLVM_ENABLE_LIBEDIT
  LLVM_ENABLE_LIBPFM
  LLVM_ENABLE_Z3_SOLVER
  LLVM_INCLUDE_UTILS
  LLVM_BUILD_UTILS
  LLVM_INCLUDE_EXAMPLES
  LLVM_INCLUDE_TESTS
  LLVM_INCLUDE_GO_TESTS
  LLVM_INCLUDE_BENCHMARKS
  LLVM_INCLUDE_DOCS
  LLVM_ENABLE_OCAMLDOC
  LLVM_ENABLE_BINDINGS
  LLVM_ENABLE_PLUGINS
)

set_cache_variables(STRING 
  VARIABLES
    LLVM_ENABLE_LIBXML2
    LLVM_ENABLE_ZLIB
  VALUE OFF)

# Ensure the standard library, runtime library, and unwinder are all set to the
# platform defaults by clearing them.
set_cache_variables(STRING
  VARIABLES
    CLANG_DEFAULT_CXX_STDLIB
    CLANG_DEFAULT_RTLIB
    CLANG_DEFAULT_UNWINDLIB
  VALUE "")

set_cache(CLANG_DEFAULT_LINKER "lld")
set_cache(LLVM_TARGETS_TO_BUILD host)
set_cache(LLVM_ENABLE_PROJECTS "clang;compiler-rt;lld")
