2011-03-12 21:01:23 +00:00
#Copied from http://www.mail-archive.com/cmake@cmake.org/msg04394.html which copied it from the rosengarden project
#see also: http://gcc.gnu.org/onlinedocs/gcc-4.0.4/gcc/Precompiled-Headers.html
2012-03-30 17:47:21 +00:00
MACRO ( ADD_PRECOMPILED_HEADER _targetName _input )
2011-03-12 21:01:23 +00:00
# Prepare environment
GET_FILENAME_COMPONENT ( _name ${ _input } NAME )
2012-04-01 12:23:29 +00:00
SET ( _pchdir "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${_targetName}_pch.dir" )
2012-03-30 17:47:21 +00:00
MAKE_DIRECTORY ( ${ _pchdir } )
2011-03-12 21:01:23 +00:00
SET ( _source "${CMAKE_CURRENT_SOURCE_DIR}/${_input}" )
2012-03-30 17:47:21 +00:00
SET ( _includeput "${_pchdir}/${_name}" )
SET ( _output "${_includeput}.gch" )
2011-03-12 21:01:23 +00:00
# Assemble the compiler command with which future stuff will be built
STRING ( TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name ) # Don't worry: CMake won't use any of the R/D/RWDI/MSR vars if the build type is not defined
if ( CMAKE_CXX_COMPILER_ARG1 ) # please, know what you're doing if you rely on this...
STRING ( STRIP ${ CMAKE_CXX_COMPILER_ARG1 } _ccache_tweak )
SET ( _ccache_tweak "\" ${ _ccache_tweak } \"")
endif ( )
SET ( _compiler_FLAGS ${ _ccache_tweak } ${ CMAKE_CXX_FLAGS } ${ ${_flags_var_name } } )
if ( CMAKE_BUILD_TYPE )
if ( ${ CMAKE_BUILD_TYPE } STREQUAL "Debug" )
LIST ( APPEND _compiler_FLAGS "-D_DEBUG" )
endif ( )
endif ( )
GET_TARGET_PROPERTY ( _compile_defines ${ _targetName } COMPILE_DEFINITIONS )
if ( _compile_defines )
FOREACH ( item ${ _compile_defines } )
STRING ( REPLACE "\" " " \\\"" item ${ item } )
LIST ( APPEND _compiler_FLAGS "-D${item}" )
ENDFOREACH ( item )
endif ( )
GET_TARGET_PROPERTY ( _compile_defines ${ _targetName } COMPILE_DEFINITIONS_ ${ CMAKE_BUILD_TYPE } )
if ( _compile_defines )
FOREACH ( item ${ _compile_defines } )
STRING ( REPLACE "\" " " \\\"" item ${ item } )
2017-02-21 11:58:56 +00:00
LIST ( APPEND _compiler_FLAGS "$<$<NOT:$<STREQUAL:,${item}>>:-D${item}>" )
2011-03-12 21:01:23 +00:00
ENDFOREACH ( item )
endif ( )
GET_DIRECTORY_PROPERTY ( _compile_defines COMPILE_DEFINITIONS )
if ( _compile_defines )
FOREACH ( item ${ _compile_defines } )
STRING ( REPLACE "\" " " \\\"" item ${ item } )
2017-02-21 11:58:56 +00:00
LIST ( APPEND _compiler_FLAGS "$<$<NOT:$<STREQUAL:,${item}>>:-D${item}>" )
2011-03-12 21:01:23 +00:00
ENDFOREACH ( item )
endif ( )
GET_DIRECTORY_PROPERTY ( _compile_defines COMPILE_DEFINITIONS_ ${ CMAKE_BUILD_TYPE } )
if ( _compile_defines )
FOREACH ( item ${ _compile_defines } )
STRING ( REPLACE "\" " " \\\"" item ${ item } )
2017-02-21 11:58:56 +00:00
LIST ( APPEND _compiler_FLAGS "$<$<NOT:$<STREQUAL:,${item}>>:-D${item}>" )
2011-03-12 21:01:23 +00:00
ENDFOREACH ( item )
endif ( )
GET_DIRECTORY_PROPERTY ( _directory_flags INCLUDE_DIRECTORIES )
FOREACH ( item ${ _directory_flags } )
LIST ( APPEND _compiler_FLAGS "-I\" ${ item } \"")
ENDFOREACH ( item )
SEPARATE_ARGUMENTS ( _compiler_FLAGS )
list ( REMOVE_DUPLICATES _compiler_FLAGS )
# Add a target with the pch
ADD_CUSTOM_COMMAND (
O U T P U T $ { _ o u t p u t }
C O M M A N D " $ { C M A K E _ C X X _ C O M P I L E R } "
$ { _ c o m p i l e r _ F L A G S } - x c + + - h e a d e r - o $ { _ o u t p u t } $ { _ s o u r c e }
I M P L I C I T _ D E P E N D S C X X $ { _ s o u r c e }
)
2012-05-01 22:25:45 +00:00
FILE ( WRITE "${_pchdir}/${_name}" "#ifdef __cplusplus\n#warning Precompiled header not used. Turn off or fix!\n#endif" ) # This file is added so the cc-units don't stumble over not being able to include the file
2011-03-12 21:01:23 +00:00
ADD_CUSTOM_TARGET ( ${ _targetName } _pch DEPENDS ${ _output } )
ADD_DEPENDENCIES ( ${ _targetName } ${ _targetName } _pch )
2012-03-30 17:47:21 +00:00
SET_TARGET_PROPERTIES ( ${ _targetName } PROPERTIES COMPILE_FLAGS "-include ${_includeput}" )
2011-03-12 21:01:23 +00:00
ENDMACRO ( ADD_PRECOMPILED_HEADER )