function(auto_sources RETURN_VALUE PATTERN SOURCE_SUBDIRS)
    if ("${SOURCE_SUBDIRS}" STREQUAL "RECURSE")
        SET(PATH ".")
        if (${ARGC} EQUAL 4)
            list(GET ARGV 3 PATH)
        endif ()
    endif ()

    if ("${SOURCE_SUBDIRS}" STREQUAL "RECURSE")
        unset(${RETURN_VALUE})
        file(GLOB SUBDIR_FILES "${PATH}/${PATTERN}")
        list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})

        file(GLOB subdirs RELATIVE ${PATH} ${PATH}/*)

        foreach (DIR ${subdirs})
            if (IS_DIRECTORY ${PATH}/${DIR})
                if (NOT "${DIR}" STREQUAL "CMakeFiles")
                    file(GLOB_RECURSE SUBDIR_FILES "${PATH}/${DIR}/${PATTERN}")
                    list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
                endif ()
            endif ()
        endforeach ()
    else ()
        file(GLOB ${RETURN_VALUE} "${PATTERN}")

        foreach (PATH ${SOURCE_SUBDIRS})
            file(GLOB SUBDIR_FILES "${PATH}/${PATTERN}")
            list(APPEND ${RETURN_VALUE} ${SUBDIR_FILES})
        endforeach ()
    endif ()

    set(${RETURN_VALUE} ${${RETURN_VALUE}} PARENT_SCOPE)
endfunction(auto_sources)