PrecompiledHeader.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # Function for setting up precompiled headers. Usage:
  2. #
  3. # add_library/executable(target
  4. # pchheader.c pchheader.cpp pchheader.h)
  5. #
  6. # add_precompiled_header(target pchheader.h
  7. # [FORCEINCLUDE]
  8. # [SOURCE_C pchheader.c]
  9. # [SOURCE_CXX pchheader.cpp])
  10. #
  11. # Options:
  12. #
  13. # FORCEINCLUDE: Add compiler flags to automatically include the
  14. # pchheader.h from every source file. Works with both GCC and
  15. # MSVC. This is recommended.
  16. #
  17. # SOURCE_C/CXX: Specifies the .c/.cpp source file that includes
  18. # pchheader.h for generating the pre-compiled header
  19. # output. Defaults to pchheader.c. Only required for MSVC.
  20. #
  21. # Caveats:
  22. #
  23. # * Its not currently possible to use the same precompiled-header in
  24. # more than a single target in the same directory (No way to set
  25. # the source file properties differently for each target).
  26. #
  27. # * MSVC: A source file with the same name as the header must exist
  28. # and be included in the target (E.g. header.cpp). Name of file
  29. # can be changed using the SOURCE_CXX/SOURCE_C options.
  30. #
  31. # License:
  32. #
  33. # Copyright (C) 2009-2017 Lars Christensen <larsch@belunktum.dk>
  34. #
  35. # Permission is hereby granted, free of charge, to any person
  36. # obtaining a copy of this software and associated documentation files
  37. # (the 'Software') deal in the Software without restriction,
  38. # including without limitation the rights to use, copy, modify, merge,
  39. # publish, distribute, sublicense, and/or sell copies of the Software,
  40. # and to permit persons to whom the Software is furnished to do so,
  41. # subject to the following conditions:
  42. #
  43. # The above copyright notice and this permission notice shall be
  44. # included in all copies or substantial portions of the Software.
  45. #
  46. # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  47. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  48. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  49. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  50. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  51. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  52. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  53. # SOFTWARE.
  54. # Use the new builtin CMake function if possible or fall back to the old one.
  55. if (CMAKE_VERSION VERSION_LESS 3.16)
  56. include(CMakeParseArguments)
  57. macro(combine_arguments _variable)
  58. set(_result "")
  59. foreach(_element ${${_variable}})
  60. set(_result "${_result} \"${_element}\"")
  61. endforeach()
  62. string(STRIP "${_result}" _result)
  63. set(${_variable} "${_result}")
  64. endmacro()
  65. function(export_all_flags _filename)
  66. set(_include_directories "$<TARGET_PROPERTY:${_target},INCLUDE_DIRECTORIES>")
  67. set(_compile_definitions "$<TARGET_PROPERTY:${_target},COMPILE_DEFINITIONS>")
  68. set(_compile_flags "$<TARGET_PROPERTY:${_target},COMPILE_FLAGS>")
  69. set(_compile_options "$<TARGET_PROPERTY:${_target},COMPILE_OPTIONS>")
  70. #handle config-specific cxx flags
  71. string(TOUPPER ${CMAKE_BUILD_TYPE} _config)
  72. set(_build_cxx_flags ${CMAKE_CXX_FLAGS_${_config}})
  73. #handle fpie option
  74. get_target_property(_fpie ${_target} POSITION_INDEPENDENT_CODE)
  75. if (_fpie AND CMAKE_POSITION_INDEPENDENT_CODE)
  76. list(APPEND _compile_options ${CMAKE_CXX_COMPILE_OPTIONS_PIC})
  77. endif()
  78. #handle compiler standard (GCC only)
  79. if(CMAKE_COMPILER_IS_GNUCXX)
  80. get_target_property(_cxx_standard ${_target} CXX_STANDARD)
  81. if ((NOT "${_cxx_standard}" STREQUAL NOTFOUND) AND (NOT "${_cxx_standard}" STREQUAL ""))
  82. get_target_property(_cxx_extensions ${_target} CXX_EXTENSIONS)
  83. get_property(_exists TARGET ${_target} PROPERTY CXX_EXTENSIONS SET)
  84. if (NOT _exists OR ${_cxx_extensions})
  85. list(APPEND _compile_options "-std=gnu++${_cxx_standard}")
  86. else()
  87. list(APPEND _compile_options "-std=c++${_cxx_standard}")
  88. endif()
  89. endif()
  90. endif()
  91. set(_include_directories "$<$<BOOL:${_include_directories}>:-I$<JOIN:${_include_directories},\n-I>\n>")
  92. set(_compile_definitions "$<$<BOOL:${_compile_definitions}>:-D$<JOIN:${_compile_definitions},\n-D>\n>")
  93. set(_compile_flags "$<$<BOOL:${_compile_flags}>:$<JOIN:${_compile_flags},\n>\n>")
  94. set(_compile_options "$<$<BOOL:${_compile_options}>:$<JOIN:${_compile_options},\n>\n>")
  95. set(_cxx_flags "$<$<BOOL:${CMAKE_CXX_FLAGS}>:${CMAKE_CXX_FLAGS}\n>$<$<BOOL:${_build_cxx_flags}>:${_build_cxx_flags}\n>")
  96. file(GENERATE OUTPUT "${_filename}" CONTENT "${_compile_definitions}${_include_directories}${_compile_flags}${_compile_options}${_cxx_flags}\n")
  97. endfunction()
  98. function(add_precompiled_header _target _input)
  99. message(STATUS "Adding precompiled header ${_input} to target ${_target} with legacy method. "
  100. "Update your cmake instance to use the native PCH functions.")
  101. cmake_parse_arguments(_PCH "FORCEINCLUDE" "SOURCE_CXX;SOURCE_C" "" ${ARGN})
  102. get_filename_component(_input_we ${_input} NAME_WE)
  103. get_filename_component(_input_full ${_input} ABSOLUTE)
  104. file(TO_NATIVE_PATH "${_input_full}" _input_fullpath)
  105. if(NOT _PCH_SOURCE_CXX)
  106. set(_PCH_SOURCE_CXX "${_input_we}.cpp")
  107. endif()
  108. if(NOT _PCH_SOURCE_C)
  109. set(_PCH_SOURCE_C "${_input_we}.c")
  110. endif()
  111. if(MSVC)
  112. set(_pch_cxx_pch "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/cxx_${_input_we}.pch")
  113. set(_pch_c_pch "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/c_${_input_we}.pch")
  114. get_target_property(sources ${_target} SOURCES)
  115. foreach(_source ${sources})
  116. set(_pch_compile_flags "")
  117. if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
  118. if(_source MATCHES \\.\(cpp|cxx|cc\)$)
  119. set(_pch_header "${_input}")
  120. set(_pch "${_pch_cxx_pch}")
  121. else()
  122. set(_pch_header "${_input}")
  123. set(_pch "${_pch_c_pch}")
  124. endif()
  125. if(_source STREQUAL "${_PCH_SOURCE_CXX}")
  126. set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" \"/Yc${_input}\"")
  127. set(_pch_source_cxx_found TRUE)
  128. set_source_files_properties("${_source}" PROPERTIES OBJECT_OUTPUTS "${_pch_cxx_pch}")
  129. elseif(_source STREQUAL "${_PCH_SOURCE_C}")
  130. set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" \"/Yc${_input}\"")
  131. set(_pch_source_c_found TRUE)
  132. set_source_files_properties("${_source}" PROPERTIES OBJECT_OUTPUTS "${_pch_c_pch}")
  133. else()
  134. if(_source MATCHES \\.\(cpp|cxx|cc\)$)
  135. set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_cxx_pch}\" \"/Yu${_input_fullpath}\"")
  136. set(_pch_source_cxx_needed TRUE)
  137. set_source_files_properties("${_source}" PROPERTIES OBJECT_DEPENDS "${_pch_cxx_pch}")
  138. else()
  139. set(_pch_compile_flags "${_pch_compile_flags} \"/Fp${_pch_c_pch}\" \"/Yu${_input_fullpath}\"")
  140. set(_pch_source_c_needed TRUE)
  141. set_source_files_properties("${_source}" PROPERTIES OBJECT_DEPENDS "${_pch_c_pch}")
  142. endif()
  143. if(_PCH_FORCEINCLUDE)
  144. set(_pch_compile_flags "${_pch_compile_flags} /FI${_input_fullpath}")
  145. endif(_PCH_FORCEINCLUDE)
  146. endif()
  147. get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
  148. if(NOT _object_depends)
  149. set(_object_depends)
  150. endif()
  151. if(_PCH_FORCEINCLUDE)
  152. list(APPEND _object_depends "${CMAKE_CURRENT_SOURCE_DIR}/${_pch_header}")
  153. endif()
  154. set_source_files_properties(${_source} PROPERTIES
  155. COMPILE_FLAGS "${_pch_compile_flags}"
  156. OBJECT_DEPENDS "${_object_depends}")
  157. endif()
  158. endforeach()
  159. if(_pch_source_cxx_needed AND NOT _pch_source_cxx_found)
  160. message(FATAL_ERROR "A source file ${_PCH_SOURCE_CXX} for ${_input} is required for MSVC builds. Can be set with the SOURCE_CXX option.")
  161. endif()
  162. if(_pch_source_c_needed AND NOT _pch_source_c_found)
  163. message(FATAL_ERROR "A source file ${_PCH_SOURCE_C} for ${_input} is required for MSVC builds. Can be set with the SOURCE_C option.")
  164. endif()
  165. endif(MSVC)
  166. if(CMAKE_COMPILER_IS_GNUCXX)
  167. get_filename_component(_name ${_input} NAME)
  168. set(_pch_header "${CMAKE_CURRENT_SOURCE_DIR}/${_input}")
  169. set(_pch_binary_dir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch")
  170. set(_pchfile "${_pch_binary_dir}/${_input}")
  171. set(_outdir "${CMAKE_CURRENT_BINARY_DIR}/${_target}_pch/${_name}.gch")
  172. file(MAKE_DIRECTORY "${_outdir}")
  173. set(_output_cxx "${_outdir}/.c++")
  174. set(_output_c "${_outdir}/.c")
  175. set(_pch_flags_file "${_pch_binary_dir}/compile_flags.rsp")
  176. export_all_flags("${_pch_flags_file}")
  177. set(_compiler_FLAGS "@${_pch_flags_file}")
  178. add_custom_command(
  179. OUTPUT "${_pchfile}"
  180. COMMAND "${CMAKE_COMMAND}" -E copy "${_pch_header}" "${_pchfile}"
  181. DEPENDS "${_pch_header}"
  182. COMMENT "Updating ${_name}")
  183. add_custom_command(
  184. OUTPUT "${_output_cxx}"
  185. COMMAND "${CMAKE_CXX_COMPILER}" ${_compiler_FLAGS} -x c++-header -o "${_output_cxx}" "${_pchfile}"
  186. DEPENDS "${_pchfile}" "${_pch_flags_file}"
  187. COMMENT "Precompiling ${_name} for ${_target} (C++)")
  188. add_custom_command(
  189. OUTPUT "${_output_c}"
  190. COMMAND "${CMAKE_C_COMPILER}" ${_compiler_FLAGS} -x c-header -o "${_output_c}" "${_pchfile}"
  191. DEPENDS "${_pchfile}" "${_pch_flags_file}"
  192. COMMENT "Precompiling ${_name} for ${_target} (C)")
  193. get_property(_sources TARGET ${_target} PROPERTY SOURCES)
  194. foreach(_source ${_sources})
  195. set(_pch_compile_flags "")
  196. if(_source MATCHES \\.\(cc|cxx|cpp|c\)$)
  197. get_source_file_property(_pch_compile_flags "${_source}" COMPILE_FLAGS)
  198. if(NOT _pch_compile_flags)
  199. set(_pch_compile_flags)
  200. endif()
  201. separate_arguments(_pch_compile_flags)
  202. list(APPEND _pch_compile_flags -Winvalid-pch)
  203. if(_PCH_FORCEINCLUDE)
  204. list(APPEND _pch_compile_flags -include "${_pchfile}")
  205. else(_PCH_FORCEINCLUDE)
  206. list(APPEND _pch_compile_flags "-I${_pch_binary_dir}")
  207. endif(_PCH_FORCEINCLUDE)
  208. get_source_file_property(_object_depends "${_source}" OBJECT_DEPENDS)
  209. if(NOT _object_depends)
  210. set(_object_depends)
  211. endif()
  212. list(APPEND _object_depends "${_pchfile}")
  213. if(_source MATCHES \\.\(cc|cxx|cpp\)$)
  214. list(APPEND _object_depends "${_output_cxx}")
  215. else()
  216. list(APPEND _object_depends "${_output_c}")
  217. endif()
  218. combine_arguments(_pch_compile_flags)
  219. set_source_files_properties(${_source} PROPERTIES
  220. COMPILE_FLAGS "${_pch_compile_flags}"
  221. OBJECT_DEPENDS "${_object_depends}")
  222. endif()
  223. endforeach()
  224. endif(CMAKE_COMPILER_IS_GNUCXX)
  225. endfunction()
  226. else ()
  227. function(add_precompiled_header _target _input)
  228. message(STATUS "Adding precompiled header ${_input} to target ${_target}.")
  229. target_precompile_headers(${_target} PRIVATE ${_input})
  230. get_target_property(_sources ${_target} SOURCES)
  231. list(FILTER _sources INCLUDE REGEX ".*\\.mm?")
  232. if (_sources)
  233. message(STATUS "PCH skipping sources: ${_sources}")
  234. endif ()
  235. set_source_files_properties(${_sources} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
  236. endfunction()
  237. endif (CMAKE_VERSION VERSION_LESS 3.16)