I've spent all day trying to get a single precompiled header to work on a series of targets within a rather complex project. I'll profess to a fair amount of stabbing in the dark.

I came up with the following, but it doesn't bother building the PCH until the *end* of the first target. And then it fails because it doesn't have the compiler definitions needed.

So, how do I get my ADD_PCH(targetname) macro to prioritize the PCH file, without making it always build the pch?

Note: All of the candidate targets use the project include files, compiler flags and definitions.//

8x --- snip --- x8

   SET(_PCH_SRC "${Project_SOURCE_DIR}/cfg/syscfg.h")
   SET(_PCH_HDR ${_PCH_SRC})    # Viable as a dependency name.
   IF ( _USE_PCH )
        IF (WINDOWS)
            SET(_PCH_HDR "${_PCH_SRC}.pch")
        ELSE ()
            SET(_PCH_HDR "${_PCH_SRC}.gch")
        ENDIF()
        SET(_pchFlags "${CMAKE_CXX_FLAGS}")
        GET_DIRECTORY_PROPERTY(_pcfIncludePaths INCLUDE_DIRECTORIES)
        FOREACH (_inc ${_pcfIncludePaths})
            LIST(APPEND _pchFlags "-I" ${_inc})
        ENDFOREACH()
        GET_DIRECTORY_PROPERTY(_pcfIncludePaths COMPILE_DEFINITIONS)
        FOREACH (_inc ${_pcfIncludePaths})
            LIST(APPEND _pchFlags "-D" ${_inc})
        ENDFOREACH()
        SEPARATE_ARGUMENTS(_pchFlags)
        ADD_CUSTOM_COMMAND(
                PRE_BUILD
                OUTPUT ${_PCH_HDR}
                COMMAND ${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1} 
${_pchFlags} -o ${_PCH_HDR} ${_PCH_SRC}
                MAIN_DEPENDENCY ${_PCH_SRC}
                IMPLICIT_DEPENDS CXX ${_PCH_SRC}
                DEPENDS ${_PCH_SRC}
        )
   ENDIF ( _USE_PCH )

   MACRO(ADD_PCH _TARGET)
     IF ( WW2_USE_PCH )
      MESSAGE(STATUS "Adding pch to ${_TARGET}")
      add_dependencies(${_TARGET} ${_PCH_HDR})
     ENDIF ( )
   ENDMACRO()

8x --- snip --- x8

For bonus points: The business of having to copy the include paths and compiler definitions seems really clumsy, it's horribly platform dependent. Surely there's a better way say "build it the same way you'd build anything else in this directory"????

- Oliver

--

Powered by www.kitware.com

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake

Reply via email to