[CMake] Secret precompiled header support?

Robert Dailey rcdailey.lists at gmail.com
Wed May 9 14:32:39 EDT 2012


To my knowledge there are no dedicated target properties or commands that
allow one to easily add precompiled headers support to a target. In my
case, I'm generating for visual studio and I have the following that I can
use to enable precompiled headers:

macro( _precompiled_headers PrecompiledHeader PrecompiledSource SourcesVar )
if( MSVC )
get_filename_component( PrecompiledBasename ${PrecompiledHeader} NAME_WE )
set( PrecompiledBinary
"${CMAKE_CURRENT_BINARY_DIR}/${PrecompiledBasename}.pch" )
set( Sources ${${SourcesVar}} )

set_property(
SOURCE ${PrecompiledSource} APPEND_STRING PROPERTY
COMPILE_FLAGS "/Yc\"${PrecompiledHeader}\" /Fp\"${PrecompiledBinary}\""
)

set_property(
SOURCE ${PrecompiledSource} APPEND PROPERTY
OBJECT_OUTPUTS "${PrecompiledBinary}"
)
 set_property(
SOURCE ${Sources} APPEND_STRING PROPERTY
COMPILE_FLAGS "/Yu\"${PrecompiledHeader}\" /Fp\"${PrecompiledBinary}\""
)
 set_property(
SOURCE ${Sources} APPEND PROPERTY
OBJECT_DEPENDS "${PrecompiledBinary}"
)
else()
message( "Precompiled header support not provided for this platform" )
endif()
 # Add precompiled header to SourcesVar
list( APPEND ${SourcesVar} ${PrecompiledSource} )
endmacro()

The one interesting thing I have noticed is the way these compile flags
translate to actual vcproj output:

<FileConfiguration
Name="MinSizeRel|Win32">
 <Tool
Name="VCCLCompilerTool"
ForcedIncludeFiles="C:/Code/work/cmake-decomp/cmake/files/source/hash_map_hack.hpp;StdAfx.cpp"

PrecompiledHeaderFile="C:/Code/work/cmake-decomp/build-vc9/server/exchange/tools/uploadlog/StdAfx.pch"
PrecompiledHeaderThrough="StdAfx.h"
 UsePrecompiledHeader="2"
AdditionalDependencies="C:\Code\work\cmake-decomp\build-vc9\server\exchange\tools\uploadlog\StdAfx.pch"
 />

CMake seems to do more than just add them as general compiler flags, it
seems to know exactly which attributes in the VCPROJ XML are mapped to
their respective command line alternatives, and uses the appropriate ones.
If this is true, why do we not have a dedicated target & source file
property that I can use to enable precompiled headers instead of using
these command line strings directly? Am I misunderstanding something?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20120509/057dea39/attachment-0001.htm>


More information about the CMake mailing list