# 2021-1-26:created by Davis
#cmake utils contains common function and macro

macro(global_set Name Value)
    set(${Name} "${Value}" CACHE STRING "NoDesc" FORCE)
endmacro()

macro(condition_set Name Value)
    if (NOT ${Name})
        global_set(${Name} ${Value})
    endif ()
endmacro()

#cstl to string split by blank
#the output value must be global
#OUTPUT is the name of output val
function(list_to_str OUTPUT)
    global_set(${OUTPUT} "")
    foreach(item ${ARGN})
        if(NOT ${OUTPUT} STREQUAL "")
            global_set(${OUTPUT} "${${OUTPUT}} ${item}")
        else()
            global_set(${OUTPUT} "${item}")
        endif()
    endforeach()
endfunction()
function(clear_flags)
    global_set(CMAKE_C_FLAGS "")
    global_set(CMAKE_CXX_FLAGS "")
    global_set(LDFLAGS "")
    global_set(CMAKE_EXE_LINKER_FLAGS "")
    global_set(CMAKE_SHARED_LINKER_FLAGS "")
    global_set(CMAKE_MODULE_LINKER_FLAGS "")
endfunction()
function(add_compile_flags WHERE)
    list_to_str(STRING_ARGS ${ARGN})
    if (${WHERE} STREQUAL C)
        global_set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${STRING_ARGS}")

    elseif (${WHERE} STREQUAL CXX)
        global_set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STRING_ARGS}")

    elseif (${WHERE} STREQUAL LD)
        global_set(LDFLAGS "${LDFLAGS} ${STRING_ARGS}")
        global_set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${STRING_ARGS}")
        global_set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${STRING_ARGS}")
        global_set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${STRING_ARGS}")

    elseif (${WHERE} STREQUAL BOTH)
        add_compile_flags(C ${ARGN})
        add_compile_flags(CXX ${ARGN})

    elseif (${WHERE} STREQUAL ALL)
        add_compile_flags(C ${ARGN})
        add_compile_flags(CXX ${ARGN})
        add_compile_flags(LD ${ARGN})
    else ()
        message(FATAL_ERROR "add_compile_flags - only support: C, CXX, BOTH, LD, ALL")
    endif ()
endfunction()

# Add lib headers
macro(header_directories parent)
    file(GLOB_RECURSE newList ${parent}/*.h)
    set(dir_list "")
    foreach (file_path ${newList})
        get_filename_component(dir_path ${file_path} DIRECTORY)
        set(dir_list ${dir_list} ${dir_path})
    endforeach ()
    list(REMOVE_DUPLICATES dir_list)
    include_directories(${dir_list})
endmacro()

macro(add_source_files)
    file(GLOB_RECURSE newlist ${ARGN})
    foreach (filepath ${newlist})
        set(SOURCE_FILES ${SOURCE_FILES} ${filepath} CACHE STRING "Source Files" FORCE)
    endforeach ()
endmacro()

#add file by reduce search parent dir
macro(add_source_files_dir parent)
    file(GLOB_RECURSE newList1 ${parent}/*.c)
    file(GLOB_RECURSE newList2 ${parent}/*.cpp)
    file(GLOB_RECURSE newList3 ${parent}/*.S)
    file(GLOB_RECURSE newList4 ${parent}/*.s)
    set(dir_list "")
    set(newList ${newList1} ${newList2} ${newList3} ${newList4})
    list(REMOVE_DUPLICATES newList)
    global_set(newStr "")
    list_to_str(newStr ${newList})
    set(SOURCE_FILES ${SOURCE_FILES} ${newList} CACHE STRING "Source Files" FORCE)
endmacro()