OpenCVPCHSupport.cmake 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. # taken from http://public.kitware.com/Bug/view.php?id=1260 and slightly adjusted
  2. # - Try to find precompiled headers support for GCC 3.4 and 4.x
  3. # Once done this will define:
  4. #
  5. # Variable:
  6. # PCHSupport_FOUND
  7. #
  8. # Macro:
  9. # ADD_PRECOMPILED_HEADER _targetName _input _dowarn
  10. # ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use _dowarn
  11. # ADD_NATIVE_PRECOMPILED_HEADER _targetName _input _dowarn
  12. # GET_NATIVE_PRECOMPILED_HEADER _targetName _input
  13. IF(CV_GCC)
  14. IF(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.2.0")
  15. SET(PCHSupport_FOUND TRUE)
  16. ENDIF()
  17. SET(_PCH_include_prefix "-I")
  18. SET(_PCH_isystem_prefix "-isystem")
  19. SET(_PCH_define_prefix "-D")
  20. ELSEIF(CMAKE_GENERATOR MATCHES "^Visual.*$")
  21. SET(PCHSupport_FOUND TRUE)
  22. SET(_PCH_include_prefix "/I")
  23. SET(_PCH_isystem_prefix "/I")
  24. SET(_PCH_define_prefix "/D")
  25. ELSE()
  26. SET(PCHSupport_FOUND FALSE)
  27. ENDIF()
  28. MACRO(_PCH_GET_COMPILE_FLAGS _out_compile_flags)
  29. STRING(TOUPPER "CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}" _flags_var_name)
  30. SET(${_out_compile_flags} ${${_flags_var_name}} )
  31. IF(CV_GCC)
  32. GET_TARGET_PROPERTY(_targetType ${_PCH_current_target} TYPE)
  33. IF(${_targetType} STREQUAL SHARED_LIBRARY AND NOT WIN32)
  34. LIST(APPEND ${_out_compile_flags} "-fPIC")
  35. ENDIF()
  36. # Processed via $<TARGET_PROPERTY:target,COMPILE_DEFINITIONS>
  37. #GET_PROPERTY(_definitions DIRECTORY PROPERTY COMPILE_DEFINITIONS)
  38. #GET_TARGET_PROPERTY(_target_definitions ${_PCH_current_target} COMPILE_DEFINITIONS)
  39. GET_TARGET_PROPERTY(_cxx_standard ${_PCH_current_target} CXX_STANDARD)
  40. if (_cxx_standard)
  41. GET_TARGET_PROPERTY(_cxx_extensions ${_PCH_current_target} CXX_EXTENSIONS)
  42. if (_cxx_extensions)
  43. LIST(APPEND ${_out_compile_flags} "${CMAKE_CXX${_cxx_standard}_EXTENSION_COMPILE_OPTION}")
  44. else()
  45. LIST(APPEND ${_out_compile_flags} "${CMAKE_CXX${_cxx_standard}_STANDARD_COMPILE_OPTION}")
  46. endif()
  47. endif()
  48. ELSE()
  49. ## TODO ... ? or does it work out of the box
  50. ENDIF()
  51. GET_DIRECTORY_PROPERTY(DIRINC INCLUDE_DIRECTORIES )
  52. FOREACH(item ${DIRINC})
  53. ocv_is_opencv_directory(__result ${item})
  54. if(__result)
  55. LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
  56. elseif(CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0" AND
  57. item MATCHES "/usr/include$")
  58. # workaround for GCC 6.x bug
  59. else()
  60. LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
  61. endif()
  62. ENDFOREACH(item)
  63. get_target_property(DIRINC ${_PCH_current_target} INCLUDE_DIRECTORIES )
  64. FOREACH(item ${DIRINC})
  65. ocv_is_opencv_directory(__result ${item})
  66. if(__result)
  67. LIST(APPEND ${_out_compile_flags} "${_PCH_include_prefix}\"${item}\"")
  68. elseif(CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0" AND
  69. item MATCHES "/usr/include$")
  70. # workaround for GCC 6.x bug
  71. else()
  72. LIST(APPEND ${_out_compile_flags} "${_PCH_isystem_prefix}\"${item}\"")
  73. endif()
  74. ENDFOREACH(item)
  75. LIST(APPEND ${_out_compile_flags} ${CMAKE_CXX_FLAGS})
  76. SEPARATE_ARGUMENTS(${_out_compile_flags})
  77. ENDMACRO(_PCH_GET_COMPILE_FLAGS)
  78. MACRO(_PCH_WRITE_PCHDEP_CXX _targetName _include_file _dephelp)
  79. set(${_dephelp} "${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch_dephelp.cxx")
  80. set(_content "")
  81. if(EXISTS "${${_dephelp}}")
  82. file(READ "${${_dephelp}}" _content)
  83. endif()
  84. set(_dummy_str
  85. "#include \"${_include_file}\"
  86. int testfunction();
  87. int testfunction()
  88. {
  89. return 0;
  90. }
  91. ")
  92. if(NOT _content STREQUAL _dummy_str)
  93. file(WRITE "${${_dephelp}}" "${_dummy_str}")
  94. endif()
  95. ENDMACRO(_PCH_WRITE_PCHDEP_CXX )
  96. MACRO(_PCH_GET_COMPILE_COMMAND out_command _input _output)
  97. FILE(TO_NATIVE_PATH ${_input} _native_input)
  98. FILE(TO_NATIVE_PATH ${_output} _native_output)
  99. if(CV_GCC)
  100. IF(CMAKE_CXX_COMPILER_ARG1)
  101. # remove leading space in compiler argument
  102. STRING(REGEX REPLACE "^ +" "" pchsupport_compiler_cxx_arg1 ${CMAKE_CXX_COMPILER_ARG1})
  103. SET(${out_command}
  104. ${CMAKE_CXX_COMPILER} ${pchsupport_compiler_cxx_arg1} ${_compile_FLAGS} -x c++-header -o ${_output} -c ${_input}
  105. )
  106. ELSE(CMAKE_CXX_COMPILER_ARG1)
  107. SET(${out_command}
  108. ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} -c ${_input}
  109. )
  110. ENDIF(CMAKE_CXX_COMPILER_ARG1)
  111. ELSE()
  112. SET(_dummy_str "#include <${_input}>")
  113. FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/pch_dummy.cpp ${_dummy_str})
  114. SET(${out_command}
  115. ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} /c /Fp${_native_output} /Yc${_native_input} pch_dummy.cpp
  116. )
  117. #/out:${_output}
  118. ENDIF()
  119. ENDMACRO(_PCH_GET_COMPILE_COMMAND )
  120. MACRO(_PCH_GET_TARGET_COMPILE_FLAGS _cflags _header_name _pch_path _dowarn )
  121. FILE(TO_NATIVE_PATH ${_pch_path} _native_pch_path)
  122. IF(CV_GCC)
  123. # for use with distcc and gcc >4.0.1 if preprocessed files are accessible
  124. # on all remote machines set
  125. # PCH_ADDITIONAL_COMPILER_FLAGS to -fpch-preprocess
  126. # if you want warnings for invalid header files (which is very inconvenient
  127. # if you have different versions of the headers for different build types
  128. # you may set _pch_dowarn
  129. IF (_dowarn)
  130. SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} -Winvalid-pch " )
  131. ELSE (_dowarn)
  132. SET(${_cflags} "${PCH_ADDITIONAL_COMPILER_FLAGS} " )
  133. ENDIF (_dowarn)
  134. ELSE()
  135. set(${_cflags} "/Fp${_native_pch_path} /Yu${_header_name}" )
  136. ENDIF()
  137. ENDMACRO(_PCH_GET_TARGET_COMPILE_FLAGS )
  138. MACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input _output)
  139. GET_FILENAME_COMPONENT(_name ${_input} NAME)
  140. GET_FILENAME_COMPONENT(_path ${_input} PATH)
  141. SET(${_output} "${CMAKE_CURRENT_BINARY_DIR}/${_name}.gch/${_targetName}_${CMAKE_BUILD_TYPE}.gch")
  142. ENDMACRO(GET_PRECOMPILED_HEADER_OUTPUT _targetName _input)
  143. MACRO(ADD_PRECOMPILED_HEADER_TO_TARGET _targetName _input _pch_output_to_use )
  144. # to do: test whether compiler flags match between target _targetName
  145. # and _pch_output_to_use
  146. GET_FILENAME_COMPONENT(_name ${_input} NAME)
  147. IF(ARGN STREQUAL "0")
  148. SET(_dowarn 0)
  149. ELSE()
  150. SET(_dowarn 1)
  151. ENDIF()
  152. _PCH_GET_TARGET_COMPILE_FLAGS(_target_cflags ${_name} ${_pch_output_to_use} ${_dowarn})
  153. #MESSAGE("Add flags ${_target_cflags} to ${_targetName} " )
  154. if(CV_GCC)
  155. set(_target_cflags "${_target_cflags} -include \"${CMAKE_CURRENT_BINARY_DIR}/${_name}\"")
  156. endif()
  157. GET_TARGET_PROPERTY(_sources ${_targetName} SOURCES)
  158. FOREACH(src ${_sources})
  159. if(NOT "${src}" MATCHES "\\.mm$" AND NOT "${src}" MATCHES "\\.rc$")
  160. get_source_file_property(_flags "${src}" COMPILE_FLAGS)
  161. get_source_file_property(_flags2 "${src}" COMPILE_DEFINITIONS)
  162. if(NOT _flags AND NOT _flags2)
  163. set_source_files_properties("${src}" PROPERTIES COMPILE_FLAGS "${_target_cflags}")
  164. else()
  165. #ocv_debug_message("Skip PCH, flags: ${oldProps} defines: ${oldProps2}, file: ${src}")
  166. endif()
  167. endif()
  168. ENDFOREACH()
  169. ADD_CUSTOM_TARGET(pch_Generate_${_targetName}
  170. DEPENDS ${_pch_output_to_use}
  171. )
  172. ADD_DEPENDENCIES(${_targetName} pch_Generate_${_targetName} )
  173. ENDMACRO(ADD_PRECOMPILED_HEADER_TO_TARGET)
  174. MACRO(ADD_PRECOMPILED_HEADER _targetName _input)
  175. SET(_PCH_current_target ${_targetName})
  176. IF(NOT CMAKE_BUILD_TYPE)
  177. MESSAGE(FATAL_ERROR
  178. "This is the ADD_PRECOMPILED_HEADER macro. "
  179. "You must set CMAKE_BUILD_TYPE!"
  180. )
  181. ENDIF()
  182. IF(ARGN STREQUAL "0")
  183. SET(_dowarn 0)
  184. ELSE()
  185. SET(_dowarn 1)
  186. ENDIF()
  187. GET_FILENAME_COMPONENT(_name ${_input} NAME)
  188. GET_FILENAME_COMPONENT(_path ${_input} PATH)
  189. GET_PRECOMPILED_HEADER_OUTPUT( ${_targetName} ${_input} _output)
  190. _PCH_WRITE_PCHDEP_CXX(${_targetName} "${_input}" _pch_dephelp_cxx)
  191. ADD_LIBRARY(${_targetName}_pch_dephelp STATIC "${_pch_dephelp_cxx}" "${_input}" )
  192. set_target_properties(${_targetName}_pch_dephelp PROPERTIES
  193. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  194. ARCHIVE_OUTPUT_DIRECTORY "${LIBRARY_OUTPUT_PATH}"
  195. )
  196. _PCH_GET_COMPILE_FLAGS(_compile_FLAGS)
  197. list(APPEND _compile_FLAGS "${_PCH_include_prefix}\"${_path}\"")
  198. get_target_property(type ${_targetName} TYPE)
  199. if(type STREQUAL "SHARED_LIBRARY")
  200. get_target_property(__DEFINES ${_targetName} DEFINE_SYMBOL)
  201. if(NOT __DEFINES MATCHES __DEFINES-NOTFOUND)
  202. list(APPEND _compile_FLAGS "${_PCH_define_prefix}${__DEFINES}")
  203. endif()
  204. endif()
  205. if(type STREQUAL "SHARED_LIBRARY" OR type STREQUAL "STATIC_LIBRARY")
  206. get_target_property(__pic ${_targetName} POSITION_INDEPENDENT_CODE)
  207. if(__pic AND CMAKE_CXX_COMPILE_OPTIONS_PIC
  208. AND NOT OPENCV_SKIP_PCH_PIC_HANDLING
  209. AND NOT OPENCV_SKIP_PCH_PIC_HANDLING_${_targetName}
  210. )
  211. list(APPEND _compile_FLAGS "${CMAKE_CXX_COMPILE_OPTIONS_PIC}")
  212. endif()
  213. elseif(type STREQUAL "EXECUTABLE")
  214. get_target_property(__pie ${_targetName} POSITION_INDEPENDENT_CODE)
  215. if(__pie AND CMAKE_CXX_COMPILE_OPTIONS_PIE
  216. AND NOT OPENCV_SKIP_PCH_PIE_HANDLING
  217. AND NOT OPENCV_SKIP_PCH_PIE_HANDLING_${_targetName}
  218. )
  219. list(APPEND _compile_FLAGS "${CMAKE_CXX_COMPILE_OPTIONS_PIE}")
  220. endif()
  221. endif()
  222. get_target_property(DIRINC ${_targetName} INCLUDE_DIRECTORIES)
  223. set_target_properties(${_targetName}_pch_dephelp PROPERTIES INCLUDE_DIRECTORIES "${DIRINC}")
  224. #MESSAGE("_compile_FLAGS: ${_compile_FLAGS}")
  225. #message("COMMAND ${CMAKE_CXX_COMPILER} ${_compile_FLAGS} -x c++-header -o ${_output} ${_input}")
  226. ADD_CUSTOM_COMMAND(
  227. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
  228. COMMAND ${CMAKE_COMMAND} -E copy_if_different "${_input}" "${CMAKE_CURRENT_BINARY_DIR}/${_name}" # ensure same directory! Required by gcc
  229. DEPENDS "${_input}"
  230. )
  231. #message("_command ${_input} ${_output}")
  232. _PCH_GET_COMPILE_COMMAND(_command ${CMAKE_CURRENT_BINARY_DIR}/${_name} ${_output} )
  233. set(_pch_generate_file_cmd "${CMAKE_CURRENT_BINARY_DIR}/${_name}.command.sh")
  234. string(REPLACE " " "\\ " _command "${_command}")
  235. string(REPLACE ";" " " _command "${_command}")
  236. file(GENERATE OUTPUT "${_pch_generate_file_cmd}" CONTENT "#!/bin/sh
  237. if [ -n \"$VERBOSE\" ]; then
  238. tail -n1 \$0
  239. fi
  240. ${_command} '-D$<JOIN:$<TARGET_PROPERTY:${_targetName},COMPILE_DEFINITIONS>,' '-D>'
  241. ")
  242. GET_FILENAME_COMPONENT(_outdir ${_output} PATH)
  243. if(NOT CMAKE_HOST_WIN32) # chmod may be not available on Win32/MinGW (and it is not required)
  244. set(_pch_prepare_command COMMAND chmod +x "${_pch_generate_file_cmd}")
  245. endif()
  246. ADD_CUSTOM_COMMAND(
  247. OUTPUT "${_output}"
  248. COMMAND ${CMAKE_COMMAND} -E make_directory "${_outdir}"
  249. ${_pch_prepare_command}
  250. COMMAND "${_pch_generate_file_cmd}"
  251. DEPENDS "${_input}" "${_pch_generate_file_cmd}"
  252. DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/${_name}"
  253. DEPENDS ${_targetName}_pch_dephelp
  254. )
  255. ADD_PRECOMPILED_HEADER_TO_TARGET(${_targetName} ${_input} ${_output} ${_dowarn})
  256. ENDMACRO(ADD_PRECOMPILED_HEADER)
  257. # Generates the use of precompiled in a target,
  258. # without using dependency targets (2 extra for each target)
  259. # Using Visual, must also add ${_targetName}_pch to sources
  260. # Not needed by Xcode
  261. MACRO(GET_NATIVE_PRECOMPILED_HEADER _targetName _input)
  262. if(ENABLE_PRECOMPILED_HEADERS)
  263. if(CMAKE_GENERATOR MATCHES "^Visual.*$")
  264. set(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cpp)
  265. endif()
  266. endif()
  267. ENDMACRO(GET_NATIVE_PRECOMPILED_HEADER)
  268. MACRO(ADD_NATIVE_PRECOMPILED_HEADER _targetName _input)
  269. IF(ARGN STREQUAL "0")
  270. SET(_dowarn 0)
  271. ELSE()
  272. SET(_dowarn 1)
  273. ENDIF()
  274. if(CMAKE_GENERATOR MATCHES "^Visual.*$")
  275. # Auto include the precompile (useful for moc processing, since the use of
  276. # precompiled is specified at the target level
  277. # and I don't want to specify /F- for each moc/res/ui generated files (using Qt)
  278. get_target_property(_sources ${_targetName} SOURCES)
  279. foreach(src ${_sources})
  280. if("${src}" MATCHES "\\.c(pp|xx)?$")
  281. get_source_file_property(oldProps "${src}" COMPILE_FLAGS)
  282. get_source_file_property(oldProps2 "${src}" COMPILE_DEFINITIONS)
  283. if(NOT oldProps AND NOT oldProps2)
  284. set(newProperties "/Yu\"${_input}\" /FI\"${_input}\"")
  285. set_source_files_properties("${src}" PROPERTIES COMPILE_FLAGS "${newProperties}")
  286. else()
  287. ocv_debug_message("Skip PCH, flags: ${oldProps} defines: ${oldProps2}, file: ${src}")
  288. endif()
  289. endif()
  290. endforeach()
  291. #also include ${oldProps} to have the same compile options
  292. GET_TARGET_PROPERTY(oldProps ${_targetName} COMPILE_FLAGS)
  293. if (oldProps MATCHES NOTFOUND)
  294. SET(oldProps "")
  295. endif()
  296. SET_SOURCE_FILES_PROPERTIES(${${_targetName}_pch} PROPERTIES COMPILE_FLAGS "${oldProps} /Yc\"${_input}\"")
  297. set(_dummy_str "#include \"${_input}\"\n")
  298. set(${_targetName}_pch ${CMAKE_CURRENT_BINARY_DIR}/${_targetName}_pch.cpp)
  299. if(EXISTS ${${_targetName}_pch})
  300. file(READ "${${_targetName}_pch}" _contents)
  301. endif()
  302. if(NOT _dummy_str STREQUAL "${_contents}")
  303. file(WRITE ${${_targetName}_pch} ${_dummy_str})
  304. endif()
  305. elseif (CMAKE_GENERATOR MATCHES Xcode)
  306. # For Xcode, cmake needs my patch to process
  307. # GCC_PREFIX_HEADER and GCC_PRECOMPILE_PREFIX_HEADER as target properties
  308. # When building out of the tree, precompiled may not be located
  309. # Use full path instead.
  310. GET_FILENAME_COMPONENT(fullPath ${_input} ABSOLUTE)
  311. SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${fullPath}")
  312. SET_TARGET_PROPERTIES(${_targetName} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
  313. else()
  314. #Fallback to the "old" precompiled support
  315. #ADD_PRECOMPILED_HEADER(${_targetName} ${_input} ${_dowarn})
  316. endif()
  317. ENDMACRO(ADD_NATIVE_PRECOMPILED_HEADER)
  318. macro(ocv_add_precompiled_header_to_target the_target pch_header)
  319. if(PCHSupport_FOUND AND ENABLE_PRECOMPILED_HEADERS AND EXISTS "${pch_header}")
  320. if(CMAKE_GENERATOR MATCHES "^Visual" OR CMAKE_GENERATOR MATCHES Xcode)
  321. add_native_precompiled_header(${the_target} ${pch_header})
  322. elseif(CV_GCC AND CMAKE_GENERATOR MATCHES "Makefiles|Ninja")
  323. add_precompiled_header(${the_target} ${pch_header})
  324. endif()
  325. endif()
  326. endmacro()