Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

4174 linhas
170 KiB

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