您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

3945 行
161 KiB

  1. # - cotire (compile time reducer)
  2. #
  3. # See the cotire manual for usage hints.
  4. #
  5. #=============================================================================
  6. # Copyright 2012-2016 Sascha Kratky
  7. #
  8. # Permission is hereby granted, free of charge, to any person
  9. # obtaining a copy of this software and associated documentation
  10. # files (the "Software"), to deal in the Software without
  11. # restriction, including without limitation the rights to use,
  12. # copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. # copies of the Software, and to permit persons to whom the
  14. # Software is furnished to do so, subject to the following
  15. # conditions:
  16. #
  17. # The above copyright notice and this permission notice shall be
  18. # included in all copies or substantial portions of the Software.
  19. #
  20. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. # OTHER DEALINGS IN THE SOFTWARE.
  28. #=============================================================================
  29. if(__COTIRE_INCLUDED)
  30. return()
  31. endif()
  32. set(__COTIRE_INCLUDED TRUE)
  33. # call cmake_minimum_required, but prevent modification of the CMake policy stack in include mode
  34. # cmake_minimum_required also sets the policy version as a side effect, which we have to avoid
  35. if (NOT CMAKE_SCRIPT_MODE_FILE)
  36. cmake_policy(PUSH)
  37. endif()
  38. cmake_minimum_required(VERSION 2.8.12)
  39. if (NOT CMAKE_SCRIPT_MODE_FILE)
  40. cmake_policy(POP)
  41. endif()
  42. set (COTIRE_CMAKE_MODULE_FILE "${CMAKE_CURRENT_LIST_FILE}")
  43. set (COTIRE_CMAKE_MODULE_VERSION "1.7.9")
  44. # activate select policies
  45. if (POLICY CMP0025)
  46. # Compiler id for Apple Clang is now AppleClang
  47. cmake_policy(SET CMP0025 NEW)
  48. endif()
  49. if (POLICY CMP0026)
  50. # disallow use of the LOCATION target property
  51. cmake_policy(SET CMP0026 NEW)
  52. endif()
  53. if (POLICY CMP0038)
  54. # targets may not link directly to themselves
  55. cmake_policy(SET CMP0038 NEW)
  56. endif()
  57. if (POLICY CMP0039)
  58. # utility targets may not have link dependencies
  59. cmake_policy(SET CMP0039 NEW)
  60. endif()
  61. if (POLICY CMP0040)
  62. # target in the TARGET signature of add_custom_command() must exist
  63. cmake_policy(SET CMP0040 NEW)
  64. endif()
  65. if (POLICY CMP0045)
  66. # error on non-existent target in get_target_property
  67. cmake_policy(SET CMP0045 NEW)
  68. endif()
  69. if (POLICY CMP0046)
  70. # error on non-existent dependency in add_dependencies
  71. cmake_policy(SET CMP0046 NEW)
  72. endif()
  73. if (POLICY CMP0049)
  74. # do not expand variables in target source entries
  75. cmake_policy(SET CMP0049 NEW)
  76. endif()
  77. if (POLICY CMP0050)
  78. # disallow add_custom_command SOURCE signatures
  79. cmake_policy(SET CMP0050 NEW)
  80. endif()
  81. if (POLICY CMP0051)
  82. # include TARGET_OBJECTS expressions in a target's SOURCES property
  83. cmake_policy(SET CMP0051 NEW)
  84. endif()
  85. if (POLICY CMP0053)
  86. # simplify variable reference and escape sequence evaluation
  87. cmake_policy(SET CMP0053 NEW)
  88. endif()
  89. if (POLICY CMP0054)
  90. # only interpret if() arguments as variables or keywords when unquoted
  91. cmake_policy(SET CMP0054 NEW)
  92. endif()
  93. include(CMakeParseArguments)
  94. include(ProcessorCount)
  95. function (cotire_get_configuration_types _configsVar)
  96. set (_configs "")
  97. if (CMAKE_CONFIGURATION_TYPES)
  98. list (APPEND _configs ${CMAKE_CONFIGURATION_TYPES})
  99. endif()
  100. if (CMAKE_BUILD_TYPE)
  101. list (APPEND _configs "${CMAKE_BUILD_TYPE}")
  102. endif()
  103. if (_configs)
  104. list (REMOVE_DUPLICATES _configs)
  105. set (${_configsVar} ${_configs} PARENT_SCOPE)
  106. else()
  107. set (${_configsVar} "None" PARENT_SCOPE)
  108. endif()
  109. endfunction()
  110. function (cotire_get_source_file_extension _sourceFile _extVar)
  111. # get_filename_component returns extension from first occurrence of . in file name
  112. # this function computes the extension from last occurrence of . in file name
  113. string (FIND "${_sourceFile}" "." _index REVERSE)
  114. if (_index GREATER -1)
  115. math (EXPR _index "${_index} + 1")
  116. string (SUBSTRING "${_sourceFile}" ${_index} -1 _sourceExt)
  117. else()
  118. set (_sourceExt "")
  119. endif()
  120. set (${_extVar} "${_sourceExt}" PARENT_SCOPE)
  121. endfunction()
  122. macro (cotire_check_is_path_relative_to _path _isRelativeVar)
  123. set (${_isRelativeVar} FALSE)
  124. if (IS_ABSOLUTE "${_path}")
  125. foreach (_dir ${ARGN})
  126. file (RELATIVE_PATH _relPath "${_dir}" "${_path}")
  127. if (NOT _relPath OR (NOT IS_ABSOLUTE "${_relPath}" AND NOT "${_relPath}" MATCHES "^\\.\\."))
  128. set (${_isRelativeVar} TRUE)
  129. break()
  130. endif()
  131. endforeach()
  132. endif()
  133. endmacro()
  134. function (cotire_filter_language_source_files _language _target _sourceFilesVar _excludedSourceFilesVar _cotiredSourceFilesVar)
  135. if (CMAKE_${_language}_SOURCE_FILE_EXTENSIONS)
  136. set (_languageExtensions "${CMAKE_${_language}_SOURCE_FILE_EXTENSIONS}")
  137. else()
  138. set (_languageExtensions "")
  139. endif()
  140. if (CMAKE_${_language}_IGNORE_EXTENSIONS)
  141. set (_ignoreExtensions "${CMAKE_${_language}_IGNORE_EXTENSIONS}")
  142. else()
  143. set (_ignoreExtensions "")
  144. endif()
  145. if (COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS)
  146. set (_excludeExtensions "${COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS}")
  147. else()
  148. set (_excludeExtensions "")
  149. endif()
  150. if (COTIRE_DEBUG AND _languageExtensions)
  151. message (STATUS "${_language} source file extensions: ${_languageExtensions}")
  152. endif()
  153. if (COTIRE_DEBUG AND _ignoreExtensions)
  154. message (STATUS "${_language} ignore extensions: ${_ignoreExtensions}")
  155. endif()
  156. if (COTIRE_DEBUG AND _excludeExtensions)
  157. message (STATUS "${_language} exclude extensions: ${_excludeExtensions}")
  158. endif()
  159. if (CMAKE_VERSION VERSION_LESS "3.1.0")
  160. set (_allSourceFiles ${ARGN})
  161. else()
  162. # as of CMake 3.1 target sources may contain generator expressions
  163. # since we cannot obtain required property information about source files added
  164. # through generator expressions at configure time, we filter them out
  165. string (GENEX_STRIP "${ARGN}" _allSourceFiles)
  166. endif()
  167. set (_filteredSourceFiles "")
  168. set (_excludedSourceFiles "")
  169. foreach (_sourceFile ${_allSourceFiles})
  170. get_source_file_property(_sourceIsHeaderOnly "${_sourceFile}" HEADER_FILE_ONLY)
  171. get_source_file_property(_sourceIsExternal "${_sourceFile}" EXTERNAL_OBJECT)
  172. get_source_file_property(_sourceIsSymbolic "${_sourceFile}" SYMBOLIC)
  173. if (NOT _sourceIsHeaderOnly AND NOT _sourceIsExternal AND NOT _sourceIsSymbolic)
  174. cotire_get_source_file_extension("${_sourceFile}" _sourceExt)
  175. if (_sourceExt)
  176. list (FIND _ignoreExtensions "${_sourceExt}" _ignoreIndex)
  177. if (_ignoreIndex LESS 0)
  178. list (FIND _excludeExtensions "${_sourceExt}" _excludeIndex)
  179. if (_excludeIndex GREATER -1)
  180. list (APPEND _excludedSourceFiles "${_sourceFile}")
  181. else()
  182. list (FIND _languageExtensions "${_sourceExt}" _sourceIndex)
  183. if (_sourceIndex GREATER -1)
  184. # consider source file unless it is excluded explicitly
  185. get_source_file_property(_sourceIsExcluded "${_sourceFile}" COTIRE_EXCLUDED)
  186. if (_sourceIsExcluded)
  187. list (APPEND _excludedSourceFiles "${_sourceFile}")
  188. else()
  189. list (APPEND _filteredSourceFiles "${_sourceFile}")
  190. endif()
  191. else()
  192. get_source_file_property(_sourceLanguage "${_sourceFile}" LANGUAGE)
  193. if ("${_sourceLanguage}" STREQUAL "${_language}")
  194. # add to excluded sources, if file is not ignored and has correct language without having the correct extension
  195. list (APPEND _excludedSourceFiles "${_sourceFile}")
  196. endif()
  197. endif()
  198. endif()
  199. endif()
  200. endif()
  201. endif()
  202. endforeach()
  203. # separate filtered source files from already cotired ones
  204. # the COTIRE_TARGET property of a source file may be set while a target is being processed by cotire
  205. set (_sourceFiles "")
  206. set (_cotiredSourceFiles "")
  207. foreach (_sourceFile ${_filteredSourceFiles})
  208. get_source_file_property(_sourceIsCotired "${_sourceFile}" COTIRE_TARGET)
  209. if (_sourceIsCotired)
  210. list (APPEND _cotiredSourceFiles "${_sourceFile}")
  211. else()
  212. get_source_file_property(_sourceCompileFlags "${_sourceFile}" COMPILE_FLAGS)
  213. if (_sourceCompileFlags)
  214. # add to excluded sources, if file has custom compile flags
  215. list (APPEND _excludedSourceFiles "${_sourceFile}")
  216. else()
  217. list (APPEND _sourceFiles "${_sourceFile}")
  218. endif()
  219. endif()
  220. endforeach()
  221. if (COTIRE_DEBUG)
  222. if (_sourceFiles)
  223. message (STATUS "Filtered ${_target} ${_language} sources: ${_sourceFiles}")
  224. endif()
  225. if (_excludedSourceFiles)
  226. message (STATUS "Excluded ${_target} ${_language} sources: ${_excludedSourceFiles}")
  227. endif()
  228. if (_cotiredSourceFiles)
  229. message (STATUS "Cotired ${_target} ${_language} sources: ${_cotiredSourceFiles}")
  230. endif()
  231. endif()
  232. set (${_sourceFilesVar} ${_sourceFiles} PARENT_SCOPE)
  233. set (${_excludedSourceFilesVar} ${_excludedSourceFiles} PARENT_SCOPE)
  234. set (${_cotiredSourceFilesVar} ${_cotiredSourceFiles} PARENT_SCOPE)
  235. endfunction()
  236. function (cotire_get_objects_with_property_on _filteredObjectsVar _property _type)
  237. set (_filteredObjects "")
  238. foreach (_object ${ARGN})
  239. get_property(_isSet ${_type} "${_object}" PROPERTY ${_property} SET)
  240. if (_isSet)
  241. get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
  242. if (_propertyValue)
  243. list (APPEND _filteredObjects "${_object}")
  244. endif()
  245. endif()
  246. endforeach()
  247. set (${_filteredObjectsVar} ${_filteredObjects} PARENT_SCOPE)
  248. endfunction()
  249. function (cotire_get_objects_with_property_off _filteredObjectsVar _property _type)
  250. set (_filteredObjects "")
  251. foreach (_object ${ARGN})
  252. get_property(_isSet ${_type} "${_object}" PROPERTY ${_property} SET)
  253. if (_isSet)
  254. get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
  255. if (NOT _propertyValue)
  256. list (APPEND _filteredObjects "${_object}")
  257. endif()
  258. endif()
  259. endforeach()
  260. set (${_filteredObjectsVar} ${_filteredObjects} PARENT_SCOPE)
  261. endfunction()
  262. function (cotire_get_source_file_property_values _valuesVar _property)
  263. set (_values "")
  264. foreach (_sourceFile ${ARGN})
  265. get_source_file_property(_propertyValue "${_sourceFile}" ${_property})
  266. if (_propertyValue)
  267. list (APPEND _values "${_propertyValue}")
  268. endif()
  269. endforeach()
  270. set (${_valuesVar} ${_values} PARENT_SCOPE)
  271. endfunction()
  272. function (cotire_resolve_config_properites _configurations _propertiesVar)
  273. set (_properties "")
  274. foreach (_property ${ARGN})
  275. if ("${_property}" MATCHES "<CONFIG>")
  276. foreach (_config ${_configurations})
  277. string (TOUPPER "${_config}" _upperConfig)
  278. string (REPLACE "<CONFIG>" "${_upperConfig}" _configProperty "${_property}")
  279. list (APPEND _properties ${_configProperty})
  280. endforeach()
  281. else()
  282. list (APPEND _properties ${_property})
  283. endif()
  284. endforeach()
  285. set (${_propertiesVar} ${_properties} PARENT_SCOPE)
  286. endfunction()
  287. function (cotire_copy_set_properites _configurations _type _source _target)
  288. cotire_resolve_config_properites("${_configurations}" _properties ${ARGN})
  289. foreach (_property ${_properties})
  290. get_property(_isSet ${_type} ${_source} PROPERTY ${_property} SET)
  291. if (_isSet)
  292. get_property(_propertyValue ${_type} ${_source} PROPERTY ${_property})
  293. set_property(${_type} ${_target} PROPERTY ${_property} "${_propertyValue}")
  294. endif()
  295. endforeach()
  296. endfunction()
  297. function (cotire_get_target_usage_requirements _target _targetRequirementsVar)
  298. set (_targetRequirements "")
  299. get_target_property(_librariesToProcess ${_target} LINK_LIBRARIES)
  300. while (_librariesToProcess)
  301. # remove from head
  302. list (GET _librariesToProcess 0 _library)
  303. list (REMOVE_AT _librariesToProcess 0)
  304. if (TARGET ${_library})
  305. list (FIND _targetRequirements ${_library} _index)
  306. if (_index LESS 0)
  307. list (APPEND _targetRequirements ${_library})
  308. # BFS traversal of transitive libraries
  309. get_target_property(_libraries ${_library} INTERFACE_LINK_LIBRARIES)
  310. if (_libraries)
  311. list (APPEND _librariesToProcess ${_libraries})
  312. list (REMOVE_DUPLICATES _librariesToProcess)
  313. endif()
  314. endif()
  315. endif()
  316. endwhile()
  317. set (${_targetRequirementsVar} ${_targetRequirements} PARENT_SCOPE)
  318. endfunction()
  319. function (cotire_filter_compile_flags _language _flagFilter _matchedOptionsVar _unmatchedOptionsVar)
  320. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  321. set (_flagPrefix "[/-]")
  322. else()
  323. set (_flagPrefix "--?")
  324. endif()
  325. set (_optionFlag "")
  326. set (_matchedOptions "")
  327. set (_unmatchedOptions "")
  328. foreach (_compileFlag ${ARGN})
  329. if (_compileFlag)
  330. if (_optionFlag AND NOT "${_compileFlag}" MATCHES "^${_flagPrefix}")
  331. # option with separate argument
  332. list (APPEND _matchedOptions "${_compileFlag}")
  333. set (_optionFlag "")
  334. elseif ("${_compileFlag}" MATCHES "^(${_flagPrefix})(${_flagFilter})$")
  335. # remember option
  336. set (_optionFlag "${CMAKE_MATCH_2}")
  337. elseif ("${_compileFlag}" MATCHES "^(${_flagPrefix})(${_flagFilter})(.+)$")
  338. # option with joined argument
  339. list (APPEND _matchedOptions "${CMAKE_MATCH_3}")
  340. set (_optionFlag "")
  341. else()
  342. # flush remembered option
  343. if (_optionFlag)
  344. list (APPEND _matchedOptions "${_optionFlag}")
  345. set (_optionFlag "")
  346. endif()
  347. # add to unfiltered options
  348. list (APPEND _unmatchedOptions "${_compileFlag}")
  349. endif()
  350. endif()
  351. endforeach()
  352. if (_optionFlag)
  353. list (APPEND _matchedOptions "${_optionFlag}")
  354. endif()
  355. if (COTIRE_DEBUG AND _matchedOptions)
  356. message (STATUS "Filter ${_flagFilter} matched: ${_matchedOptions}")
  357. endif()
  358. if (COTIRE_DEBUG AND _unmatchedOptions)
  359. message (STATUS "Filter ${_flagFilter} unmatched: ${_unmatchedOptions}")
  360. endif()
  361. set (${_matchedOptionsVar} ${_matchedOptions} PARENT_SCOPE)
  362. set (${_unmatchedOptionsVar} ${_unmatchedOptions} PARENT_SCOPE)
  363. endfunction()
  364. function (cotire_is_target_supported _target _isSupportedVar)
  365. if (NOT TARGET "${_target}")
  366. set (${_isSupportedVar} FALSE PARENT_SCOPE)
  367. return()
  368. endif()
  369. get_target_property(_imported ${_target} IMPORTED)
  370. if (_imported)
  371. set (${_isSupportedVar} FALSE PARENT_SCOPE)
  372. return()
  373. endif()
  374. get_target_property(_targetType ${_target} TYPE)
  375. if (NOT _targetType MATCHES "EXECUTABLE|(STATIC|SHARED|MODULE|OBJECT)_LIBRARY")
  376. set (${_isSupportedVar} FALSE PARENT_SCOPE)
  377. return()
  378. endif()
  379. set (${_isSupportedVar} TRUE PARENT_SCOPE)
  380. endfunction()
  381. function (cotire_get_target_compile_flags _config _language _target _flagsVar)
  382. string (TOUPPER "${_config}" _upperConfig)
  383. # collect options from CMake language variables
  384. set (_compileFlags "")
  385. if (CMAKE_${_language}_FLAGS)
  386. set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_FLAGS}")
  387. endif()
  388. if (CMAKE_${_language}_FLAGS_${_upperConfig})
  389. set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_FLAGS_${_upperConfig}}")
  390. endif()
  391. if (_target)
  392. # add target compile flags
  393. get_target_property(_targetflags ${_target} COMPILE_FLAGS)
  394. if (_targetflags)
  395. set (_compileFlags "${_compileFlags} ${_targetflags}")
  396. endif()
  397. endif()
  398. if (UNIX)
  399. separate_arguments(_compileFlags UNIX_COMMAND "${_compileFlags}")
  400. elseif(WIN32)
  401. separate_arguments(_compileFlags WINDOWS_COMMAND "${_compileFlags}")
  402. else()
  403. separate_arguments(_compileFlags)
  404. endif()
  405. # target compile options
  406. if (_target)
  407. get_target_property(_targetOptions ${_target} COMPILE_OPTIONS)
  408. if (_targetOptions)
  409. list (APPEND _compileFlags ${_targetOptions})
  410. endif()
  411. endif()
  412. # interface compile options from linked library targets
  413. if (_target)
  414. set (_linkedTargets "")
  415. cotire_get_target_usage_requirements(${_target} _linkedTargets)
  416. foreach (_linkedTarget ${_linkedTargets})
  417. get_target_property(_targetOptions ${_linkedTarget} INTERFACE_COMPILE_OPTIONS)
  418. if (_targetOptions)
  419. list (APPEND _compileFlags ${_targetOptions})
  420. endif()
  421. endforeach()
  422. endif()
  423. # handle language standard properties
  424. if (CMAKE_${_language}_STANDARD_DEFAULT)
  425. # used compiler supports language standard levels
  426. if (_target)
  427. get_target_property(_targetLanguageStandard ${_target} ${_language}_STANDARD)
  428. get_target_property(_targetLanguageExtensions ${_target} ${_language}_EXTENSIONS)
  429. if (_targetLanguageExtensions)
  430. if (CMAKE_${_language}${_targetLanguageExtensions}_EXTENSION_COMPILE_OPTION)
  431. list (APPEND _compileFlags "${CMAKE_${_language}${_targetLanguageExtensions}_EXTENSION_COMPILE_OPTION}")
  432. endif()
  433. elseif (_targetLanguageStandard)
  434. if (CMAKE_${_language}${_targetLanguageStandard}_EXTENSION_COMPILE_OPTION)
  435. list (APPEND _compileFlags "${CMAKE_${_language}${_targetLanguageStandard}_EXTENSION_COMPILE_OPTION}")
  436. endif()
  437. endif()
  438. endif()
  439. endif()
  440. # handle the POSITION_INDEPENDENT_CODE target property
  441. if (_target)
  442. get_target_property(_targetPIC ${_target} POSITION_INDEPENDENT_CODE)
  443. if (_targetPIC)
  444. get_target_property(_targetType ${_target} TYPE)
  445. if (_targetType STREQUAL "EXECUTABLE" AND CMAKE_${_language}_COMPILE_OPTIONS_PIE)
  446. list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_PIE}")
  447. elseif (CMAKE_${_language}_COMPILE_OPTIONS_PIC)
  448. list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_PIC}")
  449. endif()
  450. endif()
  451. endif()
  452. # handle visibility target properties
  453. if (_target)
  454. get_target_property(_targetVisibility ${_target} ${_language}_VISIBILITY_PRESET)
  455. if (_targetVisibility AND CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY)
  456. list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY}${_targetVisibility}")
  457. endif()
  458. get_target_property(_targetVisibilityInlines ${_target} VISIBILITY_INLINES_HIDDEN)
  459. if (_targetVisibilityInlines AND CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN)
  460. list (APPEND _compileFlags "${CMAKE_${_language}_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN}")
  461. endif()
  462. endif()
  463. # platform specific flags
  464. if (APPLE)
  465. get_target_property(_architectures ${_target} OSX_ARCHITECTURES_${_upperConfig})
  466. if (NOT _architectures)
  467. get_target_property(_architectures ${_target} OSX_ARCHITECTURES)
  468. endif()
  469. if (_architectures)
  470. foreach (_arch ${_architectures})
  471. list (APPEND _compileFlags "-arch" "${_arch}")
  472. endforeach()
  473. endif()
  474. if (CMAKE_OSX_SYSROOT)
  475. if (CMAKE_${_language}_SYSROOT_FLAG)
  476. list (APPEND _compileFlags "${CMAKE_${_language}_SYSROOT_FLAG}" "${CMAKE_OSX_SYSROOT}")
  477. else()
  478. list (APPEND _compileFlags "-isysroot" "${CMAKE_OSX_SYSROOT}")
  479. endif()
  480. endif()
  481. if (CMAKE_OSX_DEPLOYMENT_TARGET)
  482. if (CMAKE_${_language}_OSX_DEPLOYMENT_TARGET_FLAG)
  483. list (APPEND _compileFlags "${CMAKE_${_language}_OSX_DEPLOYMENT_TARGET_FLAG}${CMAKE_OSX_DEPLOYMENT_TARGET}")
  484. else()
  485. list (APPEND _compileFlags "-mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
  486. endif()
  487. endif()
  488. endif()
  489. if (COTIRE_DEBUG AND _compileFlags)
  490. message (STATUS "Target ${_target} compile flags: ${_compileFlags}")
  491. endif()
  492. set (${_flagsVar} ${_compileFlags} PARENT_SCOPE)
  493. endfunction()
  494. function (cotire_get_target_include_directories _config _language _target _includeDirsVar _systemIncludeDirsVar)
  495. set (_includeDirs "")
  496. set (_systemIncludeDirs "")
  497. # default include dirs
  498. if (CMAKE_INCLUDE_CURRENT_DIR)
  499. list (APPEND _includeDirs "${CMAKE_CURRENT_BINARY_DIR}")
  500. list (APPEND _includeDirs "${CMAKE_CURRENT_SOURCE_DIR}")
  501. endif()
  502. set (_targetFlags "")
  503. cotire_get_target_compile_flags("${_config}" "${_language}" "${_target}" _targetFlags)
  504. # parse additional include directories from target compile flags
  505. if (CMAKE_INCLUDE_FLAG_${_language})
  506. string (STRIP "${CMAKE_INCLUDE_FLAG_${_language}}" _includeFlag)
  507. string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
  508. if (_includeFlag)
  509. set (_dirs "")
  510. cotire_filter_compile_flags("${_language}" "${_includeFlag}" _dirs _ignore ${_targetFlags})
  511. if (_dirs)
  512. list (APPEND _includeDirs ${_dirs})
  513. endif()
  514. endif()
  515. endif()
  516. # parse additional system include directories from target compile flags
  517. if (CMAKE_INCLUDE_SYSTEM_FLAG_${_language})
  518. string (STRIP "${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}" _includeFlag)
  519. string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
  520. if (_includeFlag)
  521. set (_dirs "")
  522. cotire_filter_compile_flags("${_language}" "${_includeFlag}" _dirs _ignore ${_targetFlags})
  523. if (_dirs)
  524. list (APPEND _systemIncludeDirs ${_dirs})
  525. endif()
  526. endif()
  527. endif()
  528. # target include directories
  529. get_directory_property(_dirs DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" INCLUDE_DIRECTORIES)
  530. if (_target)
  531. get_target_property(_targetDirs ${_target} INCLUDE_DIRECTORIES)
  532. if (_targetDirs)
  533. list (APPEND _dirs ${_targetDirs})
  534. endif()
  535. get_target_property(_targetDirs ${_target} INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
  536. if (_targetDirs)
  537. list (APPEND _systemIncludeDirs ${_targetDirs})
  538. endif()
  539. endif()
  540. # interface include directories from linked library targets
  541. if (_target)
  542. set (_linkedTargets "")
  543. cotire_get_target_usage_requirements(${_target} _linkedTargets)
  544. foreach (_linkedTarget ${_linkedTargets})
  545. get_target_property(_linkedTargetType ${_linkedTarget} TYPE)
  546. if (CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE AND NOT CMAKE_VERSION VERSION_LESS "3.4.0" AND
  547. _linkedTargetType MATCHES "(STATIC|SHARED|MODULE|OBJECT)_LIBRARY")
  548. # CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE refers to CMAKE_CURRENT_BINARY_DIR and CMAKE_CURRENT_SOURCE_DIR
  549. # at the time, when the target was created. These correspond to the target properties BINARY_DIR and SOURCE_DIR
  550. # which are only available with CMake 3.4 or later.
  551. get_target_property(_targetDirs ${_linkedTarget} BINARY_DIR)
  552. if (_targetDirs)
  553. list (APPEND _dirs ${_targetDirs})
  554. endif()
  555. get_target_property(_targetDirs ${_linkedTarget} SOURCE_DIR)
  556. if (_targetDirs)
  557. list (APPEND _dirs ${_targetDirs})
  558. endif()
  559. endif()
  560. get_target_property(_targetDirs ${_linkedTarget} INTERFACE_INCLUDE_DIRECTORIES)
  561. if (_targetDirs)
  562. list (APPEND _dirs ${_targetDirs})
  563. endif()
  564. get_target_property(_targetDirs ${_linkedTarget} INTERFACE_SYSTEM_INCLUDE_DIRECTORIES)
  565. if (_targetDirs)
  566. list (APPEND _systemIncludeDirs ${_targetDirs})
  567. endif()
  568. endforeach()
  569. endif()
  570. if (dirs)
  571. list (REMOVE_DUPLICATES _dirs)
  572. endif()
  573. list (LENGTH _includeDirs _projectInsertIndex)
  574. foreach (_dir ${_dirs})
  575. if (CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE)
  576. cotire_check_is_path_relative_to("${_dir}" _isRelative "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}")
  577. if (_isRelative)
  578. list (LENGTH _includeDirs _len)
  579. if (_len EQUAL _projectInsertIndex)
  580. list (APPEND _includeDirs "${_dir}")
  581. else()
  582. list (INSERT _includeDirs _projectInsertIndex "${_dir}")
  583. endif()
  584. math (EXPR _projectInsertIndex "${_projectInsertIndex} + 1")
  585. else()
  586. list (APPEND _includeDirs "${_dir}")
  587. endif()
  588. else()
  589. list (APPEND _includeDirs "${_dir}")
  590. endif()
  591. endforeach()
  592. list (REMOVE_DUPLICATES _includeDirs)
  593. list (REMOVE_DUPLICATES _systemIncludeDirs)
  594. if (CMAKE_${_language}_IMPLICIT_INCLUDE_DIRECTORIES)
  595. list (REMOVE_ITEM _includeDirs ${CMAKE_${_language}_IMPLICIT_INCLUDE_DIRECTORIES})
  596. endif()
  597. if (COTIRE_DEBUG AND _includeDirs)
  598. message (STATUS "Target ${_target} include dirs: ${_includeDirs}")
  599. endif()
  600. set (${_includeDirsVar} ${_includeDirs} PARENT_SCOPE)
  601. if (COTIRE_DEBUG AND _systemIncludeDirs)
  602. message (STATUS "Target ${_target} system include dirs: ${_systemIncludeDirs}")
  603. endif()
  604. set (${_systemIncludeDirsVar} ${_systemIncludeDirs} PARENT_SCOPE)
  605. endfunction()
  606. function (cotire_get_target_export_symbol _target _exportSymbolVar)
  607. set (_exportSymbol "")
  608. get_target_property(_targetType ${_target} TYPE)
  609. get_target_property(_enableExports ${_target} ENABLE_EXPORTS)
  610. if (_targetType MATCHES "(SHARED|MODULE)_LIBRARY" OR
  611. (_targetType STREQUAL "EXECUTABLE" AND _enableExports))
  612. get_target_property(_exportSymbol ${_target} DEFINE_SYMBOL)
  613. if (NOT _exportSymbol)
  614. set (_exportSymbol "${_target}_EXPORTS")
  615. endif()
  616. string (MAKE_C_IDENTIFIER "${_exportSymbol}" _exportSymbol)
  617. endif()
  618. set (${_exportSymbolVar} ${_exportSymbol} PARENT_SCOPE)
  619. endfunction()
  620. function (cotire_get_target_compile_definitions _config _language _target _definitionsVar)
  621. string (TOUPPER "${_config}" _upperConfig)
  622. set (_configDefinitions "")
  623. # CMAKE_INTDIR for multi-configuration build systems
  624. if (NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
  625. list (APPEND _configDefinitions "CMAKE_INTDIR=\"${_config}\"")
  626. endif()
  627. # target export define symbol
  628. cotire_get_target_export_symbol("${_target}" _defineSymbol)
  629. if (_defineSymbol)
  630. list (APPEND _configDefinitions "${_defineSymbol}")
  631. endif()
  632. # directory compile definitions
  633. get_directory_property(_definitions DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" COMPILE_DEFINITIONS)
  634. if (_definitions)
  635. list (APPEND _configDefinitions ${_definitions})
  636. endif()
  637. get_directory_property(_definitions DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" COMPILE_DEFINITIONS_${_upperConfig})
  638. if (_definitions)
  639. list (APPEND _configDefinitions ${_definitions})
  640. endif()
  641. # target compile definitions
  642. get_target_property(_definitions ${_target} COMPILE_DEFINITIONS)
  643. if (_definitions)
  644. list (APPEND _configDefinitions ${_definitions})
  645. endif()
  646. get_target_property(_definitions ${_target} COMPILE_DEFINITIONS_${_upperConfig})
  647. if (_definitions)
  648. list (APPEND _configDefinitions ${_definitions})
  649. endif()
  650. # interface compile definitions from linked library targets
  651. set (_linkedTargets "")
  652. cotire_get_target_usage_requirements(${_target} _linkedTargets)
  653. foreach (_linkedTarget ${_linkedTargets})
  654. get_target_property(_definitions ${_linkedTarget} INTERFACE_COMPILE_DEFINITIONS)
  655. if (_definitions)
  656. list (APPEND _configDefinitions ${_definitions})
  657. endif()
  658. endforeach()
  659. # parse additional compile definitions from target compile flags
  660. # and don't look at directory compile definitions, which we already handled
  661. set (_targetFlags "")
  662. cotire_get_target_compile_flags("${_config}" "${_language}" "${_target}" _targetFlags)
  663. cotire_filter_compile_flags("${_language}" "D" _definitions _ignore ${_targetFlags})
  664. if (_definitions)
  665. list (APPEND _configDefinitions ${_definitions})
  666. endif()
  667. list (REMOVE_DUPLICATES _configDefinitions)
  668. if (COTIRE_DEBUG AND _configDefinitions)
  669. message (STATUS "Target ${_target} compile definitions: ${_configDefinitions}")
  670. endif()
  671. set (${_definitionsVar} ${_configDefinitions} PARENT_SCOPE)
  672. endfunction()
  673. function (cotire_get_target_compiler_flags _config _language _target _compilerFlagsVar)
  674. # parse target compile flags omitting compile definitions and include directives
  675. set (_targetFlags "")
  676. cotire_get_target_compile_flags("${_config}" "${_language}" "${_target}" _targetFlags)
  677. set (_flagFilter "D")
  678. if (CMAKE_INCLUDE_FLAG_${_language})
  679. string (STRIP "${CMAKE_INCLUDE_FLAG_${_language}}" _includeFlag)
  680. string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
  681. if (_includeFlag)
  682. set (_flagFilter "${_flagFilter}|${_includeFlag}")
  683. endif()
  684. endif()
  685. if (CMAKE_INCLUDE_SYSTEM_FLAG_${_language})
  686. string (STRIP "${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}" _includeFlag)
  687. string (REGEX REPLACE "^[-/]+" "" _includeFlag "${_includeFlag}")
  688. if (_includeFlag)
  689. set (_flagFilter "${_flagFilter}|${_includeFlag}")
  690. endif()
  691. endif()
  692. set (_compilerFlags "")
  693. cotire_filter_compile_flags("${_language}" "${_flagFilter}" _ignore _compilerFlags ${_targetFlags})
  694. if (COTIRE_DEBUG AND _compilerFlags)
  695. message (STATUS "Target ${_target} compiler flags: ${_compilerFlags}")
  696. endif()
  697. set (${_compilerFlagsVar} ${_compilerFlags} PARENT_SCOPE)
  698. endfunction()
  699. function (cotire_add_sys_root_paths _pathsVar)
  700. if (APPLE)
  701. if (CMAKE_OSX_SYSROOT AND CMAKE_${_language}_HAS_ISYSROOT)
  702. foreach (_path IN LISTS ${_pathsVar})
  703. if (IS_ABSOLUTE "${_path}")
  704. get_filename_component(_path "${CMAKE_OSX_SYSROOT}/${_path}" ABSOLUTE)
  705. if (EXISTS "${_path}")
  706. list (APPEND ${_pathsVar} "${_path}")
  707. endif()
  708. endif()
  709. endforeach()
  710. endif()
  711. endif()
  712. set (${_pathsVar} ${${_pathsVar}} PARENT_SCOPE)
  713. endfunction()
  714. function (cotire_get_source_extra_properties _sourceFile _pattern _resultVar)
  715. set (_extraProperties ${ARGN})
  716. set (_result "")
  717. if (_extraProperties)
  718. list (FIND _extraProperties "${_sourceFile}" _index)
  719. if (_index GREATER -1)
  720. math (EXPR _index "${_index} + 1")
  721. list (LENGTH _extraProperties _len)
  722. math (EXPR _len "${_len} - 1")
  723. foreach (_index RANGE ${_index} ${_len})
  724. list (GET _extraProperties ${_index} _value)
  725. if (_value MATCHES "${_pattern}")
  726. list (APPEND _result "${_value}")
  727. else()
  728. break()
  729. endif()
  730. endforeach()
  731. endif()
  732. endif()
  733. set (${_resultVar} ${_result} PARENT_SCOPE)
  734. endfunction()
  735. function (cotire_get_source_compile_definitions _config _language _sourceFile _definitionsVar)
  736. set (_compileDefinitions "")
  737. if (NOT CMAKE_SCRIPT_MODE_FILE)
  738. string (TOUPPER "${_config}" _upperConfig)
  739. get_source_file_property(_definitions "${_sourceFile}" COMPILE_DEFINITIONS)
  740. if (_definitions)
  741. list (APPEND _compileDefinitions ${_definitions})
  742. endif()
  743. get_source_file_property(_definitions "${_sourceFile}" COMPILE_DEFINITIONS_${_upperConfig})
  744. if (_definitions)
  745. list (APPEND _compileDefinitions ${_definitions})
  746. endif()
  747. endif()
  748. cotire_get_source_extra_properties("${_sourceFile}" "^[a-zA-Z0-9_]+(=.*)?$" _definitions ${ARGN})
  749. if (_definitions)
  750. list (APPEND _compileDefinitions ${_definitions})
  751. endif()
  752. if (COTIRE_DEBUG AND _compileDefinitions)
  753. message (STATUS "Source ${_sourceFile} compile definitions: ${_compileDefinitions}")
  754. endif()
  755. set (${_definitionsVar} ${_compileDefinitions} PARENT_SCOPE)
  756. endfunction()
  757. function (cotire_get_source_files_compile_definitions _config _language _definitionsVar)
  758. set (_configDefinitions "")
  759. foreach (_sourceFile ${ARGN})
  760. cotire_get_source_compile_definitions("${_config}" "${_language}" "${_sourceFile}" _sourceDefinitions)
  761. if (_sourceDefinitions)
  762. list (APPEND _configDefinitions "${_sourceFile}" ${_sourceDefinitions} "-")
  763. endif()
  764. endforeach()
  765. set (${_definitionsVar} ${_configDefinitions} PARENT_SCOPE)
  766. endfunction()
  767. function (cotire_get_source_undefs _sourceFile _property _sourceUndefsVar)
  768. set (_sourceUndefs "")
  769. if (NOT CMAKE_SCRIPT_MODE_FILE)
  770. get_source_file_property(_undefs "${_sourceFile}" ${_property})
  771. if (_undefs)
  772. list (APPEND _sourceUndefs ${_undefs})
  773. endif()
  774. endif()
  775. cotire_get_source_extra_properties("${_sourceFile}" "^[a-zA-Z0-9_]+$" _undefs ${ARGN})
  776. if (_undefs)
  777. list (APPEND _sourceUndefs ${_undefs})
  778. endif()
  779. if (COTIRE_DEBUG AND _sourceUndefs)
  780. message (STATUS "Source ${_sourceFile} ${_property} undefs: ${_sourceUndefs}")
  781. endif()
  782. set (${_sourceUndefsVar} ${_sourceUndefs} PARENT_SCOPE)
  783. endfunction()
  784. function (cotire_get_source_files_undefs _property _sourceUndefsVar)
  785. set (_sourceUndefs "")
  786. foreach (_sourceFile ${ARGN})
  787. cotire_get_source_undefs("${_sourceFile}" ${_property} _undefs)
  788. if (_undefs)
  789. list (APPEND _sourceUndefs "${_sourceFile}" ${_undefs} "-")
  790. endif()
  791. endforeach()
  792. set (${_sourceUndefsVar} ${_sourceUndefs} PARENT_SCOPE)
  793. endfunction()
  794. macro (cotire_set_cmd_to_prologue _cmdVar)
  795. set (${_cmdVar} "${CMAKE_COMMAND}")
  796. if (COTIRE_DEBUG)
  797. list (APPEND ${_cmdVar} "--warn-uninitialized")
  798. endif()
  799. list (APPEND ${_cmdVar} "-DCOTIRE_BUILD_TYPE:STRING=$<CONFIGURATION>")
  800. if (COTIRE_VERBOSE)
  801. list (APPEND ${_cmdVar} "-DCOTIRE_VERBOSE:BOOL=ON")
  802. elseif("${CMAKE_GENERATOR}" MATCHES "Makefiles")
  803. list (APPEND ${_cmdVar} "-DCOTIRE_VERBOSE:BOOL=$(VERBOSE)")
  804. endif()
  805. endmacro()
  806. function (cotire_init_compile_cmd _cmdVar _language _compilerLauncher _compilerExe _compilerArg1)
  807. if (NOT _compilerLauncher)
  808. set (_compilerLauncher ${CMAKE_${_language}_COMPILER_LAUNCHER})
  809. endif()
  810. if (NOT _compilerExe)
  811. set (_compilerExe "${CMAKE_${_language}_COMPILER}")
  812. endif()
  813. if (NOT _compilerArg1)
  814. set (_compilerArg1 ${CMAKE_${_language}_COMPILER_ARG1})
  815. endif()
  816. string (STRIP "${_compilerArg1}" _compilerArg1)
  817. if ("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
  818. # compiler launcher is only supported for Makefile and Ninja
  819. set (${_cmdVar} ${_compilerLauncher} "${_compilerExe}" ${_compilerArg1} PARENT_SCOPE)
  820. else()
  821. set (${_cmdVar} "${_compilerExe}" ${_compilerArg1} PARENT_SCOPE)
  822. endif()
  823. endfunction()
  824. macro (cotire_add_definitions_to_cmd _cmdVar _language)
  825. foreach (_definition ${ARGN})
  826. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  827. list (APPEND ${_cmdVar} "/D${_definition}")
  828. else()
  829. list (APPEND ${_cmdVar} "-D${_definition}")
  830. endif()
  831. endforeach()
  832. endmacro()
  833. function (cotire_add_includes_to_cmd _cmdVar _language _includesVar _systemIncludesVar)
  834. set (_includeDirs ${${_includesVar}} ${${_systemIncludesVar}})
  835. if (_includeDirs)
  836. list (REMOVE_DUPLICATES _includeDirs)
  837. foreach (_include ${_includeDirs})
  838. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  839. file (TO_NATIVE_PATH "${_include}" _include)
  840. list (APPEND ${_cmdVar} "${CMAKE_INCLUDE_FLAG_${_language}}${CMAKE_INCLUDE_FLAG_${_language}_SEP}${_include}")
  841. else()
  842. set (_index -1)
  843. if ("${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}" MATCHES ".+")
  844. list (FIND ${_systemIncludesVar} "${_include}" _index)
  845. endif()
  846. if (_index GREATER -1)
  847. list (APPEND ${_cmdVar} "${CMAKE_INCLUDE_SYSTEM_FLAG_${_language}}${_include}")
  848. else()
  849. list (APPEND ${_cmdVar} "${CMAKE_INCLUDE_FLAG_${_language}}${CMAKE_INCLUDE_FLAG_${_language}_SEP}${_include}")
  850. endif()
  851. endif()
  852. endforeach()
  853. endif()
  854. set (${_cmdVar} ${${_cmdVar}} PARENT_SCOPE)
  855. endfunction()
  856. function (cotire_add_frameworks_to_cmd _cmdVar _language _includesVar _systemIncludesVar)
  857. if (APPLE)
  858. set (_frameworkDirs "")
  859. foreach (_include ${${_includesVar}})
  860. if (IS_ABSOLUTE "${_include}" AND _include MATCHES "\\.framework$")
  861. get_filename_component(_frameworkDir "${_include}" DIRECTORY)
  862. list (APPEND _frameworkDirs "${_frameworkDir}")
  863. endif()
  864. endforeach()
  865. set (_systemFrameworkDirs "")
  866. foreach (_include ${${_systemIncludesVar}})
  867. if (IS_ABSOLUTE "${_include}" AND _include MATCHES "\\.framework$")
  868. get_filename_component(_frameworkDir "${_include}" DIRECTORY)
  869. list (APPEND _systemFrameworkDirs "${_frameworkDir}")
  870. endif()
  871. endforeach()
  872. if (_systemFrameworkDirs)
  873. list (APPEND _frameworkDirs ${_systemFrameworkDirs})
  874. endif()
  875. if (_frameworkDirs)
  876. list (REMOVE_DUPLICATES _frameworkDirs)
  877. foreach (_frameworkDir ${_frameworkDirs})
  878. set (_index -1)
  879. if ("${CMAKE_${_language}_SYSTEM_FRAMEWORK_SEARCH_FLAG}" MATCHES ".+")
  880. list (FIND _systemFrameworkDirs "${_frameworkDir}" _index)
  881. endif()
  882. if (_index GREATER -1)
  883. list (APPEND ${_cmdVar} "${CMAKE_${_language}_SYSTEM_FRAMEWORK_SEARCH_FLAG}${_frameworkDir}")
  884. else()
  885. list (APPEND ${_cmdVar} "${CMAKE_${_language}_FRAMEWORK_SEARCH_FLAG}${_frameworkDir}")
  886. endif()
  887. endforeach()
  888. endif()
  889. endif()
  890. set (${_cmdVar} ${${_cmdVar}} PARENT_SCOPE)
  891. endfunction()
  892. macro (cotire_add_compile_flags_to_cmd _cmdVar)
  893. foreach (_flag ${ARGN})
  894. list (APPEND ${_cmdVar} "${_flag}")
  895. endforeach()
  896. endmacro()
  897. function (cotire_check_file_up_to_date _fileIsUpToDateVar _file)
  898. if (EXISTS "${_file}")
  899. set (_triggerFile "")
  900. foreach (_dependencyFile ${ARGN})
  901. if (EXISTS "${_dependencyFile}")
  902. # IS_NEWER_THAN returns TRUE if both files have the same timestamp
  903. # thus we do the comparison in both directions to exclude ties
  904. if ("${_dependencyFile}" IS_NEWER_THAN "${_file}" AND
  905. NOT "${_file}" IS_NEWER_THAN "${_dependencyFile}")
  906. set (_triggerFile "${_dependencyFile}")
  907. break()
  908. endif()
  909. endif()
  910. endforeach()
  911. if (_triggerFile)
  912. if (COTIRE_VERBOSE)
  913. get_filename_component(_fileName "${_file}" NAME)
  914. message (STATUS "${_fileName} update triggered by ${_triggerFile} change.")
  915. endif()
  916. set (${_fileIsUpToDateVar} FALSE PARENT_SCOPE)
  917. else()
  918. if (COTIRE_VERBOSE)
  919. get_filename_component(_fileName "${_file}" NAME)
  920. message (STATUS "${_fileName} is up-to-date.")
  921. endif()
  922. set (${_fileIsUpToDateVar} TRUE PARENT_SCOPE)
  923. endif()
  924. else()
  925. if (COTIRE_VERBOSE)
  926. get_filename_component(_fileName "${_file}" NAME)
  927. message (STATUS "${_fileName} does not exist yet.")
  928. endif()
  929. set (${_fileIsUpToDateVar} FALSE PARENT_SCOPE)
  930. endif()
  931. endfunction()
  932. macro (cotire_find_closest_relative_path _headerFile _includeDirs _relPathVar)
  933. set (${_relPathVar} "")
  934. foreach (_includeDir ${_includeDirs})
  935. if (IS_DIRECTORY "${_includeDir}")
  936. file (RELATIVE_PATH _relPath "${_includeDir}" "${_headerFile}")
  937. if (NOT IS_ABSOLUTE "${_relPath}" AND NOT "${_relPath}" MATCHES "^\\.\\.")
  938. string (LENGTH "${${_relPathVar}}" _closestLen)
  939. string (LENGTH "${_relPath}" _relLen)
  940. if (_closestLen EQUAL 0 OR _relLen LESS _closestLen)
  941. set (${_relPathVar} "${_relPath}")
  942. endif()
  943. endif()
  944. elseif ("${_includeDir}" STREQUAL "${_headerFile}")
  945. # if path matches exactly, return short non-empty string
  946. set (${_relPathVar} "1")
  947. break()
  948. endif()
  949. endforeach()
  950. endmacro()
  951. macro (cotire_check_header_file_location _headerFile _insideIncludeDirs _outsideIncludeDirs _headerIsInside)
  952. # check header path against ignored and honored include directories
  953. cotire_find_closest_relative_path("${_headerFile}" "${_insideIncludeDirs}" _insideRelPath)
  954. if (_insideRelPath)
  955. # header is inside, but could be become outside if there is a shorter outside match
  956. cotire_find_closest_relative_path("${_headerFile}" "${_outsideIncludeDirs}" _outsideRelPath)
  957. if (_outsideRelPath)
  958. string (LENGTH "${_insideRelPath}" _insideRelPathLen)
  959. string (LENGTH "${_outsideRelPath}" _outsideRelPathLen)
  960. if (_outsideRelPathLen LESS _insideRelPathLen)
  961. set (${_headerIsInside} FALSE)
  962. else()
  963. set (${_headerIsInside} TRUE)
  964. endif()
  965. else()
  966. set (${_headerIsInside} TRUE)
  967. endif()
  968. else()
  969. # header is outside
  970. set (${_headerIsInside} FALSE)
  971. endif()
  972. endmacro()
  973. macro (cotire_check_ignore_header_file_path _headerFile _headerIsIgnoredVar)
  974. if (NOT EXISTS "${_headerFile}")
  975. set (${_headerIsIgnoredVar} TRUE)
  976. elseif (IS_DIRECTORY "${_headerFile}")
  977. set (${_headerIsIgnoredVar} TRUE)
  978. elseif ("${_headerFile}" MATCHES "\\.\\.|[_-]fixed" AND "${_headerFile}" MATCHES "\\.h$")
  979. # heuristic: ignore C headers with embedded parent directory references or "-fixed" or "_fixed" in path
  980. # these often stem from using GCC #include_next tricks, which may break the precompiled header compilation
  981. # with the error message "error: no include path in which to search for header.h"
  982. set (${_headerIsIgnoredVar} TRUE)
  983. else()
  984. set (${_headerIsIgnoredVar} FALSE)
  985. endif()
  986. endmacro()
  987. macro (cotire_check_ignore_header_file_ext _headerFile _ignoreExtensionsVar _headerIsIgnoredVar)
  988. # check header file extension
  989. cotire_get_source_file_extension("${_headerFile}" _headerFileExt)
  990. set (${_headerIsIgnoredVar} FALSE)
  991. if (_headerFileExt)
  992. list (FIND ${_ignoreExtensionsVar} "${_headerFileExt}" _index)
  993. if (_index GREATER -1)
  994. set (${_headerIsIgnoredVar} TRUE)
  995. endif()
  996. endif()
  997. endmacro()
  998. macro (cotire_parse_line _line _headerFileVar _headerDepthVar)
  999. if (MSVC)
  1000. # cl.exe /showIncludes output looks different depending on the language pack used, e.g.:
  1001. # English: "Note: including file: C:\directory\file"
  1002. # German: "Hinweis: Einlesen der Datei: C:\directory\file"
  1003. # We use a very general regular expression, relying on the presence of the : characters
  1004. if (_line MATCHES "( +)([a-zA-Z]:[^:]+)$")
  1005. # Visual Studio compiler output
  1006. string (LENGTH "${CMAKE_MATCH_1}" ${_headerDepthVar})
  1007. get_filename_component(${_headerFileVar} "${CMAKE_MATCH_2}" ABSOLUTE)
  1008. else()
  1009. set (${_headerFileVar} "")
  1010. set (${_headerDepthVar} 0)
  1011. endif()
  1012. else()
  1013. if (_line MATCHES "^(\\.+) (.*)$")
  1014. # GCC like output
  1015. string (LENGTH "${CMAKE_MATCH_1}" ${_headerDepthVar})
  1016. if (IS_ABSOLUTE "${CMAKE_MATCH_2}")
  1017. set (${_headerFileVar} "${CMAKE_MATCH_2}")
  1018. else()
  1019. get_filename_component(${_headerFileVar} "${CMAKE_MATCH_2}" REALPATH)
  1020. endif()
  1021. else()
  1022. set (${_headerFileVar} "")
  1023. set (${_headerDepthVar} 0)
  1024. endif()
  1025. endif()
  1026. endmacro()
  1027. function (cotire_parse_includes _language _scanOutput _ignoredIncludeDirs _honoredIncludeDirs _ignoredExtensions _selectedIncludesVar _unparsedLinesVar)
  1028. if (WIN32)
  1029. # prevent CMake macro invocation errors due to backslash characters in Windows paths
  1030. string (REPLACE "\\" "/" _scanOutput "${_scanOutput}")
  1031. endif()
  1032. # canonize slashes
  1033. string (REPLACE "//" "/" _scanOutput "${_scanOutput}")
  1034. # prevent semicolon from being interpreted as a line separator
  1035. string (REPLACE ";" "\\;" _scanOutput "${_scanOutput}")
  1036. # then separate lines
  1037. string (REGEX REPLACE "\n" ";" _scanOutput "${_scanOutput}")
  1038. list (LENGTH _scanOutput _len)
  1039. # remove duplicate lines to speed up parsing
  1040. list (REMOVE_DUPLICATES _scanOutput)
  1041. list (LENGTH _scanOutput _uniqueLen)
  1042. if (COTIRE_VERBOSE OR COTIRE_DEBUG)
  1043. message (STATUS "Scanning ${_uniqueLen} unique lines of ${_len} for includes")
  1044. if (_ignoredExtensions)
  1045. message (STATUS "Ignored extensions: ${_ignoredExtensions}")
  1046. endif()
  1047. if (_ignoredIncludeDirs)
  1048. message (STATUS "Ignored paths: ${_ignoredIncludeDirs}")
  1049. endif()
  1050. if (_honoredIncludeDirs)
  1051. message (STATUS "Included paths: ${_honoredIncludeDirs}")
  1052. endif()
  1053. endif()
  1054. set (_sourceFiles ${ARGN})
  1055. set (_selectedIncludes "")
  1056. set (_unparsedLines "")
  1057. # stack keeps track of inside/outside project status of processed header files
  1058. set (_headerIsInsideStack "")
  1059. foreach (_line IN LISTS _scanOutput)
  1060. if (_line)
  1061. cotire_parse_line("${_line}" _headerFile _headerDepth)
  1062. if (_headerFile)
  1063. cotire_check_header_file_location("${_headerFile}" "${_ignoredIncludeDirs}" "${_honoredIncludeDirs}" _headerIsInside)
  1064. if (COTIRE_DEBUG)
  1065. message (STATUS "${_headerDepth}: ${_headerFile} ${_headerIsInside}")
  1066. endif()
  1067. # update stack
  1068. list (LENGTH _headerIsInsideStack _stackLen)
  1069. if (_headerDepth GREATER _stackLen)
  1070. math (EXPR _stackLen "${_stackLen} + 1")
  1071. foreach (_index RANGE ${_stackLen} ${_headerDepth})
  1072. list (APPEND _headerIsInsideStack ${_headerIsInside})
  1073. endforeach()
  1074. else()
  1075. foreach (_index RANGE ${_headerDepth} ${_stackLen})
  1076. list (REMOVE_AT _headerIsInsideStack -1)
  1077. endforeach()
  1078. list (APPEND _headerIsInsideStack ${_headerIsInside})
  1079. endif()
  1080. if (COTIRE_DEBUG)
  1081. message (STATUS "${_headerIsInsideStack}")
  1082. endif()
  1083. # header is a candidate if it is outside project
  1084. if (NOT _headerIsInside)
  1085. # get parent header file's inside/outside status
  1086. if (_headerDepth GREATER 1)
  1087. math (EXPR _index "${_headerDepth} - 2")
  1088. list (GET _headerIsInsideStack ${_index} _parentHeaderIsInside)
  1089. else()
  1090. set (_parentHeaderIsInside TRUE)
  1091. endif()
  1092. # select header file if parent header file is inside project
  1093. # (e.g., a project header file that includes a standard header file)
  1094. if (_parentHeaderIsInside)
  1095. cotire_check_ignore_header_file_path("${_headerFile}" _headerIsIgnored)
  1096. if (NOT _headerIsIgnored)
  1097. cotire_check_ignore_header_file_ext("${_headerFile}" _ignoredExtensions _headerIsIgnored)
  1098. if (NOT _headerIsIgnored)
  1099. list (APPEND _selectedIncludes "${_headerFile}")
  1100. else()
  1101. # fix header's inside status on stack, it is ignored by extension now
  1102. list (REMOVE_AT _headerIsInsideStack -1)
  1103. list (APPEND _headerIsInsideStack TRUE)
  1104. endif()
  1105. endif()
  1106. if (COTIRE_DEBUG)
  1107. message (STATUS "${_headerFile} ${_ignoredExtensions} ${_headerIsIgnored}")
  1108. endif()
  1109. endif()
  1110. endif()
  1111. else()
  1112. if (MSVC)
  1113. # for cl.exe do not keep unparsed lines which solely consist of a source file name
  1114. string (FIND "${_sourceFiles}" "${_line}" _index)
  1115. if (_index LESS 0)
  1116. list (APPEND _unparsedLines "${_line}")
  1117. endif()
  1118. else()
  1119. list (APPEND _unparsedLines "${_line}")
  1120. endif()
  1121. endif()
  1122. endif()
  1123. endforeach()
  1124. list (REMOVE_DUPLICATES _selectedIncludes)
  1125. set (${_selectedIncludesVar} ${_selectedIncludes} PARENT_SCOPE)
  1126. set (${_unparsedLinesVar} ${_unparsedLines} PARENT_SCOPE)
  1127. endfunction()
  1128. function (cotire_scan_includes _includesVar)
  1129. set(_options "")
  1130. set(_oneValueArgs COMPILER_ID COMPILER_EXECUTABLE COMPILER_ARG1 COMPILER_VERSION LANGUAGE UNPARSED_LINES SCAN_RESULT)
  1131. set(_multiValueArgs COMPILE_DEFINITIONS COMPILE_FLAGS INCLUDE_DIRECTORIES SYSTEM_INCLUDE_DIRECTORIES
  1132. IGNORE_PATH INCLUDE_PATH IGNORE_EXTENSIONS INCLUDE_PRIORITY_PATH COMPILER_LAUNCHER)
  1133. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  1134. set (_sourceFiles ${_option_UNPARSED_ARGUMENTS})
  1135. if (NOT _option_LANGUAGE)
  1136. set (_option_LANGUAGE "CXX")
  1137. endif()
  1138. if (NOT _option_COMPILER_ID)
  1139. set (_option_COMPILER_ID "${CMAKE_${_option_LANGUAGE}_ID}")
  1140. endif()
  1141. if (NOT _option_COMPILER_VERSION)
  1142. set (_option_COMPILER_VERSION "${CMAKE_${_option_LANGUAGE}_COMPILER_VERSION}")
  1143. endif()
  1144. cotire_init_compile_cmd(_cmd "${_option_LANGUAGE}" "${_option_COMPILER_LAUNCHER}" "${_option_COMPILER_EXECUTABLE}" "${_option_COMPILER_ARG1}")
  1145. cotire_add_definitions_to_cmd(_cmd "${_option_LANGUAGE}" ${_option_COMPILE_DEFINITIONS})
  1146. cotire_add_compile_flags_to_cmd(_cmd ${_option_COMPILE_FLAGS})
  1147. cotire_add_includes_to_cmd(_cmd "${_option_LANGUAGE}" _option_INCLUDE_DIRECTORIES _option_SYSTEM_INCLUDE_DIRECTORIES)
  1148. cotire_add_frameworks_to_cmd(_cmd "${_option_LANGUAGE}" _option_INCLUDE_DIRECTORIES _option_SYSTEM_INCLUDE_DIRECTORIES)
  1149. cotire_add_makedep_flags("${_option_LANGUAGE}" "${_option_COMPILER_ID}" "${_option_COMPILER_VERSION}" _cmd)
  1150. # only consider existing source files for scanning
  1151. set (_existingSourceFiles "")
  1152. foreach (_sourceFile ${_sourceFiles})
  1153. if (EXISTS "${_sourceFile}")
  1154. list (APPEND _existingSourceFiles "${_sourceFile}")
  1155. endif()
  1156. endforeach()
  1157. if (NOT _existingSourceFiles)
  1158. set (${_includesVar} "" PARENT_SCOPE)
  1159. return()
  1160. endif()
  1161. list (APPEND _cmd ${_existingSourceFiles})
  1162. if (COTIRE_VERBOSE)
  1163. message (STATUS "execute_process: ${_cmd}")
  1164. endif()
  1165. if (_option_COMPILER_ID MATCHES "MSVC")
  1166. # cl.exe messes with the output streams unless the environment variable VS_UNICODE_OUTPUT is cleared
  1167. unset (ENV{VS_UNICODE_OUTPUT})
  1168. endif()
  1169. execute_process(
  1170. COMMAND ${_cmd}
  1171. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  1172. RESULT_VARIABLE _result
  1173. OUTPUT_QUIET
  1174. ERROR_VARIABLE _output)
  1175. if (_result)
  1176. message (STATUS "Result ${_result} scanning includes of ${_existingSourceFiles}.")
  1177. endif()
  1178. cotire_parse_includes(
  1179. "${_option_LANGUAGE}" "${_output}"
  1180. "${_option_IGNORE_PATH}" "${_option_INCLUDE_PATH}"
  1181. "${_option_IGNORE_EXTENSIONS}"
  1182. _includes _unparsedLines
  1183. ${_sourceFiles})
  1184. if (_option_INCLUDE_PRIORITY_PATH)
  1185. set (_sortedIncludes "")
  1186. foreach (_priorityPath ${_option_INCLUDE_PRIORITY_PATH})
  1187. foreach (_include ${_includes})
  1188. string (FIND ${_include} ${_priorityPath} _position)
  1189. if (_position GREATER -1)
  1190. list (APPEND _sortedIncludes ${_include})
  1191. endif()
  1192. endforeach()
  1193. endforeach()
  1194. if (_sortedIncludes)
  1195. list (INSERT _includes 0 ${_sortedIncludes})
  1196. list (REMOVE_DUPLICATES _includes)
  1197. endif()
  1198. endif()
  1199. set (${_includesVar} ${_includes} PARENT_SCOPE)
  1200. if (_option_UNPARSED_LINES)
  1201. set (${_option_UNPARSED_LINES} ${_unparsedLines} PARENT_SCOPE)
  1202. endif()
  1203. if (_option_SCAN_RESULT)
  1204. set (${_option_SCAN_RESULT} ${_result} PARENT_SCOPE)
  1205. endif()
  1206. endfunction()
  1207. macro (cotire_append_undefs _contentsVar)
  1208. set (_undefs ${ARGN})
  1209. if (_undefs)
  1210. list (REMOVE_DUPLICATES _undefs)
  1211. foreach (_definition ${_undefs})
  1212. list (APPEND ${_contentsVar} "#undef ${_definition}")
  1213. endforeach()
  1214. endif()
  1215. endmacro()
  1216. macro (cotire_comment_str _language _commentText _commentVar)
  1217. if ("${_language}" STREQUAL "CMAKE")
  1218. set (${_commentVar} "# ${_commentText}")
  1219. else()
  1220. set (${_commentVar} "/* ${_commentText} */")
  1221. endif()
  1222. endmacro()
  1223. function (cotire_write_file _language _file _contents _force)
  1224. get_filename_component(_moduleName "${COTIRE_CMAKE_MODULE_FILE}" NAME)
  1225. cotire_comment_str("${_language}" "${_moduleName} ${COTIRE_CMAKE_MODULE_VERSION} generated file" _header1)
  1226. cotire_comment_str("${_language}" "${_file}" _header2)
  1227. set (_contents "${_header1}\n${_header2}\n${_contents}")
  1228. if (COTIRE_DEBUG)
  1229. message (STATUS "${_contents}")
  1230. endif()
  1231. if (_force OR NOT EXISTS "${_file}")
  1232. file (WRITE "${_file}" "${_contents}")
  1233. else()
  1234. file (READ "${_file}" _oldContents)
  1235. if (NOT "${_oldContents}" STREQUAL "${_contents}")
  1236. file (WRITE "${_file}" "${_contents}")
  1237. else()
  1238. if (COTIRE_DEBUG)
  1239. message (STATUS "${_file} unchanged")
  1240. endif()
  1241. endif()
  1242. endif()
  1243. endfunction()
  1244. function (cotire_generate_unity_source _unityFile)
  1245. set(_options "")
  1246. set(_oneValueArgs LANGUAGE)
  1247. set(_multiValueArgs
  1248. DEPENDS SOURCES_COMPILE_DEFINITIONS
  1249. PRE_UNDEFS SOURCES_PRE_UNDEFS POST_UNDEFS SOURCES_POST_UNDEFS PROLOGUE EPILOGUE)
  1250. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  1251. if (_option_DEPENDS)
  1252. cotire_check_file_up_to_date(_unityFileIsUpToDate "${_unityFile}" ${_option_DEPENDS})
  1253. if (_unityFileIsUpToDate)
  1254. return()
  1255. endif()
  1256. endif()
  1257. set (_sourceFiles ${_option_UNPARSED_ARGUMENTS})
  1258. if (NOT _option_PRE_UNDEFS)
  1259. set (_option_PRE_UNDEFS "")
  1260. endif()
  1261. if (NOT _option_SOURCES_PRE_UNDEFS)
  1262. set (_option_SOURCES_PRE_UNDEFS "")
  1263. endif()
  1264. if (NOT _option_POST_UNDEFS)
  1265. set (_option_POST_UNDEFS "")
  1266. endif()
  1267. if (NOT _option_SOURCES_POST_UNDEFS)
  1268. set (_option_SOURCES_POST_UNDEFS "")
  1269. endif()
  1270. set (_contents "")
  1271. if (_option_PROLOGUE)
  1272. list (APPEND _contents ${_option_PROLOGUE})
  1273. endif()
  1274. if (_option_LANGUAGE AND _sourceFiles)
  1275. if ("${_option_LANGUAGE}" STREQUAL "CXX")
  1276. list (APPEND _contents "#ifdef __cplusplus")
  1277. elseif ("${_option_LANGUAGE}" STREQUAL "C")
  1278. list (APPEND _contents "#ifndef __cplusplus")
  1279. endif()
  1280. endif()
  1281. set (_compileUndefinitions "")
  1282. foreach (_sourceFile ${_sourceFiles})
  1283. cotire_get_source_compile_definitions(
  1284. "${_option_CONFIGURATION}" "${_option_LANGUAGE}" "${_sourceFile}" _compileDefinitions
  1285. ${_option_SOURCES_COMPILE_DEFINITIONS})
  1286. cotire_get_source_undefs("${_sourceFile}" COTIRE_UNITY_SOURCE_PRE_UNDEFS _sourcePreUndefs ${_option_SOURCES_PRE_UNDEFS})
  1287. cotire_get_source_undefs("${_sourceFile}" COTIRE_UNITY_SOURCE_POST_UNDEFS _sourcePostUndefs ${_option_SOURCES_POST_UNDEFS})
  1288. if (_option_PRE_UNDEFS)
  1289. list (APPEND _compileUndefinitions ${_option_PRE_UNDEFS})
  1290. endif()
  1291. if (_sourcePreUndefs)
  1292. list (APPEND _compileUndefinitions ${_sourcePreUndefs})
  1293. endif()
  1294. if (_compileUndefinitions)
  1295. cotire_append_undefs(_contents ${_compileUndefinitions})
  1296. set (_compileUndefinitions "")
  1297. endif()
  1298. if (_sourcePostUndefs)
  1299. list (APPEND _compileUndefinitions ${_sourcePostUndefs})
  1300. endif()
  1301. if (_option_POST_UNDEFS)
  1302. list (APPEND _compileUndefinitions ${_option_POST_UNDEFS})
  1303. endif()
  1304. foreach (_definition ${_compileDefinitions})
  1305. if (_definition MATCHES "^([a-zA-Z0-9_]+)=(.+)$")
  1306. list (APPEND _contents "#define ${CMAKE_MATCH_1} ${CMAKE_MATCH_2}")
  1307. list (INSERT _compileUndefinitions 0 "${CMAKE_MATCH_1}")
  1308. else()
  1309. list (APPEND _contents "#define ${_definition}")
  1310. list (INSERT _compileUndefinitions 0 "${_definition}")
  1311. endif()
  1312. endforeach()
  1313. # use absolute path as source file location
  1314. get_filename_component(_sourceFileLocation "${_sourceFile}" ABSOLUTE)
  1315. if (WIN32)
  1316. file (TO_NATIVE_PATH "${_sourceFileLocation}" _sourceFileLocation)
  1317. endif()
  1318. list (APPEND _contents "#include \"${_sourceFileLocation}\"")
  1319. endforeach()
  1320. if (_compileUndefinitions)
  1321. cotire_append_undefs(_contents ${_compileUndefinitions})
  1322. set (_compileUndefinitions "")
  1323. endif()
  1324. if (_option_LANGUAGE AND _sourceFiles)
  1325. list (APPEND _contents "#endif")
  1326. endif()
  1327. if (_option_EPILOGUE)
  1328. list (APPEND _contents ${_option_EPILOGUE})
  1329. endif()
  1330. list (APPEND _contents "")
  1331. string (REPLACE ";" "\n" _contents "${_contents}")
  1332. if (COTIRE_VERBOSE)
  1333. message ("${_contents}")
  1334. endif()
  1335. cotire_write_file("${_option_LANGUAGE}" "${_unityFile}" "${_contents}" TRUE)
  1336. endfunction()
  1337. function (cotire_generate_prefix_header _prefixFile)
  1338. set(_options "")
  1339. set(_oneValueArgs LANGUAGE COMPILER_EXECUTABLE COMPILER_ARG1 COMPILER_ID COMPILER_VERSION)
  1340. set(_multiValueArgs DEPENDS COMPILE_DEFINITIONS COMPILE_FLAGS
  1341. INCLUDE_DIRECTORIES SYSTEM_INCLUDE_DIRECTORIES IGNORE_PATH INCLUDE_PATH
  1342. IGNORE_EXTENSIONS INCLUDE_PRIORITY_PATH COMPILER_LAUNCHER)
  1343. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  1344. if (NOT _option_COMPILER_ID)
  1345. set (_option_COMPILER_ID "${CMAKE_${_option_LANGUAGE}_ID}")
  1346. endif()
  1347. if (NOT _option_COMPILER_VERSION)
  1348. set (_option_COMPILER_VERSION "${CMAKE_${_option_LANGUAGE}_COMPILER_VERSION}")
  1349. endif()
  1350. if (_option_DEPENDS)
  1351. cotire_check_file_up_to_date(_prefixFileIsUpToDate "${_prefixFile}" ${_option_DEPENDS})
  1352. if (_prefixFileIsUpToDate)
  1353. # create empty log file
  1354. set (_unparsedLinesFile "${_prefixFile}.log")
  1355. file (WRITE "${_unparsedLinesFile}" "")
  1356. return()
  1357. endif()
  1358. endif()
  1359. set (_prologue "")
  1360. set (_epilogue "")
  1361. if (_option_COMPILER_ID MATCHES "Clang")
  1362. set (_prologue "#pragma clang system_header")
  1363. elseif (_option_COMPILER_ID MATCHES "GNU")
  1364. set (_prologue "#pragma GCC system_header")
  1365. elseif (_option_COMPILER_ID MATCHES "MSVC")
  1366. set (_prologue "#pragma warning(push, 0)")
  1367. set (_epilogue "#pragma warning(pop)")
  1368. elseif (_option_COMPILER_ID MATCHES "Intel")
  1369. # Intel compiler requires hdrstop pragma to stop generating PCH file
  1370. set (_epilogue "#pragma hdrstop")
  1371. endif()
  1372. set (_sourceFiles ${_option_UNPARSED_ARGUMENTS})
  1373. cotire_scan_includes(_selectedHeaders ${_sourceFiles}
  1374. LANGUAGE "${_option_LANGUAGE}"
  1375. COMPILER_LAUNCHER "${_option_COMPILER_LAUNCHER}"
  1376. COMPILER_EXECUTABLE "${_option_COMPILER_EXECUTABLE}"
  1377. COMPILER_ARG1 "${_option_COMPILER_ARG1}"
  1378. COMPILER_ID "${_option_COMPILER_ID}"
  1379. COMPILER_VERSION "${_option_COMPILER_VERSION}"
  1380. COMPILE_DEFINITIONS ${_option_COMPILE_DEFINITIONS}
  1381. COMPILE_FLAGS ${_option_COMPILE_FLAGS}
  1382. INCLUDE_DIRECTORIES ${_option_INCLUDE_DIRECTORIES}
  1383. SYSTEM_INCLUDE_DIRECTORIES ${_option_SYSTEM_INCLUDE_DIRECTORIES}
  1384. IGNORE_PATH ${_option_IGNORE_PATH}
  1385. INCLUDE_PATH ${_option_INCLUDE_PATH}
  1386. IGNORE_EXTENSIONS ${_option_IGNORE_EXTENSIONS}
  1387. INCLUDE_PRIORITY_PATH ${_option_INCLUDE_PRIORITY_PATH}
  1388. UNPARSED_LINES _unparsedLines
  1389. SCAN_RESULT _scanResult)
  1390. cotire_generate_unity_source("${_prefixFile}"
  1391. PROLOGUE ${_prologue} EPILOGUE ${_epilogue} LANGUAGE "${_option_LANGUAGE}" ${_selectedHeaders})
  1392. set (_unparsedLinesFile "${_prefixFile}.log")
  1393. if (_unparsedLines)
  1394. if (COTIRE_VERBOSE OR _scanResult OR NOT _selectedHeaders)
  1395. list (LENGTH _unparsedLines _skippedLineCount)
  1396. message (STATUS "${_skippedLineCount} line(s) skipped, see ${_unparsedLinesFile}")
  1397. endif()
  1398. string (REPLACE ";" "\n" _unparsedLines "${_unparsedLines}")
  1399. endif()
  1400. file (WRITE "${_unparsedLinesFile}" "${_unparsedLines}")
  1401. endfunction()
  1402. function (cotire_add_makedep_flags _language _compilerID _compilerVersion _flagsVar)
  1403. set (_flags ${${_flagsVar}})
  1404. if (_compilerID MATCHES "MSVC")
  1405. # cl.exe options used
  1406. # /nologo suppresses display of sign-on banner
  1407. # /TC treat all files named on the command line as C source files
  1408. # /TP treat all files named on the command line as C++ source files
  1409. # /EP preprocess to stdout without #line directives
  1410. # /showIncludes list include files
  1411. set (_sourceFileTypeC "/TC")
  1412. set (_sourceFileTypeCXX "/TP")
  1413. if (_flags)
  1414. # append to list
  1415. list (APPEND _flags /nologo "${_sourceFileType${_language}}" /EP /showIncludes)
  1416. else()
  1417. # return as a flag string
  1418. set (_flags "${_sourceFileType${_language}} /EP /showIncludes")
  1419. endif()
  1420. elseif (_compilerID MATCHES "GNU")
  1421. # GCC options used
  1422. # -H print the name of each header file used
  1423. # -E invoke preprocessor
  1424. # -fdirectives-only do not expand macros, requires GCC >= 4.3
  1425. if (_flags)
  1426. # append to list
  1427. list (APPEND _flags -H -E)
  1428. if (NOT "${_compilerVersion}" VERSION_LESS "4.3.0")
  1429. list (APPEND _flags "-fdirectives-only")
  1430. endif()
  1431. else()
  1432. # return as a flag string
  1433. set (_flags "-H -E")
  1434. if (NOT "${_compilerVersion}" VERSION_LESS "4.3.0")
  1435. set (_flags "${_flags} -fdirectives-only")
  1436. endif()
  1437. endif()
  1438. elseif (_compilerID MATCHES "Clang")
  1439. # Clang options used
  1440. # -H print the name of each header file used
  1441. # -E invoke preprocessor
  1442. # -fno-color-diagnostics don't prints diagnostics in color
  1443. if (_flags)
  1444. # append to list
  1445. list (APPEND _flags -H -E -fno-color-diagnostics)
  1446. else()
  1447. # return as a flag string
  1448. set (_flags "-H -E -fno-color-diagnostics")
  1449. endif()
  1450. elseif (_compilerID MATCHES "Intel")
  1451. if (WIN32)
  1452. # Windows Intel options used
  1453. # /nologo do not display compiler version information
  1454. # /QH display the include file order
  1455. # /EP preprocess to stdout, omitting #line directives
  1456. # /TC process all source or unrecognized file types as C source files
  1457. # /TP process all source or unrecognized file types as C++ source files
  1458. set (_sourceFileTypeC "/TC")
  1459. set (_sourceFileTypeCXX "/TP")
  1460. if (_flags)
  1461. # append to list
  1462. list (APPEND _flags /nologo "${_sourceFileType${_language}}" /EP /QH)
  1463. else()
  1464. # return as a flag string
  1465. set (_flags "${_sourceFileType${_language}} /EP /QH")
  1466. endif()
  1467. else()
  1468. # Linux / Mac OS X Intel options used
  1469. # -H print the name of each header file used
  1470. # -EP preprocess to stdout, omitting #line directives
  1471. # -Kc++ process all source or unrecognized file types as C++ source files
  1472. if (_flags)
  1473. # append to list
  1474. if ("${_language}" STREQUAL "CXX")
  1475. list (APPEND _flags -Kc++)
  1476. endif()
  1477. list (APPEND _flags -H -EP)
  1478. else()
  1479. # return as a flag string
  1480. if ("${_language}" STREQUAL "CXX")
  1481. set (_flags "-Kc++ ")
  1482. endif()
  1483. set (_flags "${_flags}-H -EP")
  1484. endif()
  1485. endif()
  1486. else()
  1487. message (FATAL_ERROR "cotire: unsupported ${_language} compiler ${_compilerID} version ${_compilerVersion}.")
  1488. endif()
  1489. set (${_flagsVar} ${_flags} PARENT_SCOPE)
  1490. endfunction()
  1491. function (cotire_add_pch_compilation_flags _language _compilerID _compilerVersion _prefixFile _pchFile _hostFile _flagsVar)
  1492. set (_flags ${${_flagsVar}})
  1493. if (_compilerID MATCHES "MSVC")
  1494. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1495. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1496. file (TO_NATIVE_PATH "${_hostFile}" _hostFileNative)
  1497. # cl.exe options used
  1498. # /Yc creates a precompiled header file
  1499. # /Fp specifies precompiled header binary file name
  1500. # /FI forces inclusion of file
  1501. # /TC treat all files named on the command line as C source files
  1502. # /TP treat all files named on the command line as C++ source files
  1503. # /Zs syntax check only
  1504. # /Zm precompiled header memory allocation scaling factor
  1505. set (_sourceFileTypeC "/TC")
  1506. set (_sourceFileTypeCXX "/TP")
  1507. if (_flags)
  1508. # append to list
  1509. list (APPEND _flags /nologo "${_sourceFileType${_language}}"
  1510. "/Yc${_prefixFileNative}" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}" /Zs "${_hostFileNative}")
  1511. if (COTIRE_PCH_MEMORY_SCALING_FACTOR)
  1512. list (APPEND _flags "/Zm${COTIRE_PCH_MEMORY_SCALING_FACTOR}")
  1513. endif()
  1514. else()
  1515. # return as a flag string
  1516. set (_flags "/Yc\"${_prefixFileNative}\" /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1517. if (COTIRE_PCH_MEMORY_SCALING_FACTOR)
  1518. set (_flags "${_flags} /Zm${COTIRE_PCH_MEMORY_SCALING_FACTOR}")
  1519. endif()
  1520. endif()
  1521. elseif (_compilerID MATCHES "GNU|Clang")
  1522. # GCC / Clang options used
  1523. # -x specify the source language
  1524. # -c compile but do not link
  1525. # -o place output in file
  1526. # note that we cannot use -w to suppress all warnings upon pre-compiling, because turning off a warning may
  1527. # alter compile flags as a side effect (e.g., -Wwrite-string implies -fconst-strings)
  1528. set (_xLanguage_C "c-header")
  1529. set (_xLanguage_CXX "c++-header")
  1530. if (_flags)
  1531. # append to list
  1532. list (APPEND _flags "-x" "${_xLanguage_${_language}}" "-c" "${_prefixFile}" -o "${_pchFile}")
  1533. else()
  1534. # return as a flag string
  1535. set (_flags "-x ${_xLanguage_${_language}} -c \"${_prefixFile}\" -o \"${_pchFile}\"")
  1536. endif()
  1537. elseif (_compilerID MATCHES "Intel")
  1538. if (WIN32)
  1539. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1540. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1541. file (TO_NATIVE_PATH "${_hostFile}" _hostFileNative)
  1542. # Windows Intel options used
  1543. # /nologo do not display compiler version information
  1544. # /Yc create a precompiled header (PCH) file
  1545. # /Fp specify a path or file name for precompiled header files
  1546. # /FI tells the preprocessor to include a specified file name as the header file
  1547. # /TC process all source or unrecognized file types as C source files
  1548. # /TP process all source or unrecognized file types as C++ source files
  1549. # /Zs syntax check only
  1550. # /Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1551. set (_sourceFileTypeC "/TC")
  1552. set (_sourceFileTypeCXX "/TP")
  1553. if (_flags)
  1554. # append to list
  1555. list (APPEND _flags /nologo "${_sourceFileType${_language}}"
  1556. "/Yc" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}" /Zs "${_hostFileNative}")
  1557. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1558. list (APPEND _flags "/Wpch-messages")
  1559. endif()
  1560. else()
  1561. # return as a flag string
  1562. set (_flags "/Yc /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1563. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1564. set (_flags "${_flags} /Wpch-messages")
  1565. endif()
  1566. endif()
  1567. else()
  1568. # Linux / Mac OS X Intel options used
  1569. # -pch-dir location for precompiled header files
  1570. # -pch-create name of the precompiled header (PCH) to create
  1571. # -Kc++ process all source or unrecognized file types as C++ source files
  1572. # -fsyntax-only check only for correct syntax
  1573. # -Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1574. get_filename_component(_pchDir "${_pchFile}" DIRECTORY)
  1575. get_filename_component(_pchName "${_pchFile}" NAME)
  1576. set (_xLanguage_C "c-header")
  1577. set (_xLanguage_CXX "c++-header")
  1578. if (_flags)
  1579. # append to list
  1580. if ("${_language}" STREQUAL "CXX")
  1581. list (APPEND _flags -Kc++)
  1582. endif()
  1583. list (APPEND _flags "-include" "${_prefixFile}" "-pch-dir" "${_pchDir}" "-pch-create" "${_pchName}" "-fsyntax-only" "${_hostFile}")
  1584. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1585. list (APPEND _flags "-Wpch-messages")
  1586. endif()
  1587. else()
  1588. # return as a flag string
  1589. set (_flags "-include \"${_prefixFile}\" -pch-dir \"${_pchDir}\" -pch-create \"${_pchName}\"")
  1590. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1591. set (_flags "${_flags} -Wpch-messages")
  1592. endif()
  1593. endif()
  1594. endif()
  1595. else()
  1596. message (FATAL_ERROR "cotire: unsupported ${_language} compiler ${_compilerID} version ${_compilerVersion}.")
  1597. endif()
  1598. set (${_flagsVar} ${_flags} PARENT_SCOPE)
  1599. endfunction()
  1600. function (cotire_add_prefix_pch_inclusion_flags _language _compilerID _compilerVersion _prefixFile _pchFile _flagsVar)
  1601. set (_flags ${${_flagsVar}})
  1602. if (_compilerID MATCHES "MSVC")
  1603. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1604. # cl.exe options used
  1605. # /Yu uses a precompiled header file during build
  1606. # /Fp specifies precompiled header binary file name
  1607. # /FI forces inclusion of file
  1608. # /Zm precompiled header memory allocation scaling factor
  1609. if (_pchFile)
  1610. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1611. if (_flags)
  1612. # append to list
  1613. list (APPEND _flags "/Yu${_prefixFileNative}" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}")
  1614. if (COTIRE_PCH_MEMORY_SCALING_FACTOR)
  1615. list (APPEND _flags "/Zm${COTIRE_PCH_MEMORY_SCALING_FACTOR}")
  1616. endif()
  1617. else()
  1618. # return as a flag string
  1619. set (_flags "/Yu\"${_prefixFileNative}\" /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1620. if (COTIRE_PCH_MEMORY_SCALING_FACTOR)
  1621. set (_flags "${_flags} /Zm${COTIRE_PCH_MEMORY_SCALING_FACTOR}")
  1622. endif()
  1623. endif()
  1624. else()
  1625. # no precompiled header, force inclusion of prefix header
  1626. if (_flags)
  1627. # append to list
  1628. list (APPEND _flags "/FI${_prefixFileNative}")
  1629. else()
  1630. # return as a flag string
  1631. set (_flags "/FI\"${_prefixFileNative}\"")
  1632. endif()
  1633. endif()
  1634. elseif (_compilerID MATCHES "GNU")
  1635. # GCC options used
  1636. # -include process include file as the first line of the primary source file
  1637. # -Winvalid-pch warns if precompiled header is found but cannot be used
  1638. # note: ccache requires the -include flag to be used in order to process precompiled header correctly
  1639. if (_flags)
  1640. # append to list
  1641. list (APPEND _flags "-Winvalid-pch" "-include" "${_prefixFile}")
  1642. else()
  1643. # return as a flag string
  1644. set (_flags "-Winvalid-pch -include \"${_prefixFile}\"")
  1645. endif()
  1646. elseif (_compilerID MATCHES "Clang")
  1647. # Clang options used
  1648. # -include process include file as the first line of the primary source file
  1649. # -include-pch include precompiled header file
  1650. # -Qunused-arguments don't emit warning for unused driver arguments
  1651. # note: ccache requires the -include flag to be used in order to process precompiled header correctly
  1652. if (_flags)
  1653. # append to list
  1654. list (APPEND _flags "-Qunused-arguments" "-include" "${_prefixFile}")
  1655. else()
  1656. # return as a flag string
  1657. set (_flags "-Qunused-arguments -include \"${_prefixFile}\"")
  1658. endif()
  1659. elseif (_compilerID MATCHES "Intel")
  1660. if (WIN32)
  1661. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1662. # Windows Intel options used
  1663. # /Yu use a precompiled header (PCH) file
  1664. # /Fp specify a path or file name for precompiled header files
  1665. # /FI tells the preprocessor to include a specified file name as the header file
  1666. # /Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1667. if (_pchFile)
  1668. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1669. if (_flags)
  1670. # append to list
  1671. list (APPEND _flags "/Yu" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}")
  1672. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1673. list (APPEND _flags "/Wpch-messages")
  1674. endif()
  1675. else()
  1676. # return as a flag string
  1677. set (_flags "/Yu /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1678. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1679. set (_flags "${_flags} /Wpch-messages")
  1680. endif()
  1681. endif()
  1682. else()
  1683. # no precompiled header, force inclusion of prefix header
  1684. if (_flags)
  1685. # append to list
  1686. list (APPEND _flags "/FI${_prefixFileNative}")
  1687. else()
  1688. # return as a flag string
  1689. set (_flags "/FI\"${_prefixFileNative}\"")
  1690. endif()
  1691. endif()
  1692. else()
  1693. # Linux / Mac OS X Intel options used
  1694. # -pch-dir location for precompiled header files
  1695. # -pch-use name of the precompiled header (PCH) to use
  1696. # -include process include file as the first line of the primary source file
  1697. # -Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1698. if (_pchFile)
  1699. get_filename_component(_pchDir "${_pchFile}" DIRECTORY)
  1700. get_filename_component(_pchName "${_pchFile}" NAME)
  1701. if (_flags)
  1702. # append to list
  1703. list (APPEND _flags "-include" "${_prefixFile}" "-pch-dir" "${_pchDir}" "-pch-use" "${_pchName}")
  1704. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1705. list (APPEND _flags "-Wpch-messages")
  1706. endif()
  1707. else()
  1708. # return as a flag string
  1709. set (_flags "-include \"${_prefixFile}\" -pch-dir \"${_pchDir}\" -pch-use \"${_pchName}\"")
  1710. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1711. set (_flags "${_flags} -Wpch-messages")
  1712. endif()
  1713. endif()
  1714. else()
  1715. # no precompiled header, force inclusion of prefix header
  1716. if (_flags)
  1717. # append to list
  1718. list (APPEND _flags "-include" "${_prefixFile}")
  1719. else()
  1720. # return as a flag string
  1721. set (_flags "-include \"${_prefixFile}\"")
  1722. endif()
  1723. endif()
  1724. endif()
  1725. else()
  1726. message (FATAL_ERROR "cotire: unsupported ${_language} compiler ${_compilerID} version ${_compilerVersion}.")
  1727. endif()
  1728. set (${_flagsVar} ${_flags} PARENT_SCOPE)
  1729. endfunction()
  1730. function (cotire_precompile_prefix_header _prefixFile _pchFile _hostFile)
  1731. set(_options "")
  1732. set(_oneValueArgs COMPILER_EXECUTABLE COMPILER_ARG1 COMPILER_ID COMPILER_VERSION LANGUAGE)
  1733. set(_multiValueArgs COMPILE_DEFINITIONS COMPILE_FLAGS INCLUDE_DIRECTORIES SYSTEM_INCLUDE_DIRECTORIES SYS COMPILER_LAUNCHER)
  1734. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  1735. if (NOT _option_LANGUAGE)
  1736. set (_option_LANGUAGE "CXX")
  1737. endif()
  1738. if (NOT _option_COMPILER_ID)
  1739. set (_option_COMPILER_ID "${CMAKE_${_option_LANGUAGE}_ID}")
  1740. endif()
  1741. if (NOT _option_COMPILER_VERSION)
  1742. set (_option_COMPILER_VERSION "${CMAKE_${_option_LANGUAGE}_COMPILER_VERSION}")
  1743. endif()
  1744. cotire_init_compile_cmd(_cmd "${_option_LANGUAGE}" "${_option_COMPILER_LAUNCHER}" "${_option_COMPILER_EXECUTABLE}" "${_option_COMPILER_ARG1}")
  1745. cotire_add_definitions_to_cmd(_cmd "${_option_LANGUAGE}" ${_option_COMPILE_DEFINITIONS})
  1746. cotire_add_compile_flags_to_cmd(_cmd ${_option_COMPILE_FLAGS})
  1747. cotire_add_includes_to_cmd(_cmd "${_option_LANGUAGE}" _option_INCLUDE_DIRECTORIES _option_SYSTEM_INCLUDE_DIRECTORIES)
  1748. cotire_add_frameworks_to_cmd(_cmd "${_option_LANGUAGE}" _option_INCLUDE_DIRECTORIES _option_SYSTEM_INCLUDE_DIRECTORIES)
  1749. cotire_add_pch_compilation_flags(
  1750. "${_option_LANGUAGE}" "${_option_COMPILER_ID}" "${_option_COMPILER_VERSION}"
  1751. "${_prefixFile}" "${_pchFile}" "${_hostFile}" _cmd)
  1752. if (COTIRE_VERBOSE)
  1753. message (STATUS "execute_process: ${_cmd}")
  1754. endif()
  1755. if (_option_COMPILER_ID MATCHES "MSVC")
  1756. # cl.exe messes with the output streams unless the environment variable VS_UNICODE_OUTPUT is cleared
  1757. unset (ENV{VS_UNICODE_OUTPUT})
  1758. endif()
  1759. execute_process(
  1760. COMMAND ${_cmd}
  1761. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  1762. RESULT_VARIABLE _result)
  1763. if (_result)
  1764. message (FATAL_ERROR "cotire: error ${_result} precompiling ${_prefixFile}.")
  1765. endif()
  1766. endfunction()
  1767. function (cotire_check_precompiled_header_support _language _target _msgVar)
  1768. set (_unsupportedCompiler
  1769. "Precompiled headers not supported for ${_language} compiler ${CMAKE_${_language}_COMPILER_ID}")
  1770. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC")
  1771. # supported since Visual Studio C++ 6.0
  1772. # and CMake does not support an earlier version
  1773. set (${_msgVar} "" PARENT_SCOPE)
  1774. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "GNU")
  1775. # GCC PCH support requires version >= 3.4
  1776. if ("${CMAKE_${_language}_COMPILER_VERSION}" VERSION_LESS "3.4.0")
  1777. set (${_msgVar} "${_unsupportedCompiler} version ${CMAKE_${_language}_COMPILER_VERSION}." PARENT_SCOPE)
  1778. else()
  1779. set (${_msgVar} "" PARENT_SCOPE)
  1780. endif()
  1781. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "Clang")
  1782. # all Clang versions have PCH support
  1783. set (${_msgVar} "" PARENT_SCOPE)
  1784. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "Intel")
  1785. # Intel PCH support requires version >= 8.0.0
  1786. if ("${CMAKE_${_language}_COMPILER_VERSION}" VERSION_LESS "8.0.0")
  1787. set (${_msgVar} "${_unsupportedCompiler} version ${CMAKE_${_language}_COMPILER_VERSION}." PARENT_SCOPE)
  1788. else()
  1789. set (${_msgVar} "" PARENT_SCOPE)
  1790. endif()
  1791. else()
  1792. set (${_msgVar} "${_unsupportedCompiler}." PARENT_SCOPE)
  1793. endif()
  1794. get_target_property(_launcher ${_target} ${_language}_COMPILER_LAUNCHER)
  1795. if (CMAKE_${_language}_COMPILER MATCHES "ccache" OR _launcher MATCHES "ccache")
  1796. if (DEFINED ENV{CCACHE_SLOPPINESS})
  1797. if (NOT "$ENV{CCACHE_SLOPPINESS}" MATCHES "pch_defines" OR NOT "$ENV{CCACHE_SLOPPINESS}" MATCHES "time_macros")
  1798. set (${_msgVar}
  1799. "ccache requires the environment variable CCACHE_SLOPPINESS to be set to \"pch_defines,time_macros\"."
  1800. PARENT_SCOPE)
  1801. endif()
  1802. else()
  1803. if (_launcher MATCHES "ccache")
  1804. get_filename_component(_ccacheExe "${_launcher}" REALPATH)
  1805. else()
  1806. get_filename_component(_ccacheExe "${CMAKE_${_language}_COMPILER}" REALPATH)
  1807. endif()
  1808. execute_process(
  1809. COMMAND "${_ccacheExe}" "--print-config"
  1810. WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
  1811. RESULT_VARIABLE _result
  1812. OUTPUT_VARIABLE _ccacheConfig OUTPUT_STRIP_TRAILING_WHITESPACE
  1813. ERROR_QUIET)
  1814. if (_result OR NOT
  1815. _ccacheConfig MATCHES "sloppiness.*=.*time_macros" OR NOT
  1816. _ccacheConfig MATCHES "sloppiness.*=.*pch_defines")
  1817. set (${_msgVar}
  1818. "ccache requires configuration setting \"sloppiness\" to be set to \"pch_defines,time_macros\"."
  1819. PARENT_SCOPE)
  1820. endif()
  1821. endif()
  1822. endif()
  1823. if (APPLE)
  1824. # PCH compilation not supported by GCC / Clang for multi-architecture builds (e.g., i386, x86_64)
  1825. cotire_get_configuration_types(_configs)
  1826. foreach (_config ${_configs})
  1827. set (_targetFlags "")
  1828. cotire_get_target_compile_flags("${_config}" "${_language}" "${_target}" _targetFlags)
  1829. cotire_filter_compile_flags("${_language}" "arch" _architectures _ignore ${_targetFlags})
  1830. list (LENGTH _architectures _numberOfArchitectures)
  1831. if (_numberOfArchitectures GREATER 1)
  1832. string (REPLACE ";" ", " _architectureStr "${_architectures}")
  1833. set (${_msgVar}
  1834. "Precompiled headers not supported on Darwin for multi-architecture builds (${_architectureStr})."
  1835. PARENT_SCOPE)
  1836. break()
  1837. endif()
  1838. endforeach()
  1839. endif()
  1840. endfunction()
  1841. macro (cotire_get_intermediate_dir _cotireDir)
  1842. # ${CMAKE_CFG_INTDIR} may reference a build-time variable when using a generator which supports configuration types
  1843. get_filename_component(${_cotireDir} "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${COTIRE_INTDIR}" ABSOLUTE)
  1844. endmacro()
  1845. macro (cotire_setup_file_extension_variables)
  1846. set (_unityFileExt_C ".c")
  1847. set (_unityFileExt_CXX ".cxx")
  1848. set (_prefixFileExt_C ".h")
  1849. set (_prefixFileExt_CXX ".hxx")
  1850. set (_prefixSourceFileExt_C ".c")
  1851. set (_prefixSourceFileExt_CXX ".cxx")
  1852. endmacro()
  1853. function (cotire_make_single_unity_source_file_path _language _target _unityFileVar)
  1854. cotire_setup_file_extension_variables()
  1855. if (NOT DEFINED _unityFileExt_${_language})
  1856. set (${_unityFileVar} "" PARENT_SCOPE)
  1857. return()
  1858. endif()
  1859. set (_unityFileBaseName "${_target}_${_language}${COTIRE_UNITY_SOURCE_FILENAME_SUFFIX}")
  1860. set (_unityFileName "${_unityFileBaseName}${_unityFileExt_${_language}}")
  1861. cotire_get_intermediate_dir(_baseDir)
  1862. set (_unityFile "${_baseDir}/${_unityFileName}")
  1863. set (${_unityFileVar} "${_unityFile}" PARENT_SCOPE)
  1864. endfunction()
  1865. function (cotire_make_unity_source_file_paths _language _target _maxIncludes _unityFilesVar)
  1866. cotire_setup_file_extension_variables()
  1867. if (NOT DEFINED _unityFileExt_${_language})
  1868. set (${_unityFileVar} "" PARENT_SCOPE)
  1869. return()
  1870. endif()
  1871. set (_unityFileBaseName "${_target}_${_language}${COTIRE_UNITY_SOURCE_FILENAME_SUFFIX}")
  1872. cotire_get_intermediate_dir(_baseDir)
  1873. set (_startIndex 0)
  1874. set (_index 0)
  1875. set (_unityFiles "")
  1876. set (_sourceFiles ${ARGN})
  1877. foreach (_sourceFile ${_sourceFiles})
  1878. get_source_file_property(_startNew "${_sourceFile}" COTIRE_START_NEW_UNITY_SOURCE)
  1879. math (EXPR _unityFileCount "${_index} - ${_startIndex}")
  1880. if (_startNew OR (_maxIncludes GREATER 0 AND NOT _unityFileCount LESS _maxIncludes))
  1881. if (_index GREATER 0)
  1882. # start new unity file segment
  1883. math (EXPR _endIndex "${_index} - 1")
  1884. set (_unityFileName "${_unityFileBaseName}_${_startIndex}_${_endIndex}${_unityFileExt_${_language}}")
  1885. list (APPEND _unityFiles "${_baseDir}/${_unityFileName}")
  1886. endif()
  1887. set (_startIndex ${_index})
  1888. endif()
  1889. math (EXPR _index "${_index} + 1")
  1890. endforeach()
  1891. list (LENGTH _sourceFiles _numberOfSources)
  1892. if (_startIndex EQUAL 0)
  1893. # there is only a single unity file
  1894. cotire_make_single_unity_source_file_path(${_language} ${_target} _unityFiles)
  1895. elseif (_startIndex LESS _numberOfSources)
  1896. # end with final unity file segment
  1897. math (EXPR _endIndex "${_index} - 1")
  1898. set (_unityFileName "${_unityFileBaseName}_${_startIndex}_${_endIndex}${_unityFileExt_${_language}}")
  1899. list (APPEND _unityFiles "${_baseDir}/${_unityFileName}")
  1900. endif()
  1901. set (${_unityFilesVar} ${_unityFiles} PARENT_SCOPE)
  1902. if (COTIRE_DEBUG AND _unityFiles)
  1903. message (STATUS "unity files: ${_unityFiles}")
  1904. endif()
  1905. endfunction()
  1906. function (cotire_unity_to_prefix_file_path _language _target _unityFile _prefixFileVar)
  1907. cotire_setup_file_extension_variables()
  1908. if (NOT DEFINED _unityFileExt_${_language})
  1909. set (${_prefixFileVar} "" PARENT_SCOPE)
  1910. return()
  1911. endif()
  1912. set (_unityFileBaseName "${_target}_${_language}${COTIRE_UNITY_SOURCE_FILENAME_SUFFIX}")
  1913. set (_prefixFileBaseName "${_target}_${_language}${COTIRE_PREFIX_HEADER_FILENAME_SUFFIX}")
  1914. string (REPLACE "${_unityFileBaseName}" "${_prefixFileBaseName}" _prefixFile "${_unityFile}")
  1915. string (REGEX REPLACE "${_unityFileExt_${_language}}$" "${_prefixFileExt_${_language}}" _prefixFile "${_prefixFile}")
  1916. set (${_prefixFileVar} "${_prefixFile}" PARENT_SCOPE)
  1917. endfunction()
  1918. function (cotire_prefix_header_to_source_file_path _language _prefixHeaderFile _prefixSourceFileVar)
  1919. cotire_setup_file_extension_variables()
  1920. if (NOT DEFINED _prefixSourceFileExt_${_language})
  1921. set (${_prefixSourceFileVar} "" PARENT_SCOPE)
  1922. return()
  1923. endif()
  1924. string (REGEX REPLACE "${_prefixFileExt_${_language}}$" "${_prefixSourceFileExt_${_language}}" _prefixSourceFile "${_prefixHeaderFile}")
  1925. set (${_prefixSourceFileVar} "${_prefixSourceFile}" PARENT_SCOPE)
  1926. endfunction()
  1927. function (cotire_make_prefix_file_name _language _target _prefixFileBaseNameVar _prefixFileNameVar)
  1928. cotire_setup_file_extension_variables()
  1929. if (NOT _language)
  1930. set (_prefixFileBaseName "${_target}${COTIRE_PREFIX_HEADER_FILENAME_SUFFIX}")
  1931. set (_prefixFileName "${_prefixFileBaseName}${_prefixFileExt_C}")
  1932. elseif (DEFINED _prefixFileExt_${_language})
  1933. set (_prefixFileBaseName "${_target}_${_language}${COTIRE_PREFIX_HEADER_FILENAME_SUFFIX}")
  1934. set (_prefixFileName "${_prefixFileBaseName}${_prefixFileExt_${_language}}")
  1935. else()
  1936. set (_prefixFileBaseName "")
  1937. set (_prefixFileName "")
  1938. endif()
  1939. set (${_prefixFileBaseNameVar} "${_prefixFileBaseName}" PARENT_SCOPE)
  1940. set (${_prefixFileNameVar} "${_prefixFileName}" PARENT_SCOPE)
  1941. endfunction()
  1942. function (cotire_make_prefix_file_path _language _target _prefixFileVar)
  1943. cotire_make_prefix_file_name("${_language}" "${_target}" _prefixFileBaseName _prefixFileName)
  1944. set (${_prefixFileVar} "" PARENT_SCOPE)
  1945. if (_prefixFileName)
  1946. if (NOT _language)
  1947. set (_language "C")
  1948. endif()
  1949. if (CMAKE_${_language}_COMPILER_ID MATCHES "GNU|Clang|Intel|MSVC")
  1950. cotire_get_intermediate_dir(_baseDir)
  1951. set (${_prefixFileVar} "${_baseDir}/${_prefixFileName}" PARENT_SCOPE)
  1952. endif()
  1953. endif()
  1954. endfunction()
  1955. function (cotire_make_pch_file_path _language _target _pchFileVar)
  1956. cotire_make_prefix_file_name("${_language}" "${_target}" _prefixFileBaseName _prefixFileName)
  1957. set (${_pchFileVar} "" PARENT_SCOPE)
  1958. if (_prefixFileBaseName AND _prefixFileName)
  1959. cotire_check_precompiled_header_support("${_language}" "${_target}" _msg)
  1960. if (NOT _msg)
  1961. if (XCODE)
  1962. # For Xcode, we completely hand off the compilation of the prefix header to the IDE
  1963. return()
  1964. endif()
  1965. cotire_get_intermediate_dir(_baseDir)
  1966. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC")
  1967. # MSVC uses the extension .pch added to the prefix header base name
  1968. set (${_pchFileVar} "${_baseDir}/${_prefixFileBaseName}.pch" PARENT_SCOPE)
  1969. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "Clang")
  1970. # Clang looks for a precompiled header corresponding to the prefix header with the extension .pch appended
  1971. set (${_pchFileVar} "${_baseDir}/${_prefixFileName}.pch" PARENT_SCOPE)
  1972. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "GNU")
  1973. # GCC looks for a precompiled header corresponding to the prefix header with the extension .gch appended
  1974. set (${_pchFileVar} "${_baseDir}/${_prefixFileName}.gch" PARENT_SCOPE)
  1975. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "Intel")
  1976. # Intel uses the extension .pchi added to the prefix header base name
  1977. set (${_pchFileVar} "${_baseDir}/${_prefixFileBaseName}.pchi" PARENT_SCOPE)
  1978. endif()
  1979. endif()
  1980. endif()
  1981. endfunction()
  1982. function (cotire_select_unity_source_files _unityFile _sourcesVar)
  1983. set (_sourceFiles ${ARGN})
  1984. if (_sourceFiles AND "${_unityFile}" MATCHES "${COTIRE_UNITY_SOURCE_FILENAME_SUFFIX}_([0-9]+)_([0-9]+)")
  1985. set (_startIndex ${CMAKE_MATCH_1})
  1986. set (_endIndex ${CMAKE_MATCH_2})
  1987. list (LENGTH _sourceFiles _numberOfSources)
  1988. if (NOT _startIndex LESS _numberOfSources)
  1989. math (EXPR _startIndex "${_numberOfSources} - 1")
  1990. endif()
  1991. if (NOT _endIndex LESS _numberOfSources)
  1992. math (EXPR _endIndex "${_numberOfSources} - 1")
  1993. endif()
  1994. set (_files "")
  1995. foreach (_index RANGE ${_startIndex} ${_endIndex})
  1996. list (GET _sourceFiles ${_index} _file)
  1997. list (APPEND _files "${_file}")
  1998. endforeach()
  1999. else()
  2000. set (_files ${_sourceFiles})
  2001. endif()
  2002. set (${_sourcesVar} ${_files} PARENT_SCOPE)
  2003. endfunction()
  2004. function (cotire_get_unity_source_dependencies _language _target _dependencySourcesVar)
  2005. set (_dependencySources "")
  2006. # depend on target's generated source files
  2007. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2008. cotire_get_objects_with_property_on(_generatedSources GENERATED SOURCE ${_targetSourceFiles})
  2009. if (_generatedSources)
  2010. # but omit all generated source files that have the COTIRE_EXCLUDED property set to true
  2011. cotire_get_objects_with_property_on(_excludedGeneratedSources COTIRE_EXCLUDED SOURCE ${_generatedSources})
  2012. if (_excludedGeneratedSources)
  2013. list (REMOVE_ITEM _generatedSources ${_excludedGeneratedSources})
  2014. endif()
  2015. # and omit all generated source files that have the COTIRE_DEPENDENCY property set to false explicitly
  2016. cotire_get_objects_with_property_off(_excludedNonDependencySources COTIRE_DEPENDENCY SOURCE ${_generatedSources})
  2017. if (_excludedNonDependencySources)
  2018. list (REMOVE_ITEM _generatedSources ${_excludedNonDependencySources})
  2019. endif()
  2020. if (_generatedSources)
  2021. list (APPEND _dependencySources ${_generatedSources})
  2022. endif()
  2023. endif()
  2024. if (COTIRE_DEBUG AND _dependencySources)
  2025. message (STATUS "${_language} ${_target} unity source dependencies: ${_dependencySources}")
  2026. endif()
  2027. set (${_dependencySourcesVar} ${_dependencySources} PARENT_SCOPE)
  2028. endfunction()
  2029. function (cotire_get_prefix_header_dependencies _language _target _dependencySourcesVar)
  2030. set (_dependencySources "")
  2031. # depend on target source files marked with custom COTIRE_DEPENDENCY property
  2032. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2033. cotire_get_objects_with_property_on(_dependencySources COTIRE_DEPENDENCY SOURCE ${_targetSourceFiles})
  2034. if (COTIRE_DEBUG AND _dependencySources)
  2035. message (STATUS "${_language} ${_target} prefix header dependencies: ${_dependencySources}")
  2036. endif()
  2037. set (${_dependencySourcesVar} ${_dependencySources} PARENT_SCOPE)
  2038. endfunction()
  2039. function (cotire_generate_target_script _language _configurations _target _targetScriptVar _targetConfigScriptVar)
  2040. set (_targetSources ${ARGN})
  2041. cotire_get_prefix_header_dependencies(${_language} ${_target} COTIRE_TARGET_PREFIX_DEPENDS ${_targetSources})
  2042. cotire_get_unity_source_dependencies(${_language} ${_target} COTIRE_TARGET_UNITY_DEPENDS ${_targetSources})
  2043. # set up variables to be configured
  2044. set (COTIRE_TARGET_LANGUAGE "${_language}")
  2045. get_target_property(COTIRE_TARGET_IGNORE_PATH ${_target} COTIRE_PREFIX_HEADER_IGNORE_PATH)
  2046. cotire_add_sys_root_paths(COTIRE_TARGET_IGNORE_PATH)
  2047. get_target_property(COTIRE_TARGET_INCLUDE_PATH ${_target} COTIRE_PREFIX_HEADER_INCLUDE_PATH)
  2048. cotire_add_sys_root_paths(COTIRE_TARGET_INCLUDE_PATH)
  2049. get_target_property(COTIRE_TARGET_PRE_UNDEFS ${_target} COTIRE_UNITY_SOURCE_PRE_UNDEFS)
  2050. get_target_property(COTIRE_TARGET_POST_UNDEFS ${_target} COTIRE_UNITY_SOURCE_POST_UNDEFS)
  2051. get_target_property(COTIRE_TARGET_MAXIMUM_NUMBER_OF_INCLUDES ${_target} COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES)
  2052. get_target_property(COTIRE_TARGET_INCLUDE_PRIORITY_PATH ${_target} COTIRE_PREFIX_HEADER_INCLUDE_PRIORITY_PATH)
  2053. cotire_get_source_files_undefs(COTIRE_UNITY_SOURCE_PRE_UNDEFS COTIRE_TARGET_SOURCES_PRE_UNDEFS ${_targetSources})
  2054. cotire_get_source_files_undefs(COTIRE_UNITY_SOURCE_POST_UNDEFS COTIRE_TARGET_SOURCES_POST_UNDEFS ${_targetSources})
  2055. set (COTIRE_TARGET_CONFIGURATION_TYPES "${_configurations}")
  2056. foreach (_config ${_configurations})
  2057. string (TOUPPER "${_config}" _upperConfig)
  2058. cotire_get_target_include_directories(
  2059. "${_config}" "${_language}" "${_target}" COTIRE_TARGET_INCLUDE_DIRECTORIES_${_upperConfig} COTIRE_TARGET_SYSTEM_INCLUDE_DIRECTORIES_${_upperConfig})
  2060. cotire_get_target_compile_definitions(
  2061. "${_config}" "${_language}" "${_target}" COTIRE_TARGET_COMPILE_DEFINITIONS_${_upperConfig})
  2062. cotire_get_target_compiler_flags(
  2063. "${_config}" "${_language}" "${_target}" COTIRE_TARGET_COMPILE_FLAGS_${_upperConfig})
  2064. cotire_get_source_files_compile_definitions(
  2065. "${_config}" "${_language}" COTIRE_TARGET_SOURCES_COMPILE_DEFINITIONS_${_upperConfig} ${_targetSources})
  2066. endforeach()
  2067. get_target_property(COTIRE_TARGET_${_language}_COMPILER_LAUNCHER ${_target} ${_language}_COMPILER_LAUNCHER)
  2068. # set up COTIRE_TARGET_SOURCES
  2069. set (COTIRE_TARGET_SOURCES "")
  2070. foreach (_sourceFile ${_targetSources})
  2071. get_source_file_property(_generated "${_sourceFile}" GENERATED)
  2072. if (_generated)
  2073. # use absolute paths for generated files only, retrieving the LOCATION property is an expensive operation
  2074. get_source_file_property(_sourceLocation "${_sourceFile}" LOCATION)
  2075. list (APPEND COTIRE_TARGET_SOURCES "${_sourceLocation}")
  2076. else()
  2077. list (APPEND COTIRE_TARGET_SOURCES "${_sourceFile}")
  2078. endif()
  2079. endforeach()
  2080. # copy variable definitions to cotire target script
  2081. get_cmake_property(_vars VARIABLES)
  2082. string (REGEX MATCHALL "COTIRE_[A-Za-z0-9_]+" _matchVars "${_vars}")
  2083. # omit COTIRE_*_INIT variables
  2084. string (REGEX MATCHALL "COTIRE_[A-Za-z0-9_]+_INIT" _initVars "${_matchVars}")
  2085. if (_initVars)
  2086. list (REMOVE_ITEM _matchVars ${_initVars})
  2087. endif()
  2088. # omit COTIRE_VERBOSE which is passed as a CMake define on command line
  2089. list (REMOVE_ITEM _matchVars COTIRE_VERBOSE)
  2090. set (_contents "")
  2091. set (_contentsHasGeneratorExpressions FALSE)
  2092. foreach (_var IN LISTS _matchVars ITEMS
  2093. XCODE MSVC CMAKE_GENERATOR CMAKE_BUILD_TYPE CMAKE_CONFIGURATION_TYPES
  2094. CMAKE_${_language}_COMPILER_ID CMAKE_${_language}_COMPILER_VERSION
  2095. CMAKE_${_language}_COMPILER_LAUNCHER CMAKE_${_language}_COMPILER CMAKE_${_language}_COMPILER_ARG1
  2096. CMAKE_INCLUDE_FLAG_${_language} CMAKE_INCLUDE_FLAG_${_language}_SEP
  2097. CMAKE_INCLUDE_SYSTEM_FLAG_${_language}
  2098. CMAKE_${_language}_FRAMEWORK_SEARCH_FLAG
  2099. CMAKE_${_language}_SYSTEM_FRAMEWORK_SEARCH_FLAG
  2100. CMAKE_${_language}_SOURCE_FILE_EXTENSIONS)
  2101. if (DEFINED ${_var})
  2102. string (REPLACE "\"" "\\\"" _value "${${_var}}")
  2103. set (_contents "${_contents}set (${_var} \"${_value}\")\n")
  2104. if (NOT _contentsHasGeneratorExpressions)
  2105. if ("${_value}" MATCHES "\\$<.*>")
  2106. set (_contentsHasGeneratorExpressions TRUE)
  2107. endif()
  2108. endif()
  2109. endif()
  2110. endforeach()
  2111. # generate target script file
  2112. get_filename_component(_moduleName "${COTIRE_CMAKE_MODULE_FILE}" NAME)
  2113. set (_targetCotireScript "${CMAKE_CURRENT_BINARY_DIR}/${_target}_${_language}_${_moduleName}")
  2114. cotire_write_file("CMAKE" "${_targetCotireScript}" "${_contents}" FALSE)
  2115. if (_contentsHasGeneratorExpressions)
  2116. # use file(GENERATE ...) to expand generator expressions in the target script at CMake generate-time
  2117. set (_configNameOrNoneGeneratorExpression "$<$<CONFIG:>:None>$<$<NOT:$<CONFIG:>>:$<CONFIGURATION>>")
  2118. set (_targetCotireConfigScript "${CMAKE_CURRENT_BINARY_DIR}/${_target}_${_language}_${_configNameOrNoneGeneratorExpression}_${_moduleName}")
  2119. file (GENERATE OUTPUT "${_targetCotireConfigScript}" INPUT "${_targetCotireScript}")
  2120. else()
  2121. set (_targetCotireConfigScript "${_targetCotireScript}")
  2122. endif()
  2123. set (${_targetScriptVar} "${_targetCotireScript}" PARENT_SCOPE)
  2124. set (${_targetConfigScriptVar} "${_targetCotireConfigScript}" PARENT_SCOPE)
  2125. endfunction()
  2126. function (cotire_setup_pch_file_compilation _language _target _targetScript _prefixFile _pchFile _hostFile)
  2127. set (_sourceFiles ${ARGN})
  2128. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  2129. # for Visual Studio and Intel, we attach the precompiled header compilation to the host file
  2130. # the remaining files include the precompiled header, see cotire_setup_pch_file_inclusion
  2131. if (_sourceFiles)
  2132. set (_flags "")
  2133. cotire_add_pch_compilation_flags(
  2134. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${CMAKE_${_language}_COMPILER_VERSION}"
  2135. "${_prefixFile}" "${_pchFile}" "${_hostFile}" _flags)
  2136. set_property (SOURCE ${_hostFile} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  2137. set_property (SOURCE ${_hostFile} APPEND PROPERTY OBJECT_OUTPUTS "${_pchFile}")
  2138. # make object file generated from host file depend on prefix header
  2139. set_property (SOURCE ${_hostFile} APPEND PROPERTY OBJECT_DEPENDS "${_prefixFile}")
  2140. # mark host file as cotired to prevent it from being used in another cotired target
  2141. set_property (SOURCE ${_hostFile} PROPERTY COTIRE_TARGET "${_target}")
  2142. endif()
  2143. elseif ("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
  2144. # for makefile based generator, we add a custom command to precompile the prefix header
  2145. if (_targetScript)
  2146. cotire_set_cmd_to_prologue(_cmds)
  2147. list (APPEND _cmds -P "${COTIRE_CMAKE_MODULE_FILE}" "precompile" "${_targetScript}" "${_prefixFile}" "${_pchFile}" "${_hostFile}")
  2148. if (MSVC_IDE)
  2149. file (TO_NATIVE_PATH "${_pchFile}" _pchFileLogPath)
  2150. else()
  2151. file (RELATIVE_PATH _pchFileLogPath "${CMAKE_BINARY_DIR}" "${_pchFile}")
  2152. endif()
  2153. # make precompiled header compilation depend on the actual compiler executable to force
  2154. # re-compilation when the compiler executable is updated. This prevents "created by a different GCC executable"
  2155. # warnings when the precompiled header is included.
  2156. get_filename_component(_realCompilerExe "${CMAKE_${_language}_COMPILER}" ABSOLUTE)
  2157. if (COTIRE_DEBUG)
  2158. message (STATUS "add_custom_command: OUTPUT ${_pchFile} ${_cmds} DEPENDS ${_prefixFile} ${_realCompilerExe} IMPLICIT_DEPENDS ${_language} ${_prefixFile}")
  2159. endif()
  2160. set_property (SOURCE "${_pchFile}" PROPERTY GENERATED TRUE)
  2161. add_custom_command(
  2162. OUTPUT "${_pchFile}"
  2163. COMMAND ${_cmds}
  2164. DEPENDS "${_prefixFile}" "${_realCompilerExe}"
  2165. IMPLICIT_DEPENDS ${_language} "${_prefixFile}"
  2166. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  2167. COMMENT "Building ${_language} precompiled header ${_pchFileLogPath}"
  2168. VERBATIM)
  2169. endif()
  2170. endif()
  2171. endfunction()
  2172. function (cotire_setup_pch_file_inclusion _language _target _wholeTarget _prefixFile _pchFile _hostFile)
  2173. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  2174. # for Visual Studio and Intel, we include the precompiled header in all but the host file
  2175. # the host file does the precompiled header compilation, see cotire_setup_pch_file_compilation
  2176. set (_sourceFiles ${ARGN})
  2177. list (LENGTH _sourceFiles _numberOfSourceFiles)
  2178. if (_numberOfSourceFiles GREATER 0)
  2179. # mark sources as cotired to prevent them from being used in another cotired target
  2180. set_source_files_properties(${_sourceFiles} PROPERTIES COTIRE_TARGET "${_target}")
  2181. set (_flags "")
  2182. cotire_add_prefix_pch_inclusion_flags(
  2183. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${CMAKE_${_language}_COMPILER_VERSION}"
  2184. "${_prefixFile}" "${_pchFile}" _flags)
  2185. set_property (SOURCE ${_sourceFiles} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  2186. # make object files generated from source files depend on precompiled header
  2187. set_property (SOURCE ${_sourceFiles} APPEND PROPERTY OBJECT_DEPENDS "${_pchFile}")
  2188. endif()
  2189. elseif ("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
  2190. set (_sourceFiles ${_hostFile} ${ARGN})
  2191. if (NOT _wholeTarget)
  2192. # for makefile based generator, we force the inclusion of the prefix header for a subset
  2193. # of the source files, if this is a multi-language target or has excluded files
  2194. set (_flags "")
  2195. cotire_add_prefix_pch_inclusion_flags(
  2196. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${CMAKE_${_language}_COMPILER_VERSION}"
  2197. "${_prefixFile}" "${_pchFile}" _flags)
  2198. set_property (SOURCE ${_sourceFiles} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  2199. # mark sources as cotired to prevent them from being used in another cotired target
  2200. set_source_files_properties(${_sourceFiles} PROPERTIES COTIRE_TARGET "${_target}")
  2201. endif()
  2202. # make object files generated from source files depend on precompiled header
  2203. set_property (SOURCE ${_sourceFiles} APPEND PROPERTY OBJECT_DEPENDS "${_pchFile}")
  2204. endif()
  2205. endfunction()
  2206. function (cotire_setup_prefix_file_inclusion _language _target _prefixFile)
  2207. set (_sourceFiles ${ARGN})
  2208. # force the inclusion of the prefix header for the given source files
  2209. set (_flags "")
  2210. set (_pchFile "")
  2211. cotire_add_prefix_pch_inclusion_flags(
  2212. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${CMAKE_${_language}_COMPILER_VERSION}"
  2213. "${_prefixFile}" "${_pchFile}" _flags)
  2214. set_property (SOURCE ${_sourceFiles} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  2215. # mark sources as cotired to prevent them from being used in another cotired target
  2216. set_source_files_properties(${_sourceFiles} PROPERTIES COTIRE_TARGET "${_target}")
  2217. # make object files generated from source files depend on prefix header
  2218. set_property (SOURCE ${_sourceFiles} APPEND PROPERTY OBJECT_DEPENDS "${_prefixFile}")
  2219. endfunction()
  2220. function (cotire_get_first_set_property_value _propertyValueVar _type _object)
  2221. set (_properties ${ARGN})
  2222. foreach (_property ${_properties})
  2223. get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
  2224. if (_propertyValue)
  2225. set (${_propertyValueVar} ${_propertyValue} PARENT_SCOPE)
  2226. return()
  2227. endif()
  2228. endforeach()
  2229. set (${_propertyValueVar} "" PARENT_SCOPE)
  2230. endfunction()
  2231. function (cotire_setup_combine_command _language _targetScript _joinedFile _cmdsVar)
  2232. set (_files ${ARGN})
  2233. set (_filesPaths "")
  2234. foreach (_file ${_files})
  2235. get_filename_component(_filePath "${_file}" ABSOLUTE)
  2236. list (APPEND _filesPaths "${_filePath}")
  2237. endforeach()
  2238. cotire_set_cmd_to_prologue(_prefixCmd)
  2239. list (APPEND _prefixCmd -P "${COTIRE_CMAKE_MODULE_FILE}" "combine")
  2240. if (_targetScript)
  2241. list (APPEND _prefixCmd "${_targetScript}")
  2242. endif()
  2243. list (APPEND _prefixCmd "${_joinedFile}" ${_filesPaths})
  2244. if (COTIRE_DEBUG)
  2245. message (STATUS "add_custom_command: OUTPUT ${_joinedFile} COMMAND ${_prefixCmd} DEPENDS ${_files}")
  2246. endif()
  2247. set_property (SOURCE "${_joinedFile}" PROPERTY GENERATED TRUE)
  2248. if (MSVC_IDE)
  2249. file (TO_NATIVE_PATH "${_joinedFile}" _joinedFileLogPath)
  2250. else()
  2251. file (RELATIVE_PATH _joinedFileLogPath "${CMAKE_BINARY_DIR}" "${_joinedFile}")
  2252. endif()
  2253. get_filename_component(_joinedFileBaseName "${_joinedFile}" NAME_WE)
  2254. get_filename_component(_joinedFileExt "${_joinedFile}" EXT)
  2255. if (_language AND _joinedFileBaseName MATCHES "${COTIRE_UNITY_SOURCE_FILENAME_SUFFIX}$")
  2256. set (_comment "Generating ${_language} unity source ${_joinedFileLogPath}")
  2257. elseif (_language AND _joinedFileBaseName MATCHES "${COTIRE_PREFIX_HEADER_FILENAME_SUFFIX}$")
  2258. if (_joinedFileExt MATCHES "^\\.c")
  2259. set (_comment "Generating ${_language} prefix source ${_joinedFileLogPath}")
  2260. else()
  2261. set (_comment "Generating ${_language} prefix header ${_joinedFileLogPath}")
  2262. endif()
  2263. else()
  2264. set (_comment "Generating ${_joinedFileLogPath}")
  2265. endif()
  2266. add_custom_command(
  2267. OUTPUT "${_joinedFile}"
  2268. COMMAND ${_prefixCmd}
  2269. DEPENDS ${_files}
  2270. COMMENT "${_comment}"
  2271. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  2272. VERBATIM)
  2273. list (APPEND ${_cmdsVar} COMMAND ${_prefixCmd})
  2274. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  2275. endfunction()
  2276. function (cotire_setup_target_pch_usage _languages _target _wholeTarget)
  2277. if (XCODE)
  2278. # for Xcode, we attach a pre-build action to generate the unity sources and prefix headers
  2279. set (_prefixFiles "")
  2280. foreach (_language ${_languages})
  2281. get_property(_prefixFile TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER)
  2282. if (_prefixFile)
  2283. list (APPEND _prefixFiles "${_prefixFile}")
  2284. endif()
  2285. endforeach()
  2286. set (_cmds ${ARGN})
  2287. list (LENGTH _prefixFiles _numberOfPrefixFiles)
  2288. if (_numberOfPrefixFiles GREATER 1)
  2289. # we also generate a generic, single prefix header which includes all language specific prefix headers
  2290. set (_language "")
  2291. set (_targetScript "")
  2292. cotire_make_prefix_file_path("${_language}" ${_target} _prefixHeader)
  2293. cotire_setup_combine_command("${_language}" "${_targetScript}" "${_prefixHeader}" _cmds ${_prefixFiles})
  2294. else()
  2295. set (_prefixHeader "${_prefixFiles}")
  2296. endif()
  2297. if (COTIRE_DEBUG)
  2298. message (STATUS "add_custom_command: TARGET ${_target} PRE_BUILD ${_cmds}")
  2299. endif()
  2300. # because CMake PRE_BUILD command does not support dependencies,
  2301. # we check dependencies explicity in cotire script mode when the pre-build action is run
  2302. add_custom_command(
  2303. TARGET "${_target}"
  2304. PRE_BUILD ${_cmds}
  2305. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  2306. COMMENT "Updating target ${_target} prefix headers"
  2307. VERBATIM)
  2308. # make Xcode precompile the generated prefix header with ProcessPCH and ProcessPCH++
  2309. set_target_properties(${_target} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
  2310. set_target_properties(${_target} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${_prefixHeader}")
  2311. elseif ("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
  2312. # for makefile based generator, we force inclusion of the prefix header for all target source files
  2313. # if this is a single-language target without any excluded files
  2314. if (_wholeTarget)
  2315. set (_language "${_languages}")
  2316. # for Visual Studio and Intel, precompiled header inclusion is always done on the source file level
  2317. # see cotire_setup_pch_file_inclusion
  2318. if (NOT CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  2319. get_property(_prefixFile TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER)
  2320. if (_prefixFile)
  2321. get_property(_pchFile TARGET ${_target} PROPERTY COTIRE_${_language}_PRECOMPILED_HEADER)
  2322. set (_options COMPILE_OPTIONS)
  2323. cotire_add_prefix_pch_inclusion_flags(
  2324. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${CMAKE_${_language}_COMPILER_VERSION}"
  2325. "${_prefixFile}" "${_pchFile}" _options)
  2326. set_property(TARGET ${_target} APPEND PROPERTY ${_options})
  2327. endif()
  2328. endif()
  2329. endif()
  2330. endif()
  2331. endfunction()
  2332. function (cotire_setup_unity_generation_commands _language _target _targetScript _targetConfigScript _unityFiles _cmdsVar)
  2333. set (_dependencySources "")
  2334. cotire_get_unity_source_dependencies(${_language} ${_target} _dependencySources ${ARGN})
  2335. foreach (_unityFile ${_unityFiles})
  2336. set_property (SOURCE "${_unityFile}" PROPERTY GENERATED TRUE)
  2337. # set up compiled unity source dependencies via OBJECT_DEPENDS
  2338. # this ensures that missing source files are generated before the unity file is compiled
  2339. if (COTIRE_DEBUG AND _dependencySources)
  2340. message (STATUS "${_unityFile} OBJECT_DEPENDS ${_dependencySources}")
  2341. endif()
  2342. if (_dependencySources)
  2343. # the OBJECT_DEPENDS property requires a list of full paths
  2344. set (_objectDependsPaths "")
  2345. foreach (_sourceFile ${_dependencySources})
  2346. get_source_file_property(_sourceLocation "${_sourceFile}" LOCATION)
  2347. list (APPEND _objectDependsPaths "${_sourceLocation}")
  2348. endforeach()
  2349. set_property (SOURCE "${_unityFile}" PROPERTY OBJECT_DEPENDS ${_objectDependsPaths})
  2350. endif()
  2351. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  2352. # unity file compilation results in potentially huge object file, thus use /bigobj by default unter MSVC and Windows Intel
  2353. set_property (SOURCE "${_unityFile}" APPEND_STRING PROPERTY COMPILE_FLAGS "/bigobj")
  2354. endif()
  2355. cotire_set_cmd_to_prologue(_unityCmd)
  2356. list (APPEND _unityCmd -P "${COTIRE_CMAKE_MODULE_FILE}" "unity" "${_targetConfigScript}" "${_unityFile}")
  2357. if (CMAKE_VERSION VERSION_LESS "3.1.0")
  2358. set (_unityCmdDepends "${_targetScript}")
  2359. else()
  2360. # CMake 3.1.0 supports generator expressions in arguments to DEPENDS
  2361. set (_unityCmdDepends "${_targetConfigScript}")
  2362. endif()
  2363. if (MSVC_IDE)
  2364. file (TO_NATIVE_PATH "${_unityFile}" _unityFileLogPath)
  2365. else()
  2366. file (RELATIVE_PATH _unityFileLogPath "${CMAKE_BINARY_DIR}" "${_unityFile}")
  2367. endif()
  2368. if (COTIRE_DEBUG)
  2369. message (STATUS "add_custom_command: OUTPUT ${_unityFile} COMMAND ${_unityCmd} DEPENDS ${_unityCmdDepends}")
  2370. endif()
  2371. add_custom_command(
  2372. OUTPUT "${_unityFile}"
  2373. COMMAND ${_unityCmd}
  2374. DEPENDS ${_unityCmdDepends}
  2375. COMMENT "Generating ${_language} unity source ${_unityFileLogPath}"
  2376. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  2377. VERBATIM)
  2378. list (APPEND ${_cmdsVar} COMMAND ${_unityCmd})
  2379. endforeach()
  2380. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  2381. endfunction()
  2382. function (cotire_setup_prefix_generation_command _language _target _targetScript _prefixFile _unityFiles _cmdsVar)
  2383. set (_sourceFiles ${ARGN})
  2384. set (_dependencySources "")
  2385. cotire_get_prefix_header_dependencies(${_language} ${_target} _dependencySources ${_sourceFiles})
  2386. cotire_set_cmd_to_prologue(_prefixCmd)
  2387. list (APPEND _prefixCmd -P "${COTIRE_CMAKE_MODULE_FILE}" "prefix" "${_targetScript}" "${_prefixFile}" ${_unityFiles})
  2388. set_property (SOURCE "${_prefixFile}" PROPERTY GENERATED TRUE)
  2389. if (COTIRE_DEBUG)
  2390. message (STATUS "add_custom_command: OUTPUT ${_prefixFile} COMMAND ${_prefixCmd} DEPENDS ${_unityFile} ${_dependencySources}")
  2391. endif()
  2392. if (MSVC_IDE)
  2393. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileLogPath)
  2394. else()
  2395. file (RELATIVE_PATH _prefixFileLogPath "${CMAKE_BINARY_DIR}" "${_prefixFile}")
  2396. endif()
  2397. get_filename_component(_prefixFileExt "${_prefixFile}" EXT)
  2398. if (_prefixFileExt MATCHES "^\\.c")
  2399. set (_comment "Generating ${_language} prefix source ${_prefixFileLogPath}")
  2400. else()
  2401. set (_comment "Generating ${_language} prefix header ${_prefixFileLogPath}")
  2402. endif()
  2403. # prevent pre-processing errors upon generating the prefix header when a target's generated include file does not yet exist
  2404. # we do not add a file-level dependency for the target's generated files though, because we only want to depend on their existence
  2405. # thus we make the prefix header generation depend on a custom helper target which triggers the generation of the files
  2406. set (_preTargetName "${_target}${COTIRE_PCH_TARGET_SUFFIX}_pre")
  2407. if (TARGET ${_preTargetName})
  2408. # custom helper target has already been generated while processing a different language
  2409. list (APPEND _dependencySources ${_preTargetName})
  2410. else()
  2411. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2412. cotire_get_objects_with_property_on(_generatedSources GENERATED SOURCE ${_targetSourceFiles})
  2413. if (_generatedSources)
  2414. add_custom_target("${_preTargetName}" DEPENDS ${_generatedSources})
  2415. cotire_init_target("${_preTargetName}")
  2416. list (APPEND _dependencySources ${_preTargetName})
  2417. endif()
  2418. endif()
  2419. add_custom_command(
  2420. OUTPUT "${_prefixFile}" "${_prefixFile}.log"
  2421. COMMAND ${_prefixCmd}
  2422. DEPENDS ${_unityFiles} ${_dependencySources}
  2423. COMMENT "${_comment}"
  2424. WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  2425. VERBATIM)
  2426. list (APPEND ${_cmdsVar} COMMAND ${_prefixCmd})
  2427. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  2428. endfunction()
  2429. function (cotire_setup_prefix_generation_from_unity_command _language _target _targetScript _prefixFile _unityFiles _cmdsVar)
  2430. set (_sourceFiles ${ARGN})
  2431. if (CMAKE_${_language}_COMPILER_ID MATCHES "GNU|Clang")
  2432. # GNU and Clang require indirect compilation of the prefix header to make them honor the system_header pragma
  2433. cotire_prefix_header_to_source_file_path(${_language} "${_prefixFile}" _prefixSourceFile)
  2434. else()
  2435. set (_prefixSourceFile "${_prefixFile}")
  2436. endif()
  2437. cotire_setup_prefix_generation_command(
  2438. ${_language} ${_target} "${_targetScript}"
  2439. "${_prefixSourceFile}" "${_unityFiles}" ${_cmdsVar} ${_sourceFiles})
  2440. if (CMAKE_${_language}_COMPILER_ID MATCHES "GNU|Clang")
  2441. # set up generation of a prefix source file which includes the prefix header
  2442. cotire_setup_combine_command(${_language} "${_targetScript}" "${_prefixFile}" _cmds ${_prefixSourceFile})
  2443. endif()
  2444. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  2445. endfunction()
  2446. function (cotire_setup_prefix_generation_from_provided_command _language _target _targetScript _prefixFile _cmdsVar)
  2447. set (_prefixHeaderFiles ${ARGN})
  2448. if (CMAKE_${_language}_COMPILER_ID MATCHES "GNU|Clang")
  2449. # GNU and Clang require indirect compilation of the prefix header to make them honor the system_header pragma
  2450. cotire_prefix_header_to_source_file_path(${_language} "${_prefixFile}" _prefixSourceFile)
  2451. else()
  2452. set (_prefixSourceFile "${_prefixFile}")
  2453. endif()
  2454. cotire_setup_combine_command(${_language} "${_targetScript}" "${_prefixSourceFile}" _cmds ${_prefixHeaderFiles})
  2455. if (CMAKE_${_language}_COMPILER_ID MATCHES "GNU|Clang")
  2456. # set up generation of a prefix source file which includes the prefix header
  2457. cotire_setup_combine_command(${_language} "${_targetScript}" "${_prefixFile}" _cmds ${_prefixSourceFile})
  2458. endif()
  2459. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  2460. endfunction()
  2461. function (cotire_init_cotire_target_properties _target)
  2462. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_ENABLE_PRECOMPILED_HEADER SET)
  2463. if (NOT _isSet)
  2464. set_property(TARGET ${_target} PROPERTY COTIRE_ENABLE_PRECOMPILED_HEADER TRUE)
  2465. endif()
  2466. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_ADD_UNITY_BUILD SET)
  2467. if (NOT _isSet)
  2468. set_property(TARGET ${_target} PROPERTY COTIRE_ADD_UNITY_BUILD TRUE)
  2469. endif()
  2470. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_ADD_CLEAN SET)
  2471. if (NOT _isSet)
  2472. set_property(TARGET ${_target} PROPERTY COTIRE_ADD_CLEAN FALSE)
  2473. endif()
  2474. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_IGNORE_PATH SET)
  2475. if (NOT _isSet)
  2476. set_property(TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_IGNORE_PATH "${CMAKE_SOURCE_DIR}")
  2477. cotire_check_is_path_relative_to("${CMAKE_BINARY_DIR}" _isRelative "${CMAKE_SOURCE_DIR}")
  2478. if (NOT _isRelative)
  2479. set_property(TARGET ${_target} APPEND PROPERTY COTIRE_PREFIX_HEADER_IGNORE_PATH "${CMAKE_BINARY_DIR}")
  2480. endif()
  2481. endif()
  2482. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_INCLUDE_PATH SET)
  2483. if (NOT _isSet)
  2484. set_property(TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_INCLUDE_PATH "")
  2485. endif()
  2486. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_INCLUDE_PRIORITY_PATH SET)
  2487. if (NOT _isSet)
  2488. set_property(TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_INCLUDE_PRIORITY_PATH "")
  2489. endif()
  2490. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_PRE_UNDEFS SET)
  2491. if (NOT _isSet)
  2492. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_PRE_UNDEFS "")
  2493. endif()
  2494. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_POST_UNDEFS SET)
  2495. if (NOT _isSet)
  2496. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_POST_UNDEFS "")
  2497. endif()
  2498. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_UNITY_LINK_LIBRARIES_INIT SET)
  2499. if (NOT _isSet)
  2500. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_LINK_LIBRARIES_INIT "COPY_UNITY")
  2501. endif()
  2502. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES SET)
  2503. if (NOT _isSet)
  2504. if (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES)
  2505. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES "${COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES}")
  2506. else()
  2507. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES "")
  2508. endif()
  2509. endif()
  2510. endfunction()
  2511. function (cotire_make_target_message _target _languages _disableMsg _targetMsgVar)
  2512. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  2513. get_target_property(_targetAddSCU ${_target} COTIRE_ADD_UNITY_BUILD)
  2514. string (REPLACE ";" " " _languagesStr "${_languages}")
  2515. math (EXPR _numberOfExcludedFiles "${ARGC} - 4")
  2516. if (_numberOfExcludedFiles EQUAL 0)
  2517. set (_excludedStr "")
  2518. elseif (COTIRE_VERBOSE OR _numberOfExcludedFiles LESS 4)
  2519. string (REPLACE ";" ", " _excludedStr "excluding ${ARGN}")
  2520. else()
  2521. set (_excludedStr "excluding ${_numberOfExcludedFiles} files")
  2522. endif()
  2523. set (_targetMsg "")
  2524. if (NOT _languages)
  2525. set (_targetMsg "Target ${_target} cannot be cotired.")
  2526. if (_disableMsg)
  2527. set (_targetMsg "${_targetMsg} ${_disableMsg}")
  2528. endif()
  2529. elseif (NOT _targetUsePCH AND NOT _targetAddSCU)
  2530. set (_targetMsg "${_languagesStr} target ${_target} cotired without unity build and precompiled header.")
  2531. if (_disableMsg)
  2532. set (_targetMsg "${_targetMsg} ${_disableMsg}")
  2533. endif()
  2534. elseif (NOT _targetUsePCH)
  2535. if (_excludedStr)
  2536. set (_targetMsg "${_languagesStr} target ${_target} cotired without precompiled header ${_excludedStr}.")
  2537. else()
  2538. set (_targetMsg "${_languagesStr} target ${_target} cotired without precompiled header.")
  2539. endif()
  2540. if (_disableMsg)
  2541. set (_targetMsg "${_targetMsg} ${_disableMsg}")
  2542. endif()
  2543. elseif (NOT _targetAddSCU)
  2544. if (_excludedStr)
  2545. set (_targetMsg "${_languagesStr} target ${_target} cotired without unity build ${_excludedStr}.")
  2546. else()
  2547. set (_targetMsg "${_languagesStr} target ${_target} cotired without unity build.")
  2548. endif()
  2549. else()
  2550. if (_excludedStr)
  2551. set (_targetMsg "${_languagesStr} target ${_target} cotired ${_excludedStr}.")
  2552. else()
  2553. set (_targetMsg "${_languagesStr} target ${_target} cotired.")
  2554. endif()
  2555. endif()
  2556. set (${_targetMsgVar} "${_targetMsg}" PARENT_SCOPE)
  2557. endfunction()
  2558. function (cotire_choose_target_languages _target _targetLanguagesVar _wholeTargetVar)
  2559. set (_languages ${ARGN})
  2560. set (_allSourceFiles "")
  2561. set (_allExcludedSourceFiles "")
  2562. set (_allCotiredSourceFiles "")
  2563. set (_targetLanguages "")
  2564. set (_pchEligibleTargetLanguages "")
  2565. get_target_property(_targetType ${_target} TYPE)
  2566. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2567. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  2568. get_target_property(_targetAddSCU ${_target} COTIRE_ADD_UNITY_BUILD)
  2569. set (_disableMsg "")
  2570. foreach (_language ${_languages})
  2571. get_target_property(_prefixHeader ${_target} COTIRE_${_language}_PREFIX_HEADER)
  2572. get_target_property(_unityBuildFile ${_target} COTIRE_${_language}_UNITY_SOURCE)
  2573. if (_prefixHeader OR _unityBuildFile)
  2574. message (STATUS "cotire: target ${_target} has already been cotired.")
  2575. set (${_targetLanguagesVar} "" PARENT_SCOPE)
  2576. return()
  2577. endif()
  2578. if (_targetUsePCH AND "${_language}" MATCHES "^C|CXX$" AND DEFINED CMAKE_${_language}_COMPILER_ID)
  2579. if (CMAKE_${_language}_COMPILER_ID)
  2580. cotire_check_precompiled_header_support("${_language}" "${_target}" _disableMsg)
  2581. if (_disableMsg)
  2582. set (_targetUsePCH FALSE)
  2583. endif()
  2584. endif()
  2585. endif()
  2586. set (_sourceFiles "")
  2587. set (_excludedSources "")
  2588. set (_cotiredSources "")
  2589. cotire_filter_language_source_files(${_language} ${_target} _sourceFiles _excludedSources _cotiredSources ${_targetSourceFiles})
  2590. if (_sourceFiles OR _excludedSources OR _cotiredSources)
  2591. list (APPEND _targetLanguages ${_language})
  2592. endif()
  2593. if (_sourceFiles)
  2594. list (APPEND _allSourceFiles ${_sourceFiles})
  2595. endif()
  2596. list (LENGTH _sourceFiles _numberOfSources)
  2597. if (NOT _numberOfSources LESS ${COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES})
  2598. list (APPEND _pchEligibleTargetLanguages ${_language})
  2599. endif()
  2600. if (_excludedSources)
  2601. list (APPEND _allExcludedSourceFiles ${_excludedSources})
  2602. endif()
  2603. if (_cotiredSources)
  2604. list (APPEND _allCotiredSourceFiles ${_cotiredSources})
  2605. endif()
  2606. endforeach()
  2607. set (_targetMsgLevel STATUS)
  2608. if (NOT _targetLanguages)
  2609. string (REPLACE ";" " or " _languagesStr "${_languages}")
  2610. set (_disableMsg "No ${_languagesStr} source files.")
  2611. set (_targetUsePCH FALSE)
  2612. set (_targetAddSCU FALSE)
  2613. endif()
  2614. if (_targetUsePCH)
  2615. if (_allCotiredSourceFiles)
  2616. cotire_get_source_file_property_values(_cotireTargets COTIRE_TARGET ${_allCotiredSourceFiles})
  2617. list (REMOVE_DUPLICATES _cotireTargets)
  2618. string (REPLACE ";" ", " _cotireTargetsStr "${_cotireTargets}")
  2619. set (_disableMsg "Target sources already include a precompiled header for target(s) ${_cotireTargets}.")
  2620. set (_disableMsg "${_disableMsg} Set target property COTIRE_ENABLE_PRECOMPILED_HEADER to FALSE for targets ${_target},")
  2621. set (_disableMsg "${_disableMsg} ${_cotireTargetsStr} to get a workable build system.")
  2622. set (_targetMsgLevel SEND_ERROR)
  2623. set (_targetUsePCH FALSE)
  2624. elseif (NOT _pchEligibleTargetLanguages)
  2625. set (_disableMsg "Too few applicable sources.")
  2626. set (_targetUsePCH FALSE)
  2627. elseif (XCODE AND _allExcludedSourceFiles)
  2628. # for Xcode, we cannot apply the precompiled header to individual sources, only to the whole target
  2629. set (_disableMsg "Exclusion of source files not supported for generator Xcode.")
  2630. set (_targetUsePCH FALSE)
  2631. elseif (XCODE AND "${_targetType}" STREQUAL "OBJECT_LIBRARY")
  2632. # for Xcode, we cannot apply the required PRE_BUILD action to generate the prefix header to an OBJECT_LIBRARY target
  2633. set (_disableMsg "Required PRE_BUILD action not supported for OBJECT_LIBRARY targets for generator Xcode.")
  2634. set (_targetUsePCH FALSE)
  2635. endif()
  2636. endif()
  2637. set_property(TARGET ${_target} PROPERTY COTIRE_ENABLE_PRECOMPILED_HEADER ${_targetUsePCH})
  2638. set_property(TARGET ${_target} PROPERTY COTIRE_ADD_UNITY_BUILD ${_targetAddSCU})
  2639. cotire_make_target_message(${_target} "${_targetLanguages}" "${_disableMsg}" _targetMsg ${_allExcludedSourceFiles})
  2640. if (_targetMsg)
  2641. if (NOT DEFINED COTIREMSG_${_target})
  2642. set (COTIREMSG_${_target} "")
  2643. endif()
  2644. if (COTIRE_VERBOSE OR NOT "${_targetMsgLevel}" STREQUAL "STATUS" OR
  2645. NOT "${COTIREMSG_${_target}}" STREQUAL "${_targetMsg}")
  2646. # cache message to avoid redundant messages on re-configure
  2647. set (COTIREMSG_${_target} "${_targetMsg}" CACHE INTERNAL "${_target} cotire message.")
  2648. message (${_targetMsgLevel} "${_targetMsg}")
  2649. endif()
  2650. endif()
  2651. list (LENGTH _targetLanguages _numberOfLanguages)
  2652. if (_numberOfLanguages GREATER 1 OR _allExcludedSourceFiles)
  2653. set (${_wholeTargetVar} FALSE PARENT_SCOPE)
  2654. else()
  2655. set (${_wholeTargetVar} TRUE PARENT_SCOPE)
  2656. endif()
  2657. set (${_targetLanguagesVar} ${_targetLanguages} PARENT_SCOPE)
  2658. endfunction()
  2659. function (cotire_compute_unity_max_number_of_includes _target _maxIncludesVar)
  2660. set (_sourceFiles ${ARGN})
  2661. get_target_property(_maxIncludes ${_target} COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES)
  2662. if (_maxIncludes MATCHES "(-j|--parallel|--jobs) ?([0-9]*)")
  2663. set (_numberOfThreads "${CMAKE_MATCH_2}")
  2664. if (NOT _numberOfThreads)
  2665. # use all available cores
  2666. ProcessorCount(_numberOfThreads)
  2667. endif()
  2668. list (LENGTH _sourceFiles _numberOfSources)
  2669. math (EXPR _maxIncludes "(${_numberOfSources} + ${_numberOfThreads} - 1) / ${_numberOfThreads}")
  2670. elseif (NOT _maxIncludes MATCHES "[0-9]+")
  2671. set (_maxIncludes 0)
  2672. endif()
  2673. if (COTIRE_DEBUG)
  2674. message (STATUS "${_target} unity source max includes: ${_maxIncludes}")
  2675. endif()
  2676. set (${_maxIncludesVar} ${_maxIncludes} PARENT_SCOPE)
  2677. endfunction()
  2678. function (cotire_process_target_language _language _configurations _target _wholeTarget _cmdsVar)
  2679. set (${_cmdsVar} "" PARENT_SCOPE)
  2680. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2681. set (_sourceFiles "")
  2682. set (_excludedSources "")
  2683. set (_cotiredSources "")
  2684. cotire_filter_language_source_files(${_language} ${_target} _sourceFiles _excludedSources _cotiredSources ${_targetSourceFiles})
  2685. if (NOT _sourceFiles AND NOT _cotiredSources)
  2686. return()
  2687. endif()
  2688. set (_cmds "")
  2689. # check for user provided unity source file list
  2690. get_property(_unitySourceFiles TARGET ${_target} PROPERTY COTIRE_${_language}_UNITY_SOURCE_INIT)
  2691. if (NOT _unitySourceFiles)
  2692. set (_unitySourceFiles ${_sourceFiles} ${_cotiredSources})
  2693. endif()
  2694. cotire_generate_target_script(
  2695. ${_language} "${_configurations}" ${_target} _targetScript _targetConfigScript ${_unitySourceFiles})
  2696. # set up unity files for parallel compilation
  2697. cotire_compute_unity_max_number_of_includes(${_target} _maxIncludes ${_unitySourceFiles})
  2698. cotire_make_unity_source_file_paths(${_language} ${_target} ${_maxIncludes} _unityFiles ${_unitySourceFiles})
  2699. list (LENGTH _unityFiles _numberOfUnityFiles)
  2700. if (_numberOfUnityFiles EQUAL 0)
  2701. return()
  2702. elseif (_numberOfUnityFiles GREATER 1)
  2703. cotire_setup_unity_generation_commands(
  2704. ${_language} ${_target} "${_targetScript}" "${_targetConfigScript}" "${_unityFiles}" _cmds ${_unitySourceFiles})
  2705. endif()
  2706. # set up single unity file for prefix header generation
  2707. cotire_make_single_unity_source_file_path(${_language} ${_target} _unityFile)
  2708. cotire_setup_unity_generation_commands(
  2709. ${_language} ${_target} "${_targetScript}" "${_targetConfigScript}" "${_unityFile}" _cmds ${_unitySourceFiles})
  2710. cotire_make_prefix_file_path(${_language} ${_target} _prefixFile)
  2711. # set up prefix header
  2712. if (_prefixFile)
  2713. # check for user provided prefix header files
  2714. get_property(_prefixHeaderFiles TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER_INIT)
  2715. if (_prefixHeaderFiles)
  2716. cotire_setup_prefix_generation_from_provided_command(
  2717. ${_language} ${_target} "${_targetConfigScript}" "${_prefixFile}" _cmds ${_prefixHeaderFiles})
  2718. else()
  2719. cotire_setup_prefix_generation_from_unity_command(
  2720. ${_language} ${_target} "${_targetConfigScript}" "${_prefixFile}" "${_unityFile}" _cmds ${_unitySourceFiles})
  2721. endif()
  2722. # check if selected language has enough sources at all
  2723. list (LENGTH _sourceFiles _numberOfSources)
  2724. if (_numberOfSources LESS ${COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES})
  2725. set (_targetUsePCH FALSE)
  2726. else()
  2727. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  2728. endif()
  2729. if (_targetUsePCH)
  2730. cotire_make_pch_file_path(${_language} ${_target} _pchFile)
  2731. if (_pchFile)
  2732. # first file in _sourceFiles is passed as the host file
  2733. cotire_setup_pch_file_compilation(
  2734. ${_language} ${_target} "${_targetConfigScript}" "${_prefixFile}" "${_pchFile}" ${_sourceFiles})
  2735. cotire_setup_pch_file_inclusion(
  2736. ${_language} ${_target} ${_wholeTarget} "${_prefixFile}" "${_pchFile}" ${_sourceFiles})
  2737. endif()
  2738. elseif (_prefixHeaderFiles)
  2739. # user provided prefix header must be included unconditionally
  2740. cotire_setup_prefix_file_inclusion(${_language} ${_target} "${_prefixFile}" ${_sourceFiles})
  2741. endif()
  2742. endif()
  2743. # mark target as cotired for language
  2744. set_property(TARGET ${_target} PROPERTY COTIRE_${_language}_UNITY_SOURCE "${_unityFiles}")
  2745. if (_prefixFile)
  2746. set_property(TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER "${_prefixFile}")
  2747. if (_targetUsePCH AND _pchFile)
  2748. set_property(TARGET ${_target} PROPERTY COTIRE_${_language}_PRECOMPILED_HEADER "${_pchFile}")
  2749. endif()
  2750. endif()
  2751. set (${_cmdsVar} ${_cmds} PARENT_SCOPE)
  2752. endfunction()
  2753. function (cotire_setup_clean_target _target)
  2754. set (_cleanTargetName "${_target}${COTIRE_CLEAN_TARGET_SUFFIX}")
  2755. if (NOT TARGET "${_cleanTargetName}")
  2756. cotire_set_cmd_to_prologue(_cmds)
  2757. get_filename_component(_outputDir "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}" ABSOLUTE)
  2758. list (APPEND _cmds -P "${COTIRE_CMAKE_MODULE_FILE}" "cleanup" "${_outputDir}" "${COTIRE_INTDIR}" "${_target}")
  2759. add_custom_target(${_cleanTargetName}
  2760. COMMAND ${_cmds}
  2761. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  2762. COMMENT "Cleaning up target ${_target} cotire generated files"
  2763. VERBATIM)
  2764. cotire_init_target("${_cleanTargetName}")
  2765. endif()
  2766. endfunction()
  2767. function (cotire_setup_pch_target _languages _configurations _target)
  2768. if ("${CMAKE_GENERATOR}" MATCHES "Make|Ninja")
  2769. # for makefile based generators, we add a custom target to trigger the generation of the cotire related files
  2770. set (_dependsFiles "")
  2771. foreach (_language ${_languages})
  2772. set (_props COTIRE_${_language}_PREFIX_HEADER COTIRE_${_language}_UNITY_SOURCE)
  2773. if (NOT CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  2774. # Visual Studio and Intel only create precompiled header as a side effect
  2775. list (INSERT _props 0 COTIRE_${_language}_PRECOMPILED_HEADER)
  2776. endif()
  2777. cotire_get_first_set_property_value(_dependsFile TARGET ${_target} ${_props})
  2778. if (_dependsFile)
  2779. list (APPEND _dependsFiles "${_dependsFile}")
  2780. endif()
  2781. endforeach()
  2782. if (_dependsFiles)
  2783. set (_pchTargetName "${_target}${COTIRE_PCH_TARGET_SUFFIX}")
  2784. add_custom_target("${_pchTargetName}" DEPENDS ${_dependsFiles})
  2785. cotire_init_target("${_pchTargetName}")
  2786. cotire_add_to_pch_all_target(${_pchTargetName})
  2787. endif()
  2788. else()
  2789. # for other generators, we add the "clean all" target to clean up the precompiled header
  2790. cotire_setup_clean_all_target()
  2791. endif()
  2792. endfunction()
  2793. function (cotire_collect_unity_target_sources _target _languages _unityTargetSourcesVar)
  2794. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2795. set (_unityTargetSources ${_targetSourceFiles})
  2796. foreach (_language ${_languages})
  2797. get_property(_unityFiles TARGET ${_target} PROPERTY COTIRE_${_language}_UNITY_SOURCE)
  2798. if (_unityFiles)
  2799. # remove source files that are included in the unity source
  2800. set (_sourceFiles "")
  2801. set (_excludedSources "")
  2802. set (_cotiredSources "")
  2803. cotire_filter_language_source_files(${_language} ${_target} _sourceFiles _excludedSources _cotiredSources ${_targetSourceFiles})
  2804. if (_sourceFiles OR _cotiredSources)
  2805. list (REMOVE_ITEM _unityTargetSources ${_sourceFiles} ${_cotiredSources})
  2806. endif()
  2807. # add unity source files instead
  2808. list (APPEND _unityTargetSources ${_unityFiles})
  2809. endif()
  2810. endforeach()
  2811. set (${_unityTargetSourcesVar} ${_unityTargetSources} PARENT_SCOPE)
  2812. endfunction()
  2813. function (cotire_setup_unity_target_pch_usage _languages _target)
  2814. foreach (_language ${_languages})
  2815. get_property(_unityFiles TARGET ${_target} PROPERTY COTIRE_${_language}_UNITY_SOURCE)
  2816. if (_unityFiles)
  2817. get_property(_userPrefixFile TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER_INIT)
  2818. get_property(_prefixFile TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER)
  2819. if (_userPrefixFile AND _prefixFile)
  2820. # user provided prefix header must be included unconditionally by unity sources
  2821. cotire_setup_prefix_file_inclusion(${_language} ${_target} "${_prefixFile}" ${_unityFiles})
  2822. endif()
  2823. endif()
  2824. endforeach()
  2825. endfunction()
  2826. function (cotire_setup_unity_build_target _languages _configurations _target)
  2827. get_target_property(_unityTargetName ${_target} COTIRE_UNITY_TARGET_NAME)
  2828. if (NOT _unityTargetName)
  2829. set (_unityTargetName "${_target}${COTIRE_UNITY_BUILD_TARGET_SUFFIX}")
  2830. endif()
  2831. # determine unity target sub type
  2832. get_target_property(_targetType ${_target} TYPE)
  2833. if ("${_targetType}" STREQUAL "EXECUTABLE")
  2834. set (_unityTargetSubType "")
  2835. elseif (_targetType MATCHES "(STATIC|SHARED|MODULE|OBJECT)_LIBRARY")
  2836. set (_unityTargetSubType "${CMAKE_MATCH_1}")
  2837. else()
  2838. message (WARNING "cotire: target ${_target} has unknown target type ${_targetType}.")
  2839. return()
  2840. endif()
  2841. # determine unity target sources
  2842. set (_unityTargetSources "")
  2843. cotire_collect_unity_target_sources(${_target} "${_languages}" _unityTargetSources)
  2844. # handle automatic Qt processing
  2845. get_target_property(_targetAutoMoc ${_target} AUTOMOC)
  2846. get_target_property(_targetAutoUic ${_target} AUTOUIC)
  2847. get_target_property(_targetAutoRcc ${_target} AUTORCC)
  2848. if (_targetAutoMoc OR _targetAutoUic OR _targetAutoRcc)
  2849. # if the original target sources are subject to CMake's automatic Qt processing,
  2850. # also include implicitly generated <targetname>_automoc.cpp file
  2851. list (APPEND _unityTargetSources "${_target}_automoc.cpp")
  2852. set_property (SOURCE "${_target}_automoc.cpp" PROPERTY GENERATED TRUE)
  2853. endif()
  2854. # prevent AUTOMOC, AUTOUIC and AUTORCC properties from being set when the unity target is created
  2855. set (CMAKE_AUTOMOC OFF)
  2856. set (CMAKE_AUTOUIC OFF)
  2857. set (CMAKE_AUTORCC OFF)
  2858. if (COTIRE_DEBUG)
  2859. message (STATUS "add target ${_targetType} ${_unityTargetName} ${_unityTargetSubType} EXCLUDE_FROM_ALL ${_unityTargetSources}")
  2860. endif()
  2861. # generate unity target
  2862. if ("${_targetType}" STREQUAL "EXECUTABLE")
  2863. add_executable(${_unityTargetName} ${_unityTargetSubType} EXCLUDE_FROM_ALL ${_unityTargetSources})
  2864. else()
  2865. add_library(${_unityTargetName} ${_unityTargetSubType} EXCLUDE_FROM_ALL ${_unityTargetSources})
  2866. endif()
  2867. if (_targetAutoMoc OR _targetAutoUic OR _targetAutoRcc)
  2868. # depend on the original target's implicity generated <targetname>_automoc target
  2869. add_dependencies(${_unityTargetName} ${_target}_automoc)
  2870. endif()
  2871. # copy output location properties
  2872. set (_outputDirProperties
  2873. ARCHIVE_OUTPUT_DIRECTORY ARCHIVE_OUTPUT_DIRECTORY_<CONFIG>
  2874. LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_<CONFIG>
  2875. RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_<CONFIG>)
  2876. if (COTIRE_UNITY_OUTPUT_DIRECTORY)
  2877. set (_setDefaultOutputDir TRUE)
  2878. if (IS_ABSOLUTE "${COTIRE_UNITY_OUTPUT_DIRECTORY}")
  2879. set (_outputDir "${COTIRE_UNITY_OUTPUT_DIRECTORY}")
  2880. else()
  2881. # append relative COTIRE_UNITY_OUTPUT_DIRECTORY to target's actual output directory
  2882. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName} ${_outputDirProperties})
  2883. cotire_resolve_config_properites("${_configurations}" _properties ${_outputDirProperties})
  2884. foreach (_property ${_properties})
  2885. get_property(_outputDir TARGET ${_target} PROPERTY ${_property})
  2886. if (_outputDir)
  2887. get_filename_component(_outputDir "${_outputDir}/${COTIRE_UNITY_OUTPUT_DIRECTORY}" ABSOLUTE)
  2888. set_property(TARGET ${_unityTargetName} PROPERTY ${_property} "${_outputDir}")
  2889. set (_setDefaultOutputDir FALSE)
  2890. endif()
  2891. endforeach()
  2892. if (_setDefaultOutputDir)
  2893. get_filename_component(_outputDir "${CMAKE_CURRENT_BINARY_DIR}/${COTIRE_UNITY_OUTPUT_DIRECTORY}" ABSOLUTE)
  2894. endif()
  2895. endif()
  2896. if (_setDefaultOutputDir)
  2897. set_target_properties(${_unityTargetName} PROPERTIES
  2898. ARCHIVE_OUTPUT_DIRECTORY "${_outputDir}"
  2899. LIBRARY_OUTPUT_DIRECTORY "${_outputDir}"
  2900. RUNTIME_OUTPUT_DIRECTORY "${_outputDir}")
  2901. endif()
  2902. else()
  2903. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2904. ${_outputDirProperties})
  2905. endif()
  2906. # copy output name
  2907. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2908. ARCHIVE_OUTPUT_NAME ARCHIVE_OUTPUT_NAME_<CONFIG>
  2909. LIBRARY_OUTPUT_NAME LIBRARY_OUTPUT_NAME_<CONFIG>
  2910. OUTPUT_NAME OUTPUT_NAME_<CONFIG>
  2911. RUNTIME_OUTPUT_NAME RUNTIME_OUTPUT_NAME_<CONFIG>
  2912. PREFIX <CONFIG>_POSTFIX SUFFIX
  2913. IMPORT_PREFIX IMPORT_SUFFIX)
  2914. # copy compile stuff
  2915. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2916. COMPILE_DEFINITIONS COMPILE_DEFINITIONS_<CONFIG>
  2917. COMPILE_FLAGS COMPILE_OPTIONS
  2918. Fortran_FORMAT Fortran_MODULE_DIRECTORY
  2919. INCLUDE_DIRECTORIES
  2920. INTERPROCEDURAL_OPTIMIZATION INTERPROCEDURAL_OPTIMIZATION_<CONFIG>
  2921. POSITION_INDEPENDENT_CODE
  2922. C_COMPILER_LAUNCHER CXX_COMPILER_LAUNCHER
  2923. C_INCLUDE_WHAT_YOU_USE CXX_INCLUDE_WHAT_YOU_USE
  2924. C_VISIBILITY_PRESET CXX_VISIBILITY_PRESET VISIBILITY_INLINES_HIDDEN)
  2925. # copy compile features
  2926. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2927. C_EXTENSIONS C_STANDARD C_STANDARD_REQUIRED
  2928. CXX_EXTENSIONS CXX_STANDARD CXX_STANDARD_REQUIRED
  2929. COMPILE_FEATURES)
  2930. # copy interface stuff
  2931. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2932. COMPATIBLE_INTERFACE_BOOL COMPATIBLE_INTERFACE_NUMBER_MAX COMPATIBLE_INTERFACE_NUMBER_MIN
  2933. COMPATIBLE_INTERFACE_STRING
  2934. INTERFACE_COMPILE_DEFINITIONS INTERFACE_COMPILE_FEATURES INTERFACE_COMPILE_OPTIONS
  2935. INTERFACE_INCLUDE_DIRECTORIES INTERFACE_SOURCES
  2936. INTERFACE_POSITION_INDEPENDENT_CODE INTERFACE_SYSTEM_INCLUDE_DIRECTORIES
  2937. INTERFACE_AUTOUIC_OPTIONS NO_SYSTEM_FROM_IMPORTED)
  2938. # copy link stuff
  2939. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2940. BUILD_WITH_INSTALL_RPATH INSTALL_RPATH INSTALL_RPATH_USE_LINK_PATH SKIP_BUILD_RPATH
  2941. LINKER_LANGUAGE LINK_DEPENDS LINK_DEPENDS_NO_SHARED
  2942. LINK_FLAGS LINK_FLAGS_<CONFIG>
  2943. LINK_INTERFACE_LIBRARIES LINK_INTERFACE_LIBRARIES_<CONFIG>
  2944. LINK_INTERFACE_MULTIPLICITY LINK_INTERFACE_MULTIPLICITY_<CONFIG>
  2945. LINK_SEARCH_START_STATIC LINK_SEARCH_END_STATIC
  2946. STATIC_LIBRARY_FLAGS STATIC_LIBRARY_FLAGS_<CONFIG>
  2947. NO_SONAME SOVERSION VERSION)
  2948. # copy cmake stuff
  2949. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2950. IMPLICIT_DEPENDS_INCLUDE_TRANSFORM RULE_LAUNCH_COMPILE RULE_LAUNCH_CUSTOM RULE_LAUNCH_LINK)
  2951. # copy Apple platform specific stuff
  2952. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2953. BUNDLE BUNDLE_EXTENSION FRAMEWORK FRAMEWORK_VERSION INSTALL_NAME_DIR
  2954. MACOSX_BUNDLE MACOSX_BUNDLE_INFO_PLIST MACOSX_FRAMEWORK_INFO_PLIST MACOSX_RPATH
  2955. OSX_ARCHITECTURES OSX_ARCHITECTURES_<CONFIG> PRIVATE_HEADER PUBLIC_HEADER RESOURCE XCTEST
  2956. IOS_INSTALL_COMBINED)
  2957. # copy Windows platform specific stuff
  2958. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2959. GNUtoMS
  2960. COMPILE_PDB_NAME COMPILE_PDB_NAME_<CONFIG>
  2961. COMPILE_PDB_OUTPUT_DIRECTORY COMPILE_PDB_OUTPUT_DIRECTORY_<CONFIG>
  2962. PDB_NAME PDB_NAME_<CONFIG> PDB_OUTPUT_DIRECTORY PDB_OUTPUT_DIRECTORY_<CONFIG>
  2963. VS_DESKTOP_EXTENSIONS_VERSION VS_DOTNET_REFERENCES VS_DOTNET_TARGET_FRAMEWORK_VERSION
  2964. VS_GLOBAL_KEYWORD VS_GLOBAL_PROJECT_TYPES VS_GLOBAL_ROOTNAMESPACE
  2965. VS_IOT_EXTENSIONS_VERSION VS_IOT_STARTUP_TASK
  2966. VS_KEYWORD VS_MOBILE_EXTENSIONS_VERSION
  2967. VS_SCC_AUXPATH VS_SCC_LOCALPATH VS_SCC_PROJECTNAME VS_SCC_PROVIDER
  2968. VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION
  2969. VS_WINRT_COMPONENT VS_WINRT_EXTENSIONS VS_WINRT_REFERENCES
  2970. WIN32_EXECUTABLE WINDOWS_EXPORT_ALL_SYMBOLS)
  2971. # copy Android platform specific stuff
  2972. cotire_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2973. ANDROID_API ANDROID_API_MIN ANDROID_GUI
  2974. ANDROID_ANT_ADDITIONAL_OPTIONS ANDROID_ARCH ANDROID_ASSETS_DIRECTORIES
  2975. ANDROID_JAR_DEPENDENCIES ANDROID_JAR_DIRECTORIES ANDROID_JAVA_SOURCE_DIR
  2976. ANDROID_NATIVE_LIB_DEPENDENCIES ANDROID_NATIVE_LIB_DIRECTORIES
  2977. ANDROID_PROCESS_MAX ANDROID_PROGUARD ANDROID_PROGUARD_CONFIG_PATH
  2978. ANDROID_SECURE_PROPS_PATH ANDROID_SKIP_ANT_STEP ANDROID_STL_TYPE)
  2979. # use output name from original target
  2980. get_target_property(_targetOutputName ${_unityTargetName} OUTPUT_NAME)
  2981. if (NOT _targetOutputName)
  2982. set_property(TARGET ${_unityTargetName} PROPERTY OUTPUT_NAME "${_target}")
  2983. endif()
  2984. # use export symbol from original target
  2985. cotire_get_target_export_symbol("${_target}" _defineSymbol)
  2986. if (_defineSymbol)
  2987. set_property(TARGET ${_unityTargetName} PROPERTY DEFINE_SYMBOL "${_defineSymbol}")
  2988. if ("${_targetType}" STREQUAL "EXECUTABLE")
  2989. set_property(TARGET ${_unityTargetName} PROPERTY ENABLE_EXPORTS TRUE)
  2990. endif()
  2991. endif()
  2992. cotire_init_target(${_unityTargetName})
  2993. cotire_add_to_unity_all_target(${_unityTargetName})
  2994. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_TARGET_NAME "${_unityTargetName}")
  2995. endfunction(cotire_setup_unity_build_target)
  2996. function (cotire_target _target)
  2997. set(_options "")
  2998. set(_oneValueArgs "")
  2999. set(_multiValueArgs LANGUAGES CONFIGURATIONS)
  3000. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  3001. if (NOT _option_LANGUAGES)
  3002. get_property (_option_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  3003. endif()
  3004. if (NOT _option_CONFIGURATIONS)
  3005. cotire_get_configuration_types(_option_CONFIGURATIONS)
  3006. endif()
  3007. # check if cotire can be applied to target at all
  3008. cotire_is_target_supported(${_target} _isSupported)
  3009. if (NOT _isSupported)
  3010. get_target_property(_imported ${_target} IMPORTED)
  3011. get_target_property(_targetType ${_target} TYPE)
  3012. if (_imported)
  3013. message (WARNING "cotire: imported ${_targetType} target ${_target} cannot be cotired.")
  3014. else()
  3015. message (STATUS "cotire: ${_targetType} target ${_target} cannot be cotired.")
  3016. endif()
  3017. return()
  3018. endif()
  3019. # resolve alias
  3020. get_target_property(_aliasName ${_target} ALIASED_TARGET)
  3021. if (_aliasName)
  3022. if (COTIRE_DEBUG)
  3023. message (STATUS "${_target} is an alias. Applying cotire to aliased target ${_aliasName} instead.")
  3024. endif()
  3025. set (_target ${_aliasName})
  3026. endif()
  3027. # check if target needs to be cotired for build type
  3028. # when using configuration types, the test is performed at build time
  3029. cotire_init_cotire_target_properties(${_target})
  3030. if (NOT CMAKE_CONFIGURATION_TYPES)
  3031. if (CMAKE_BUILD_TYPE)
  3032. list (FIND _option_CONFIGURATIONS "${CMAKE_BUILD_TYPE}" _index)
  3033. else()
  3034. list (FIND _option_CONFIGURATIONS "None" _index)
  3035. endif()
  3036. if (_index EQUAL -1)
  3037. if (COTIRE_DEBUG)
  3038. message (STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} not cotired (${_option_CONFIGURATIONS})")
  3039. endif()
  3040. return()
  3041. endif()
  3042. endif()
  3043. # when not using configuration types, immediately create cotire intermediate dir
  3044. if (NOT CMAKE_CONFIGURATION_TYPES)
  3045. cotire_get_intermediate_dir(_baseDir)
  3046. file (MAKE_DIRECTORY "${_baseDir}")
  3047. endif()
  3048. # choose languages that apply to the target
  3049. cotire_choose_target_languages("${_target}" _targetLanguages _wholeTarget ${_option_LANGUAGES})
  3050. if (NOT _targetLanguages)
  3051. return()
  3052. endif()
  3053. set (_cmds "")
  3054. foreach (_language ${_targetLanguages})
  3055. cotire_process_target_language("${_language}" "${_option_CONFIGURATIONS}" ${_target} ${_wholeTarget} _cmd)
  3056. if (_cmd)
  3057. list (APPEND _cmds ${_cmd})
  3058. endif()
  3059. endforeach()
  3060. get_target_property(_targetAddSCU ${_target} COTIRE_ADD_UNITY_BUILD)
  3061. if (_targetAddSCU)
  3062. cotire_setup_unity_build_target("${_targetLanguages}" "${_option_CONFIGURATIONS}" ${_target})
  3063. endif()
  3064. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  3065. if (_targetUsePCH)
  3066. cotire_setup_target_pch_usage("${_targetLanguages}" ${_target} ${_wholeTarget} ${_cmds})
  3067. cotire_setup_pch_target("${_targetLanguages}" "${_option_CONFIGURATIONS}" ${_target})
  3068. if (_targetAddSCU)
  3069. cotire_setup_unity_target_pch_usage("${_targetLanguages}" ${_target})
  3070. endif()
  3071. endif()
  3072. get_target_property(_targetAddCleanTarget ${_target} COTIRE_ADD_CLEAN)
  3073. if (_targetAddCleanTarget)
  3074. cotire_setup_clean_target(${_target})
  3075. endif()
  3076. endfunction(cotire_target)
  3077. function (cotire_map_libraries _strategy _mappedLibrariesVar)
  3078. set (_mappedLibraries "")
  3079. foreach (_library ${ARGN})
  3080. if (_library MATCHES "^\\$<LINK_ONLY:(.+)>$")
  3081. set (_libraryName "${CMAKE_MATCH_1}")
  3082. set (_linkOnly TRUE)
  3083. else()
  3084. set (_libraryName "${_library}")
  3085. set (_linkOnly FALSE)
  3086. endif()
  3087. if ("${_strategy}" MATCHES "COPY_UNITY")
  3088. cotire_is_target_supported(${_libraryName} _isSupported)
  3089. if (_isSupported)
  3090. # use target's corresponding unity target, if available
  3091. get_target_property(_libraryUnityTargetName ${_libraryName} COTIRE_UNITY_TARGET_NAME)
  3092. if (TARGET "${_libraryUnityTargetName}")
  3093. if (_linkOnly)
  3094. list (APPEND _mappedLibraries "$<LINK_ONLY:${_libraryUnityTargetName}>")
  3095. else()
  3096. list (APPEND _mappedLibraries "${_libraryUnityTargetName}")
  3097. endif()
  3098. else()
  3099. list (APPEND _mappedLibraries "${_library}")
  3100. endif()
  3101. else()
  3102. list (APPEND _mappedLibraries "${_library}")
  3103. endif()
  3104. else()
  3105. list (APPEND _mappedLibraries "${_library}")
  3106. endif()
  3107. endforeach()
  3108. list (REMOVE_DUPLICATES _mappedLibraries)
  3109. set (${_mappedLibrariesVar} ${_mappedLibraries} PARENT_SCOPE)
  3110. endfunction()
  3111. function (cotire_target_link_libraries _target)
  3112. cotire_is_target_supported(${_target} _isSupported)
  3113. if (NOT _isSupported)
  3114. return()
  3115. endif()
  3116. get_target_property(_unityTargetName ${_target} COTIRE_UNITY_TARGET_NAME)
  3117. if (TARGET "${_unityTargetName}")
  3118. get_target_property(_linkLibrariesStrategy ${_target} COTIRE_UNITY_LINK_LIBRARIES_INIT)
  3119. if (COTIRE_DEBUG)
  3120. message (STATUS "unity target ${_unityTargetName} link strategy: ${_linkLibrariesStrategy}")
  3121. endif()
  3122. if ("${_linkLibrariesStrategy}" MATCHES "^(COPY|COPY_UNITY)$")
  3123. get_target_property(_linkLibraries ${_target} LINK_LIBRARIES)
  3124. if (_linkLibraries)
  3125. cotire_map_libraries("${_linkLibrariesStrategy}" _unityLinkLibraries ${_linkLibraries})
  3126. set_target_properties(${_unityTargetName} PROPERTIES LINK_LIBRARIES "${_unityLinkLibraries}")
  3127. if (COTIRE_DEBUG)
  3128. message (STATUS "unity target ${_unityTargetName} link libraries: ${_unityLinkLibraries}")
  3129. endif()
  3130. endif()
  3131. get_target_property(_interfaceLinkLibraries ${_target} INTERFACE_LINK_LIBRARIES)
  3132. if (_interfaceLinkLibraries)
  3133. cotire_map_libraries("${_linkLibrariesStrategy}" _unityLinkInterfaceLibraries ${_interfaceLinkLibraries})
  3134. set_target_properties(${_unityTargetName} PROPERTIES INTERFACE_LINK_LIBRARIES "${_unityLinkInterfaceLibraries}")
  3135. if (COTIRE_DEBUG)
  3136. message (STATUS "unity target ${_unityTargetName} interface link libraries: ${_unityLinkInterfaceLibraries}")
  3137. endif()
  3138. endif()
  3139. endif()
  3140. endif()
  3141. endfunction(cotire_target_link_libraries)
  3142. function (cotire_cleanup _binaryDir _cotireIntermediateDirName _targetName)
  3143. if (_targetName)
  3144. file (GLOB_RECURSE _cotireFiles "${_binaryDir}/${_targetName}*.*")
  3145. else()
  3146. file (GLOB_RECURSE _cotireFiles "${_binaryDir}/*.*")
  3147. endif()
  3148. # filter files in intermediate directory
  3149. set (_filesToRemove "")
  3150. foreach (_file ${_cotireFiles})
  3151. get_filename_component(_dir "${_file}" DIRECTORY)
  3152. get_filename_component(_dirName "${_dir}" NAME)
  3153. if ("${_dirName}" STREQUAL "${_cotireIntermediateDirName}")
  3154. list (APPEND _filesToRemove "${_file}")
  3155. endif()
  3156. endforeach()
  3157. if (_filesToRemove)
  3158. if (COTIRE_VERBOSE)
  3159. message (STATUS "cleaning up ${_filesToRemove}")
  3160. endif()
  3161. file (REMOVE ${_filesToRemove})
  3162. endif()
  3163. endfunction()
  3164. function (cotire_init_target _targetName)
  3165. if (COTIRE_TARGETS_FOLDER)
  3166. set_target_properties(${_targetName} PROPERTIES FOLDER "${COTIRE_TARGETS_FOLDER}")
  3167. endif()
  3168. set_target_properties(${_targetName} PROPERTIES EXCLUDE_FROM_ALL TRUE)
  3169. if (MSVC_IDE)
  3170. set_target_properties(${_targetName} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD TRUE)
  3171. endif()
  3172. endfunction()
  3173. function (cotire_add_to_pch_all_target _pchTargetName)
  3174. set (_targetName "${COTIRE_PCH_ALL_TARGET_NAME}")
  3175. if (NOT TARGET "${_targetName}")
  3176. add_custom_target("${_targetName}"
  3177. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  3178. VERBATIM)
  3179. cotire_init_target("${_targetName}")
  3180. endif()
  3181. cotire_setup_clean_all_target()
  3182. add_dependencies(${_targetName} ${_pchTargetName})
  3183. endfunction()
  3184. function (cotire_add_to_unity_all_target _unityTargetName)
  3185. set (_targetName "${COTIRE_UNITY_BUILD_ALL_TARGET_NAME}")
  3186. if (NOT TARGET "${_targetName}")
  3187. add_custom_target("${_targetName}"
  3188. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  3189. VERBATIM)
  3190. cotire_init_target("${_targetName}")
  3191. endif()
  3192. cotire_setup_clean_all_target()
  3193. add_dependencies(${_targetName} ${_unityTargetName})
  3194. endfunction()
  3195. function (cotire_setup_clean_all_target)
  3196. set (_targetName "${COTIRE_CLEAN_ALL_TARGET_NAME}")
  3197. if (NOT TARGET "${_targetName}")
  3198. cotire_set_cmd_to_prologue(_cmds)
  3199. list (APPEND _cmds -P "${COTIRE_CMAKE_MODULE_FILE}" "cleanup" "${CMAKE_BINARY_DIR}" "${COTIRE_INTDIR}")
  3200. add_custom_target(${_targetName}
  3201. COMMAND ${_cmds}
  3202. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  3203. COMMENT "Cleaning up all cotire generated files"
  3204. VERBATIM)
  3205. cotire_init_target("${_targetName}")
  3206. endif()
  3207. endfunction()
  3208. function (cotire)
  3209. set(_options "")
  3210. set(_oneValueArgs "")
  3211. set(_multiValueArgs LANGUAGES CONFIGURATIONS)
  3212. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  3213. set (_targets ${_option_UNPARSED_ARGUMENTS})
  3214. foreach (_target ${_targets})
  3215. if (TARGET ${_target})
  3216. cotire_target(${_target} LANGUAGES ${_option_LANGUAGES} CONFIGURATIONS ${_option_CONFIGURATIONS})
  3217. else()
  3218. message (WARNING "cotire: ${_target} is not a target.")
  3219. endif()
  3220. endforeach()
  3221. foreach (_target ${_targets})
  3222. if (TARGET ${_target})
  3223. cotire_target_link_libraries(${_target})
  3224. endif()
  3225. endforeach()
  3226. endfunction()
  3227. if (CMAKE_SCRIPT_MODE_FILE)
  3228. # cotire is being run in script mode
  3229. # locate -P on command args
  3230. set (COTIRE_ARGC -1)
  3231. foreach (_index RANGE ${CMAKE_ARGC})
  3232. if (COTIRE_ARGC GREATER -1)
  3233. set (COTIRE_ARGV${COTIRE_ARGC} "${CMAKE_ARGV${_index}}")
  3234. math (EXPR COTIRE_ARGC "${COTIRE_ARGC} + 1")
  3235. elseif ("${CMAKE_ARGV${_index}}" STREQUAL "-P")
  3236. set (COTIRE_ARGC 0)
  3237. endif()
  3238. endforeach()
  3239. # include target script if available
  3240. if ("${COTIRE_ARGV2}" MATCHES "\\.cmake$")
  3241. # the included target scripts sets up additional variables relating to the target (e.g., COTIRE_TARGET_SOURCES)
  3242. include("${COTIRE_ARGV2}")
  3243. endif()
  3244. if (COTIRE_DEBUG)
  3245. message (STATUS "${COTIRE_ARGV0} ${COTIRE_ARGV1} ${COTIRE_ARGV2} ${COTIRE_ARGV3} ${COTIRE_ARGV4} ${COTIRE_ARGV5}")
  3246. endif()
  3247. if (NOT COTIRE_BUILD_TYPE)
  3248. set (COTIRE_BUILD_TYPE "None")
  3249. endif()
  3250. string (TOUPPER "${COTIRE_BUILD_TYPE}" _upperConfig)
  3251. set (_includeDirs ${COTIRE_TARGET_INCLUDE_DIRECTORIES_${_upperConfig}})
  3252. set (_systemIncludeDirs ${COTIRE_TARGET_SYSTEM_INCLUDE_DIRECTORIES_${_upperConfig}})
  3253. set (_compileDefinitions ${COTIRE_TARGET_COMPILE_DEFINITIONS_${_upperConfig}})
  3254. set (_compileFlags ${COTIRE_TARGET_COMPILE_FLAGS_${_upperConfig}})
  3255. # check if target has been cotired for actual build type COTIRE_BUILD_TYPE
  3256. list (FIND COTIRE_TARGET_CONFIGURATION_TYPES "${COTIRE_BUILD_TYPE}" _index)
  3257. if (_index GREATER -1)
  3258. set (_sources ${COTIRE_TARGET_SOURCES})
  3259. set (_sourcesDefinitions ${COTIRE_TARGET_SOURCES_COMPILE_DEFINITIONS_${_upperConfig}})
  3260. else()
  3261. if (COTIRE_DEBUG)
  3262. message (STATUS "COTIRE_BUILD_TYPE=${COTIRE_BUILD_TYPE} not cotired (${COTIRE_TARGET_CONFIGURATION_TYPES})")
  3263. endif()
  3264. set (_sources "")
  3265. set (_sourcesDefinitions "")
  3266. endif()
  3267. set (_targetPreUndefs ${COTIRE_TARGET_PRE_UNDEFS})
  3268. set (_targetPostUndefs ${COTIRE_TARGET_POST_UNDEFS})
  3269. set (_sourcesPreUndefs ${COTIRE_TARGET_SOURCES_PRE_UNDEFS})
  3270. set (_sourcesPostUndefs ${COTIRE_TARGET_SOURCES_POST_UNDEFS})
  3271. if ("${COTIRE_ARGV1}" STREQUAL "unity")
  3272. if (XCODE)
  3273. # executing pre-build action under Xcode, check dependency on target script
  3274. set (_dependsOption DEPENDS "${COTIRE_ARGV2}")
  3275. else()
  3276. # executing custom command, no need to re-check for dependencies
  3277. set (_dependsOption "")
  3278. endif()
  3279. cotire_select_unity_source_files("${COTIRE_ARGV3}" _sources ${_sources})
  3280. cotire_generate_unity_source(
  3281. "${COTIRE_ARGV3}" ${_sources}
  3282. LANGUAGE "${COTIRE_TARGET_LANGUAGE}"
  3283. SOURCES_COMPILE_DEFINITIONS ${_sourcesDefinitions}
  3284. PRE_UNDEFS ${_targetPreUndefs}
  3285. POST_UNDEFS ${_targetPostUndefs}
  3286. SOURCES_PRE_UNDEFS ${_sourcesPreUndefs}
  3287. SOURCES_POST_UNDEFS ${_sourcesPostUndefs}
  3288. ${_dependsOption})
  3289. elseif ("${COTIRE_ARGV1}" STREQUAL "prefix")
  3290. if (XCODE)
  3291. # executing pre-build action under Xcode, check dependency on unity file and prefix dependencies
  3292. set (_dependsOption DEPENDS "${COTIRE_ARGV4}" ${COTIRE_TARGET_PREFIX_DEPENDS})
  3293. else()
  3294. # executing custom command, no need to re-check for dependencies
  3295. set (_dependsOption "")
  3296. endif()
  3297. set (_files "")
  3298. foreach (_index RANGE 4 ${COTIRE_ARGC})
  3299. if (COTIRE_ARGV${_index})
  3300. list (APPEND _files "${COTIRE_ARGV${_index}}")
  3301. endif()
  3302. endforeach()
  3303. cotire_generate_prefix_header(
  3304. "${COTIRE_ARGV3}" ${_files}
  3305. COMPILER_LAUNCHER "${COTIRE_TARGET_${COTIRE_TARGET_LANGUAGE}_COMPILER_LAUNCHER}"
  3306. COMPILER_EXECUTABLE "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER}"
  3307. COMPILER_ARG1 ${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ARG1}
  3308. COMPILER_ID "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ID}"
  3309. COMPILER_VERSION "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_VERSION}"
  3310. LANGUAGE "${COTIRE_TARGET_LANGUAGE}"
  3311. IGNORE_PATH "${COTIRE_TARGET_IGNORE_PATH};${COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_PATH}"
  3312. INCLUDE_PATH ${COTIRE_TARGET_INCLUDE_PATH}
  3313. IGNORE_EXTENSIONS "${CMAKE_${COTIRE_TARGET_LANGUAGE}_SOURCE_FILE_EXTENSIONS};${COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_EXTENSIONS}"
  3314. INCLUDE_PRIORITY_PATH ${COTIRE_TARGET_INCLUDE_PRIORITY_PATH}
  3315. INCLUDE_DIRECTORIES ${_includeDirs}
  3316. SYSTEM_INCLUDE_DIRECTORIES ${_systemIncludeDirs}
  3317. COMPILE_DEFINITIONS ${_compileDefinitions}
  3318. COMPILE_FLAGS ${_compileFlags}
  3319. ${_dependsOption})
  3320. elseif ("${COTIRE_ARGV1}" STREQUAL "precompile")
  3321. set (_files "")
  3322. foreach (_index RANGE 5 ${COTIRE_ARGC})
  3323. if (COTIRE_ARGV${_index})
  3324. list (APPEND _files "${COTIRE_ARGV${_index}}")
  3325. endif()
  3326. endforeach()
  3327. cotire_precompile_prefix_header(
  3328. "${COTIRE_ARGV3}" "${COTIRE_ARGV4}" "${COTIRE_ARGV5}"
  3329. COMPILER_LAUNCHER "${COTIRE_TARGET_${COTIRE_TARGET_LANGUAGE}_COMPILER_LAUNCHER}"
  3330. COMPILER_EXECUTABLE "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER}"
  3331. COMPILER_ARG1 ${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ARG1}
  3332. COMPILER_ID "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ID}"
  3333. COMPILER_VERSION "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_VERSION}"
  3334. LANGUAGE "${COTIRE_TARGET_LANGUAGE}"
  3335. INCLUDE_DIRECTORIES ${_includeDirs}
  3336. SYSTEM_INCLUDE_DIRECTORIES ${_systemIncludeDirs}
  3337. COMPILE_DEFINITIONS ${_compileDefinitions}
  3338. COMPILE_FLAGS ${_compileFlags})
  3339. elseif ("${COTIRE_ARGV1}" STREQUAL "combine")
  3340. if (COTIRE_TARGET_LANGUAGE)
  3341. set (_combinedFile "${COTIRE_ARGV3}")
  3342. set (_startIndex 4)
  3343. else()
  3344. set (_combinedFile "${COTIRE_ARGV2}")
  3345. set (_startIndex 3)
  3346. endif()
  3347. set (_files "")
  3348. foreach (_index RANGE ${_startIndex} ${COTIRE_ARGC})
  3349. if (COTIRE_ARGV${_index})
  3350. list (APPEND _files "${COTIRE_ARGV${_index}}")
  3351. endif()
  3352. endforeach()
  3353. if (XCODE)
  3354. # executing pre-build action under Xcode, check dependency on files to be combined
  3355. set (_dependsOption DEPENDS ${_files})
  3356. else()
  3357. # executing custom command, no need to re-check for dependencies
  3358. set (_dependsOption "")
  3359. endif()
  3360. if (COTIRE_TARGET_LANGUAGE)
  3361. cotire_generate_unity_source(
  3362. "${_combinedFile}" ${_files}
  3363. LANGUAGE "${COTIRE_TARGET_LANGUAGE}"
  3364. ${_dependsOption})
  3365. else()
  3366. cotire_generate_unity_source("${_combinedFile}" ${_files} ${_dependsOption})
  3367. endif()
  3368. elseif ("${COTIRE_ARGV1}" STREQUAL "cleanup")
  3369. cotire_cleanup("${COTIRE_ARGV2}" "${COTIRE_ARGV3}" "${COTIRE_ARGV4}")
  3370. else()
  3371. message (FATAL_ERROR "cotire: unknown command \"${COTIRE_ARGV1}\".")
  3372. endif()
  3373. else()
  3374. # cotire is being run in include mode
  3375. # set up all variable and property definitions
  3376. if (NOT DEFINED COTIRE_DEBUG_INIT)
  3377. if (DEFINED COTIRE_DEBUG)
  3378. set (COTIRE_DEBUG_INIT ${COTIRE_DEBUG})
  3379. else()
  3380. set (COTIRE_DEBUG_INIT FALSE)
  3381. endif()
  3382. endif()
  3383. option (COTIRE_DEBUG "Enable cotire debugging output?" ${COTIRE_DEBUG_INIT})
  3384. if (NOT DEFINED COTIRE_VERBOSE_INIT)
  3385. if (DEFINED COTIRE_VERBOSE)
  3386. set (COTIRE_VERBOSE_INIT ${COTIRE_VERBOSE})
  3387. else()
  3388. set (COTIRE_VERBOSE_INIT FALSE)
  3389. endif()
  3390. endif()
  3391. option (COTIRE_VERBOSE "Enable cotire verbose output?" ${COTIRE_VERBOSE_INIT})
  3392. set (COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_EXTENSIONS "inc;inl;ipp" CACHE STRING
  3393. "Ignore headers with the listed file extensions from the generated prefix header.")
  3394. set (COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_PATH "" CACHE STRING
  3395. "Ignore headers from these directories when generating the prefix header.")
  3396. set (COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS "m;mm" CACHE STRING
  3397. "Ignore sources with the listed file extensions from the generated unity source.")
  3398. set (COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES "3" CACHE STRING
  3399. "Minimum number of sources in target required to enable use of precompiled header.")
  3400. if (NOT DEFINED COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES_INIT)
  3401. if (DEFINED COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES)
  3402. set (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES_INIT ${COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES})
  3403. elseif ("${CMAKE_GENERATOR}" MATCHES "JOM|Ninja|Visual Studio")
  3404. # enable parallelization for generators that run multiple jobs by default
  3405. set (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES_INIT "-j")
  3406. else()
  3407. set (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES_INIT "0")
  3408. endif()
  3409. endif()
  3410. set (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES "${COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES_INIT}" CACHE STRING
  3411. "Maximum number of source files to include in a single unity source file.")
  3412. if (NOT COTIRE_PREFIX_HEADER_FILENAME_SUFFIX)
  3413. set (COTIRE_PREFIX_HEADER_FILENAME_SUFFIX "_prefix")
  3414. endif()
  3415. if (NOT COTIRE_UNITY_SOURCE_FILENAME_SUFFIX)
  3416. set (COTIRE_UNITY_SOURCE_FILENAME_SUFFIX "_unity")
  3417. endif()
  3418. if (NOT COTIRE_INTDIR)
  3419. set (COTIRE_INTDIR "cotire")
  3420. endif()
  3421. if (NOT COTIRE_PCH_ALL_TARGET_NAME)
  3422. set (COTIRE_PCH_ALL_TARGET_NAME "all_pch")
  3423. endif()
  3424. if (NOT COTIRE_UNITY_BUILD_ALL_TARGET_NAME)
  3425. set (COTIRE_UNITY_BUILD_ALL_TARGET_NAME "all_unity")
  3426. endif()
  3427. if (NOT COTIRE_CLEAN_ALL_TARGET_NAME)
  3428. set (COTIRE_CLEAN_ALL_TARGET_NAME "clean_cotire")
  3429. endif()
  3430. if (NOT COTIRE_CLEAN_TARGET_SUFFIX)
  3431. set (COTIRE_CLEAN_TARGET_SUFFIX "_clean_cotire")
  3432. endif()
  3433. if (NOT COTIRE_PCH_TARGET_SUFFIX)
  3434. set (COTIRE_PCH_TARGET_SUFFIX "_pch")
  3435. endif()
  3436. if (MSVC)
  3437. # MSVC default PCH memory scaling factor of 100 percent (75 MB) is too small for template heavy C++ code
  3438. # use a bigger default factor of 170 percent (128 MB)
  3439. if (NOT DEFINED COTIRE_PCH_MEMORY_SCALING_FACTOR)
  3440. set (COTIRE_PCH_MEMORY_SCALING_FACTOR "170")
  3441. endif()
  3442. endif()
  3443. if (NOT COTIRE_UNITY_BUILD_TARGET_SUFFIX)
  3444. set (COTIRE_UNITY_BUILD_TARGET_SUFFIX "_unity")
  3445. endif()
  3446. if (NOT DEFINED COTIRE_TARGETS_FOLDER)
  3447. set (COTIRE_TARGETS_FOLDER "cotire")
  3448. endif()
  3449. if (NOT DEFINED COTIRE_UNITY_OUTPUT_DIRECTORY)
  3450. if ("${CMAKE_GENERATOR}" MATCHES "Ninja")
  3451. # generated Ninja build files do not work if the unity target produces the same output file as the cotired target
  3452. set (COTIRE_UNITY_OUTPUT_DIRECTORY "unity")
  3453. else()
  3454. set (COTIRE_UNITY_OUTPUT_DIRECTORY "")
  3455. endif()
  3456. endif()
  3457. # define cotire cache variables
  3458. define_property(
  3459. CACHED_VARIABLE PROPERTY "COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_PATH"
  3460. BRIEF_DOCS "Ignore headers from these directories when generating the prefix header."
  3461. FULL_DOCS
  3462. "The variable can be set to a semicolon separated list of include directories."
  3463. "If a header file is found in one of these directories or sub-directories, it will be excluded from the generated prefix header."
  3464. "If not defined, defaults to empty list."
  3465. )
  3466. define_property(
  3467. CACHED_VARIABLE PROPERTY "COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_EXTENSIONS"
  3468. BRIEF_DOCS "Ignore includes with the listed file extensions from the generated prefix header."
  3469. FULL_DOCS
  3470. "The variable can be set to a semicolon separated list of file extensions."
  3471. "If a header file extension matches one in the list, it will be excluded from the generated prefix header."
  3472. "Includes with an extension in CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS are always ignored."
  3473. "If not defined, defaults to inc;inl;ipp."
  3474. )
  3475. define_property(
  3476. CACHED_VARIABLE PROPERTY "COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS"
  3477. BRIEF_DOCS "Exclude sources with the listed file extensions from the generated unity source."
  3478. FULL_DOCS
  3479. "The variable can be set to a semicolon separated list of file extensions."
  3480. "If a source file extension matches one in the list, it will be excluded from the generated unity source file."
  3481. "Source files with an extension in CMAKE_<LANG>_IGNORE_EXTENSIONS are always excluded."
  3482. "If not defined, defaults to m;mm."
  3483. )
  3484. define_property(
  3485. CACHED_VARIABLE PROPERTY "COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES"
  3486. BRIEF_DOCS "Minimum number of sources in target required to enable use of precompiled header."
  3487. FULL_DOCS
  3488. "The variable can be set to an integer > 0."
  3489. "If a target contains less than that number of source files, cotire will not enable the use of the precompiled header for the target."
  3490. "If not defined, defaults to 3."
  3491. )
  3492. define_property(
  3493. CACHED_VARIABLE PROPERTY "COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES"
  3494. BRIEF_DOCS "Maximum number of source files to include in a single unity source file."
  3495. FULL_DOCS
  3496. "This may be set to an integer >= 0."
  3497. "If 0, cotire will only create a single unity source file."
  3498. "If a target contains more than that number of source files, cotire will create multiple unity source files for it."
  3499. "Can be set to \"-j\" to optimize the count of unity source files for the number of available processor cores."
  3500. "Can be set to \"-j jobs\" to optimize the number of unity source files for the given number of simultaneous jobs."
  3501. "Is used to initialize the target property COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES."
  3502. "Defaults to \"-j\" for the generators Visual Studio, JOM or Ninja. Defaults to 0 otherwise."
  3503. )
  3504. # define cotire directory properties
  3505. define_property(
  3506. DIRECTORY PROPERTY "COTIRE_ENABLE_PRECOMPILED_HEADER"
  3507. BRIEF_DOCS "Modify build command of cotired targets added in this directory to make use of the generated precompiled header."
  3508. FULL_DOCS
  3509. "See target property COTIRE_ENABLE_PRECOMPILED_HEADER."
  3510. )
  3511. define_property(
  3512. DIRECTORY PROPERTY "COTIRE_ADD_UNITY_BUILD"
  3513. BRIEF_DOCS "Add a new target that performs a unity build for cotired targets added in this directory."
  3514. FULL_DOCS
  3515. "See target property COTIRE_ADD_UNITY_BUILD."
  3516. )
  3517. define_property(
  3518. DIRECTORY PROPERTY "COTIRE_ADD_CLEAN"
  3519. BRIEF_DOCS "Add a new target that cleans all cotire generated files for cotired targets added in this directory."
  3520. FULL_DOCS
  3521. "See target property COTIRE_ADD_CLEAN."
  3522. )
  3523. define_property(
  3524. DIRECTORY PROPERTY "COTIRE_PREFIX_HEADER_IGNORE_PATH"
  3525. BRIEF_DOCS "Ignore headers from these directories when generating the prefix header."
  3526. FULL_DOCS
  3527. "See target property COTIRE_PREFIX_HEADER_IGNORE_PATH."
  3528. )
  3529. define_property(
  3530. DIRECTORY PROPERTY "COTIRE_PREFIX_HEADER_INCLUDE_PATH"
  3531. BRIEF_DOCS "Honor headers from these directories when generating the prefix header."
  3532. FULL_DOCS
  3533. "See target property COTIRE_PREFIX_HEADER_INCLUDE_PATH."
  3534. )
  3535. define_property(
  3536. DIRECTORY PROPERTY "COTIRE_PREFIX_HEADER_INCLUDE_PRIORITY_PATH"
  3537. BRIEF_DOCS "Header paths matching one of these directories are put at the top of the prefix header."
  3538. FULL_DOCS
  3539. "See target property COTIRE_PREFIX_HEADER_INCLUDE_PRIORITY_PATH."
  3540. )
  3541. define_property(
  3542. DIRECTORY PROPERTY "COTIRE_UNITY_SOURCE_PRE_UNDEFS"
  3543. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file before the inclusion of each source file."
  3544. FULL_DOCS
  3545. "See target property COTIRE_UNITY_SOURCE_PRE_UNDEFS."
  3546. )
  3547. define_property(
  3548. DIRECTORY PROPERTY "COTIRE_UNITY_SOURCE_POST_UNDEFS"
  3549. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file after the inclusion of each source file."
  3550. FULL_DOCS
  3551. "See target property COTIRE_UNITY_SOURCE_POST_UNDEFS."
  3552. )
  3553. define_property(
  3554. DIRECTORY PROPERTY "COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES"
  3555. BRIEF_DOCS "Maximum number of source files to include in a single unity source file."
  3556. FULL_DOCS
  3557. "See target property COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES."
  3558. )
  3559. define_property(
  3560. DIRECTORY PROPERTY "COTIRE_UNITY_LINK_LIBRARIES_INIT"
  3561. BRIEF_DOCS "Define strategy for setting up the unity target's link libraries."
  3562. FULL_DOCS
  3563. "See target property COTIRE_UNITY_LINK_LIBRARIES_INIT."
  3564. )
  3565. # define cotire target properties
  3566. define_property(
  3567. TARGET PROPERTY "COTIRE_ENABLE_PRECOMPILED_HEADER" INHERITED
  3568. BRIEF_DOCS "Modify this target's build command to make use of the generated precompiled header."
  3569. FULL_DOCS
  3570. "If this property is set to TRUE, cotire will modify the build command to make use of the generated precompiled header."
  3571. "Irrespective of the value of this property, cotire will setup custom commands to generate the unity source and prefix header for the target."
  3572. "For makefile based generators cotire will also set up a custom target to manually invoke the generation of the precompiled header."
  3573. "The target name will be set to this target's name with the suffix _pch appended."
  3574. "Inherited from directory."
  3575. "Defaults to TRUE."
  3576. )
  3577. define_property(
  3578. TARGET PROPERTY "COTIRE_ADD_UNITY_BUILD" INHERITED
  3579. BRIEF_DOCS "Add a new target that performs a unity build for this target."
  3580. FULL_DOCS
  3581. "If this property is set to TRUE, cotire creates a new target of the same type that uses the generated unity source file instead of the target sources."
  3582. "Most of the relevant target properties will be copied from this target to the new unity build target."
  3583. "Target dependencies and linked libraries have to be manually set up for the new unity build target."
  3584. "The unity target name will be set to this target's name with the suffix _unity appended."
  3585. "Inherited from directory."
  3586. "Defaults to TRUE."
  3587. )
  3588. define_property(
  3589. TARGET PROPERTY "COTIRE_ADD_CLEAN" INHERITED
  3590. BRIEF_DOCS "Add a new target that cleans all cotire generated files for this target."
  3591. FULL_DOCS
  3592. "If this property is set to TRUE, cotire creates a new target that clean all files (unity source, prefix header, precompiled header)."
  3593. "The clean target name will be set to this target's name with the suffix _clean_cotire appended."
  3594. "Inherited from directory."
  3595. "Defaults to FALSE."
  3596. )
  3597. define_property(
  3598. TARGET PROPERTY "COTIRE_PREFIX_HEADER_IGNORE_PATH" INHERITED
  3599. BRIEF_DOCS "Ignore headers from these directories when generating the prefix header."
  3600. FULL_DOCS
  3601. "The property can be set to a list of directories."
  3602. "If a header file is found in one of these directories or sub-directories, it will be excluded from the generated prefix header."
  3603. "Inherited from directory."
  3604. "If not set, this property is initialized to \${CMAKE_SOURCE_DIR};\${CMAKE_BINARY_DIR}."
  3605. )
  3606. define_property(
  3607. TARGET PROPERTY "COTIRE_PREFIX_HEADER_INCLUDE_PATH" INHERITED
  3608. BRIEF_DOCS "Honor headers from these directories when generating the prefix header."
  3609. FULL_DOCS
  3610. "The property can be set to a list of directories."
  3611. "If a header file is found in one of these directories or sub-directories, it will be included in the generated prefix header."
  3612. "If a header file is both selected by COTIRE_PREFIX_HEADER_IGNORE_PATH and COTIRE_PREFIX_HEADER_INCLUDE_PATH,"
  3613. "the option which yields the closer relative path match wins."
  3614. "Inherited from directory."
  3615. "If not set, this property is initialized to the empty list."
  3616. )
  3617. define_property(
  3618. TARGET PROPERTY "COTIRE_PREFIX_HEADER_INCLUDE_PRIORITY_PATH" INHERITED
  3619. BRIEF_DOCS "Header paths matching one of these directories are put at the top of prefix header."
  3620. FULL_DOCS
  3621. "The property can be set to a list of directories."
  3622. "Header file paths matching one of these directories will be inserted at the beginning of the generated prefix header."
  3623. "Header files are sorted according to the order of the directories in the property."
  3624. "If not set, this property is initialized to the empty list."
  3625. )
  3626. define_property(
  3627. TARGET PROPERTY "COTIRE_UNITY_SOURCE_PRE_UNDEFS" INHERITED
  3628. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file before the inclusion of each target source file."
  3629. FULL_DOCS
  3630. "This may be set to a semicolon-separated list of preprocessor symbols."
  3631. "cotire will add corresponding #undef directives to the generated unit source file before each target source file."
  3632. "Inherited from directory."
  3633. "Defaults to empty string."
  3634. )
  3635. define_property(
  3636. TARGET PROPERTY "COTIRE_UNITY_SOURCE_POST_UNDEFS" INHERITED
  3637. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file after the inclusion of each target source file."
  3638. FULL_DOCS
  3639. "This may be set to a semicolon-separated list of preprocessor symbols."
  3640. "cotire will add corresponding #undef directives to the generated unit source file after each target source file."
  3641. "Inherited from directory."
  3642. "Defaults to empty string."
  3643. )
  3644. define_property(
  3645. TARGET PROPERTY "COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES" INHERITED
  3646. BRIEF_DOCS "Maximum number of source files to include in a single unity source file."
  3647. FULL_DOCS
  3648. "This may be set to an integer > 0."
  3649. "If a target contains more than that number of source files, cotire will create multiple unity build files for it."
  3650. "If not set, cotire will only create a single unity source file."
  3651. "Inherited from directory."
  3652. "Defaults to empty."
  3653. )
  3654. define_property(
  3655. TARGET PROPERTY "COTIRE_<LANG>_UNITY_SOURCE_INIT"
  3656. BRIEF_DOCS "User provided unity source file to be used instead of the automatically generated one."
  3657. FULL_DOCS
  3658. "If set, cotire will only add the given file(s) to the generated unity source file."
  3659. "If not set, cotire will add all the target source files to the generated unity source file."
  3660. "The property can be set to a user provided unity source file."
  3661. "Defaults to empty."
  3662. )
  3663. define_property(
  3664. TARGET PROPERTY "COTIRE_<LANG>_PREFIX_HEADER_INIT"
  3665. BRIEF_DOCS "User provided prefix header file to be used instead of the automatically generated one."
  3666. FULL_DOCS
  3667. "If set, cotire will add the given header file(s) to the generated prefix header file."
  3668. "If not set, cotire will generate a prefix header by tracking the header files included by the unity source file."
  3669. "The property can be set to a user provided prefix header file (e.g., stdafx.h)."
  3670. "Defaults to empty."
  3671. )
  3672. define_property(
  3673. TARGET PROPERTY "COTIRE_UNITY_LINK_LIBRARIES_INIT" INHERITED
  3674. BRIEF_DOCS "Define strategy for setting up unity target's link libraries."
  3675. FULL_DOCS
  3676. "If this property is empty or set to NONE, the generated unity target's link libraries have to be set up manually."
  3677. "If this property is set to COPY, the unity target's link libraries will be copied from this target."
  3678. "If this property is set to COPY_UNITY, the unity target's link libraries will be copied from this target with considering existing unity targets."
  3679. "Inherited from directory."
  3680. "Defaults to empty."
  3681. )
  3682. define_property(
  3683. TARGET PROPERTY "COTIRE_<LANG>_UNITY_SOURCE"
  3684. BRIEF_DOCS "Read-only property. The generated <LANG> unity source file(s)."
  3685. FULL_DOCS
  3686. "cotire sets this property to the path of the generated <LANG> single computation unit source file for the target."
  3687. "Defaults to empty string."
  3688. )
  3689. define_property(
  3690. TARGET PROPERTY "COTIRE_<LANG>_PREFIX_HEADER"
  3691. BRIEF_DOCS "Read-only property. The generated <LANG> prefix header file."
  3692. FULL_DOCS
  3693. "cotire sets this property to the full path of the generated <LANG> language prefix header for the target."
  3694. "Defaults to empty string."
  3695. )
  3696. define_property(
  3697. TARGET PROPERTY "COTIRE_<LANG>_PRECOMPILED_HEADER"
  3698. BRIEF_DOCS "Read-only property. The generated <LANG> precompiled header file."
  3699. FULL_DOCS
  3700. "cotire sets this property to the full path of the generated <LANG> language precompiled header binary for the target."
  3701. "Defaults to empty string."
  3702. )
  3703. define_property(
  3704. TARGET PROPERTY "COTIRE_UNITY_TARGET_NAME"
  3705. BRIEF_DOCS "The name of the generated unity build target corresponding to this target."
  3706. FULL_DOCS
  3707. "This property can be set to the desired name of the unity target that will be created by cotire."
  3708. "If not set, the unity target name will be set to this target's name with the suffix _unity appended."
  3709. "After this target has been processed by cotire, the property is set to the actual name of the generated unity target."
  3710. "Defaults to empty string."
  3711. )
  3712. # define cotire source properties
  3713. define_property(
  3714. SOURCE PROPERTY "COTIRE_EXCLUDED"
  3715. BRIEF_DOCS "Do not modify source file's build command."
  3716. FULL_DOCS
  3717. "If this property is set to TRUE, the source file's build command will not be modified to make use of the precompiled header."
  3718. "The source file will also be excluded from the generated unity source file."
  3719. "Source files that have their COMPILE_FLAGS property set will be excluded by default."
  3720. "Defaults to FALSE."
  3721. )
  3722. define_property(
  3723. SOURCE PROPERTY "COTIRE_DEPENDENCY"
  3724. BRIEF_DOCS "Add this source file to dependencies of the automatically generated prefix header file."
  3725. FULL_DOCS
  3726. "If this property is set to TRUE, the source file is added to dependencies of the generated prefix header file."
  3727. "If the file is modified, cotire will re-generate the prefix header source upon build."
  3728. "Defaults to FALSE."
  3729. )
  3730. define_property(
  3731. SOURCE PROPERTY "COTIRE_UNITY_SOURCE_PRE_UNDEFS"
  3732. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file before the inclusion of this source file."
  3733. FULL_DOCS
  3734. "This may be set to a semicolon-separated list of preprocessor symbols."
  3735. "cotire will add corresponding #undef directives to the generated unit source file before this file is included."
  3736. "Defaults to empty string."
  3737. )
  3738. define_property(
  3739. SOURCE PROPERTY "COTIRE_UNITY_SOURCE_POST_UNDEFS"
  3740. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file after the inclusion of this source file."
  3741. FULL_DOCS
  3742. "This may be set to a semicolon-separated list of preprocessor symbols."
  3743. "cotire will add corresponding #undef directives to the generated unit source file after this file is included."
  3744. "Defaults to empty string."
  3745. )
  3746. define_property(
  3747. SOURCE PROPERTY "COTIRE_START_NEW_UNITY_SOURCE"
  3748. BRIEF_DOCS "Start a new unity source file which includes this source file as the first one."
  3749. FULL_DOCS
  3750. "If this property is set to TRUE, cotire will complete the current unity file and start a new one."
  3751. "The new unity source file will include this source file as the first one."
  3752. "This property essentially works as a separator for unity source files."
  3753. "Defaults to FALSE."
  3754. )
  3755. define_property(
  3756. SOURCE PROPERTY "COTIRE_TARGET"
  3757. BRIEF_DOCS "Read-only property. Mark this source file as cotired for the given target."
  3758. FULL_DOCS
  3759. "cotire sets this property to the name of target, that the source file's build command has been altered for."
  3760. "Defaults to empty string."
  3761. )
  3762. message (STATUS "cotire ${COTIRE_CMAKE_MODULE_VERSION} loaded.")
  3763. endif()