public
Last active

cmake module for setting up precompiled headers (MSVC & GCC)

  • Download Gist
PrecompiledHeader.cmake
CMake
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
# Macro for setting up precompiled headers. Usage:
#
# add_precompiled_header(target header.h [FORCEINCLUDE])
#
# MSVC: A source file with the same name as the header must exist and
# be included in the target (E.g. header.cpp).
#
# MSVC: Add FORCEINCLUDE to automatically include the precompiled
# header file from every source file.
#
# GCC: The precompiled header is always automatically included from
# every header file.
#
# Copyright (C) 2009-2013 Lars Christensen <larsch@belunktum.dk>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the ‘Software’), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
 
MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
GET_FILENAME_COMPONENT(_inputWe ${_input} NAME_WE)
SET(pch_source ${_inputWe}.cpp)
FOREACH(arg ${ARGN})
IF(arg STREQUAL FORCEINCLUDE)
SET(FORCEINCLUDE ON)
ELSE(arg STREQUAL FORCEINCLUDE)
SET(FORCEINCLUDE OFF)
ENDIF(arg STREQUAL FORCEINCLUDE)
ENDFOREACH(arg)
 
IF(MSVC)
GET_TARGET_PROPERTY(sources ${_targetName} SOURCES)
SET(_sourceFound FALSE)
FOREACH(_source ${sources})
SET(PCH_COMPILE_FLAGS "")
IF(_source MATCHES \\.\(cc|cxx|cpp\)$)
GET_FILENAME_COMPONENT(_sourceWe ${_source} NAME_WE)
IF(_sourceWe STREQUAL ${_inputWe})
SET(PCH_COMPILE_FLAGS "${PCH_COMPILE_FLAGS} /Yc${_input}")
SET(_sourceFound TRUE)
ELSE(_sourceWe STREQUAL ${_inputWe})
SET(PCH_COMPILE_FLAGS "${PCH_COMPILE_FLAGS} /Yu${_input}")
IF(FORCEINCLUDE)
SET(PCH_COMPILE_FLAGS "${PCH_COMPILE_FLAGS} /FI${_input}")
ENDIF(FORCEINCLUDE)
ENDIF(_sourceWe STREQUAL ${_inputWe})
SET_SOURCE_FILES_PROPERTIES(${_source} PROPERTIES COMPILE_FLAGS "${PCH_COMPILE_FLAGS}")
ENDIF(_source MATCHES \\.\(cc|cxx|cpp\)$)
ENDFOREACH()
IF(NOT _sourceFound)
MESSAGE(FATAL_ERROR "A source file for ${_input} was not found. Required for MSVC builds.")
ENDIF(NOT _sourceFound)
ENDIF(MSVC)
 
IF(CMAKE_COMPILER_IS_GNUCXX)
GET_FILENAME_COMPONENT(_name ${_input} NAME)
SET(_source "${CMAKE_CURRENT_SOURCE_DIR}/${_input}")
SET(_outdir "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch")
MAKE_DIRECTORY(${_outdir})
SET(_output "${_outdir}/.c++")
STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
SET(_compiler_FLAGS ${${_flags_var_name}})
GET_DIRECTORY_PROPERTY(_directory_flags INCLUDE_DIRECTORIES)
FOREACH(item ${_directory_flags})
LIST(APPEND _compiler_FLAGS "-I${item}")
ENDFOREACH(item)
 
GET_DIRECTORY_PROPERTY(_directory_flags DEFINITIONS)
LIST(APPEND _compiler_FLAGS ${_directory_flags})
 
SEPARATE_ARGUMENTS(_compiler_FLAGS)
MESSAGE("${CMAKE_CXX_COMPILER} -DPCHCOMPILE ${_compiler_FLAGS} -x c++-header -o {_output} ${_source}")
ADD_CUSTOM_COMMAND(
OUTPUT ${_output}
COMMAND ${CMAKE_CXX_COMPILER} ${_compiler_FLAGS} -x c++-header -o ${_output} ${_source}
DEPENDS ${_source} )
ADD_CUSTOM_TARGET(${_targetName}_gch DEPENDS ${_output})
ADD_DEPENDENCIES(${_targetName} ${_targetName}_gch)
SET_TARGET_PROPERTIES(${_targetName} PROPERTIES COMPILE_FLAGS "-include ${_name} -Winvalid-pch")
ENDIF(CMAKE_COMPILER_IS_GNUCXX)
ENDMACRO()

Hi,
I've got one suggestion for your macro. Change it into a "FUNCTION". The variable FORCEINCLUDE is a global one and when this macro is used for the 2nd execution it fails. If you used a FUNCTION it will work better.

But anyway it will set props for all the sources in multiple targets.

Ivan

I'm not very familiar with cmake, but I keep on getting this error:

$ make -j4 LoopingLibrary
make[3]: *** No rule to make target Includes.h', needed byIncludes.h.gch/.c++'. Stop.
make[2]: *** [CMakeFiles/LoopingLibrary_gch.dir/all] Error 2
make[1]: *** [CMakeFiles/LoopingLibrary.dir/rule] Error 2
make: *** [LoopingLibrary] Error 2

I suggest using this (below) MACRO to check for the forceinclude option.
http://www.cmake.org/Wiki/CMakeMacroParseArguments

I'd suggest using GET_PROPERTY(_target_flags TARGET ${_targetName} PROPERTY INCLUDE_DIRECTORIES) instead of GET_DIRECTORY_PROPERTY. The current code doesn't pick up include directories added by the qt5_use_modules command.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.