# Reference from Belledonne Communications, Grenoble France.
macro(utils_apply_compile_flags SOURCE_FILES)
    if(${SOURCE_FILES})
        set(options "")
        foreach(a ${ARGN})
            if(${a})
                string(REPLACE ";" " " options_${a} "${${a}}")
                set(options "${options} ${options_${a}}")
            endif()
        endforeach()
        if(options)
            set_source_files_properties(${${SOURCE_FILES}} PROPERTIES COMPILE_FLAGS "${options}")
        endif()
    endif()
endmacro()

macro(use_c99)
    if (CMAKE_VERSION VERSION_LESS "3.1")
        if (CMAKE_C_COMPILER_ID STREQUAL "GNU")
            set (CMAKE_C_FLAGS "-std=gnu99 ${CMAKE_C_FLAGS}")
        endif ()
    else ()
        set (CMAKE_C_STANDARD 99)
    endif ()
endmacro(use_c99)

macro(get_dir_basename basename)
    STRING( REGEX REPLACE ".*/(.*)" "\\1" ${basename} ${CMAKE_CURRENT_SOURCE_DIR} )
endmacro()

macro(add_dir_basename OUT_FILES)
    set(BASENAME )
    get_dir_basename(BASENAME)
    foreach(a ${ARGN})
        string(REPLACE "${a}" "${BASENAME}/${a}" o_${a} "${a}")
        list(APPEND ${OUT_FILES} ${o_${a}})
    endforeach()
endmacro()

macro(add_head_foreach OUT_FILES head)
    foreach(a ${ARGN})
        string(REPLACE "${a}" "${head}/${a}" o_${a} "${a}")
        list(APPEND ${OUT_FILES} ${o_${a}})
    endforeach()
endmacro()

macro(get_file_basename basename filename)
    STRING( REGEX REPLACE "^/?([^/]*/)*([^/]*)\\..*$" "\\2" ${basename} "${filename}" )
endmacro()