cmake_minimum_required(VERSION 3.20.0)
project(SimpleProject)

# Find LLVM package and save all the imported targets into a list.
find_package(LLVM REQUIRED CONFIG)
get_property(LLVMImportedTargets DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)

# Find Clang package and save all the imported targets into a list. As Clang's
# find_package depends on LLVM - it calls find_package(LLVM) internally - this
# implicitly import all the LLVM targets too, so we have to filter these out
# with the previous list.
find_package(Clang REQUIRED CONFIG)
get_property(ClangImportedTargets DIRECTORY "${CMAKE_SOURCE_DIR}" PROPERTY IMPORTED_TARGETS)
list(REMOVE_ITEM ClangImportedTargets ${LLVMImportedTargets})

# Filter out targets imported from internal find_package such as FFI, LibEdit, ZLIB, etc
# These might or might not be installed in the user's system. These are prefixed
# with "<MODULE>::" (e.g. LibEdit::LibEdit).
list(FILTER LLVMImportedTargets EXCLUDE REGEX "::")

# For all imported targets by LLVM get the location of the object and dump It
# to an output.txt file. The test script will parse it later.
foreach(target IN LISTS LLVMImportedTargets)
    get_target_property(tgt_loc ${target} LOCATION)
    file(APPEND ${CMAKE_BINARY_DIR}/llvm_targets.txt "${tgt_loc}\n")
endforeach()

# Do the same operation with Clang imported targets
foreach(target IN LISTS ClangImportedTargets)
    get_target_property(tgt_loc ${target} LOCATION)
    file(APPEND ${CMAKE_BINARY_DIR}/clang_targets.txt "${tgt_loc}\n")
endforeach()
