MACRO(LIST_FILES LIST_FILE VAR GLOB)
  SET(OUTPUT_LIST ${CMAKE_CURRENT_BINARY_DIR}/${LIST_FILE})
  SET(OUTPUT_LIST_TMP ${CMAKE_CURRENT_BINARY_DIR}/${LIST_FILE}.tmp)
  SET(GLOB_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${GLOB})
  ADD_CUSTOM_TARGET(${LIST_FILE}
    COMMAND echo "SET\\(${VAR} " > ${OUTPUT_LIST_TMP}
    COMMAND ls ${GLOB_PATH} | sort >> ${OUTPUT_LIST_TMP}
    COMMAND echo "\\)" >> ${OUTPUT_LIST_TMP}
    COMMAND diff ${OUTPUT_LIST} ${OUTPUT_LIST_TMP} > /dev/null || mv ${OUTPUT_LIST_TMP} ${OUTPUT_LIST})
  FILE(GLOB INITIAL_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${GLOB})
  FILE(WRITE ${OUTPUT_LIST} "SET(${VAR} ${INITIAL_LIST})")
  INCLUDE(${OUTPUT_LIST})
ENDMACRO(LIST_FILES LIST_FILE VAR GLOB)

