
function (getListOfVarsStartingWith _prefix _varResult)
    get_cmake_property(_vars VARIABLES)
    string (REGEX MATCHALL "(^|;)${_prefix}[A-Za-z0-9_]*" _matchedVars "${_vars}")
    set (${_varResult} ${_matchedVars} PARENT_SCOPE)
endfunction()

function (ListToString result delim)
    list(GET ARGV 2 temp)
    math(EXPR N "${ARGC}-1")
    foreach(IDX RANGE 3 ${N})
        list(GET ARGV ${IDX} STR)
        set(temp "${temp}${delim}${STR}")
    endforeach()
    set(${result} "${temp}" PARENT_SCOPE)
endfunction(ListToString)


function (TryAddStaticLibToTarget __target __item __lib_path)
    #check redhat case
    get_filename_component(__lib_dir ${__lib_path} DIRECTORY )
    set(__lib64_path "${__lib_dir}64/lib${__item}.a" )

    if(EXISTS "${__lib_path}")
         message(STATUS "found static lib: ${__lib_path}")
         if(NOT TARGET ${item})
             add_library( ${__item} STATIC IMPORTED )
         else()
             message(STATUS "${item} is already in target")
             set_property( TARGET ${__item} PROPERTY STATIC )
         endif()

         set_property( TARGET ${__item} PROPERTY IMPORTED_LOCATION ${__lib_path} )
         target_link_libraries(${__target} ${__item})
    elseif(EXISTS "${__lib64_path}")
        message(STATUS "found static lib: ${__lib64_path}")
         if(NOT TARGET ${item})
             add_library( ${__item} STATIC IMPORTED )
         else()
             message(STATUS "${item} is already in target")
             set_property( TARGET ${__item} PROPERTY STATIC )
         endif()

         set_property( TARGET ${__item} PROPERTY IMPORTED_LOCATION ${__lib64_path} )
         target_link_libraries(${__target} ${__item})
    else()
        message(STATUS "invalid path to static lib: ${__lib_path}")
    endif()
endfunction()

function (AddStaticLibraries _target _libdir )
    foreach(item ${ARGN})
        TryAddStaticLibToTarget(${_target} ${item} ${_libdir}/lib${item}.a)
    endforeach()
endfunction()

function (AddCompilerStaticLibraries _target)
    foreach(item ${ARGN})
	execute_process(COMMAND ${CMAKE_CXX_COMPILER} "-print-file-name=lib${item}.a" OUTPUT_VARIABLE _lib_path) 
	string(STRIP ${_lib_path} _lib_path)
	TryAddStaticLibToTarget(${_target} ${item} ${_lib_path})
    unset(_lib_path CACHE)
   endforeach()
endfunction()

function (AddSystemStaticLibraries _target)
    foreach(item ${ARGN})
	find_library (_lib_path "lib${item}.a")
	TryAddStaticLibToTarget(${_target} ${item} ${_lib_path})
	unset(_lib_path CACHE)
   endforeach()
endfunction()

macro(ternary var boolean value1 value2)
    if(${boolean})
        set(${var} ${value1})
    else()
        set(${var} ${value2})
    endif()
endmacro()

macro(expand var value)
    set(${var} ${value})
endmacro()
