[CMake] Set Source Property For Specific Target

Kawicki, Ryan H Ryan.H.Kawicki at boeing.com
Thu Mar 17 13:53:44 EDT 2011


> > Quick question.
> > 
> > I am trying to add precompiled headers to a project that 
> has been converted to use Cmake.
> > 
> > In our library directory, we include all our source and 
> build two targets (one static and one dynamic), but setting 
> multiple source values only affects the source and not the 
> source associated to a particular target.  Is there a way to 
> get around this issue or something in the works to correct 
> this, like an optional target command?
> > 
> > Thanks...
> > 
> > Code below describing the issue:
> > 
> > FUNCTION (ADD_MSVC_PRECOMPILED_HEADERS
> >           PrecompiledSource PrecompiledHeader TargetProject)
> >   IF (MSVC)
> >     # define the location of the precompiled header
> >     SET(PrecompiledBinary 
> "${CMAKE_CURRENT_BINARY_DIR}/${TargetProject}.pch")
> >     # set the target project to use the precompiled binary
> >     SET_PROPERTY(TARGET ${TargetProject}
> >                  APPEND
> >                  PROPERTY COMPILE_FLAGS
> >                  "/Yu\"${PrecompiledHeader}\" 
> /Fp\"${PrecompiledBinary}\"")
> >     # set the source file to compile out the pch
> >     SET_PROPERTY(SOURCE ${PrecompiledSource}
> >                  APPEND
> >                  PROPERTY COMPILE_FLAGS
> >                  "/Yc\"${PrecompiledHeader}\" 
> /Fp\"${PrecompiledBinary}\"")
> >   ENDIF (MSVC)
> > ENDFUNCTION (ADD_MSVC_PRECOMPILED_HEADERS)
> > 
> > ADD_LIBRARY(MyLibDynamic SHARED ${SRC}) 
> ADD_LIBRARY(MyLibStatic STATIC 
> > ${SRC})
> > 
> > ADD_MSVC_PRECOMPILED_HEADERS(StdAfx.cpp StdAfx.h MyLibDynamic) 
> > ADD_MSVC_PRECOMPILED_HEADERS(StdAfx.cpp StdAfx.h MyLibStatic)
> > 
> > When MyLibDynamic builds, it builds MyLibStatic.pch but 
> tries to find MyLibDynamic.pch.
> > This is because the second function call to setup 
> precompiled headers 
> > modifies what appears to be a property that is not unique to all 
> > targets added at the same directory level of the CMakeLists.
> > 
> > Thanks again. . .
> 
> You might copy or symlink the concerned sources, one per 
> target, and impose the properties on these symlinks, e.g. as follows:
> 
> ADD_CUSTOM_COMMAND(
>     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/StdAfxDynamic.cpp
>     OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/StdAfxStatic.cpp
>     COMMAND ${CMAKE_COMMAND} -E create_symlink
>         ${CMAKE_CURRENT_SOURCE_DIR}/StdAfx.cpp
>         ${CMAKE_CURRENT_BINARY_DIR}/StdAfxDynamic.cpp
>     COMMAND ${CMAKE_COMMAND} -E create_symlink
>         ${CMAKE_CURRENT_SOURCE_DIR}/StdAfx.cpp
>         ${CMAKE_CURRENT_BINARY_DIR}/StdAfxStatic.cpp
>     DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/StdAfx.cpp
> )
> 
> ADD_LIBRARY(MyLibDynamic SHARED StdAfxDynamic.cpp ${SRC}) 
> ADD_LIBRARY(MyLibStatic STATIC StdAfxStatic.cpp ${SRC})
> 
> ADD_MSVC_PRECOMPILED_HEADERS(StdAfxDynamic.cpp StdAfx.h 
> MyLibDynamic) ADD_MSVC_PRECOMPILED_HEADERS(StdAfxStatic.cpp 
> StdAfx.h MyLibStatic)
> 
> Of course, SRC mustn't contain StdAfx.cpp anymore for this to work.
> 
> 'hope that helps.
> 
> Regards,
> 
> Michael

Thanks for the reply.  While I haven't tested this solution out,
I don't think I would prefer this kind of solution, as I would
hope that CMake would support this option out of the box.

Looks like I'll have to see if there is a ticket for this
kind of change or search the code to see how hard this
can be added to CMake.

Thanks,

Ryan


More information about the CMake mailing list