include(HandleLitArguments)
add_subdirectory(tools)

# Install the library at a fake location so we can run the test suite against it.
# This ensures that we run the test suite against a setup that matches what we ship
# in production as closely as possible (in terms of file paths, rpaths, etc).
set(LIBCXX_TESTING_INSTALL_PREFIX "${LIBCXX_BINARY_DIR}/test-suite-install")
set(libcxx_test_suite_install_targets cxx-headers cxx cxx-modules)
if (LIBCXX_CXX_ABI STREQUAL "libcxxabi")
  list(APPEND libcxx_test_suite_install_targets cxxabi-headers cxxabi)
endif()
if (LIBCXXABI_USE_LLVM_UNWINDER AND TARGET unwind)
  list(APPEND libcxx_test_suite_install_targets unwind-headers unwind)
endif()
foreach(target IN LISTS libcxx_test_suite_install_targets)
  add_custom_target(libcxx-test-suite-install-${target} DEPENDS "${target}"
                    COMMAND "${CMAKE_COMMAND}" --install "${CMAKE_BINARY_DIR}"
                                               --prefix "${LIBCXX_TESTING_INSTALL_PREFIX}"
                                               --component "${target}")
  add_dependencies(cxx-test-depends libcxx-test-suite-install-${target})
endforeach()

set(AUTO_GEN_COMMENT "## Autogenerated by libcxx configuration.\n# Do not edit!")
set(SERIALIZED_LIT_PARAMS "# Lit parameters serialized here for llvm-lit to pick them up\n")

serialize_lit_string_param(SERIALIZED_LIT_PARAMS compiler "${CMAKE_CXX_COMPILER}")

if (LIBCXX_INCLUDE_BENCHMARKS)
  add_subdirectory(benchmarks)
  set(_libcxx_benchmark_mode "dry-run")
else()
  serialize_lit_string_param(SERIALIZED_LIT_PARAMS enable_benchmarks "no")
  set(_libcxx_benchmark_mode "no")
endif()

if (NOT LIBCXX_ENABLE_EXCEPTIONS)
  serialize_lit_param(SERIALIZED_LIT_PARAMS enable_exceptions False)
endif()

if (NOT LIBCXX_ENABLE_RTTI)
  serialize_lit_param(SERIALIZED_LIT_PARAMS enable_rtti False)
endif()

serialize_lit_string_param(SERIALIZED_LIT_PARAMS hardening_mode "${LIBCXX_HARDENING_MODE}")

if (CMAKE_CXX_COMPILER_TARGET)
  serialize_lit_string_param(SERIALIZED_LIT_PARAMS target_triple "${CMAKE_CXX_COMPILER_TARGET}")
else()
  serialize_lit_string_param(SERIALIZED_LIT_PARAMS target_triple "${LLVM_DEFAULT_TARGET_TRIPLE}")
endif()

if (LLVM_USE_SANITIZER)
  serialize_lit_string_param(SERIALIZED_LIT_PARAMS use_sanitizer "${LLVM_USE_SANITIZER}")
endif()

serialize_lit_params_list(SERIALIZED_LIT_PARAMS LIBCXX_TEST_PARAMS)

include(AddLLVM) # for configure_lit_site_cfg and add_lit_testsuite

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/configs/cmake-bridge.cfg.in"
                "${CMAKE_CURRENT_BINARY_DIR}/cmake-bridge.cfg"
                @ONLY)

configure_lit_site_cfg(
  "${LIBCXX_TEST_CONFIG}"
  ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg
  MAIN_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py")

add_lit_testsuite(check-cxx
  "Running libcxx tests"
  ${CMAKE_CURRENT_BINARY_DIR}
  PARAMS enable_benchmarks="${_libcxx_benchmark_mode}"
  DEPENDS cxx-test-depends)
