# Based on a script from
# http://www.cmake.org/Bug/view.php?id=1260#c21475
MACRO (ADD_PCH_RULE HEADER SOURCES)
  SET(PRECOMPILED_HEADER "${HEADER}.gch")
  LIST(APPEND ${SOURCES} ${PRECOMPILED_HEADER})

  SET(CURRENT_INC_FLAGS)
  GET_DIRECTORY_PROPERTY(_dirs INCLUDE_DIRECTORIES)
  FOREACH (_dir ${_dirs})
    STRING(REGEX REPLACE ".framework$" ".framework/Headers" _dir ${_dir})
    LIST(APPEND CURRENT_INC_FLAGS "-I${_dir}")
  ENDFOREACH(_dir ${_dirs})

  GET_DIRECTORY_PROPERTY(DEFS DEFINITIONS)

  STRING(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
  IF(QT4_FOUND)
    IF(${BUILD_TYPE} STREQUAL "DEBUG")
      LIST(APPEND DEFS -DQT_DEBUG)
    ELSE(${BUILD_TYPE} STREQUAL "DEBUG")
      LIST(APPEND DEFS -DQT_NO_DEBUG)
    ENDIF(${BUILD_TYPE} STREQUAL "DEBUG")
  ENDIF(QT4_FOUND)

  SET(PCH_FLAGS
    ${DEFS}
    ${PCH_EXTRA_CXX_FLAGS}
    ${CMAKE_CXX_FLAGS}
    ${CMAKE_CXX_FLAGS_${BUILD_TYPE}}
    ${COMPILE_FLAGS}
    ${CURRENT_INC_FLAGS})
  SEPARATE_ARGUMENTS(PCH_FLAGS)
  STRING(REPLACE ";" " " PRETTY_PCH_FLAGS "${PCH_FLAGS}")

  ADD_CUSTOM_COMMAND(OUTPUT ${PRECOMPILED_HEADER}
    COMMAND rm -f ${PRECOMPILED_HEADER}
    COMMAND ${CMAKE_CXX_COMPILER}
                  ${PCH_FLAGS}
                  -c ${CMAKE_CURRENT_SOURCE_DIR}/${HEADER}
                  -o ${CMAKE_CURRENT_BINARY_DIR}/${PRECOMPILED_HEADER}
    DEPENDS ${HEADER})
ENDMACRO(ADD_PCH_RULE HEADER SOURCES)


