set(TOOL_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/third-party/include)
set(TOOL_LIB_DIR ${CMAKE_SOURCE_DIR}/third-party/lib)
include(CMakeParseArguments)

function(find_include target)
    set(options REQUIRED)
    set(multiValueArgs HEADERS PATHS)
    cmake_parse_arguments(FIND_INC "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    list(APPEND FIND_INC_PATHS ${TOOL_INCLUDE_DIR})
    foreach (INCNAME ${FIND_INC_HEADERS})
        find_path(PINC_${INCNAME} NAMES ${INCNAME}  PATHS ${FIND_INC_PATHS})
        if (NOT PINC_${INCNAME})
            if (FIND_INC_REQUIRED)
                message(FATAL_ERROR "${INCNAME} not found")
            else ()
                message(STATUS "${INCNAME} not found")
            endif (FIND_INC_REQUIRED)
            set(${target} PARENT_SCOPE)
            return()
        endif ()
        list(APPEND ${target} ${PINC_${INCNAME}})
#        message(STATUS "found include ${INCNAME}=${PINC_${INCNAME}}")
    endforeach ()
    set(${target} ${${target}} PARENT_SCOPE)
endfunction()

function(find_lib target)
    set(options STATIC DYNAMIC PICO_AUTO REQUIRED)
    set(multiValueArgs LIBS PATHS)

    cmake_parse_arguments(FIND_LIB "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
    if (FIND_LIB_PICO_AUTO)
        if (PICO_DEP_LINK_MODE STREQUAL "static")
            set(FIND_LIB_STATIC TRUE)
        endif ()
    endif (FIND_LIB_PICO_AUTO)
    if (FIND_LIB_STATIC)
        set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
        set(suffix "_static")
    elseif (FIND_LIB_DYNAMIC)
        set(CMAKE_FIND_LIBRARY_SUFFIXES .so .dylib .dll)
        set(suffix "")
    endif ()
    list(APPEND FIND_LIB_PATHS ${TOOL_LIB_DIR})

    foreach (LIBNAME ${FIND_LIB_LIBS})
 
        find_library(PLIB_${LIBNAME}${suffix} NAMES ${LIBNAME} PATHS ${FIND_LIB_PATHS})

        if (NOT PLIB_${LIBNAME}${suffix})
            if (FIND_LIB_REQUIRED)
                message(FATAL_ERROR "${LIBNAME}${suffix} not found")
            else ()
                message(STATUS "${LIBNAME}${suffix} not found")
            endif (FIND_LIB_REQUIRED)
            set(${target} PARENT_SCOPE)
            return()
        endif ()
        list(APPEND ${target} ${PLIB_${LIBNAME}${suffix}})
        message(STATUS "found lib ${LIBNAME}=${PLIB_${LIBNAME}${suffix}}")
    endforeach ()
    set(${target} ${${target}} PARENT_SCOPE)
endfunction()

#find_include(P_INCLUDE HEADERS P_cblas.h PATHS ${PROOT}/include /opt/intel/P/include)
#
#find_lib(P_LIB STATIC
#        LIBS P_intel_lp64 P_sequential P_core
#        PATHS ${PROOT}/lib ${PROOT}/lib/intel64)
#find_lib(P_LIB PICO_AUTO
#        LIBS P_intel_lp64 P_sequential P_core
#        PATHS ${PROOT}/lib ${PROOT}/lib/intel64)
#find_lib(P_LIB DYNAMIC
#        LIBS P_rt
#        PATHS ${PROOT}/lib ${PROOT}/lib/intel64)
