# cuDSS: Example CMake Project
#
# Minimal examples building C examples
#

cmake_minimum_required(VERSION 3.12)

project(cudss_examples LANGUAGES C CUDA CXX)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type: Release, Debug, etc")
endif()

set(CMAKE_C_STANDARD 99)

option(BUILD_STATIC "Building cuDSS examples with static linking" ON)

# Find cuDSS
find_package(cudss REQUIRED) # COMPONENTS cudss)

enable_testing()

set(CUDSS_EXAMPLE_SOURCES
    cudss_simple.cpp
    cudss_simple_complex.cpp
    cudss_get_set.cpp
    cudss_dense_matrix_helpers.cpp
    cudss_sparse_matrix_helpers.cpp
    cudss_memory_handlers.cpp
)

foreach(src ${CUDSS_EXAMPLE_SOURCES})
    set_source_files_properties(${src} PROPERTIES LANGUAGE CUDA)

    get_filename_component(name "${src}" NAME_WE)

    add_executable(${name} ${src})

    if (WIN32)
        target_include_directories(${name} PUBLIC ${CUDSS_INSTALL_DIR}/include)
        target_link_directories(${name} PUBLIC ${CUDSS_INSTALL_DIR}/lib)
    endif()

    target_compile_options(${name} PRIVATE
        "$<$<CONFIG:Debug>:-lineinfo -g>"
        "$<$<CONFIG:RelWithDebInfo>:-lineinfo -g>"
    )

    target_link_libraries(${name} PUBLIC
        cudss
    )

    add_test(${name} ${name})
    install(TARGETS ${name}
            RUNTIME DESTINATION examples
            COMPONENT Examples)

    if (BUILD_STATIC)
        add_executable(${name}_static ${src})

        if (WIN32)
            target_include_directories(${name}_static PUBLIC ${CUDSS_INSTALL_DIR}/include)
            target_link_directories(${name}_static PUBLIC ${CUDSS_INSTALL_DIR}/lib)
            target_link_libraries(${name}_static PUBLIC
                cudss
            )
        else()
            target_link_libraries(${name}_static PUBLIC
                cudss_static
            )
        endif()


        add_test(${name}_static ${name}_static)
        install(TARGETS ${name}_static
                RUNTIME DESTINATION example
                COMPONENT Examples
        )

    endif()
endforeach()
