# One exact Clang/LLVM release is used for headers, libraries and IR producers.
find_package(LLVM 21.1.8 EXACT REQUIRED CONFIG)
find_package(Clang REQUIRED CONFIG HINTS "${LLVM_DIR}/../clang")
if(NOT Clang_VERSION VERSION_EQUAL "21.1.8")
  message(FATAL_ERROR "Clang 21.1.8 is required with LLVM 21.1.8")
endif()
include("${CMAKE_CURRENT_SOURCE_DIR}/LLVMBuildCompatibility.cmake")
message(STATUS "LLVM ${LLVM_PACKAGE_VERSION}: compiler-tool RTTI=${LLVM_ENABLE_RTTI}")
add_executable(fpt-ast-analyze ast_analyzer.cpp)
add_executable(fpt-ir-analyze ir_analyzer.cpp)
foreach(target fpt-ast-analyze fpt-ir-analyze)
  target_include_directories(${target} SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(${target} PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic)
  set_target_properties(${target} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endforeach()
target_link_libraries(fpt-ast-analyze PRIVATE clang-cpp LLVM)
target_link_libraries(fpt-ir-analyze PRIVATE LLVM)
if(FPT_BUILD_MCA)
  add_executable(fpt-mca-instrument mca_instrument.cpp)
  target_include_directories(fpt-mca-instrument SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS})
  target_compile_definitions(fpt-mca-instrument PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-mca-instrument PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-mca-instrument PRIVATE LLVM)
  set_target_properties(fpt-mca-instrument PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()
if(FPT_BUILD_CESTAC)
  add_executable(fpt-cestac-instrument cestac_instrument.cpp)
  target_include_directories(fpt-cestac-instrument SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS})
  target_compile_definitions(fpt-cestac-instrument PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-cestac-instrument PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-cestac-instrument PRIVATE LLVM)
  set_target_properties(fpt-cestac-instrument PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()
if(FPT_BUILD_TRANSFORM)
  add_executable(fpt-baseline-types baseline_types.cpp)
  target_include_directories(fpt-baseline-types SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(fpt-baseline-types PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-baseline-types PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-baseline-types PRIVATE clang-cpp LLVM)
  set_target_properties(fpt-baseline-types PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
  add_executable(fpt-hifp-profile hifp_profile.cpp)
  target_include_directories(fpt-hifp-profile SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(fpt-hifp-profile PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-hifp-profile PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-hifp-profile PRIVATE clang-cpp LLVM)
  set_target_properties(fpt-hifp-profile PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
  add_executable(fpt-benchmark-adapt benchmark_adapt.cpp)
  target_include_directories(fpt-benchmark-adapt SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(fpt-benchmark-adapt PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-benchmark-adapt PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-benchmark-adapt PRIVATE clang-cpp LLVM)
  set_target_properties(fpt-benchmark-adapt PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
  add_executable(fpt-precision-transform precision_transform.cpp)
  target_include_directories(fpt-precision-transform SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(fpt-precision-transform PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-precision-transform PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-precision-transform PRIVATE clang-cpp LLVM)
  set_target_properties(fpt-precision-transform PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()

if(FPT_BUILD_TRANSFORM)
  add_executable(fpt-cpu-simd cpu_simd.cpp)
  target_include_directories(fpt-cpu-simd SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(fpt-cpu-simd PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-cpu-simd PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-cpu-simd PRIVATE clang-cpp LLVM)
  set_target_properties(fpt-cpu-simd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()
if(FPT_BUILD_TRANSFORM)
  add_executable(fpt-cpu-simd-verify cpu_simd_verify.cpp)
  target_include_directories(fpt-cpu-simd-verify SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS})
  target_compile_definitions(fpt-cpu-simd-verify PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-cpu-simd-verify PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-cpu-simd-verify PRIVATE LLVM)
  set_target_properties(fpt-cpu-simd-verify PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()

# Restricted complete-application owned point storage adapter (Altis Kmeans).
if(FPT_BUILD_TRANSFORM)
  add_executable(fpt-kmeans-storage kmeans_storage.cpp)
  target_include_directories(fpt-kmeans-storage SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(fpt-kmeans-storage PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-kmeans-storage PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-kmeans-storage PRIVATE clang-cpp LLVM)
  set_target_properties(fpt-kmeans-storage PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()

# Restricted complete-executable CPU owned input storage conversion.
if(FPT_BUILD_TRANSFORM)
  add_executable(fpt-owned-inputs owned_inputs.cpp)
  target_include_directories(fpt-owned-inputs SYSTEM PRIVATE ${LLVM_INCLUDE_DIRS} ${CLANG_INCLUDE_DIRS})
  target_compile_definitions(fpt-owned-inputs PRIVATE ${LLVM_DEFINITIONS})
  target_compile_options(fpt-owned-inputs PRIVATE -Wall -Wextra -Wpedantic)
  target_link_libraries(fpt-owned-inputs PRIVATE clang-cpp LLVM)
  set_target_properties(fpt-owned-inputs PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
endif()

# All targets created in this directory consume LLVM C++ interfaces, including
# optional instrumentation and transformation tools. Do not affect sibling dirs.
get_property(fpt_llvm_targets DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
foreach(target IN LISTS fpt_llvm_targets)
  get_target_property(fpt_llvm_target_type ${target} TYPE)
  if(fpt_llvm_target_type STREQUAL "EXECUTABLE")
    fpt_match_llvm_rtti(${target})
  endif()
endforeach()
