INCLUDE(ExternalProject)

SET(prefix "${CMAKE_BINARY_DIR}/third_party/clFFT")
SET(clFFT_location ${prefix}/lib/import/${CMAKE_STATIC_LIBRARY_PREFIX}clFFT${CMAKE_STATIC_LIBRARY_SUFFIX})
IF(CMAKE_VERSION VERSION_LESS 3.2)
    IF(CMAKE_GENERATOR MATCHES "Ninja")
        MESSAGE(WARNING "Building clFFT with Ninja has known issues with CMake older than 3.2")
    endif()
    SET(byproducts)
ELSE()
    SET(byproducts BYPRODUCTS ${clFFT_location})
ENDIF()

ExternalProject_Add(
    clFFT-ext
    GIT_REPOSITORY https://github.com/arrayfire/clFFT.git
    GIT_TAG af3.3.1
    PREFIX "${prefix}"
    INSTALL_DIR "${prefix}"
    UPDATE_COMMAND ""
    CONFIGURE_COMMAND ${CMAKE_COMMAND} -Wno-dev "-G${CMAKE_GENERATOR}" <SOURCE_DIR>/src
    -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
    "-DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS} -w -fPIC"
    -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
    "-DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS} -w -fPIC"
    -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
    -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
    -DBUILD_SHARED_LIBS:BOOL=OFF
    -DBUILD_EXAMPLES:BOOL=OFF
    -DBUILD_CLIENT:BOOL=OFF
    -DBUILD_TEST:BOOL=OFF
    -DSUFFIX_LIB:STRING=
    -DUSE_SYSTEM_GTEST:BOOL=ON
    ${byproducts}
    )

ExternalProject_Get_Property(clFFT-ext install_dir)
ADD_LIBRARY(clFFT IMPORTED STATIC)
SET_TARGET_PROPERTIES(clFFT PROPERTIES IMPORTED_LOCATION ${clFFT_location})
ADD_DEPENDENCIES(clFFT clFFT-ext)
SET(CLFFT_INCLUDE_DIRS ${install_dir}/include)
SET(CLFFT_LIBRARIES clFFT)
SET(CLFFT_FOUND ON)
