You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3109 lines
127 KiB

2 months ago
  1. # - cotire (compile time reducer)
  2. #
  3. # See the cotire manual for usage hints.
  4. #
  5. #=============================================================================
  6. # Copyright 2012-2013 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. # we need the CMake variables CMAKE_SCRIPT_MODE_FILE and CMAKE_ARGV available since 2.8.5
  39. # we need APPEND_STRING option for set_property available since 2.8.6
  40. cmake_minimum_required(VERSION 2.8.6)
  41. if (NOT CMAKE_SCRIPT_MODE_FILE)
  42. cmake_policy(POP)
  43. endif()
  44. set (COTIRE_CMAKE_MODULE_FILE "cotire.cmake")
  45. set (COTIRE_CMAKE_MODULE_VERSION "1.3.4")
  46. include(CMakeParseArguments)
  47. function (cotire_determine_compiler_version _language _versionPrefix)
  48. if (NOT ${_versionPrefix}_VERSION)
  49. if (MSVC)
  50. # use CMake's predefined version variable for MSVC, if available
  51. if (DEFINED MSVC_VERSION)
  52. set (${_versionPrefix}_VERSION "${MSVC_VERSION}")
  53. else()
  54. # cl.exe messes with the output streams unless the environment variable VS_UNICODE_OUTPUT is cleared
  55. unset (ENV{VS_UNICODE_OUTPUT})
  56. string (STRIP "${CMAKE_${_language}_COMPILER_ARG1}" _compilerArg1)
  57. execute_process (COMMAND ${CMAKE_${_language}_COMPILER} ${_compilerArg1}
  58. ERROR_VARIABLE _versionLine OUTPUT_QUIET TIMEOUT 10)
  59. string (REGEX REPLACE ".*Version *([0-9]+(\\.[0-9]+)*).*" "\\1"
  60. ${_versionPrefix}_VERSION "${_versionLine}")
  61. endif()
  62. else()
  63. # use CMake's predefined compiler version variable (available since CMake 2.8.8)
  64. if (DEFINED CMAKE_${_language}_COMPILER_VERSION)
  65. set (${_versionPrefix}_VERSION "${CMAKE_${_language}_COMPILER_VERSION}")
  66. else()
  67. # assume GCC like command line interface
  68. string (STRIP "${CMAKE_${_language}_COMPILER_ARG1}" _compilerArg1)
  69. execute_process (COMMAND ${CMAKE_${_language}_COMPILER} ${_compilerArg1} "-dumpversion"
  70. OUTPUT_VARIABLE ${_versionPrefix}_VERSION
  71. OUTPUT_STRIP_TRAILING_WHITESPACE TIMEOUT 10)
  72. endif()
  73. endif()
  74. if (${_versionPrefix}_VERSION)
  75. set (${_versionPrefix}_VERSION "${${_versionPrefix}_VERSION}" CACHE INTERNAL "${_language} compiler version")
  76. endif()
  77. set (${_versionPrefix}_VERSION "${${_versionPrefix}_VERSION}" PARENT_SCOPE)
  78. if (COTIRE_DEBUG)
  79. message (STATUS "${CMAKE_${_language}_COMPILER} version ${${_versionPrefix}_VERSION}")
  80. endif()
  81. endif()
  82. endfunction()
  83. function (cotire_get_source_file_extension _sourceFile _extVar)
  84. # get_filename_component returns extension from first occurrence of . in file name
  85. # this function computes the extension from last occurrence of . in file name
  86. string (FIND "${_sourceFile}" ".." _index REVERSE)
  87. if (_index GREATER -1)
  88. math (EXPR _index "${_index} + 1")
  89. string (SUBSTRING "${_sourceFile}" ${_index} -1 _sourceExt)
  90. else()
  91. set (_sourceExt "")
  92. endif()
  93. set (${_extVar} "${_sourceExt}" PARENT_SCOPE)
  94. endfunction()
  95. macro (cotire_check_is_path_relative_to _path _isRelativeVar)
  96. set (${_isRelativeVar} FALSE)
  97. if (IS_ABSOLUTE "${_path}")
  98. foreach (_dir ${ARGN})
  99. file (RELATIVE_PATH _relPath "${_dir}" "${_path}")
  100. if (NOT _relPath OR (NOT IS_ABSOLUTE "${_relPath}" AND NOT "${_relPath}" MATCHES "^\\.\\."))
  101. set (${_isRelativeVar} TRUE)
  102. break()
  103. endif()
  104. endforeach()
  105. endif()
  106. endmacro()
  107. function (cotire_filter_language_source_files _language _sourceFilesVar _excludedSourceFilesVar _cotiredSourceFilesVar)
  108. set (_sourceFiles "")
  109. set (_excludedSourceFiles "")
  110. set (_cotiredSourceFiles "")
  111. if (CMAKE_${_language}_SOURCE_FILE_EXTENSIONS)
  112. set (_languageExtensions "${CMAKE_${_language}_SOURCE_FILE_EXTENSIONS}")
  113. else()
  114. set (_languageExtensions "")
  115. endif()
  116. if (CMAKE_${_language}_IGNORE_EXTENSIONS)
  117. set (_ignoreExtensions "${CMAKE_${_language}_IGNORE_EXTENSIONS}")
  118. else()
  119. set (_ignoreExtensions "")
  120. endif()
  121. if (COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS)
  122. set (_excludeExtensions "${COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS}")
  123. else()
  124. set (_excludeExtensions "")
  125. endif()
  126. if (COTIRE_DEBUG)
  127. message (STATUS "${_language} source file extensions: ${_languageExtensions}")
  128. message (STATUS "${_language} ignore extensions: ${_ignoreExtensions}")
  129. message (STATUS "${_language} exclude extensions: ${_excludeExtensions}")
  130. endif()
  131. foreach (_sourceFile ${ARGN})
  132. get_source_file_property(_sourceIsHeaderOnly "${_sourceFile}" HEADER_FILE_ONLY)
  133. get_source_file_property(_sourceIsExternal "${_sourceFile}" EXTERNAL_OBJECT)
  134. get_source_file_property(_sourceIsSymbolic "${_sourceFile}" SYMBOLIC)
  135. get_source_file_property(_sourceLanguage "${_sourceFile}" LANGUAGE)
  136. set (_sourceIsFiltered FALSE)
  137. if (NOT _sourceIsHeaderOnly AND NOT _sourceIsExternal AND NOT _sourceIsSymbolic)
  138. cotire_get_source_file_extension("${_sourceFile}" _sourceExt)
  139. if (_sourceExt)
  140. list (FIND _ignoreExtensions "${_sourceExt}" _ignoreIndex)
  141. if (_ignoreIndex LESS 0)
  142. list (FIND _excludeExtensions "${_sourceExt}" _excludeIndex)
  143. if (_excludeIndex GREATER -1)
  144. list (APPEND _excludedSourceFiles "${_sourceFile}")
  145. else()
  146. list (FIND _languageExtensions "${_sourceExt}" _sourceIndex)
  147. if (_sourceIndex GREATER -1)
  148. set (_sourceIsFiltered TRUE)
  149. elseif ("${_sourceLanguage}" STREQUAL "${_language}")
  150. # add to excluded sources, if file is not ignored and has correct language without having the correct extension
  151. list (APPEND _excludedSourceFiles "${_sourceFile}")
  152. endif()
  153. endif()
  154. endif()
  155. endif()
  156. endif()
  157. if (COTIRE_DEBUG)
  158. message (STATUS "${_sourceFile} filtered=${_sourceIsFiltered} language=${_sourceLanguage} header=${_sourceIsHeaderOnly}")
  159. endif()
  160. if (_sourceIsFiltered)
  161. get_source_file_property(_sourceIsExcluded "${_sourceFile}" COTIRE_EXCLUDED)
  162. get_source_file_property(_sourceIsCotired "${_sourceFile}" COTIRE_TARGET)
  163. get_source_file_property(_sourceCompileFlags "${_sourceFile}" COMPILE_FLAGS)
  164. if (COTIRE_DEBUG)
  165. message (STATUS "${_sourceFile} excluded=${_sourceIsExcluded} cotired=${_sourceIsCotired}")
  166. endif()
  167. if (_sourceIsCotired)
  168. list (APPEND _cotiredSourceFiles "${_sourceFile}")
  169. elseif (_sourceIsExcluded OR _sourceCompileFlags)
  170. list (APPEND _excludedSourceFiles "${_sourceFile}")
  171. else()
  172. list (APPEND _sourceFiles "${_sourceFile}")
  173. endif()
  174. endif()
  175. endforeach()
  176. if (COTIRE_DEBUG)
  177. message (STATUS "All: ${ARGN}")
  178. message (STATUS "${_language}: ${_sourceFiles}")
  179. message (STATUS "Excluded: ${_excludedSourceFiles}")
  180. message (STATUS "Cotired: ${_cotiredSourceFiles}")
  181. endif()
  182. set (${_sourceFilesVar} ${_sourceFiles} PARENT_SCOPE)
  183. set (${_excludedSourceFilesVar} ${_excludedSourceFiles} PARENT_SCOPE)
  184. set (${_cotiredSourceFilesVar} ${_cotiredSourceFiles} PARENT_SCOPE)
  185. endfunction()
  186. function (cotire_get_objects_with_property_on _filteredObjectsVar _property _type)
  187. set (_filteredObjects "")
  188. foreach (_object ${ARGN})
  189. get_property(_isSet ${_type} "${_object}" PROPERTY ${_property} SET)
  190. if (_isSet)
  191. get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
  192. if (_propertyValue)
  193. list (APPEND _filteredObjects "${_object}")
  194. endif()
  195. endif()
  196. endforeach()
  197. set (${_filteredObjectsVar} ${_filteredObjects} PARENT_SCOPE)
  198. endfunction()
  199. function (cotire_get_objects_with_property_off _filteredObjectsVar _property _type)
  200. set (_filteredObjects "")
  201. foreach (_object ${ARGN})
  202. get_property(_isSet ${_type} "${_object}" PROPERTY ${_property} SET)
  203. if (_isSet)
  204. get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
  205. if (NOT _propertyValue)
  206. list (APPEND _filteredObjects "${_object}")
  207. endif()
  208. endif()
  209. endforeach()
  210. set (${_filteredObjectsVar} ${_filteredObjects} PARENT_SCOPE)
  211. endfunction()
  212. function (cotire_get_source_file_property_values _valuesVar _property)
  213. set (_values "")
  214. foreach (_sourceFile ${ARGN})
  215. get_source_file_property(_propertyValue "${_sourceFile}" ${_property})
  216. if (_propertyValue)
  217. list (APPEND _values "${_propertyValue}")
  218. endif()
  219. endforeach()
  220. set (${_valuesVar} ${_values} PARENT_SCOPE)
  221. endfunction()
  222. function (cotrie_resolve_config_properites _configurations _propertiesVar)
  223. set (_properties "")
  224. foreach (_property ${ARGN})
  225. if ("${_property}" MATCHES "<CONFIG>")
  226. foreach (_config ${_configurations})
  227. string (TOUPPER "${_config}" _upperConfig)
  228. string (REPLACE "<CONFIG>" "${_upperConfig}" _configProperty "${_property}")
  229. list (APPEND _properties ${_configProperty})
  230. endforeach()
  231. else()
  232. list (APPEND _properties ${_property})
  233. endif()
  234. endforeach()
  235. set (${_propertiesVar} ${_properties} PARENT_SCOPE)
  236. endfunction()
  237. function (cotrie_copy_set_properites _configurations _type _source _target)
  238. cotrie_resolve_config_properites("${_configurations}" _properties ${ARGN})
  239. foreach (_property ${_properties})
  240. get_property(_isSet ${_type} ${_source} PROPERTY ${_property} SET)
  241. if (_isSet)
  242. get_property(_propertyValue ${_type} ${_source} PROPERTY ${_property})
  243. set_property(${_type} ${_target} PROPERTY ${_property} "${_propertyValue}")
  244. endif()
  245. endforeach()
  246. endfunction()
  247. function (cotire_filter_compile_flags _language _flagFilter _matchedOptionsVar _unmatchedOptionsVar)
  248. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  249. set (_flagPrefix "[/-]")
  250. else()
  251. set (_flagPrefix "--?")
  252. endif()
  253. set (_optionFlag "")
  254. set (_matchedOptions "")
  255. set (_unmatchedOptions "")
  256. foreach (_compileFlag ${ARGN})
  257. if (_compileFlag)
  258. if (_optionFlag AND NOT "${_compileFlag}" MATCHES "^${_flagPrefix}")
  259. # option with separate argument
  260. list (APPEND _matchedOptions "${_compileFlag}")
  261. set (_optionFlag "")
  262. elseif ("${_compileFlag}" MATCHES "^(${_flagPrefix})(${_flagFilter})$")
  263. # remember option
  264. set (_optionFlag "${CMAKE_MATCH_2}")
  265. elseif ("${_compileFlag}" MATCHES "^(${_flagPrefix})(${_flagFilter})(.+)$")
  266. # option with joined argument
  267. list (APPEND _matchedOptions "${CMAKE_MATCH_3}")
  268. set (_optionFlag "")
  269. else()
  270. # flush remembered option
  271. if (_optionFlag)
  272. list (APPEND _matchedOptions "${_optionFlag}")
  273. set (_optionFlag "")
  274. endif()
  275. # add to unfiltered options
  276. list (APPEND _unmatchedOptions "${_compileFlag}")
  277. endif()
  278. endif()
  279. endforeach()
  280. if (_optionFlag)
  281. list (APPEND _matchedOptions "${_optionFlag}")
  282. endif()
  283. if (COTIRE_DEBUG)
  284. message (STATUS "Filter ${_flagFilter}")
  285. if (_matchedOptions)
  286. message (STATUS "Matched ${_matchedOptions}")
  287. endif()
  288. if (_unmatchedOptions)
  289. message (STATUS "Unmatched ${_unmatchedOptions}")
  290. endif()
  291. endif()
  292. set (${_matchedOptionsVar} ${_matchedOptions} PARENT_SCOPE)
  293. set (${_unmatchedOptionsVar} ${_unmatchedOptions} PARENT_SCOPE)
  294. endfunction()
  295. function (cotire_get_target_compile_flags _config _language _directory _target _flagsVar)
  296. string (TOUPPER "${_config}" _upperConfig)
  297. # collect options from CMake language variables
  298. set (_compileFlags "")
  299. if (CMAKE_${_language}_FLAGS)
  300. set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_FLAGS}")
  301. endif()
  302. if (CMAKE_${_language}_FLAGS_${_upperConfig})
  303. set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_FLAGS_${_upperConfig}}")
  304. endif()
  305. if (_target)
  306. # add option from CMake target type variable
  307. get_target_property(_targetType ${_target} TYPE)
  308. if (POLICY CMP0018)
  309. # handle POSITION_INDEPENDENT_CODE property introduced with CMake 2.8.9 if policy CMP0018 is turned on
  310. cmake_policy(GET CMP0018 _PIC_Policy)
  311. else()
  312. # default to old behavior
  313. set (_PIC_Policy "OLD")
  314. endif()
  315. if (COTIRE_DEBUG)
  316. message(STATUS "CMP0018=${_PIC_Policy}")
  317. endif()
  318. if (_PIC_Policy STREQUAL "NEW")
  319. # NEW behavior: honor the POSITION_INDEPENDENT_CODE target property
  320. get_target_property(_targetPIC ${_target} POSITION_INDEPENDENT_CODE)
  321. if (_targetPIC)
  322. if (_targetType STREQUAL "EXECUTABLE" AND CMAKE_${_language}_COMPILE_OPTIONS_PIE)
  323. set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_COMPILE_OPTIONS_PIE}")
  324. elseif (CMAKE_${_language}_COMPILE_OPTIONS_PIC)
  325. set (_compileFlags "${_compileFlags} ${CMAKE_${_language}_COMPILE_OPTIONS_PIC}")
  326. endif()
  327. endif()
  328. else()
  329. # OLD behavior or policy not set: use the value of CMAKE_SHARED_LIBRARY_<Lang>_FLAGS
  330. if (_targetType STREQUAL "MODULE_LIBRARY")
  331. # flags variable for module library uses different name SHARED_MODULE
  332. # (e.g., CMAKE_SHARED_MODULE_C_FLAGS)
  333. set (_targetType SHARED_MODULE)
  334. endif()
  335. if (CMAKE_${_targetType}_${_language}_FLAGS)
  336. set (_compileFlags "${_compileFlags} ${CMAKE_${_targetType}_${_language}_FLAGS}")
  337. endif()
  338. endif()
  339. endif()
  340. if (_directory)
  341. # add_definitions may have been used to add flags to the compiler command
  342. get_directory_property(_dirDefinitions DIRECTORY "${_directory}" DEFINITIONS)
  343. if (_dirDefinitions)
  344. set (_compileFlags "${_compileFlags} ${_dirDefinitions}")
  345. endif()
  346. endif()
  347. if (_target)
  348. # add target compile options
  349. get_target_property(_targetflags ${_target} COMPILE_FLAGS)
  350. if (_targetflags)
  351. set (_compileFlags "${_compileFlags} ${_targetflags}")
  352. endif()
  353. endif()
  354. if (UNIX)
  355. separate_arguments(_compileFlags UNIX_COMMAND "${_compileFlags}")
  356. elseif(WIN32)
  357. separate_arguments(_compileFlags WINDOWS_COMMAND "${_compileFlags}")
  358. else()
  359. separate_arguments(_compileFlags)
  360. endif()
  361. # platform specific flags
  362. if (APPLE)
  363. get_target_property(_architectures ${_target} OSX_ARCHITECTURES_${_upperConfig})
  364. if (NOT _architectures)
  365. get_target_property(_architectures ${_target} OSX_ARCHITECTURES)
  366. endif()
  367. foreach (_arch ${_architectures})
  368. list (APPEND _compileFlags "-arch" "${_arch}")
  369. endforeach()
  370. if (CMAKE_OSX_SYSROOT AND CMAKE_OSX_SYSROOT_DEFAULT AND CMAKE_${_language}_HAS_ISYSROOT)
  371. if (NOT "${CMAKE_OSX_SYSROOT}" STREQUAL "${CMAKE_OSX_SYSROOT_DEFAULT}")
  372. list (APPEND _compileFlags "-isysroot" "${CMAKE_OSX_SYSROOT}")
  373. endif()
  374. endif()
  375. if (CMAKE_OSX_DEPLOYMENT_TARGET AND CMAKE_${_language}_OSX_DEPLOYMENT_TARGET_FLAG)
  376. list (APPEND _compileFlags "${CMAKE_${_language}_OSX_DEPLOYMENT_TARGET_FLAG}${CMAKE_OSX_DEPLOYMENT_TARGET}")
  377. endif()
  378. endif()
  379. if (COTIRE_DEBUG AND _compileFlags)
  380. message (STATUS "Target ${_target} compile flags ${_compileFlags}")
  381. endif()
  382. set (${_flagsVar} ${_compileFlags} PARENT_SCOPE)
  383. endfunction()
  384. function (cotire_get_target_include_directories _config _language _targetSourceDir _targetBinaryDir _target _includeDirsVar)
  385. set (_includeDirs "")
  386. # default include dirs
  387. if (CMAKE_INCLUDE_CURRENT_DIR)
  388. list (APPEND _includeDirs "${_targetBinaryDir}")
  389. list (APPEND _includeDirs "${_targetSourceDir}")
  390. endif()
  391. # parse additional include directories from target compile flags
  392. set (_targetFlags "")
  393. cotire_get_target_compile_flags("${_config}" "${_language}" "${_targetSourceDir}" "${_target}" _targetFlags)
  394. cotire_filter_compile_flags("${_language}" "I" _dirs _ignore ${_targetFlags})
  395. if (_dirs)
  396. list (APPEND _includeDirs ${_dirs})
  397. endif()
  398. # target include directories
  399. get_directory_property(_dirs DIRECTORY "${_targetSourceDir}" INCLUDE_DIRECTORIES)
  400. if (_target)
  401. get_target_property(_targetDirs ${_target} INCLUDE_DIRECTORIES)
  402. if (_targetDirs)
  403. list (APPEND _dirs ${_targetDirs})
  404. list (REMOVE_DUPLICATES _dirs)
  405. endif()
  406. endif()
  407. list (LENGTH _includeDirs _projectInsertIndex)
  408. foreach (_dir ${_dirs})
  409. if (CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE)
  410. cotire_check_is_path_relative_to("${_dir}" _isRelative "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}")
  411. if (_isRelative)
  412. list (LENGTH _includeDirs _len)
  413. if (_len EQUAL _projectInsertIndex)
  414. list (APPEND _includeDirs "${_dir}")
  415. else()
  416. list (INSERT _includeDirs _projectInsertIndex "${_dir}")
  417. endif()
  418. math (EXPR _projectInsertIndex "${_projectInsertIndex} + 1")
  419. else()
  420. list (APPEND _includeDirs "${_dir}")
  421. endif()
  422. else()
  423. list (APPEND _includeDirs "${_dir}")
  424. endif()
  425. endforeach()
  426. list (REMOVE_DUPLICATES _includeDirs)
  427. if (CMAKE_${_language}_IMPLICIT_INCLUDE_DIRECTORIES)
  428. list (REMOVE_ITEM _includeDirs ${CMAKE_${_language}_IMPLICIT_INCLUDE_DIRECTORIES})
  429. endif()
  430. if (COTIRE_DEBUG AND _includeDirs)
  431. message (STATUS "Target ${_target} include dirs ${_includeDirs}")
  432. endif()
  433. set (${_includeDirsVar} ${_includeDirs} PARENT_SCOPE)
  434. endfunction()
  435. macro (cotire_make_C_identifier _identifierVar _str)
  436. # mimic CMake SystemTools::MakeCindentifier behavior
  437. if ("${_str}" MATCHES "^[0-9].+$")
  438. set (_str "_${str}")
  439. endif()
  440. string (REGEX REPLACE "[^a-zA-Z0-9]" "_" ${_identifierVar} "${_str}")
  441. endmacro()
  442. function (cotire_get_target_export_symbol _target _exportSymbolVar)
  443. set (_exportSymbol "")
  444. get_target_property(_targetType ${_target} TYPE)
  445. get_target_property(_enableExports ${_target} ENABLE_EXPORTS)
  446. if (_targetType MATCHES "(SHARED|MODULE)_LIBRARY" OR
  447. (_targetType STREQUAL "EXECUTABLE" AND _enableExports))
  448. get_target_property(_exportSymbol ${_target} DEFINE_SYMBOL)
  449. if (NOT _exportSymbol)
  450. set (_exportSymbol "${_target}_EXPORTS")
  451. endif()
  452. cotire_make_C_identifier(_exportSymbol "${_exportSymbol}")
  453. endif()
  454. set (${_exportSymbolVar} ${_exportSymbol} PARENT_SCOPE)
  455. endfunction()
  456. function (cotire_get_target_compile_definitions _config _language _directory _target _definitionsVar)
  457. string (TOUPPER "${_config}" _upperConfig)
  458. set (_configDefinitions "")
  459. # CMAKE_INTDIR for multi-configuration build systems
  460. if (NOT "${CMAKE_CFG_INTDIR}" STREQUAL "..")
  461. list (APPEND _configDefinitions "CMAKE_INTDIR=\"${_config}\"")
  462. endif()
  463. # target export define symbol
  464. cotire_get_target_export_symbol("${_target}" _defineSymbol)
  465. if (_defineSymbol)
  466. list (APPEND _configDefinitions "${_defineSymbol}")
  467. endif()
  468. # directory compile definitions
  469. get_directory_property(_definitions DIRECTORY "${_directory}" COMPILE_DEFINITIONS)
  470. if (_definitions)
  471. list (APPEND _configDefinitions ${_definitions})
  472. endif()
  473. get_directory_property(_definitions DIRECTORY "${_directory}" COMPILE_DEFINITIONS_${_upperConfig})
  474. if (_definitions)
  475. list (APPEND _configDefinitions ${_definitions})
  476. endif()
  477. # target compile definitions
  478. get_target_property(_definitions ${_target} COMPILE_DEFINITIONS)
  479. if (_definitions)
  480. list (APPEND _configDefinitions ${_definitions})
  481. endif()
  482. get_target_property(_definitions ${_target} COMPILE_DEFINITIONS_${_upperConfig})
  483. if (_definitions)
  484. list (APPEND _configDefinitions ${_definitions})
  485. endif()
  486. # parse additional compile definitions from target compile flags
  487. # and don't look at directory compile definitions, which we already handled
  488. set (_targetFlags "")
  489. cotire_get_target_compile_flags("${_config}" "${_language}" "" "${_target}" _targetFlags)
  490. cotire_filter_compile_flags("${_language}" "D" _definitions _ignore ${_targetFlags})
  491. if (_definitions)
  492. list (APPEND _configDefinitions ${_definitions})
  493. endif()
  494. list (REMOVE_DUPLICATES _configDefinitions)
  495. if (COTIRE_DEBUG AND _configDefinitions)
  496. message (STATUS "Target ${_target} compile definitions ${_configDefinitions}")
  497. endif()
  498. set (${_definitionsVar} ${_configDefinitions} PARENT_SCOPE)
  499. endfunction()
  500. function (cotire_get_target_compiler_flags _config _language _directory _target _compilerFlagsVar)
  501. # parse target compile flags omitting compile definitions and include directives
  502. set (_targetFlags "")
  503. cotire_get_target_compile_flags("${_config}" "${_language}" "${_directory}" "${_target}" _targetFlags)
  504. set (_compilerFlags "")
  505. cotire_filter_compile_flags("${_language}" "[ID]" _ignore _compilerFlags ${_targetFlags})
  506. if (COTIRE_DEBUG AND _compilerFlags)
  507. message (STATUS "Target ${_target} compiler flags ${_compilerFlags}")
  508. endif()
  509. set (${_compilerFlagsVar} ${_compilerFlags} PARENT_SCOPE)
  510. endfunction()
  511. function (cotire_add_sys_root_paths _pathsVar)
  512. if (APPLE)
  513. if (CMAKE_OSX_SYSROOT AND CMAKE_${_language}_HAS_ISYSROOT)
  514. foreach (_path IN LISTS ${_pathsVar})
  515. if (IS_ABSOLUTE "${_path}")
  516. get_filename_component(_path "${CMAKE_OSX_SYSROOT}/${_path}" ABSOLUTE)
  517. if (EXISTS "${_path}")
  518. list (APPEND ${_pathsVar} "${_path}")
  519. endif()
  520. endif()
  521. endforeach()
  522. endif()
  523. endif()
  524. set (${_pathsVar} ${${_pathsVar}} PARENT_SCOPE)
  525. if (COTIRE_DEBUG)
  526. message (STATUS "${_pathsVar}=${${_pathsVar}}")
  527. endif()
  528. endfunction()
  529. function (cotire_get_source_extra_properties _sourceFile _pattern _resultVar)
  530. set (_extraProperties ${ARGN})
  531. set (_result "")
  532. if (_extraProperties)
  533. list (FIND _extraProperties "${_sourceFile}" _index)
  534. if (_index GREATER -1)
  535. math (EXPR _index "${_index} + 1")
  536. list (LENGTH _extraProperties _len)
  537. math (EXPR _len "${_len} - 1")
  538. foreach (_index RANGE ${_index} ${_len})
  539. list (GET _extraProperties ${_index} _value)
  540. if ("${_value}" MATCHES "${_pattern}")
  541. list (APPEND _result "${_value}")
  542. else()
  543. break()
  544. endif()
  545. endforeach()
  546. endif()
  547. endif()
  548. set (${_resultVar} ${_result} PARENT_SCOPE)
  549. endfunction()
  550. function (cotire_get_source_compile_definitions _config _language _sourceFile _definitionsVar)
  551. set (_compileDefinitions "")
  552. if (NOT CMAKE_SCRIPT_MODE_FILE)
  553. string (TOUPPER "${_config}" _upperConfig)
  554. get_source_file_property(_definitions "${_sourceFile}" COMPILE_DEFINITIONS)
  555. if (_definitions)
  556. list (APPEND _compileDefinitions ${_definitions})
  557. endif()
  558. get_source_file_property(_definitions "${_sourceFile}" COMPILE_DEFINITIONS_${_upperConfig})
  559. if (_definitions)
  560. list (APPEND _compileDefinitions ${_definitions})
  561. endif()
  562. endif()
  563. cotire_get_source_extra_properties("${_sourceFile}" "^[a-zA-Z0-9_]+(=.*)?$" _definitions ${ARGN})
  564. if (_definitions)
  565. list (APPEND _compileDefinitions ${_definitions})
  566. endif()
  567. if (COTIRE_DEBUG AND _compileDefinitions)
  568. message (STATUS "Source ${_sourceFile} compile definitions ${_compileDefinitions}")
  569. endif()
  570. set (${_definitionsVar} ${_compileDefinitions} PARENT_SCOPE)
  571. endfunction()
  572. function (cotire_get_source_files_compile_definitions _config _language _definitionsVar)
  573. set (_configDefinitions "")
  574. foreach (_sourceFile ${ARGN})
  575. cotire_get_source_compile_definitions("${_config}" "${_language}" "${_sourceFile}" _sourceDefinitions)
  576. if (_sourceDefinitions)
  577. list (APPEND _configDefinitions "${_sourceFile}" ${_sourceDefinitions} "-")
  578. endif()
  579. endforeach()
  580. set (${_definitionsVar} ${_configDefinitions} PARENT_SCOPE)
  581. endfunction()
  582. function (cotire_get_source_undefs _sourceFile _property _sourceUndefsVar)
  583. set (_sourceUndefs "")
  584. if (NOT CMAKE_SCRIPT_MODE_FILE)
  585. get_source_file_property(_undefs "${_sourceFile}" ${_property})
  586. if (_undefs)
  587. list (APPEND _sourceUndefs ${_undefs})
  588. endif()
  589. endif()
  590. cotire_get_source_extra_properties("${_sourceFile}" "^[a-zA-Z0-9_]+$" _undefs ${ARGN})
  591. if (_undefs)
  592. list (APPEND _sourceUndefs ${_undefs})
  593. endif()
  594. if (COTIRE_DEBUG AND _sourceUndefs)
  595. message (STATUS "Source ${_sourceFile} ${_property} undefs ${_sourceUndefs}")
  596. endif()
  597. set (${_sourceUndefsVar} ${_sourceUndefs} PARENT_SCOPE)
  598. endfunction()
  599. function (cotire_get_source_files_undefs _property _sourceUndefsVar)
  600. set (_sourceUndefs "")
  601. foreach (_sourceFile ${ARGN})
  602. cotire_get_source_undefs("${_sourceFile}" ${_property} _undefs)
  603. if (_undefs)
  604. list (APPEND _sourceUndefs "${_sourceFile}" ${_undefs} "-")
  605. endif()
  606. endforeach()
  607. set (${_sourceUndefsVar} ${_sourceUndefs} PARENT_SCOPE)
  608. endfunction()
  609. macro (cotire_set_cmd_to_prologue _cmdVar)
  610. set (${_cmdVar} "${CMAKE_COMMAND}")
  611. if (COTIRE_DEBUG)
  612. list (APPEND ${_cmdVar} "--warn-uninitialized")
  613. endif()
  614. list (APPEND ${_cmdVar} "-DCOTIRE_BUILD_TYPE:STRING=$<CONFIGURATION>")
  615. if (COTIRE_VERBOSE)
  616. list (APPEND ${_cmdVar} "-DCOTIRE_VERBOSE:BOOL=ON")
  617. elseif("${CMAKE_GENERATOR}" MATCHES "Makefiles")
  618. list (APPEND ${_cmdVar} "-DCOTIRE_VERBOSE:BOOL=$(VERBOSE)")
  619. endif()
  620. endmacro()
  621. function (cotire_init_compile_cmd _cmdVar _language _compilerExe _compilerArg1)
  622. if (NOT _compilerExe)
  623. set (_compilerExe "${CMAKE_${_language}_COMPILER}")
  624. endif()
  625. if (NOT _compilerArg1)
  626. set (_compilerArg1 ${CMAKE_${_language}_COMPILER_ARG1})
  627. endif()
  628. string (STRIP "${_compilerArg1}" _compilerArg1)
  629. set (${_cmdVar} "${_compilerExe}" ${_compilerArg1} PARENT_SCOPE)
  630. endfunction()
  631. macro (cotire_add_definitions_to_cmd _cmdVar _language)
  632. foreach (_definition ${ARGN})
  633. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  634. list (APPEND ${_cmdVar} "/D${_definition}")
  635. else()
  636. list (APPEND ${_cmdVar} "-D${_definition}")
  637. endif()
  638. endforeach()
  639. endmacro()
  640. macro (cotire_add_includes_to_cmd _cmdVar _language)
  641. foreach (_include ${ARGN})
  642. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  643. file (TO_NATIVE_PATH "${_include}" _include)
  644. list (APPEND ${_cmdVar} "/I${_include}")
  645. else()
  646. list (APPEND ${_cmdVar} "-I${_include}")
  647. endif()
  648. endforeach()
  649. endmacro()
  650. macro (cotire_add_compile_flags_to_cmd _cmdVar)
  651. foreach (_flag ${ARGN})
  652. list (APPEND ${_cmdVar} "${_flag}")
  653. endforeach()
  654. endmacro()
  655. function (cotire_check_file_up_to_date _fileIsUpToDateVar _file)
  656. set (${_fileIsUpToDateVar} FALSE PARENT_SCOPE)
  657. set (_triggerFile "")
  658. foreach (_dependencyFile ${ARGN})
  659. if (EXISTS "${_dependencyFile}" AND "${_dependencyFile}" IS_NEWER_THAN "${_file}")
  660. set (_triggerFile "${_dependencyFile}")
  661. break()
  662. endif()
  663. endforeach()
  664. get_filename_component(_fileName "${_file}" NAME)
  665. if (EXISTS "${_file}")
  666. if (_triggerFile)
  667. if (COTIRE_VERBOSE)
  668. message (STATUS "${_fileName} update triggered by ${_triggerFile} change.")
  669. endif()
  670. else()
  671. if (COTIRE_VERBOSE)
  672. message (STATUS "${_fileName} is up-to-date.")
  673. endif()
  674. set (${_fileIsUpToDateVar} TRUE PARENT_SCOPE)
  675. endif()
  676. else()
  677. if (COTIRE_VERBOSE)
  678. message (STATUS "${_fileName} does not exist yet.")
  679. endif()
  680. endif()
  681. endfunction()
  682. macro (cotire_find_closest_relative_path _headerFile _includeDirs _relPathVar)
  683. set (${_relPathVar} "")
  684. foreach (_includeDir ${_includeDirs})
  685. if (IS_DIRECTORY "${_includeDir}")
  686. file (RELATIVE_PATH _relPath "${_includeDir}" "${_headerFile}")
  687. if (NOT IS_ABSOLUTE "${_relPath}" AND NOT "${_relPath}" MATCHES "^\\.\\.")
  688. string (LENGTH "${${_relPathVar}}" _closestLen)
  689. string (LENGTH "${_relPath}" _relLen)
  690. if (_closestLen EQUAL 0 OR _relLen LESS _closestLen)
  691. set (${_relPathVar} "${_relPath}")
  692. endif()
  693. endif()
  694. elseif ("${_includeDir}" STREQUAL "${_headerFile}")
  695. # if path matches exactly, return short non-empty string
  696. set (${_relPathVar} "1")
  697. break()
  698. endif()
  699. endforeach()
  700. endmacro()
  701. macro (cotire_check_header_file_location _headerFile _insideIncudeDirs _outsideIncudeDirs _headerIsInside)
  702. # check header path against ignored and honored include directories
  703. cotire_find_closest_relative_path("${_headerFile}" "${_insideIncudeDirs}" _insideRelPath)
  704. if (_insideRelPath)
  705. # header is inside, but could be become outside if there is a shorter outside match
  706. cotire_find_closest_relative_path("${_headerFile}" "${_outsideIncudeDirs}" _outsideRelPath)
  707. if (_outsideRelPath)
  708. string (LENGTH "${_insideRelPath}" _insideRelPathLen)
  709. string (LENGTH "${_outsideRelPath}" _outsideRelPathLen)
  710. if (_outsideRelPathLen LESS _insideRelPathLen)
  711. set (${_headerIsInside} FALSE)
  712. else()
  713. set (${_headerIsInside} TRUE)
  714. endif()
  715. else()
  716. set (${_headerIsInside} TRUE)
  717. endif()
  718. else()
  719. # header is outside
  720. set (${_headerIsInside} FALSE)
  721. endif()
  722. endmacro()
  723. macro (cotire_check_ignore_header_file_path _headerFile _headerIsIgnoredVar)
  724. if (NOT EXISTS "${_headerFile}")
  725. set (${_headerIsIgnoredVar} TRUE)
  726. elseif (IS_DIRECTORY "${_headerFile}")
  727. set (${_headerIsIgnoredVar} TRUE)
  728. elseif ("${_headerFile}" MATCHES "\\.\\.|[_-]fixed" AND "${_headerFile}" MATCHES "\\.h$")
  729. # heuristic: ignore C headers with embedded parent directory references or "-fixed" or "_fixed" in path
  730. # these often stem from using GCC #include_next tricks, which may break the precompiled header compilation
  731. # with the error message "error: no include path in which to search for header.h"
  732. set (${_headerIsIgnoredVar} TRUE)
  733. else()
  734. set (${_headerIsIgnoredVar} FALSE)
  735. endif()
  736. endmacro()
  737. macro (cotire_check_ignore_header_file_ext _headerFile _ignoreExtensionsVar _headerIsIgnoredVar)
  738. # check header file extension
  739. cotire_get_source_file_extension("${_headerFile}" _headerFileExt)
  740. set (${_headerIsIgnoredVar} FALSE)
  741. if (_headerFileExt)
  742. list (FIND ${_ignoreExtensionsVar} "${_headerFileExt}" _index)
  743. if (_index GREATER -1)
  744. set (${_headerIsIgnoredVar} TRUE)
  745. endif()
  746. endif()
  747. endmacro()
  748. macro (cotire_parse_line _line _headerFileVar _headerDepthVar)
  749. if (MSVC)
  750. # cl.exe /showIncludes output looks different depending on the language pack used, e.g.:
  751. # English: "Note: including file: C:\directory\file"
  752. # German: "Hinweis: Einlesen der Datei: C:\directory\file"
  753. # We use a very general regular expression, relying on the presence of the : characters
  754. if ("${_line}" MATCHES ":( +)([^:]+:[^:]+)$")
  755. # Visual Studio compiler output
  756. string (LENGTH "${CMAKE_MATCH_1}" ${_headerDepthVar})
  757. get_filename_component(${_headerFileVar} "${CMAKE_MATCH_2}" ABSOLUTE)
  758. else()
  759. set (${_headerFileVar} "")
  760. set (${_headerDepthVar} 0)
  761. endif()
  762. else()
  763. if ("${_line}" MATCHES "^(\\.+) (.*)$")
  764. # GCC like output
  765. string (LENGTH "${CMAKE_MATCH_1}" ${_headerDepthVar})
  766. if (IS_ABSOLUTE "${CMAKE_MATCH_2}")
  767. set (${_headerFileVar} "${CMAKE_MATCH_2}")
  768. else()
  769. get_filename_component(${_headerFileVar} "${CMAKE_MATCH_2}" REALPATH)
  770. endif()
  771. else()
  772. set (${_headerFileVar} "")
  773. set (${_headerDepthVar} 0)
  774. endif()
  775. endif()
  776. endmacro()
  777. function (cotire_parse_includes _language _scanOutput _ignoredIncudeDirs _honoredIncudeDirs _ignoredExtensions _selectedIncludesVar _unparsedLinesVar)
  778. if (WIN32)
  779. # prevent CMake macro invocation errors due to backslash characters in Windows paths
  780. string (REPLACE "\\" "." _scanOutput "${_scanOutput}")
  781. endif()
  782. # canonize slashes
  783. string (REPLACE "//" "." _scanOutput "${_scanOutput}")
  784. # prevent semicolon from being interpreted as a line separator
  785. string (REPLACE ";" "\\;" _scanOutput "${_scanOutput}")
  786. # then separate lines
  787. string (REGEX REPLACE "\n" ";" _scanOutput "${_scanOutput}")
  788. list (LENGTH _scanOutput _len)
  789. # remove duplicate lines to speed up parsing
  790. list (REMOVE_DUPLICATES _scanOutput)
  791. list (LENGTH _scanOutput _uniqueLen)
  792. if (COTIRE_VERBOSE)
  793. message (STATUS "Scanning ${_uniqueLen} unique lines of ${_len} for includes")
  794. if (_ignoredExtensions)
  795. message (STATUS "Ignored extensions: ${_ignoredExtensions}")
  796. endif()
  797. if (_ignoredIncudeDirs)
  798. message (STATUS "Ignored paths: ${_ignoredIncudeDirs}")
  799. endif()
  800. if (_honoredIncudeDirs)
  801. message (STATUS "Included paths: ${_honoredIncudeDirs}")
  802. endif()
  803. endif()
  804. set (_sourceFiles ${ARGN})
  805. set (_selectedIncludes "")
  806. set (_unparsedLines "")
  807. # stack keeps track of inside/outside project status of processed header files
  808. set (_headerIsInsideStack "")
  809. foreach (_line IN LISTS _scanOutput)
  810. if (_line)
  811. cotire_parse_line("${_line}" _headerFile _headerDepth)
  812. if (_headerFile)
  813. cotire_check_header_file_location("${_headerFile}" "${_ignoredIncudeDirs}" "${_honoredIncudeDirs}" _headerIsInside)
  814. if (COTIRE_DEBUG)
  815. message (STATUS "${_headerDepth}: ${_headerFile} ${_headerIsInside}")
  816. endif()
  817. # update stack
  818. list (LENGTH _headerIsInsideStack _stackLen)
  819. if (_headerDepth GREATER _stackLen)
  820. math (EXPR _stackLen "${_stackLen} + 1")
  821. foreach (_index RANGE ${_stackLen} ${_headerDepth})
  822. list (APPEND _headerIsInsideStack ${_headerIsInside})
  823. endforeach()
  824. else()
  825. foreach (_index RANGE ${_headerDepth} ${_stackLen})
  826. list (REMOVE_AT _headerIsInsideStack -1)
  827. endforeach()
  828. list (APPEND _headerIsInsideStack ${_headerIsInside})
  829. endif()
  830. if (COTIRE_DEBUG)
  831. message (STATUS "${_headerIsInsideStack}")
  832. endif()
  833. # header is a candidate if it is outside project
  834. if (NOT _headerIsInside)
  835. # get parent header file's inside/outside status
  836. if (_headerDepth GREATER 1)
  837. math (EXPR _index "${_headerDepth} - 2")
  838. list (GET _headerIsInsideStack ${_index} _parentHeaderIsInside)
  839. else()
  840. set (_parentHeaderIsInside TRUE)
  841. endif()
  842. # select header file if parent header file is inside project
  843. # (e.g., a project header file that includes a standard header file)
  844. if (_parentHeaderIsInside)
  845. cotire_check_ignore_header_file_path("${_headerFile}" _headerIsIgnored)
  846. if (NOT _headerIsIgnored)
  847. cotire_check_ignore_header_file_ext("${_headerFile}" _ignoredExtensions _headerIsIgnored)
  848. if (NOT _headerIsIgnored)
  849. list (APPEND _selectedIncludes "${_headerFile}")
  850. else()
  851. # fix header's inside status on stack, it is ignored by extension now
  852. list (REMOVE_AT _headerIsInsideStack -1)
  853. list (APPEND _headerIsInsideStack TRUE)
  854. endif()
  855. endif()
  856. if (COTIRE_DEBUG)
  857. message (STATUS "${_headerFile} ${_ignoredExtensions} ${_headerIsIgnored}")
  858. endif()
  859. endif()
  860. endif()
  861. else()
  862. if (MSVC)
  863. # for cl.exe do not keep unparsed lines which solely consist of a source file name
  864. string (FIND "${_sourceFiles}" "${_line}" _index)
  865. if (_index LESS 0)
  866. list (APPEND _unparsedLines "${_line}")
  867. endif()
  868. else()
  869. list (APPEND _unparsedLines "${_line}")
  870. endif()
  871. endif()
  872. endif()
  873. endforeach()
  874. list (REMOVE_DUPLICATES _selectedIncludes)
  875. set (${_selectedIncludesVar} ${_selectedIncludes} PARENT_SCOPE)
  876. set (${_unparsedLinesVar} ${_unparsedLines} PARENT_SCOPE)
  877. endfunction()
  878. function (cotire_scan_includes _includesVar)
  879. set(_options "")
  880. set(_oneValueArgs COMPILER_ID COMPILER_EXECUTABLE COMPILER_VERSION LANGUAGE UNPARSED_LINES)
  881. set(_multiValueArgs COMPILE_DEFINITIONS COMPILE_FLAGS INCLUDE_DIRECTORIES IGNORE_PATH INCLUDE_PATH IGNORE_EXTENSIONS)
  882. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  883. set (_sourceFiles ${_option_UNPARSED_ARGUMENTS})
  884. if (NOT _option_LANGUAGE)
  885. set (_option_LANGUAGE "CXX")
  886. endif()
  887. if (NOT _option_COMPILER_ID)
  888. set (_option_COMPILER_ID "${CMAKE_${_option_LANGUAGE}_ID}")
  889. endif()
  890. set (_cmd "${_option_COMPILER_EXECUTABLE}" ${_option_COMPILER_ARG1})
  891. cotire_init_compile_cmd(_cmd "${_option_LANGUAGE}" "${_option_COMPILER_EXECUTABLE}" "${_option_COMPILER_ARG1}")
  892. cotire_add_definitions_to_cmd(_cmd "${_option_LANGUAGE}" ${_option_COMPILE_DEFINITIONS})
  893. cotire_add_compile_flags_to_cmd(_cmd ${_option_COMPILE_FLAGS})
  894. cotire_add_includes_to_cmd(_cmd "${_option_LANGUAGE}" ${_option_INCLUDE_DIRECTORIES})
  895. cotire_add_makedep_flags("${_option_LANGUAGE}" "${_option_COMPILER_ID}" "${_option_COMPILER_VERSION}" _cmd)
  896. # only consider existing source files for scanning
  897. set (_existingSourceFiles "")
  898. foreach (_sourceFile ${_sourceFiles})
  899. if (EXISTS "${_sourceFile}")
  900. list (APPEND _existingSourceFiles "${_sourceFile}")
  901. endif()
  902. endforeach()
  903. if (NOT _existingSourceFiles)
  904. set (${_includesVar} "" PARENT_SCOPE)
  905. return()
  906. endif()
  907. list (APPEND _cmd ${_existingSourceFiles})
  908. if (COTIRE_VERBOSE)
  909. message (STATUS "execute_process: ${_cmd}")
  910. endif()
  911. if (_option_COMPILER_ID MATCHES "MSVC")
  912. if (COTIRE_DEBUG)
  913. message (STATUS "clearing VS_UNICODE_OUTPUT")
  914. endif()
  915. # cl.exe messes with the output streams unless the environment variable VS_UNICODE_OUTPUT is cleared
  916. unset (ENV{VS_UNICODE_OUTPUT})
  917. endif()
  918. execute_process(COMMAND ${_cmd} WORKING_DIRECTORY ".."
  919. RESULT_VARIABLE _result OUTPUT_QUIET ERROR_VARIABLE _output)
  920. if (_result)
  921. message (STATUS "Result ${_result} scanning includes of ${_existingSourceFiles}.")
  922. endif()
  923. cotire_parse_includes(
  924. "${_option_LANGUAGE}" "${_output}"
  925. "${_option_IGNORE_PATH}" "${_option_INCLUDE_PATH}"
  926. "${_option_IGNORE_EXTENSIONS}"
  927. _includes _unparsedLines
  928. ${_sourceFiles})
  929. set (${_includesVar} ${_includes} PARENT_SCOPE)
  930. if (_option_UNPARSED_LINES)
  931. set (${_option_UNPARSED_LINES} ${_unparsedLines} PARENT_SCOPE)
  932. endif()
  933. endfunction()
  934. macro (cotire_append_undefs _contentsVar)
  935. set (_undefs ${ARGN})
  936. if (_undefs)
  937. list (REMOVE_DUPLICATES _undefs)
  938. foreach (_definition ${_undefs})
  939. list (APPEND ${_contentsVar} "#undef ${_definition}")
  940. endforeach()
  941. endif()
  942. endmacro()
  943. macro (cotire_comment_str _language _commentText _commentVar)
  944. if ("${_language}" STREQUAL "CMAKE")
  945. set (${_commentVar} "# ${_commentText}")
  946. else()
  947. set (${_commentVar} "/* ${_commentText} */")
  948. endif()
  949. endmacro()
  950. function (cotire_write_file _language _file _contents _force)
  951. get_filename_component(_moduleName "${COTIRE_CMAKE_MODULE_FILE}" NAME)
  952. cotire_comment_str("${_language}" "${_moduleName} ${COTIRE_CMAKE_MODULE_VERSION} generated file" _header1)
  953. cotire_comment_str("${_language}" "${_file}" _header2)
  954. set (_contents "${_header1}\n${_header2}\n${_contents}")
  955. if (COTIRE_DEBUG)
  956. message (STATUS "${_contents}")
  957. endif()
  958. if (_force OR NOT EXISTS "${_file}")
  959. file (WRITE "${_file}" "${_contents}")
  960. else()
  961. file (READ "${_file}" _oldContents)
  962. if (NOT "${_oldContents}" STREQUAL "${_contents}")
  963. file (WRITE "${_file}" "${_contents}")
  964. else()
  965. if (COTIRE_DEBUG)
  966. message (STATUS "${_file} unchanged")
  967. endif()
  968. endif()
  969. endif()
  970. endfunction()
  971. function (cotire_generate_unity_source _unityFile)
  972. set(_options "")
  973. set(_oneValueArgs LANGUAGE)
  974. set(_multiValueArgs
  975. DEPENDS SOURCES_COMPILE_DEFINITIONS
  976. PRE_UNDEFS SOURCES_PRE_UNDEFS POST_UNDEFS SOURCES_POST_UNDEFS PROLOGUE EPILOGUE)
  977. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  978. if (_option_DEPENDS)
  979. cotire_check_file_up_to_date(_unityFileIsUpToDate "${_unityFile}" ${_option_DEPENDS})
  980. if (_unityFileIsUpToDate)
  981. return()
  982. endif()
  983. endif()
  984. set (_sourceFiles ${_option_UNPARSED_ARGUMENTS})
  985. if (NOT _option_PRE_UNDEFS)
  986. set (_option_PRE_UNDEFS "")
  987. endif()
  988. if (NOT _option_SOURCES_PRE_UNDEFS)
  989. set (_option_SOURCES_PRE_UNDEFS "")
  990. endif()
  991. if (NOT _option_POST_UNDEFS)
  992. set (_option_POST_UNDEFS "")
  993. endif()
  994. if (NOT _option_SOURCES_POST_UNDEFS)
  995. set (_option_SOURCES_POST_UNDEFS "")
  996. endif()
  997. set (_contents "")
  998. if (_option_PROLOGUE)
  999. list (APPEND _contents ${_option_PROLOGUE})
  1000. endif()
  1001. if (_option_LANGUAGE AND _sourceFiles)
  1002. if ("${_option_LANGUAGE}" STREQUAL "CXX")
  1003. list (APPEND _contents "#ifdef __cplusplus")
  1004. elseif ("${_option_LANGUAGE}" STREQUAL "C")
  1005. list (APPEND _contents "#ifndef __cplusplus")
  1006. endif()
  1007. endif()
  1008. set (_compileUndefinitions "")
  1009. foreach (_sourceFile ${_sourceFiles})
  1010. cotire_get_source_compile_definitions(
  1011. "${_option_CONFIGURATION}" "${_option_LANGUAGE}" "${_sourceFile}" _compileDefinitions
  1012. ${_option_SOURCES_COMPILE_DEFINITIONS})
  1013. cotire_get_source_undefs("${_sourceFile}" COTIRE_UNITY_SOURCE_PRE_UNDEFS _sourcePreUndefs ${_option_SOURCES_PRE_UNDEFS})
  1014. cotire_get_source_undefs("${_sourceFile}" COTIRE_UNITY_SOURCE_POST_UNDEFS _sourcePostUndefs ${_option_SOURCES_POST_UNDEFS})
  1015. if (_option_PRE_UNDEFS)
  1016. list (APPEND _compileUndefinitions ${_option_PRE_UNDEFS})
  1017. endif()
  1018. if (_sourcePreUndefs)
  1019. list (APPEND _compileUndefinitions ${_sourcePreUndefs})
  1020. endif()
  1021. if (_compileUndefinitions)
  1022. cotire_append_undefs(_contents ${_compileUndefinitions})
  1023. set (_compileUndefinitions "")
  1024. endif()
  1025. if (_sourcePostUndefs)
  1026. list (APPEND _compileUndefinitions ${_sourcePostUndefs})
  1027. endif()
  1028. if (_option_POST_UNDEFS)
  1029. list (APPEND _compileUndefinitions ${_option_POST_UNDEFS})
  1030. endif()
  1031. foreach (_definition ${_compileDefinitions})
  1032. if ("${_definition}" MATCHES "^([a-zA-Z0-9_]+)=(.+)$")
  1033. list (APPEND _contents "#define ${CMAKE_MATCH_1} ${CMAKE_MATCH_2}")
  1034. list (INSERT _compileUndefinitions 0 "${CMAKE_MATCH_1}")
  1035. else()
  1036. list (APPEND _contents "#define ${_definition}")
  1037. list (INSERT _compileUndefinitions 0 "${_definition}")
  1038. endif()
  1039. endforeach()
  1040. get_filename_component(_sourceFile "${_sourceFile}" ABSOLUTE)
  1041. if (WIN32)
  1042. file (TO_NATIVE_PATH "${_sourceFile}" _sourceFile)
  1043. endif()
  1044. list (APPEND _contents "#include \"${_sourceFile}\"")
  1045. endforeach()
  1046. if (_compileUndefinitions)
  1047. cotire_append_undefs(_contents ${_compileUndefinitions})
  1048. set (_compileUndefinitions "")
  1049. endif()
  1050. if (_option_LANGUAGE AND _sourceFiles)
  1051. list (APPEND _contents "#endif")
  1052. endif()
  1053. if (_option_EPILOGUE)
  1054. list (APPEND _contents ${_option_EPILOGUE})
  1055. endif()
  1056. list (APPEND _contents "")
  1057. string (REPLACE ";" "\n" _contents "${_contents}")
  1058. if (COTIRE_VERBOSE)
  1059. message ("${_contents}")
  1060. endif()
  1061. cotire_write_file("${_option_LANGUAGE}" "${_unityFile}" "${_contents}" TRUE)
  1062. endfunction()
  1063. function (cotire_generate_prefix_header _prefixFile)
  1064. set(_options "")
  1065. set(_oneValueArgs LANGUAGE COMPILER_EXECUTABLE COMPILER_ID COMPILER_VERSION)
  1066. set(_multiValueArgs DEPENDS COMPILE_DEFINITIONS COMPILE_FLAGS
  1067. INCLUDE_DIRECTORIES IGNORE_PATH INCLUDE_PATH IGNORE_EXTENSIONS)
  1068. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  1069. if (_option_DEPENDS)
  1070. cotire_check_file_up_to_date(_prefixFileIsUpToDate "${_prefixFile}" ${_option_DEPENDS})
  1071. if (_prefixFileIsUpToDate)
  1072. return()
  1073. endif()
  1074. endif()
  1075. set (_epilogue "")
  1076. if (_option_COMPILER_ID MATCHES "Intel")
  1077. # Intel compiler requires hdrstop pragma to stop generating PCH file
  1078. set (_epilogue "#pragma hdrstop")
  1079. endif()
  1080. set (_sourceFiles ${_option_UNPARSED_ARGUMENTS})
  1081. cotire_scan_includes(_selectedHeaders ${_sourceFiles}
  1082. LANGUAGE "${_option_LANGUAGE}"
  1083. COMPILER_EXECUTABLE "${_option_COMPILER_EXECUTABLE}"
  1084. COMPILER_ID "${_option_COMPILER_ID}"
  1085. COMPILER_VERSION "${_option_COMPILER_VERSION}"
  1086. COMPILE_DEFINITIONS ${_option_COMPILE_DEFINITIONS}
  1087. COMPILE_FLAGS ${_option_COMPILE_FLAGS}
  1088. INCLUDE_DIRECTORIES ${_option_INCLUDE_DIRECTORIES}
  1089. IGNORE_PATH ${_option_IGNORE_PATH}
  1090. INCLUDE_PATH ${_option_INCLUDE_PATH}
  1091. IGNORE_EXTENSIONS ${_option_IGNORE_EXTENSIONS}
  1092. UNPARSED_LINES _unparsedLines)
  1093. cotire_generate_unity_source("${_prefixFile}" EPILOGUE ${_epilogue} LANGUAGE "${_option_LANGUAGE}" ${_selectedHeaders})
  1094. set (_unparsedLinesFile "${_prefixFile}.log")
  1095. if (_unparsedLines)
  1096. if (COTIRE_VERBOSE OR NOT _selectedHeaders)
  1097. list (LENGTH _unparsedLines _skippedLineCount)
  1098. file (RELATIVE_PATH _unparsedLinesFileRelPath "${CMAKE_BINARY_DIR}" "${_unparsedLinesFile}")
  1099. message (STATUS "${_skippedLineCount} line(s) skipped, see ${_unparsedLinesFileRelPath}")
  1100. endif()
  1101. string (REPLACE ";" "\n" _unparsedLines "${_unparsedLines}")
  1102. endif()
  1103. file (WRITE "${_unparsedLinesFile}" "${_unparsedLines}\n")
  1104. endfunction()
  1105. function (cotire_add_makedep_flags _language _compilerID _compilerVersion _flagsVar)
  1106. set (_flags ${${_flagsVar}})
  1107. if (_compilerID MATCHES "MSVC")
  1108. # cl.exe options used
  1109. # /nologo suppresses display of sign-on banner
  1110. # /TC treat all files named on the command line as C source files
  1111. # /TP treat all files named on the command line as C++ source files
  1112. # /EP preprocess to stdout without #line directives
  1113. # /showIncludes list include files
  1114. set (_sourceFileTypeC "/TC")
  1115. set (_sourceFileTypeCXX "/TP")
  1116. if (_flags)
  1117. # append to list
  1118. list (APPEND _flags /nologo "${_sourceFileType${_language}}" /EP /showIncludes)
  1119. else()
  1120. # return as a flag string
  1121. set (_flags "${_sourceFileType${_language}} /EP /showIncludes")
  1122. endif()
  1123. elseif (_compilerID MATCHES "GNU")
  1124. # GCC options used
  1125. # -H print the name of each header file used
  1126. # -E invoke preprocessor
  1127. # -fdirectives-only do not expand macros, requires GCC >= 4.3
  1128. if (_flags)
  1129. # append to list
  1130. list (APPEND _flags -H -E)
  1131. if (NOT "${_compilerVersion}" VERSION_LESS "4.3.0")
  1132. list (APPEND _flags "-fdirectives-only")
  1133. endif()
  1134. else()
  1135. # return as a flag string
  1136. set (_flags "-H -E")
  1137. if (NOT "${_compilerVersion}" VERSION_LESS "4.3.0")
  1138. set (_flags "${_flags} -fdirectives-only")
  1139. endif()
  1140. endif()
  1141. elseif (_compilerID MATCHES "Clang")
  1142. # Clang options used
  1143. # -H print the name of each header file used
  1144. # -E invoke preprocessor
  1145. if (_flags)
  1146. # append to list
  1147. list (APPEND _flags -H -E)
  1148. else()
  1149. # return as a flag string
  1150. set (_flags "-H -E")
  1151. endif()
  1152. elseif (_compilerID MATCHES "Intel")
  1153. if (WIN32)
  1154. # Windows Intel options used
  1155. # /nologo do not display compiler version information
  1156. # /QH display the include file order
  1157. # /EP preprocess to stdout, omitting #line directives
  1158. # /TC process all source or unrecognized file types as C source files
  1159. # /TP process all source or unrecognized file types as C++ source files
  1160. set (_sourceFileTypeC "/TC")
  1161. set (_sourceFileTypeCXX "/TP")
  1162. if (_flags)
  1163. # append to list
  1164. list (APPEND _flags /nologo "${_sourceFileType${_language}}" /EP /QH)
  1165. else()
  1166. # return as a flag string
  1167. set (_flags "${_sourceFileType${_language}} /EP /QH")
  1168. endif()
  1169. else()
  1170. # Linux / Mac OS X Intel options used
  1171. # -H print the name of each header file used
  1172. # -EP preprocess to stdout, omitting #line directives
  1173. # -Kc++ process all source or unrecognized file types as C++ source files
  1174. if (_flags)
  1175. # append to list
  1176. if ("${_language}" STREQUAL "CXX")
  1177. list (APPEND _flags -Kc++)
  1178. endif()
  1179. list (APPEND _flags -H -EP)
  1180. else()
  1181. # return as a flag string
  1182. if ("${_language}" STREQUAL "CXX")
  1183. set (_flags "-Kc++ ")
  1184. endif()
  1185. set (_flags "${_flags}-H -EP")
  1186. endif()
  1187. endif()
  1188. else()
  1189. message (FATAL_ERROR "Unsupported ${_language} compiler ${_compilerID} version ${_compilerVersion}.")
  1190. endif()
  1191. set (${_flagsVar} ${_flags} PARENT_SCOPE)
  1192. endfunction()
  1193. function (cotire_add_pch_compilation_flags _language _compilerID _compilerVersion _prefixFile _pchFile _hostFile _flagsVar)
  1194. set (_flags ${${_flagsVar}})
  1195. if (_compilerID MATCHES "MSVC")
  1196. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1197. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1198. file (TO_NATIVE_PATH "${_hostFile}" _hostFileNative)
  1199. # cl.exe options used
  1200. # /Yc creates a precompiled header file
  1201. # /Fp specifies precompiled header binary file name
  1202. # /FI forces inclusion of file
  1203. # /TC treat all files named on the command line as C source files
  1204. # /TP treat all files named on the command line as C++ source files
  1205. # /Zs syntax check only
  1206. set (_sourceFileTypeC "/TC")
  1207. set (_sourceFileTypeCXX "/TP")
  1208. if (_flags)
  1209. # append to list
  1210. list (APPEND _flags /nologo "${_sourceFileType${_language}}"
  1211. "/Yc${_prefixFileNative}" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}" /Zs "${_hostFileNative}")
  1212. else()
  1213. # return as a flag string
  1214. set (_flags "/Yc\"${_prefixFileNative}\" /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1215. endif()
  1216. elseif (_compilerID MATCHES "GNU|Clang")
  1217. # GCC / Clang options used
  1218. # -x specify the source language
  1219. # -c compile but do not link
  1220. # -o place output in file
  1221. set (_xLanguage_C "c-header")
  1222. set (_xLanguage_CXX "c++-header")
  1223. if (_flags)
  1224. # append to list
  1225. list (APPEND _flags "-x" "${_xLanguage_${_language}}" "-c" "${_prefixFile}" -o "${_pchFile}")
  1226. else()
  1227. # return as a flag string
  1228. set (_flags "-x ${_xLanguage_${_language}} -c \"${_prefixFile}\" -o \"${_pchFile}\"")
  1229. endif()
  1230. elseif (_compilerID MATCHES "Intel")
  1231. if (WIN32)
  1232. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1233. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1234. file (TO_NATIVE_PATH "${_hostFile}" _hostFileNative)
  1235. # Windows Intel options used
  1236. # /nologo do not display compiler version information
  1237. # /Yc create a precompiled header (PCH) file
  1238. # /Fp specify a path or file name for precompiled header files
  1239. # /FI tells the preprocessor to include a specified file name as the header file
  1240. # /TC process all source or unrecognized file types as C source files
  1241. # /TP process all source or unrecognized file types as C++ source files
  1242. # /Zs syntax check only
  1243. # /Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1244. set (_sourceFileTypeC "/TC")
  1245. set (_sourceFileTypeCXX "/TP")
  1246. if (_flags)
  1247. # append to list
  1248. list (APPEND _flags /nologo "${_sourceFileType${_language}}"
  1249. "/Yc" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}" /Zs "${_hostFileNative}")
  1250. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1251. list (APPEND _flags "/Wpch-messages")
  1252. endif()
  1253. else()
  1254. # return as a flag string
  1255. set (_flags "/Yc /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1256. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1257. set (_flags "${_flags} /Wpch-messages")
  1258. endif()
  1259. endif()
  1260. else()
  1261. # Linux / Mac OS X Intel options used
  1262. # -pch-dir location for precompiled header files
  1263. # -pch-create name of the precompiled header (PCH) to create
  1264. # -Kc++ process all source or unrecognized file types as C++ source files
  1265. # -fsyntax-only check only for correct syntax
  1266. # -Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1267. get_filename_component(_pchDir "${_pchFile}" PATH)
  1268. get_filename_component(_pchName "${_pchFile}" NAME)
  1269. set (_xLanguage_C "c-header")
  1270. set (_xLanguage_CXX "c++-header")
  1271. if (_flags)
  1272. # append to list
  1273. if ("${_language}" STREQUAL "CXX")
  1274. list (APPEND _flags -Kc++)
  1275. endif()
  1276. list (APPEND _flags "-include" "${_prefixFile}" "-pch-dir" "${_pchDir}" "-pch-create" "${_pchName}" "-fsyntax-only" "${_hostFile}")
  1277. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1278. list (APPEND _flags "-Wpch-messages")
  1279. endif()
  1280. else()
  1281. # return as a flag string
  1282. set (_flags "-include \"${_prefixFile}\" -pch-dir \"${_pchDir}\" -pch-create \"${_pchName}\"")
  1283. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1284. set (_flags "${_flags} -Wpch-messages")
  1285. endif()
  1286. endif()
  1287. endif()
  1288. else()
  1289. message (FATAL_ERROR "Unsupported ${_language} compiler ${_compilerID} version ${_compilerVersion}.")
  1290. endif()
  1291. set (${_flagsVar} ${_flags} PARENT_SCOPE)
  1292. endfunction()
  1293. function (cotire_add_pch_inclusion_flags _language _compilerID _compilerVersion _prefixFile _pchFile _flagsVar)
  1294. set (_flags ${${_flagsVar}})
  1295. if (_compilerID MATCHES "MSVC")
  1296. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1297. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1298. # cl.exe options used
  1299. # /Yu uses a precompiled header file during build
  1300. # /Fp specifies precompiled header binary file name
  1301. # /FI forces inclusion of file
  1302. if (_flags)
  1303. # append to list
  1304. list (APPEND _flags "/Yu${_prefixFileNative}" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}")
  1305. else()
  1306. # return as a flag string
  1307. set (_flags "/Yu\"${_prefixFileNative}\" /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1308. endif()
  1309. elseif (_compilerID MATCHES "GNU")
  1310. # GCC options used
  1311. # -include process include file as the first line of the primary source file
  1312. # -Winvalid-pch warns if precompiled header is found but cannot be used
  1313. if (_flags)
  1314. # append to list
  1315. list (APPEND _flags "-include" "${_prefixFile}" "-Winvalid-pch")
  1316. else()
  1317. # return as a flag string
  1318. set (_flags "-include \"${_prefixFile}\" -Winvalid-pch")
  1319. endif()
  1320. elseif (_compilerID MATCHES "Clang")
  1321. # Clang options used
  1322. # -include process include file as the first line of the primary source file
  1323. # -Qunused-arguments don't emit warning for unused driver arguments
  1324. if (_flags)
  1325. # append to list
  1326. list (APPEND _flags "-include" "${_prefixFile}" "-Qunused-arguments")
  1327. else()
  1328. # return as a flag string
  1329. set (_flags "-include \"${_prefixFile}\" -Qunused-arguments")
  1330. endif()
  1331. elseif (_compilerID MATCHES "Intel")
  1332. if (WIN32)
  1333. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1334. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1335. # Windows Intel options used
  1336. # /Yu use a precompiled header (PCH) file
  1337. # /Fp specify a path or file name for precompiled header files
  1338. # /FI tells the preprocessor to include a specified file name as the header file
  1339. # /Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1340. if (_flags)
  1341. # append to list
  1342. list (APPEND _flags "/Yu" "/Fp${_pchFileNative}" "/FI${_prefixFileNative}")
  1343. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1344. list (APPEND _flags "/Wpch-messages")
  1345. endif()
  1346. else()
  1347. # return as a flag string
  1348. set (_flags "/Yu /Fp\"${_pchFileNative}\" /FI\"${_prefixFileNative}\"")
  1349. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1350. set (_flags "${_flags} /Wpch-messages")
  1351. endif()
  1352. endif()
  1353. else()
  1354. # Linux / Mac OS X Intel options used
  1355. # -pch-dir location for precompiled header files
  1356. # -pch-use name of the precompiled header (PCH) to use
  1357. # -include process include file as the first line of the primary source file
  1358. # -Wpch-messages enable diagnostics related to pre-compiled headers (requires Intel XE 2013 Update 2)
  1359. get_filename_component(_pchDir "${_pchFile}" PATH)
  1360. get_filename_component(_pchName "${_pchFile}" NAME)
  1361. if (_flags)
  1362. # append to list
  1363. list (APPEND _flags "-include" "${_prefixFile}" "-pch-dir" "${_pchDir}" "-pch-use" "${_pchName}")
  1364. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1365. list (APPEND _flags "-Wpch-messages")
  1366. endif()
  1367. else()
  1368. # return as a flag string
  1369. set (_flags "-include \"${_prefixFile}\" -pch-dir \"${_pchDir}\" -pch-use \"${_pchName}\"")
  1370. if (NOT "${_compilerVersion}" VERSION_LESS "13.1.0")
  1371. set (_flags "${_flags} -Wpch-messages")
  1372. endif()
  1373. endif()
  1374. endif()
  1375. else()
  1376. message (FATAL_ERROR "Unsupported ${_language} compiler ${_compilerID} version ${_compilerVersion}.")
  1377. endif()
  1378. set (${_flagsVar} ${_flags} PARENT_SCOPE)
  1379. endfunction()
  1380. function (cotire_precompile_prefix_header _prefixFile _pchFile _hostFile)
  1381. set(_options "")
  1382. set(_oneValueArgs COMPILER_EXECUTABLE COMPILER_ID COMPILER_VERSION LANGUAGE)
  1383. set(_multiValueArgs COMPILE_DEFINITIONS COMPILE_FLAGS INCLUDE_DIRECTORIES)
  1384. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  1385. if (NOT _option_LANGUAGE)
  1386. set (_option_LANGUAGE "CXX")
  1387. endif()
  1388. if (NOT _option_COMPILER_ID)
  1389. set (_option_COMPILER_ID "${CMAKE_${_option_LANGUAGE}_ID}")
  1390. endif()
  1391. cotire_init_compile_cmd(_cmd "${_option_LANGUAGE}" "${_option_COMPILER_EXECUTABLE}" "${_option_COMPILER_ARG1}")
  1392. cotire_add_definitions_to_cmd(_cmd "${_option_LANGUAGE}" ${_option_COMPILE_DEFINITIONS})
  1393. cotire_add_compile_flags_to_cmd(_cmd ${_option_COMPILE_FLAGS})
  1394. cotire_add_includes_to_cmd(_cmd "${_option_LANGUAGE}" ${_option_INCLUDE_DIRECTORIES})
  1395. cotire_add_pch_compilation_flags(
  1396. "${_option_LANGUAGE}" "${_option_COMPILER_ID}" "${_option_COMPILER_VERSION}"
  1397. "${_prefixFile}" "${_pchFile}" "${_hostFile}" _cmd)
  1398. if (COTIRE_VERBOSE)
  1399. message (STATUS "execute_process: ${_cmd}")
  1400. endif()
  1401. if (_option_COMPILER_ID MATCHES "MSVC")
  1402. if (COTIRE_DEBUG)
  1403. message (STATUS "clearing VS_UNICODE_OUTPUT")
  1404. endif()
  1405. # cl.exe messes with the output streams unless the environment variable VS_UNICODE_OUTPUT is cleared
  1406. unset (ENV{VS_UNICODE_OUTPUT})
  1407. endif()
  1408. execute_process(COMMAND ${_cmd} WORKING_DIRECTORY ".." RESULT_VARIABLE _result)
  1409. if (_result)
  1410. message (FATAL_ERROR "Error ${_result} precompiling ${_prefixFile}.")
  1411. endif()
  1412. endfunction()
  1413. function (cotire_check_precompiled_header_support _language _targetSourceDir _target _msgVar)
  1414. set (_unsupportedCompiler
  1415. "Precompiled headers not supported for ${_language} compiler ${CMAKE_${_language}_COMPILER_ID}")
  1416. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC")
  1417. # supported since Visual Studio C++ 6.0
  1418. # and CMake does not support an earlier version
  1419. set (${_msgVar} "" PARENT_SCOPE)
  1420. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "GNU")
  1421. # GCC PCH support requires version >= 3.4
  1422. cotire_determine_compiler_version("${_language}" COTIRE_${_language}_COMPILER)
  1423. if ("${COTIRE_${_language}_COMPILER_VERSION}" MATCHES ".+" AND
  1424. "${COTIRE_${_language}_COMPILER_VERSION}" VERSION_LESS "3.4.0")
  1425. set (${_msgVar} "${_unsupportedCompiler} version ${COTIRE_${_language}_COMPILER_VERSION}." PARENT_SCOPE)
  1426. else()
  1427. set (${_msgVar} "" PARENT_SCOPE)
  1428. endif()
  1429. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "Clang")
  1430. # all Clang versions have PCH support
  1431. set (${_msgVar} "" PARENT_SCOPE)
  1432. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "Intel")
  1433. # Intel PCH support requires version >= 8.0.0
  1434. cotire_determine_compiler_version("${_language}" COTIRE_${_language}_COMPILER)
  1435. if ("${COTIRE_${_language}_COMPILER_VERSION}" MATCHES ".+" AND
  1436. "${COTIRE_${_language}_COMPILER_VERSION}" VERSION_LESS "8.0.0")
  1437. set (${_msgVar} "${_unsupportedCompiler} version ${COTIRE_${_language}_COMPILER_VERSION}." PARENT_SCOPE)
  1438. else()
  1439. set (${_msgVar} "" PARENT_SCOPE)
  1440. endif()
  1441. else()
  1442. set (${_msgVar} "${_unsupportedCompiler}." PARENT_SCOPE)
  1443. endif()
  1444. if (APPLE)
  1445. # PCH compilation not supported by GCC / Clang for multi-architecture builds (e.g., i386, x86_64)
  1446. if (CMAKE_CONFIGURATION_TYPES)
  1447. set (_configs ${CMAKE_CONFIGURATION_TYPES})
  1448. elseif (CMAKE_BUILD_TYPE)
  1449. set (_configs ${CMAKE_BUILD_TYPE})
  1450. else()
  1451. set (_configs "None")
  1452. endif()
  1453. foreach (_config ${_configs})
  1454. set (_targetFlags "")
  1455. cotire_get_target_compile_flags("${_config}" "${_language}" "${_targetSourceDir}" "${_target}" _targetFlags)
  1456. cotire_filter_compile_flags("${_language}" "arch" _architectures _ignore ${_targetFlags})
  1457. list (LENGTH _architectures _numberOfArchitectures)
  1458. if (_numberOfArchitectures GREATER 1)
  1459. string (REPLACE ";" ", " _architectureStr "${_architectures}")
  1460. set (${_msgVar}
  1461. "Precompiled headers not supported on Darwin for multi-architecture builds (${_architectureStr})."
  1462. PARENT_SCOPE)
  1463. break()
  1464. endif()
  1465. endforeach()
  1466. endif()
  1467. endfunction()
  1468. macro (cotire_get_intermediate_dir _cotireDir)
  1469. get_filename_component(${_cotireDir} "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/${COTIRE_INTDIR}" ABSOLUTE)
  1470. endmacro()
  1471. function (cotire_make_untiy_source_file_paths _language _target _maxIncludes _unityFilesVar)
  1472. set (_sourceFiles ${ARGN})
  1473. list (LENGTH _sourceFiles _numberOfSources)
  1474. set (_unityFileExt_C ".c")
  1475. set (_unityFileExt_CXX ".cxx")
  1476. if (NOT DEFINED _unityFileExt_${_language})
  1477. set (${_unityFileVar} "" PARENT_SCOPE)
  1478. return()
  1479. endif()
  1480. set (_unityFileBaseName "${_target}_${_language}${COTIRE_UNITY_SOURCE_FILENAME_SUFFIX}")
  1481. cotire_get_intermediate_dir(_baseDir)
  1482. set (_startIndex 0)
  1483. set (_index 0)
  1484. set (_unityFiles "")
  1485. foreach (_sourceFile ${_sourceFiles})
  1486. get_source_file_property(_startNew "${_sourceFile}" COTIRE_START_NEW_UNITY_SOURCE)
  1487. math (EXPR _unityFileCount "${_index} - ${_startIndex}")
  1488. if (_startNew OR (_maxIncludes GREATER 0 AND NOT _unityFileCount LESS _maxIncludes))
  1489. if (_index GREATER 0)
  1490. math (EXPR _endIndex "${_index} - 1")
  1491. set (_unityFileName "${_unityFileBaseName}_${_startIndex}_${_endIndex}${_unityFileExt_${_language}}")
  1492. list (APPEND _unityFiles "${_baseDir}/${_unityFileName}")
  1493. endif()
  1494. set (_startIndex ${_index})
  1495. endif()
  1496. math (EXPR _index "${_index} + 1")
  1497. endforeach()
  1498. if (_startIndex EQUAL 0)
  1499. set (_unityFileName "${_unityFileBaseName}${_unityFileExt_${_language}}")
  1500. list (APPEND _unityFiles "${_baseDir}/${_unityFileName}")
  1501. elseif (_startIndex LESS _numberOfSources)
  1502. math (EXPR _endIndex "${_index} - 1")
  1503. set (_unityFileName "${_unityFileBaseName}_${_startIndex}_${_endIndex}${_unityFileExt_${_language}}")
  1504. list (APPEND _unityFiles "${_baseDir}/${_unityFileName}")
  1505. endif()
  1506. set (${_unityFilesVar} ${_unityFiles} PARENT_SCOPE)
  1507. if (COTIRE_DEBUG)
  1508. message(STATUS "${_unityFiles}")
  1509. endif()
  1510. endfunction()
  1511. function (cotire_make_prefix_file_name _language _target _prefixFileBaseNameVar _prefixFileNameVar)
  1512. set (_prefixFileExt_C ".h")
  1513. set (_prefixFileExt_CXX ".hxx")
  1514. if (NOT _language)
  1515. set (_prefixFileBaseName "${_target}${COTIRE_PREFIX_HEADER_FILENAME_SUFFIX}")
  1516. set (_prefixFileName "${_prefixFileBaseName}${_prefixFileExt_C}")
  1517. elseif (DEFINED _prefixFileExt_${_language})
  1518. set (_prefixFileBaseName "${_target}_${_language}${COTIRE_PREFIX_HEADER_FILENAME_SUFFIX}")
  1519. set (_prefixFileName "${_prefixFileBaseName}${_prefixFileExt_${_language}}")
  1520. else()
  1521. set (_prefixFileBaseName "")
  1522. set (_prefixFileName "")
  1523. endif()
  1524. set (${_prefixFileBaseNameVar} "${_prefixFileBaseName}" PARENT_SCOPE)
  1525. set (${_prefixFileNameVar} "${_prefixFileName}" PARENT_SCOPE)
  1526. endfunction()
  1527. function (cotire_make_prefix_file_path _language _target _prefixFileVar)
  1528. cotire_make_prefix_file_name("${_language}" "${_target}" _prefixFileBaseName _prefixFileName)
  1529. set (${_prefixFileVar} "" PARENT_SCOPE)
  1530. if (_prefixFileName)
  1531. if (NOT _language)
  1532. set (_language "C")
  1533. endif()
  1534. if (MSVC OR CMAKE_${_language}_COMPILER_ID MATCHES "GNU|Clang|Intel")
  1535. cotire_get_intermediate_dir(_baseDir)
  1536. set (${_prefixFileVar} "${_baseDir}/${_prefixFileName}" PARENT_SCOPE)
  1537. endif()
  1538. endif()
  1539. endfunction()
  1540. function (cotire_make_pch_file_path _language _targetSourceDir _target _pchFileVar)
  1541. cotire_make_prefix_file_name("${_language}" "${_target}" _prefixFileBaseName _prefixFileName)
  1542. set (${_pchFileVar} "" PARENT_SCOPE)
  1543. if (_prefixFileBaseName AND _prefixFileName)
  1544. cotire_check_precompiled_header_support("${_language}" "${_targetSourceDir}" "${_target}" _msg)
  1545. if (NOT _msg)
  1546. if (XCODE)
  1547. # For Xcode, we completely hand off the compilation of the prefix header to the IDE
  1548. return()
  1549. endif()
  1550. cotire_get_intermediate_dir(_baseDir)
  1551. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC")
  1552. # MSVC uses the extension .pch added to the prefix header base name
  1553. set (${_pchFileVar} "${_baseDir}/${_prefixFileBaseName}.pch" PARENT_SCOPE)
  1554. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "GNU|Clang")
  1555. # GCC / Clang look for a precompiled header corresponding to the prefix header with the extension .gch appended
  1556. set (${_pchFileVar} "${_baseDir}/${_prefixFileName}.gch" PARENT_SCOPE)
  1557. elseif (CMAKE_${_language}_COMPILER_ID MATCHES "Intel")
  1558. # Intel uses the extension .pchi added to the prefix header base name
  1559. set (${_pchFileVar} "${_baseDir}/${_prefixFileBaseName}.pchi" PARENT_SCOPE)
  1560. endif()
  1561. endif()
  1562. endif()
  1563. endfunction()
  1564. function (cotire_select_unity_source_files _unityFile _sourcesVar)
  1565. set (_sourceFiles ${ARGN})
  1566. if (_sourceFiles AND "${_unityFile}" MATCHES "${COTIRE_UNITY_SOURCE_FILENAME_SUFFIX}_([0-9]+)_([0-9]+)")
  1567. set (_startIndex ${CMAKE_MATCH_1})
  1568. set (_endIndex ${CMAKE_MATCH_2})
  1569. list (LENGTH _sourceFiles _numberOfSources)
  1570. if (NOT _startIndex LESS _numberOfSources)
  1571. math (EXPR _startIndex "${_numberOfSources} - 1")
  1572. endif()
  1573. if (NOT _endIndex LESS _numberOfSources)
  1574. math (EXPR _endIndex "${_numberOfSources} - 1")
  1575. endif()
  1576. set (_files "")
  1577. foreach (_index RANGE ${_startIndex} ${_endIndex})
  1578. list (GET _sourceFiles ${_index} _file)
  1579. list (APPEND _files "${_file}")
  1580. endforeach()
  1581. else()
  1582. set (_files ${_sourceFiles})
  1583. endif()
  1584. set (${_sourcesVar} ${_files} PARENT_SCOPE)
  1585. endfunction()
  1586. function (cotire_get_unity_source_dependencies _language _target _dependencySourcesVar)
  1587. set (_dependencySources "")
  1588. # depend on target's generated source files
  1589. cotire_get_objects_with_property_on(_generatedSources GENERATED SOURCE ${ARGN})
  1590. if (_generatedSources)
  1591. # but omit all generated source files that have the COTIRE_EXCLUDED property set to true
  1592. cotire_get_objects_with_property_on(_excludedGeneratedSources COTIRE_EXCLUDED SOURCE ${_generatedSources})
  1593. if (_excludedGeneratedSources)
  1594. list (REMOVE_ITEM _generatedSources ${_excludedGeneratedSources})
  1595. endif()
  1596. # and omit all generated source files that have the COTIRE_DEPENDENCY property set to false explicitly
  1597. cotire_get_objects_with_property_off(_excludedNonDependencySources COTIRE_DEPENDENCY SOURCE ${_generatedSources})
  1598. if (_excludedNonDependencySources)
  1599. list (REMOVE_ITEM _generatedSources ${_excludedNonDependencySources})
  1600. endif()
  1601. if (_generatedSources)
  1602. list (APPEND _dependencySources ${_generatedSources})
  1603. endif()
  1604. endif()
  1605. if (COTIRE_DEBUG AND _dependencySources)
  1606. message (STATUS "${_language} ${_target} unity source depends on ${_dependencySources}")
  1607. endif()
  1608. set (${_dependencySourcesVar} ${_dependencySources} PARENT_SCOPE)
  1609. endfunction()
  1610. function (cotire_get_prefix_header_dependencies _language _target _dependencySourcesVar)
  1611. # depend on target source files marked with custom COTIRE_DEPENDENCY property
  1612. set (_dependencySources "")
  1613. cotire_get_objects_with_property_on(_dependencySources COTIRE_DEPENDENCY SOURCE ${ARGN})
  1614. if (COTIRE_DEBUG AND _dependencySources)
  1615. message (STATUS "${_language} ${_target} prefix header DEPENDS ${_dependencySources}")
  1616. endif()
  1617. set (${_dependencySourcesVar} ${_dependencySources} PARENT_SCOPE)
  1618. endfunction()
  1619. function (cotire_generate_target_script _language _configurations _targetSourceDir _targetBinaryDir _target _targetScriptVar)
  1620. set (COTIRE_TARGET_SOURCES ${ARGN})
  1621. get_filename_component(_moduleName "${COTIRE_CMAKE_MODULE_FILE}" NAME)
  1622. set (_targetCotireScript "${CMAKE_CURRENT_BINARY_DIR}/${_target}_${_language}_${_moduleName}")
  1623. cotire_get_prefix_header_dependencies(${_language} ${_target} COTIRE_TARGET_PREFIX_DEPENDS ${COTIRE_TARGET_SOURCES})
  1624. cotire_get_unity_source_dependencies(${_language} ${_target} COTIRE_TARGET_UNITY_DEPENDS ${COTIRE_TARGET_SOURCES})
  1625. # set up variables to be configured
  1626. set (COTIRE_TARGET_LANGUAGE "${_language}")
  1627. cotire_determine_compiler_version("${COTIRE_TARGET_LANGUAGE}" COTIRE_${_language}_COMPILER)
  1628. get_target_property(COTIRE_TARGET_IGNORE_PATH ${_target} COTIRE_PREFIX_HEADER_IGNORE_PATH)
  1629. cotire_add_sys_root_paths(COTIRE_TARGET_IGNORE_PATH)
  1630. get_target_property(COTIRE_TARGET_INCLUDE_PATH ${_target} COTIRE_PREFIX_HEADER_INCLUDE_PATH)
  1631. cotire_add_sys_root_paths(COTIRE_TARGET_INCLUDE_PATH)
  1632. get_target_property(COTIRE_TARGET_PRE_UNDEFS ${_target} COTIRE_UNITY_SOURCE_PRE_UNDEFS)
  1633. get_target_property(COTIRE_TARGET_POST_UNDEFS ${_target} COTIRE_UNITY_SOURCE_POST_UNDEFS)
  1634. get_target_property(COTIRE_TARGET_MAXIMUM_NUMBER_OF_INCLUDES ${_target} COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES)
  1635. cotire_get_source_files_undefs(COTIRE_UNITY_SOURCE_PRE_UNDEFS COTIRE_TARGET_SOURCES_PRE_UNDEFS ${COTIRE_TARGET_SOURCES})
  1636. cotire_get_source_files_undefs(COTIRE_UNITY_SOURCE_POST_UNDEFS COTIRE_TARGET_SOURCES_POST_UNDEFS ${COTIRE_TARGET_SOURCES})
  1637. set (COTIRE_TARGET_CONFIGURATION_TYPES "${_configurations}")
  1638. foreach (_config ${_configurations})
  1639. string (TOUPPER "${_config}" _upperConfig)
  1640. cotire_get_target_include_directories(
  1641. "${_config}" "${_language}" "${_targetSourceDir}" "${_targetBinaryDir}" "${_target}" COTIRE_TARGET_INCLUDE_DIRECTORIES_${_upperConfig})
  1642. cotire_get_target_compile_definitions(
  1643. "${_config}" "${_language}" "${_targetSourceDir}" "${_target}" COTIRE_TARGET_COMPILE_DEFINITIONS_${_upperConfig})
  1644. cotire_get_target_compiler_flags(
  1645. "${_config}" "${_language}" "${_targetSourceDir}" "${_target}" COTIRE_TARGET_COMPILE_FLAGS_${_upperConfig})
  1646. cotire_get_source_files_compile_definitions(
  1647. "${_config}" "${_language}" COTIRE_TARGET_SOURCES_COMPILE_DEFINITIONS_${_upperConfig} ${COTIRE_TARGET_SOURCES})
  1648. endforeach()
  1649. get_cmake_property(_vars VARIABLES)
  1650. string (REGEX MATCHALL "COTIRE_[A-Za-z0-9_]+" _matchVars "${_vars}")
  1651. # remove COTIRE_VERBOSE which is passed as a CMake define on command line
  1652. list (REMOVE_ITEM _matchVars COTIRE_VERBOSE)
  1653. set (_contents "")
  1654. foreach (_var IN LISTS _matchVars ITEMS
  1655. MSVC CMAKE_GENERATOR CMAKE_BUILD_TYPE CMAKE_CONFIGURATION_TYPES
  1656. CMAKE_${_language}_COMPILER_ID CMAKE_${_language}_COMPILER CMAKE_${_language}_COMPILER_ARG1
  1657. CMAKE_${_language}_SOURCE_FILE_EXTENSIONS)
  1658. if (DEFINED ${_var})
  1659. string (REPLACE "\"" "\\\"" _value "${${_var}}")
  1660. set (_contents "${_contents}set (${_var} \"${_value}\")\n")
  1661. endif()
  1662. endforeach()
  1663. cotire_write_file("CMAKE" "${_targetCotireScript}" "${_contents}" FALSE)
  1664. set (${_targetScriptVar} "${_targetCotireScript}" PARENT_SCOPE)
  1665. endfunction()
  1666. function (cotire_setup_pch_file_compilation _language _targetBinaryDir _targetScript _prefixFile _pchFile)
  1667. set (_sourceFiles ${ARGN})
  1668. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  1669. # for Visual Studio and Intel, we attach the precompiled header compilation to the first source file
  1670. # the remaining files include the precompiled header, see cotire_setup_prefix_file_inclusion
  1671. if (_sourceFiles)
  1672. file (TO_NATIVE_PATH "${_prefixFile}" _prefixFileNative)
  1673. file (TO_NATIVE_PATH "${_pchFile}" _pchFileNative)
  1674. list (GET _sourceFiles 0 _hostFile)
  1675. set (_flags "")
  1676. cotire_determine_compiler_version("${_language}" COTIRE_${_language}_COMPILER)
  1677. cotire_add_pch_compilation_flags(
  1678. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${COTIRE_${_language}_COMPILER_VERSION}"
  1679. "${_prefixFile}" "${_pchFile}" "${_hostFile}" _flags)
  1680. set_property (SOURCE ${_hostFile} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  1681. set_property (SOURCE ${_hostFile} APPEND PROPERTY OBJECT_OUTPUTS "${_pchFile}")
  1682. # make first source file depend on prefix header
  1683. set_property (SOURCE ${_hostFile} APPEND PROPERTY OBJECT_DEPENDS "${_prefixFile}")
  1684. endif()
  1685. elseif ("${CMAKE_GENERATOR}" MATCHES "Makefiles|Ninja")
  1686. # for makefile based generator, we add a custom command to precompile the prefix header
  1687. if (_targetScript)
  1688. cotire_set_cmd_to_prologue(_cmds)
  1689. list (GET _sourceFiles 0 _hostFile)
  1690. list (APPEND _cmds -P "${COTIRE_CMAKE_MODULE_FILE}" "precompile" "${_targetScript}" "${_prefixFile}" "${_pchFile}" "${_hostFile}")
  1691. file (RELATIVE_PATH _pchFileRelPath "${CMAKE_BINARY_DIR}" "${_pchFile}")
  1692. if (COTIRE_DEBUG)
  1693. message (STATUS "add_custom_command: OUTPUT ${_pchFile} ${_cmds} DEPENDS ${_prefixFile} IMPLICIT_DEPENDS ${_language} ${_prefixFile}")
  1694. endif()
  1695. set_property (SOURCE "${_pchFile}" PROPERTY GENERATED TRUE)
  1696. add_custom_command(OUTPUT "${_pchFile}"
  1697. COMMAND ${_cmds}
  1698. DEPENDS "${_prefixFile}"
  1699. IMPLICIT_DEPENDS ${_language} "${_prefixFile}"
  1700. WORKING_DIRECTORY "${_targetSourceDir}"
  1701. COMMENT "Building ${_language} precompiled header ${_pchFileRelPath}" VERBATIM)
  1702. endif()
  1703. endif()
  1704. endfunction()
  1705. function (cotire_setup_prefix_file_inclusion _language _target _wholeTarget _prefixFile _pchFile)
  1706. set (_sourceFiles ${ARGN})
  1707. if (CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  1708. # for Visual Studio and Intel, we include the precompiled header in all but the first source file
  1709. # the first source file does the precompiled header compilation, see cotire_setup_pch_file_compilation
  1710. list (LENGTH _sourceFiles _numberOfSourceFiles)
  1711. if (_numberOfSourceFiles GREATER 1)
  1712. # mark sources as cotired to prevent them from being used in another cotired target
  1713. set_source_files_properties(${_sourceFiles} PROPERTIES COTIRE_TARGET "${_target}")
  1714. list (REMOVE_AT _sourceFiles 0)
  1715. set (_flags "")
  1716. cotire_determine_compiler_version("${_language}" COTIRE_${_language}_COMPILER)
  1717. cotire_add_pch_inclusion_flags(
  1718. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${COTIRE_${_language}_COMPILER_VERSION}"
  1719. "${_prefixFile}" "${_pchFile}" _flags)
  1720. set_property (SOURCE ${_sourceFiles} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  1721. # make source files depend on precompiled header
  1722. set_property (SOURCE ${_sourceFiles} APPEND PROPERTY OBJECT_DEPENDS "${_pchFile}")
  1723. endif()
  1724. elseif ("${CMAKE_GENERATOR}" MATCHES "Makefiles|Ninja")
  1725. if (NOT _wholeTarget)
  1726. # for makefile based generator, we force the inclusion of the prefix header for a subset
  1727. # of the source files, if this is a multi-language target or has excluded files
  1728. set (_flags "")
  1729. cotire_determine_compiler_version("${_language}" COTIRE_${_language}_COMPILER)
  1730. cotire_add_pch_inclusion_flags(
  1731. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${COTIRE_${_language}_COMPILER_VERSION}"
  1732. "${_prefixFile}" "${_pchFile}" _flags)
  1733. set_property (SOURCE ${_sourceFiles} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  1734. # mark sources as cotired to prevent them from being used in another cotired target
  1735. set_source_files_properties(${_sourceFiles} PROPERTIES COTIRE_TARGET "${_target}")
  1736. endif()
  1737. # make source files depend on precompiled header
  1738. set_property (SOURCE ${_sourceFiles} APPEND PROPERTY OBJECT_DEPENDS "${_pchFile}")
  1739. endif()
  1740. endfunction()
  1741. function (cotire_get_first_set_property_value _propertyValueVar _type _object)
  1742. set (_properties ${ARGN})
  1743. foreach (_property ${_properties})
  1744. get_property(_propertyValue ${_type} "${_object}" PROPERTY ${_property})
  1745. if (_propertyValue)
  1746. set (${_propertyValueVar} ${_propertyValue} PARENT_SCOPE)
  1747. return()
  1748. endif()
  1749. endforeach()
  1750. set (${_propertyValueVar} "" PARENT_SCOPE)
  1751. endfunction()
  1752. function (cotire_setup_target_pch_usage _languages _targetSourceDir _target _wholeTarget)
  1753. if (XCODE)
  1754. # for Xcode, we attach a pre-build action to generate the unity sources and prefix headers
  1755. # if necessary, we also generate a single prefix header which includes all language specific prefix headers
  1756. set (_prefixFiles "")
  1757. foreach (_language ${_languages})
  1758. get_property(_prefixFile TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER)
  1759. if (_prefixFile)
  1760. list (APPEND _prefixFiles "${_prefixFile}")
  1761. endif()
  1762. endforeach()
  1763. set (_cmds ${ARGN})
  1764. list (LENGTH _prefixFiles _numberOfPrefixFiles)
  1765. if (_numberOfPrefixFiles GREATER 1)
  1766. cotire_make_prefix_file_path("" ${_target} _prefixHeader)
  1767. cotire_setup_combine_command("${_targetSourceDir}" "" "${_prefixHeader}" "${_prefixFiles}" _cmds)
  1768. else()
  1769. set (_prefixHeader "${_prefixFiles}")
  1770. endif()
  1771. if (COTIRE_DEBUG)
  1772. message (STATUS "add_custom_command: TARGET ${_target} PRE_BUILD ${_cmds}")
  1773. endif()
  1774. add_custom_command(TARGET "${_target}"
  1775. PRE_BUILD ${_cmds}
  1776. WORKING_DIRECTORY "${_targetSourceDir}"
  1777. COMMENT "Updating target ${_target} prefix headers" VERBATIM)
  1778. # make Xcode precompile the generated prefix header with ProcessPCH and ProcessPCH++
  1779. set_target_properties(${_target} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER "YES")
  1780. set_target_properties(${_target} PROPERTIES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER "${_prefixHeader}")
  1781. elseif ("${CMAKE_GENERATOR}" MATCHES "Makefiles|Ninja")
  1782. # for makefile based generator, we force inclusion of the prefix header for all target source files
  1783. # if this is a single-language target without any excluded files
  1784. if (_wholeTarget)
  1785. set (_language "${_languages}")
  1786. # for Visual Studio and Intel, precompiled header inclusion is always done on the source file level
  1787. # see cotire_setup_prefix_file_inclusion
  1788. if (NOT CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  1789. get_property(_prefixFile TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER)
  1790. get_property(_pchFile TARGET ${_target} PROPERTY COTIRE_${_language}_PRECOMPILED_HEADER)
  1791. set (_flags "")
  1792. cotire_determine_compiler_version("${_language}" COTIRE_${_language}_COMPILER)
  1793. cotire_add_pch_inclusion_flags(
  1794. "${_language}" "${CMAKE_${_language}_COMPILER_ID}" "${COTIRE_${_language}_COMPILER_VERSION}"
  1795. "${_prefixFile}" "${_pchFile}" _flags)
  1796. set_property (TARGET ${_target} APPEND_STRING PROPERTY COMPILE_FLAGS " ${_flags} ")
  1797. endif()
  1798. endif()
  1799. endif()
  1800. endfunction()
  1801. function (cotire_setup_unity_generation_commands _language _targetSourceDir _target _targetScript _unityFiles _cmdsVar)
  1802. set (_dependencySources "")
  1803. cotire_get_unity_source_dependencies(${_language} ${_target} _dependencySources ${ARGN})
  1804. foreach (_unityFile ${_unityFiles})
  1805. file (RELATIVE_PATH _unityFileRelPath "${CMAKE_BINARY_DIR}" "${_unityFile}")
  1806. set_property (SOURCE "${_unityFile}" PROPERTY GENERATED TRUE)
  1807. # set up compiled unity source dependencies
  1808. # this ensures that missing source files are generated before the unity file is compiled
  1809. if (COTIRE_DEBUG AND _dependencySources)
  1810. message (STATUS "${_unityFile} OBJECT_DEPENDS ${_dependencySources}")
  1811. endif()
  1812. if (_dependencySources)
  1813. set_property (SOURCE "${_unityFile}" PROPERTY OBJECT_DEPENDS ${_dependencySources})
  1814. endif()
  1815. if (WIN32 AND CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  1816. # unity file compilation results in potentially huge object file, thus use /bigobj by default unter MSVC and Windows Intel
  1817. set_property (SOURCE "${_unityFile}" APPEND_STRING PROPERTY COMPILE_FLAGS "/bigobj")
  1818. endif()
  1819. cotire_set_cmd_to_prologue(_unityCmd)
  1820. list (APPEND _unityCmd -P "${COTIRE_CMAKE_MODULE_FILE}" "unity" "${_targetScript}" "${_unityFile}")
  1821. if (COTIRE_DEBUG)
  1822. message (STATUS "add_custom_command: OUTPUT ${_unityFile} COMMAND ${_unityCmd} DEPENDS ${_targetScript}")
  1823. endif()
  1824. add_custom_command(
  1825. OUTPUT "${_unityFile}"
  1826. COMMAND ${_unityCmd}
  1827. DEPENDS "${_targetScript}"
  1828. COMMENT "Generating ${_language} unity source ${_unityFileRelPath}"
  1829. WORKING_DIRECTORY "${_targetSourceDir}" VERBATIM)
  1830. list (APPEND ${_cmdsVar} COMMAND ${_unityCmd})
  1831. endforeach()
  1832. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  1833. endfunction()
  1834. function (cotire_setup_prefix_generation_command _language _target _targetSourceDir _targetScript _prefixFile _unityFiles _cmdsVar)
  1835. set (_sourceFiles ${ARGN})
  1836. list (LENGTH _unityFiles _numberOfUnityFiles)
  1837. if (_numberOfUnityFiles GREATER 1)
  1838. # create a joint unity file from all unity file segments
  1839. cotire_make_untiy_source_file_paths(${_language} ${_target} 0 _unityFile ${_unityFiles})
  1840. cotire_setup_combine_command("${_targetSourceDir}" "${_targetScript}" "${_unityFile}" "${_unityFiles}" ${_cmdsVar})
  1841. else()
  1842. set (_unityFile "${_unityFiles}")
  1843. endif()
  1844. file (RELATIVE_PATH _prefixFileRelPath "${CMAKE_BINARY_DIR}" "${_prefixFile}")
  1845. set (_dependencySources "")
  1846. cotire_get_prefix_header_dependencies(${_language} ${_target} _dependencySources ${_sourceFiles})
  1847. cotire_set_cmd_to_prologue(_prefixCmd)
  1848. list (APPEND _prefixCmd -P "${COTIRE_CMAKE_MODULE_FILE}" "prefix" "${_targetScript}" "${_prefixFile}" "${_unityFile}")
  1849. set_property (SOURCE "${_prefixFile}" PROPERTY GENERATED TRUE)
  1850. if (COTIRE_DEBUG)
  1851. message (STATUS "add_custom_command: OUTPUT ${_prefixFile} COMMAND ${_prefixCmd} DEPENDS ${_targetScript} ${_unityFile} ${_dependencySources}")
  1852. endif()
  1853. add_custom_command(
  1854. OUTPUT "${_prefixFile}" "${_prefixFile}.log"
  1855. COMMAND ${_prefixCmd}
  1856. DEPENDS "${_targetScript}" "${_unityFile}" ${_dependencySources}
  1857. COMMENT "Generating ${_language} prefix header ${_prefixFileRelPath}"
  1858. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" VERBATIM)
  1859. list (APPEND ${_cmdsVar} COMMAND ${_prefixCmd})
  1860. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  1861. endfunction()
  1862. function (cotire_setup_combine_command _sourceDir _targetScript _joinedFile _files _cmdsVar)
  1863. set (_filesPaths "")
  1864. foreach (_file ${_files})
  1865. if (IS_ABSOLUTE "${_file}")
  1866. set (_filePath "${_file}")
  1867. else()
  1868. get_filename_component(_filePath "${_sourceDir}/${_file}" ABSOLUTE)
  1869. endif()
  1870. file (RELATIVE_PATH _fileRelPath "${CMAKE_BINARY_DIR}" "${_filePath}")
  1871. if (NOT IS_ABSOLUTE "${_fileRelPath}" AND NOT "${_fileRelPath}" MATCHES "^\\.\\.")
  1872. list (APPEND _filesPaths "${_fileRelPath}")
  1873. else()
  1874. list (APPEND _filesPaths "${_filePath}")
  1875. endif()
  1876. endforeach()
  1877. cotire_set_cmd_to_prologue(_prefixCmd)
  1878. list (APPEND _prefixCmd -P "${COTIRE_CMAKE_MODULE_FILE}" "combine")
  1879. if (_targetScript)
  1880. list (APPEND _prefixCmd "${_targetScript}")
  1881. endif()
  1882. list (APPEND _prefixCmd "${_joinedFile}" ${_filesPaths})
  1883. if (COTIRE_DEBUG)
  1884. message (STATUS "add_custom_command: OUTPUT ${_joinedFile} COMMAND ${_prefixCmd} DEPENDS ${_files}")
  1885. endif()
  1886. set_property (SOURCE "${_joinedFile}" PROPERTY GENERATED TRUE)
  1887. file (RELATIVE_PATH _joinedFileRelPath "${CMAKE_BINARY_DIR}" "${_joinedFile}")
  1888. add_custom_command(
  1889. OUTPUT "${_joinedFile}"
  1890. COMMAND ${_prefixCmd}
  1891. DEPENDS ${_files}
  1892. COMMENT "Generating ${_joinedFileRelPath}"
  1893. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" VERBATIM)
  1894. list (APPEND ${_cmdsVar} COMMAND ${_prefixCmd})
  1895. set (${_cmdsVar} ${${_cmdsVar}} PARENT_SCOPE)
  1896. endfunction()
  1897. function (cotire_init_cotire_target_properties _target)
  1898. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_ENABLE_PRECOMPILED_HEADER SET)
  1899. if (NOT _isSet)
  1900. set_property(TARGET ${_target} PROPERTY COTIRE_ENABLE_PRECOMPILED_HEADER TRUE)
  1901. endif()
  1902. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_ADD_UNITY_BUILD SET)
  1903. if (NOT _isSet)
  1904. set_property(TARGET ${_target} PROPERTY COTIRE_ADD_UNITY_BUILD TRUE)
  1905. endif()
  1906. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_ADD_CLEAN SET)
  1907. if (NOT _isSet)
  1908. set_property(TARGET ${_target} PROPERTY COTIRE_ADD_CLEAN FALSE)
  1909. endif()
  1910. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_IGNORE_PATH SET)
  1911. if (NOT _isSet)
  1912. set_property(TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_IGNORE_PATH "${CMAKE_SOURCE_DIR}")
  1913. cotire_check_is_path_relative_to("${CMAKE_BINARY_DIR}" _isRelative "${CMAKE_SOURCE_DIR}")
  1914. if (NOT _isRelative)
  1915. set_property(TARGET ${_target} APPEND PROPERTY COTIRE_PREFIX_HEADER_IGNORE_PATH "${CMAKE_BINARY_DIR}")
  1916. endif()
  1917. endif()
  1918. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_INCLUDE_PATH SET)
  1919. if (NOT _isSet)
  1920. set_property(TARGET ${_target} PROPERTY COTIRE_PREFIX_HEADER_INCLUDE_PATH "")
  1921. endif()
  1922. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_PRE_UNDEFS SET)
  1923. if (NOT _isSet)
  1924. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_PRE_UNDEFS "")
  1925. endif()
  1926. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_POST_UNDEFS SET)
  1927. if (NOT _isSet)
  1928. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_POST_UNDEFS "")
  1929. endif()
  1930. get_property(_isSet TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES SET)
  1931. if (NOT _isSet)
  1932. if (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES)
  1933. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES "${COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES}")
  1934. else()
  1935. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES "")
  1936. endif()
  1937. endif()
  1938. endfunction()
  1939. function (cotire_make_target_message _target _languages _disableMsg _targetMsgVar)
  1940. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  1941. get_target_property(_targetAddSCU ${_target} COTIRE_ADD_UNITY_BUILD)
  1942. string (REPLACE ";" " " _languagesStr "${_languages}")
  1943. string (REPLACE ";" ", " _excludedStr "${ARGN}")
  1944. set (_targetMsg "")
  1945. if (NOT _languages)
  1946. set (_targetMsg "Target ${_target} cannot be cotired.")
  1947. if (_disableMsg)
  1948. set (_targetMsg "${_targetMsg} ${_disableMsg}")
  1949. endif()
  1950. elseif (NOT _targetUsePCH AND NOT _targetAddSCU)
  1951. set (_targetMsg "${_languagesStr} target ${_target} cotired without unity build and precompiled header.")
  1952. if (_disableMsg)
  1953. set (_targetMsg "${_targetMsg} ${_disableMsg}")
  1954. endif()
  1955. elseif (NOT _targetUsePCH)
  1956. if (_allExcludedSourceFiles)
  1957. set (_targetMsg "${_languagesStr} target ${_target} cotired excluding files ${_excludedStr} without precompiled header.")
  1958. else()
  1959. set (_targetMsg "${_languagesStr} target ${_target} cotired without precompiled header.")
  1960. endif()
  1961. if (_disableMsg)
  1962. set (_targetMsg "${_targetMsg} ${_disableMsg}")
  1963. endif()
  1964. elseif (NOT _targetAddSCU)
  1965. if (_allExcludedSourceFiles)
  1966. set (_targetMsg "${_languagesStr} target ${_target} cotired excluding files ${_excludedStr} without unity build.")
  1967. else()
  1968. set (_targetMsg "${_languagesStr} target ${_target} cotired without unity build.")
  1969. endif()
  1970. else()
  1971. if (_allExcludedSourceFiles)
  1972. set (_targetMsg "${_languagesStr} target ${_target} cotired excluding files ${_excludedStr}.")
  1973. else()
  1974. set (_targetMsg "${_languagesStr} target ${_target} cotired.")
  1975. endif()
  1976. endif()
  1977. set (${_targetMsgVar} "${_targetMsg}" PARENT_SCOPE)
  1978. endfunction()
  1979. function (cotire_choose_target_languages _targetSourceDir _target _targetLanguagesVar)
  1980. set (_languages ${ARGN})
  1981. set (_allSourceFiles "")
  1982. set (_allExcludedSourceFiles "")
  1983. set (_allCotiredSourceFiles "")
  1984. set (_targetLanguages "")
  1985. get_target_property(_targetType ${_target} TYPE)
  1986. get_target_property(_targetSourceFiles ${_target} SOURCES)
  1987. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  1988. get_target_property(_targetAddSCU ${_target} COTIRE_ADD_UNITY_BUILD)
  1989. set (_disableMsg "")
  1990. foreach (_language ${_languages})
  1991. get_target_property(_prefixHeader ${_target} COTIRE_${_language}_PREFIX_HEADER)
  1992. get_target_property(_unityBuildFile ${_target} COTIRE_${_language}_UNITY_SOURCE)
  1993. if (_prefixHeader OR _unityBuildFile)
  1994. message (WARNING "Target ${_target} has already been cotired.")
  1995. set (${_targetLanguagesVar} "" PARENT_SCOPE)
  1996. return()
  1997. endif()
  1998. if (_targetUsePCH AND "${_language}" STREQUAL "C" OR "${_language}" STREQUAL "CXX")
  1999. cotire_check_precompiled_header_support("${_language}" "${_targetSourceDir}" "${_target}" _disableMsg)
  2000. if (_disableMsg)
  2001. set (_targetUsePCH FALSE)
  2002. endif()
  2003. endif()
  2004. set (_sourceFiles "")
  2005. set (_excludedSources "")
  2006. set (_cotiredSources "")
  2007. cotire_filter_language_source_files(${_language} _sourceFiles _excludedSources _cotiredSources ${_targetSourceFiles})
  2008. if (_sourceFiles OR _excludedSources OR _cotiredSources)
  2009. list (APPEND _targetLanguages ${_language})
  2010. endif()
  2011. if (_sourceFiles)
  2012. list (APPEND _allSourceFiles ${_sourceFiles})
  2013. endif()
  2014. if (_excludedSources)
  2015. list (APPEND _allExcludedSourceFiles ${_excludedSources})
  2016. endif()
  2017. if (_cotiredSources)
  2018. list (APPEND _allCotiredSourceFiles ${_cotiredSources})
  2019. endif()
  2020. endforeach()
  2021. set (_targetMsgLevel STATUS)
  2022. if (NOT _targetLanguages)
  2023. string (REPLACE ";" " or " _languagesStr "${_languages}")
  2024. set (_disableMsg "No ${_languagesStr} source files.")
  2025. set (_targetUsePCH FALSE)
  2026. set (_targetAddSCU FALSE)
  2027. endif()
  2028. if (_targetUsePCH)
  2029. list (LENGTH _allSourceFiles _numberOfSources)
  2030. if (_numberOfSources LESS ${COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES})
  2031. set (_disableMsg "Too few applicable sources.")
  2032. set (_targetUsePCH FALSE)
  2033. elseif (_allCotiredSourceFiles)
  2034. cotire_get_source_file_property_values(_cotireTargets COTIRE_TARGET ${_allCotiredSourceFiles})
  2035. list (REMOVE_DUPLICATES _cotireTargets)
  2036. string (REPLACE ";" ", " _cotireTargetsStr "${_cotireTargets}")
  2037. set (_disableMsg "Target sources already include a precompiled header for target(s) ${_cotireTargets}.")
  2038. set (_disableMsg "${_disableMsg} Set target property COTIRE_ENABLE_PRECOMPILED_HEADER to FALSE for targets ${_target},")
  2039. set (_disableMsg "${_disableMsg} ${_cotireTargetsStr} to get a workable build system.")
  2040. set (_targetMsgLevel SEND_ERROR)
  2041. set (_targetUsePCH FALSE)
  2042. elseif (XCODE AND _allExcludedSourceFiles)
  2043. # for Xcode, we cannot apply the precompiled header to individual sources, only to the whole target
  2044. set (_disableMsg "Exclusion of source files not supported for generator Xcode.")
  2045. set (_targetUsePCH FALSE)
  2046. elseif (XCODE AND "${_targetType}" STREQUAL "OBJECT_LIBRARY")
  2047. # for Xcode, we cannot apply the required PRE_BUILD action to generate the prefix header to an OBJECT_LIBRARY target
  2048. set (_disableMsg "Required PRE_BUILD action not supported for OBJECT_LIBRARY targets for generator Xcode.")
  2049. set (_targetUsePCH FALSE)
  2050. endif()
  2051. endif()
  2052. set_property(TARGET ${_target} PROPERTY COTIRE_ENABLE_PRECOMPILED_HEADER ${_targetUsePCH})
  2053. set_property(TARGET ${_target} PROPERTY COTIRE_ADD_UNITY_BUILD ${_targetAddSCU})
  2054. cotire_make_target_message(${_target} "${_targetLanguages}" "${_disableMsg}" _targetMsg ${_allExcludedSourceFiles})
  2055. if (_targetMsg)
  2056. if (NOT DEFINED COTIREMSG_${_target})
  2057. set (COTIREMSG_${_target} "")
  2058. endif()
  2059. if (COTIRE_VERBOSE OR NOT "${_targetMsgLevel}" STREQUAL "STATUS" OR
  2060. NOT "${COTIREMSG_${_target}}" STREQUAL "${_targetMsg}")
  2061. # cache message to avoid redundant messages on re-configure
  2062. set (COTIREMSG_${_target} "${_targetMsg}" CACHE INTERNAL "${_target} cotire message.")
  2063. message (${_targetMsgLevel} "${_targetMsg}")
  2064. endif()
  2065. endif()
  2066. set (${_targetLanguagesVar} ${_targetLanguages} PARENT_SCOPE)
  2067. endfunction()
  2068. function (cotire_process_target_language _language _configurations _targetSourceDir _targetBinaryDir _target _wholeTargetVar _cmdsVar)
  2069. set (${_cmdsVar} "" PARENT_SCOPE)
  2070. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2071. set (_sourceFiles "")
  2072. set (_excludedSources "")
  2073. set (_cotiredSources "")
  2074. cotire_filter_language_source_files(${_language} _sourceFiles _excludedSources _cotiredSources ${_targetSourceFiles})
  2075. if (NOT _sourceFiles)
  2076. return()
  2077. endif()
  2078. set (_wholeTarget ${${_wholeTargetVar}})
  2079. set (_cmds "")
  2080. # check for user provided unity source file list
  2081. get_property(_unitySourceFiles TARGET ${_target} PROPERTY COTIRE_${_language}_UNITY_SOURCE_INIT)
  2082. if (NOT _unitySourceFiles)
  2083. set (_unitySourceFiles ${_sourceFiles} ${_cotiredSources})
  2084. endif()
  2085. cotire_generate_target_script(
  2086. ${_language} "${_configurations}" "${_targetSourceDir}" "${_targetBinaryDir}" ${_target} _targetScript ${_unitySourceFiles})
  2087. get_target_property(_maxIncludes ${_target} COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES)
  2088. if (NOT _maxIncludes)
  2089. set (_maxIncludes 0)
  2090. endif()
  2091. cotire_make_untiy_source_file_paths(${_language} ${_target} ${_maxIncludes} _unityFiles ${_unitySourceFiles})
  2092. if (NOT _unityFiles)
  2093. return()
  2094. endif()
  2095. cotire_setup_unity_generation_commands(
  2096. ${_language} "${_targetSourceDir}" ${_target} "${_targetScript}" "${_unityFiles}" _cmds ${_unitySourceFiles})
  2097. cotire_make_prefix_file_path(${_language} ${_target} _prefixFile)
  2098. if (_prefixFile)
  2099. # check for user provided prefix header files
  2100. get_property(_prefixHeaderFiles TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER_INIT)
  2101. if (_prefixHeaderFiles)
  2102. cotire_setup_combine_command("${_targetSourceDir}" "${_targetScript}" "${_prefixFile}" "${_prefixHeaderFiles}" _cmds)
  2103. else()
  2104. cotire_setup_prefix_generation_command(
  2105. ${_language} ${_target} "${_targetSourceDir}" "${_targetScript}" "${_prefixFile}" "${_unityFiles}" _cmds ${_unitySourceFiles})
  2106. endif()
  2107. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  2108. if (_targetUsePCH)
  2109. cotire_make_pch_file_path(${_language} "${_targetSourceDir}" ${_target} _pchFile)
  2110. if (_pchFile)
  2111. cotire_setup_pch_file_compilation(
  2112. ${_language} "${_targetBinaryDir}" "${_targetScript}" "${_prefixFile}" "${_pchFile}" ${_sourceFiles})
  2113. if (_excludedSources)
  2114. set (_wholeTarget FALSE)
  2115. endif()
  2116. cotire_setup_prefix_file_inclusion(
  2117. ${_language} ${_target} ${_wholeTarget} "${_prefixFile}" "${_pchFile}" ${_sourceFiles})
  2118. endif()
  2119. endif()
  2120. endif()
  2121. # mark target as cotired for language
  2122. set_property(TARGET ${_target} PROPERTY COTIRE_${_language}_UNITY_SOURCE "${_unityFiles}")
  2123. if (_prefixFile)
  2124. set_property(TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER "${_prefixFile}")
  2125. if (_targetUsePCH AND _pchFile)
  2126. set_property(TARGET ${_target} PROPERTY COTIRE_${_language}_PRECOMPILED_HEADER "${_pchFile}")
  2127. endif()
  2128. endif()
  2129. set (${_wholeTargetVar} ${_wholeTarget} PARENT_SCOPE)
  2130. set (${_cmdsVar} ${_cmds} PARENT_SCOPE)
  2131. endfunction()
  2132. function (cotire_setup_clean_target _target)
  2133. set (_cleanTargetName "${_target}${COTIRE_CLEAN_TARGET_SUFFIX}")
  2134. if (NOT TARGET "${_cleanTargetName}")
  2135. cotire_set_cmd_to_prologue(_cmds)
  2136. get_filename_component(_outputDir "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}" ABSOLUTE)
  2137. list (APPEND _cmds -P "${COTIRE_CMAKE_MODULE_FILE}" "cleanup" "${_outputDir}" "${COTIRE_INTDIR}" "${_target}")
  2138. add_custom_target(${_cleanTargetName} COMMAND ${_cmds} WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
  2139. COMMENT "Cleaning up target ${_target} cotire generated files" VERBATIM)
  2140. cotire_init_target("${_cleanTargetName}")
  2141. endif()
  2142. endfunction()
  2143. function (cotire_setup_pch_target _languages _configurations _target)
  2144. if ("${CMAKE_GENERATOR}" MATCHES "Makefiles|Ninja")
  2145. # for makefile based generators, we add a custom target to trigger the generation of the cotire related files
  2146. set (_dependsFiles "")
  2147. foreach (_language ${_languages})
  2148. set (_props COTIRE_${_language}_PREFIX_HEADER COTIRE_${_language}_UNITY_SOURCE)
  2149. if (NOT CMAKE_${_language}_COMPILER_ID MATCHES "MSVC|Intel")
  2150. # Visual Studio and Intel only create precompiled header as a side effect
  2151. list (INSERT _props 0 COTIRE_${_language}_PRECOMPILED_HEADER)
  2152. endif()
  2153. cotire_get_first_set_property_value(_dependsFile TARGET ${_target} ${_props})
  2154. if (_dependsFile)
  2155. list (APPEND _dependsFiles "${_dependsFile}")
  2156. endif()
  2157. endforeach()
  2158. if (_dependsFiles)
  2159. set (_pchTargetName "${_target}${COTIRE_PCH_TARGET_SUFFIX}")
  2160. add_custom_target("${_pchTargetName}" DEPENDS ${_dependsFiles})
  2161. cotire_init_target("${_pchTargetName}")
  2162. cotire_add_to_pch_all_target(${_pchTargetName})
  2163. endif()
  2164. else()
  2165. # for other generators, we add the "clean all" target to clean up the precompiled header
  2166. cotire_setup_clean_all_target()
  2167. endif()
  2168. endfunction()
  2169. function (cotire_setup_unity_build_target _languages _configurations _targetSourceDir _target)
  2170. get_target_property(_unityTargetName ${_target} COTIRE_UNITY_TARGET_NAME)
  2171. if (NOT _unityTargetName)
  2172. set (_unityTargetName "${_target}${COTIRE_UNITY_BUILD_TARGET_SUFFIX}")
  2173. endif()
  2174. # determine unity target sub type
  2175. get_target_property(_targetType ${_target} TYPE)
  2176. if ("${_targetType}" STREQUAL "EXECUTABLE")
  2177. get_target_property(_isWin32 ${_target} WIN32_EXECUTABLE)
  2178. get_target_property(_isMacOSX_Bundle ${_target} MACOSX_BUNDLE)
  2179. if (_isWin32)
  2180. set (_unityTargetSubType WIN32)
  2181. elseif (_isMacOSX_Bundle)
  2182. set (_unityTargetSubType MACOSX_BUNDLE)
  2183. else()
  2184. set (_unityTargetSubType "")
  2185. endif()
  2186. elseif (_targetType MATCHES "(STATIC|SHARED|MODULE|OBJECT)_LIBRARY")
  2187. set (_unityTargetSubType "${CMAKE_MATCH_1}")
  2188. else()
  2189. message (WARNING "Unknown target type ${_targetType}.")
  2190. return()
  2191. endif()
  2192. # determine unity target sources
  2193. get_target_property(_targetSourceFiles ${_target} SOURCES)
  2194. set (_unityTargetSources ${_targetSourceFiles})
  2195. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  2196. foreach (_language ${_languages})
  2197. get_property(_unityFiles TARGET ${_target} PROPERTY COTIRE_${_language}_UNITY_SOURCE)
  2198. if (_unityFiles)
  2199. # remove source files that are included in the unity source
  2200. set (_sourceFiles "")
  2201. set (_excludedSources "")
  2202. set (_cotiredSources "")
  2203. cotire_filter_language_source_files(${_language} _sourceFiles _excludedSources _cotiredSources ${_targetSourceFiles})
  2204. if (_sourceFiles OR _cotiredSources)
  2205. list (REMOVE_ITEM _unityTargetSources ${_sourceFiles} ${_cotiredSources})
  2206. endif()
  2207. # if cotire is applied to a target which has not been added in the current source dir,
  2208. # non-existing files cannot be referenced from the unity build target (this is a CMake restriction)
  2209. if (NOT "${_targetSourceDir}" STREQUAL "..")
  2210. set (_nonExistingFiles "")
  2211. foreach (_file ${_unityTargetSources})
  2212. if (NOT EXISTS "${_file}")
  2213. list (APPEND _nonExistingFiles "${_file}")
  2214. endif()
  2215. endforeach()
  2216. if (_nonExistingFiles)
  2217. if (COTIRE_VERBOSE)
  2218. message (STATUS "removing non-existing ${_nonExistingFiles} from ${_unityTargetName}")
  2219. endif()
  2220. list (REMOVE_ITEM _unityTargetSources ${_nonExistingFiles})
  2221. endif()
  2222. endif()
  2223. # add unity source files instead
  2224. list (APPEND _unityTargetSources ${_unityFiles})
  2225. # make unity files use precompiled header if there are multiple unity files
  2226. list (LENGTH _unityFiles _numberOfUnityFiles)
  2227. if (_targetUsePCH AND _numberOfUnityFiles GREATER ${COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES})
  2228. get_property(_prefixFile TARGET ${_target} PROPERTY COTIRE_${_language}_PREFIX_HEADER)
  2229. get_property(_pchFile TARGET ${_target} PROPERTY COTIRE_${_language}_PRECOMPILED_HEADER)
  2230. if (_prefixFile AND _pchFile)
  2231. cotire_setup_pch_file_compilation(
  2232. ${_language} "${_targetBinaryDir}" "" "${_prefixFile}" "${_pchFile}" ${_unityFiles})
  2233. cotire_setup_prefix_file_inclusion(
  2234. ${_language} ${_target} FALSE "${_prefixFile}" "${_pchFile}" ${_unityFiles})
  2235. # add the prefix header to unity target sources
  2236. list (APPEND _unityTargetSources "${_prefixFile}")
  2237. endif()
  2238. endif()
  2239. endif()
  2240. endforeach()
  2241. if (COTIRE_DEBUG)
  2242. message (STATUS "add ${_targetType} ${_unityTargetName} ${_unityTargetSubType} EXCLUDE_FROM_ALL ${_unityTargetSources}")
  2243. endif()
  2244. # generate unity target
  2245. if ("${_targetType}" STREQUAL "EXECUTABLE")
  2246. add_executable(${_unityTargetName} ${_unityTargetSubType} EXCLUDE_FROM_ALL ${_unityTargetSources})
  2247. else()
  2248. add_library(${_unityTargetName} ${_unityTargetSubType} EXCLUDE_FROM_ALL ${_unityTargetSources})
  2249. endif()
  2250. set (_outputDirProperties
  2251. ARCHIVE_OUTPUT_DIRECTORY ARCHIVE_OUTPUT_DIRECTORY_<CONFIG>
  2252. LIBRARY_OUTPUT_DIRECTORY LIBRARY_OUTPUT_DIRECTORY_<CONFIG>
  2253. RUNTIME_OUTPUT_DIRECTORY RUNTIME_OUTPUT_DIRECTORY_<CONFIG>)
  2254. # copy output location properties
  2255. if (COTIRE_UNITY_OUTPUT_DIRECTORY)
  2256. set (_setDefaultOutputDir TRUE)
  2257. if (IS_ABSOLUTE "${COTIRE_UNITY_OUTPUT_DIRECTORY}")
  2258. set (_outputDir "${COTIRE_UNITY_OUTPUT_DIRECTORY}")
  2259. else()
  2260. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName} ${_outputDirProperties})
  2261. cotrie_resolve_config_properites("${_configurations}" _properties ${_outputDirProperties})
  2262. foreach (_property ${_properties})
  2263. get_property(_outputDir TARGET ${_target} PROPERTY ${_property})
  2264. if (_outputDir)
  2265. get_filename_component(_outputDir "${_outputDir}/${COTIRE_UNITY_OUTPUT_DIRECTORY}" ABSOLUTE)
  2266. set_property(TARGET ${_unityTargetName} PROPERTY ${_property} "${_outputDir}")
  2267. set (_setDefaultOutputDir FALSE)
  2268. endif()
  2269. endforeach()
  2270. if (_setDefaultOutputDir)
  2271. get_filename_component(_outputDir "${CMAKE_CURRENT_BINARY_DIR}/${COTIRE_UNITY_OUTPUT_DIRECTORY}" ABSOLUTE)
  2272. endif()
  2273. endif()
  2274. if (_setDefaultOutputDir)
  2275. set_target_properties(${_unityTargetName} PROPERTIES
  2276. ARCHIVE_OUTPUT_DIRECTORY "${_outputDir}"
  2277. LIBRARY_OUTPUT_DIRECTORY "${_outputDir}"
  2278. RUNTIME_OUTPUT_DIRECTORY "${_outputDir}")
  2279. endif()
  2280. else()
  2281. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName} ${_outputDirProperties})
  2282. endif()
  2283. # copy output name
  2284. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2285. ARCHIVE_OUTPUT_NAME ARCHIVE_OUTPUT_NAME_<CONFIG>
  2286. LIBRARY_OUTPUT_NAME LIBRARY_OUTPUT_NAME_<CONFIG>
  2287. OUTPUT_NAME OUTPUT_NAME_<CONFIG>
  2288. RUNTIME_OUTPUT_NAME RUNTIME_OUTPUT_NAME_<CONFIG>
  2289. PREFIX <CONFIG>_POSTFIX SUFFIX)
  2290. # copy compile stuff
  2291. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2292. COMPILE_DEFINITIONS COMPILE_DEFINITIONS_<CONFIG>
  2293. COMPILE_FLAGS Fortran_FORMAT
  2294. INCLUDE_DIRECTORIES
  2295. INTERPROCEDURAL_OPTIMIZATION INTERPROCEDURAL_OPTIMIZATION_<CONFIG>
  2296. POSITION_INDEPENDENT_CODE)
  2297. # copy link stuff
  2298. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2299. BUILD_WITH_INSTALL_RPATH INSTALL_RPATH INSTALL_RPATH_USE_LINK_PATH SKIP_BUILD_RPATH
  2300. LINKER_LANGUAGE LINK_DEPENDS
  2301. LINK_FLAGS LINK_FLAGS_<CONFIG>
  2302. LINK_INTERFACE_LIBRARIES LINK_INTERFACE_LIBRARIES_<CONFIG>
  2303. LINK_INTERFACE_MULTIPLICITY LINK_INTERFACE_MULTIPLICITY_<CONFIG>
  2304. LINK_SEARCH_START_STATIC LINK_SEARCH_END_STATIC
  2305. STATIC_LIBRARY_FLAGS STATIC_LIBRARY_FLAGS_<CONFIG>
  2306. NO_SONAME SOVERSION VERSION)
  2307. # copy Qt stuff
  2308. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2309. AUTOMOC AUTOMOC_MOC_OPTIONS)
  2310. # copy cmake stuff
  2311. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2312. IMPLICIT_DEPENDS_INCLUDE_TRANSFORM RULE_LAUNCH_COMPILE RULE_LAUNCH_CUSTOM RULE_LAUNCH_LINK)
  2313. # copy platform stuff
  2314. if (APPLE)
  2315. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2316. BUNDLE BUNDLE_EXTENSION FRAMEWORK INSTALL_NAME_DIR MACOSX_BUNDLE_INFO_PLIST MACOSX_FRAMEWORK_INFO_PLIST
  2317. OSX_ARCHITECTURES OSX_ARCHITECTURES_<CONFIG> PRIVATE_HEADER PUBLIC_HEADER RESOURCE)
  2318. elseif (WIN32)
  2319. cotrie_copy_set_properites("${_configurations}" TARGET ${_target} ${_unityTargetName}
  2320. GNUtoMS
  2321. PDB_NAME PDB_NAME_<CONFIG> PDB_OUTPUT_DIRECTORY PDB_OUTPUT_DIRECTORY_<CONFIG>
  2322. VS_DOTNET_REFERENCES VS_GLOBAL_KEYWORD VS_GLOBAL_PROJECT_TYPES VS_KEYWORD
  2323. VS_SCC_AUXPATH VS_SCC_LOCALPATH VS_SCC_PROJECTNAME VS_SCC_PROVIDER
  2324. VS_WINRT_EXTENSIONS VS_WINRT_REFERENCES)
  2325. endif()
  2326. # use output name from original target
  2327. get_target_property(_targetOutputName ${_unityTargetName} OUTPUT_NAME)
  2328. if (NOT _targetOutputName)
  2329. set_property(TARGET ${_unityTargetName} PROPERTY OUTPUT_NAME "${_target}")
  2330. endif()
  2331. # use export symbol from original target
  2332. cotire_get_target_export_symbol("${_target}" _defineSymbol)
  2333. if (_defineSymbol)
  2334. set_property(TARGET ${_unityTargetName} PROPERTY DEFINE_SYMBOL "${_defineSymbol}")
  2335. if ("${_targetType}" STREQUAL "EXECUTABLE")
  2336. set_property(TARGET ${_unityTargetName} PROPERTY ENABLE_EXPORTS TRUE)
  2337. endif()
  2338. endif()
  2339. cotire_init_target(${_unityTargetName})
  2340. cotire_add_to_unity_all_target(${_unityTargetName})
  2341. set_property(TARGET ${_target} PROPERTY COTIRE_UNITY_TARGET_NAME "${_unityTargetName}")
  2342. endfunction(cotire_setup_unity_build_target)
  2343. function (cotire_target _target)
  2344. set(_options "")
  2345. set(_oneValueArgs SOURCE_DIR BINARY_DIR)
  2346. set(_multiValueArgs LANGUAGES CONFIGURATIONS)
  2347. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  2348. if (NOT _option_SOURCE_DIR)
  2349. set (_option_SOURCE_DIR "..")
  2350. endif()
  2351. if (NOT _option_BINARY_DIR)
  2352. set (_option_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  2353. endif()
  2354. if (NOT _option_LANGUAGES)
  2355. get_property (_option_LANGUAGES GLOBAL PROPERTY ENABLED_LANGUAGES)
  2356. endif()
  2357. if (NOT _option_CONFIGURATIONS)
  2358. if (CMAKE_CONFIGURATION_TYPES)
  2359. set (_option_CONFIGURATIONS ${CMAKE_CONFIGURATION_TYPES})
  2360. elseif (CMAKE_BUILD_TYPE)
  2361. set (_option_CONFIGURATIONS "${CMAKE_BUILD_TYPE}")
  2362. else()
  2363. set (_option_CONFIGURATIONS "None")
  2364. endif()
  2365. endif()
  2366. # trivial checks
  2367. get_target_property(_imported ${_target} IMPORTED)
  2368. if (_imported)
  2369. message (WARNING "Imported target ${_target} cannot be cotired.")
  2370. return()
  2371. endif()
  2372. # check if target needs to be cotired for build type
  2373. # when using configuration types, the test is performed at build time
  2374. cotire_init_cotire_target_properties(${_target})
  2375. if (NOT CMAKE_CONFIGURATION_TYPES)
  2376. if (CMAKE_BUILD_TYPE)
  2377. list (FIND _option_CONFIGURATIONS "${CMAKE_BUILD_TYPE}" _index)
  2378. else()
  2379. list (FIND _option_CONFIGURATIONS "None" _index)
  2380. endif()
  2381. if (_index EQUAL -1)
  2382. if (COTIRE_DEBUG)
  2383. message (STATUS "CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} not cotired (${_option_CONFIGURATIONS})")
  2384. endif()
  2385. return()
  2386. endif()
  2387. endif()
  2388. # choose languages that apply to the target
  2389. cotire_choose_target_languages("${_option_SOURCE_DIR}" "${_target}" _targetLanguages ${_option_LANGUAGES})
  2390. if (NOT _targetLanguages)
  2391. return()
  2392. endif()
  2393. list (LENGTH _targetLanguages _numberOfLanguages)
  2394. if (_numberOfLanguages GREATER 1)
  2395. set (_wholeTarget FALSE)
  2396. else()
  2397. set (_wholeTarget TRUE)
  2398. endif()
  2399. set (_cmds "")
  2400. foreach (_language ${_targetLanguages})
  2401. cotire_process_target_language("${_language}" "${_option_CONFIGURATIONS}"
  2402. "${_option_SOURCE_DIR}" "${_option_BINARY_DIR}" ${_target} _wholeTarget _cmd)
  2403. if (_cmd)
  2404. list (APPEND _cmds ${_cmd})
  2405. endif()
  2406. endforeach()
  2407. get_target_property(_targetAddSCU ${_target} COTIRE_ADD_UNITY_BUILD)
  2408. if (_targetAddSCU)
  2409. cotire_setup_unity_build_target("${_targetLanguages}" "${_option_CONFIGURATIONS}" "${_option_SOURCE_DIR}" ${_target})
  2410. endif()
  2411. get_target_property(_targetUsePCH ${_target} COTIRE_ENABLE_PRECOMPILED_HEADER)
  2412. if (_targetUsePCH)
  2413. cotire_setup_target_pch_usage("${_targetLanguages}" "${_option_SOURCE_DIR}" ${_target} ${_wholeTarget} ${_cmds})
  2414. cotire_setup_pch_target("${_targetLanguages}" "${_option_CONFIGURATIONS}" ${_target})
  2415. endif()
  2416. get_target_property(_targetAddCleanTarget ${_target} COTIRE_ADD_CLEAN)
  2417. if (_targetAddCleanTarget)
  2418. cotire_setup_clean_target(${_target})
  2419. endif()
  2420. endfunction()
  2421. function (cotire_cleanup _binaryDir _cotireIntermediateDirName _targetName)
  2422. if (_targetName)
  2423. file (GLOB_RECURSE _cotireFiles "${_binaryDir}/${_targetName}*.*")
  2424. else()
  2425. file (GLOB_RECURSE _cotireFiles "${_binaryDir}/*.*")
  2426. endif()
  2427. # filter files in intermediate directory
  2428. set (_filesToRemove "")
  2429. foreach (_file ${_cotireFiles})
  2430. get_filename_component(_dir "${_file}" PATH)
  2431. get_filename_component(_dirName "${_dir}" NAME)
  2432. if ("${_dirName}" STREQUAL "${_cotireIntermediateDirName}")
  2433. list (APPEND _filesToRemove "${_file}")
  2434. endif()
  2435. endforeach()
  2436. if (_filesToRemove)
  2437. if (COTIRE_VERBOSE)
  2438. message (STATUS "removing ${_filesToRemove}")
  2439. endif()
  2440. file (REMOVE ${_filesToRemove})
  2441. endif()
  2442. endfunction()
  2443. function (cotire_init_target _targetName)
  2444. if (COTIRE_TARGETS_FOLDER)
  2445. set_target_properties(${_targetName} PROPERTIES FOLDER "${COTIRE_TARGETS_FOLDER}")
  2446. endif()
  2447. if (MSVC_IDE)
  2448. set_target_properties(${_targetName} PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD TRUE)
  2449. endif()
  2450. endfunction()
  2451. function (cotire_add_to_pch_all_target _pchTargetName)
  2452. set (_targetName "${COTIRE_PCH_ALL_TARGET_NAME}")
  2453. if (NOT TARGET "${_targetName}")
  2454. add_custom_target("${_targetName}" WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" VERBATIM)
  2455. cotire_init_target("${_targetName}")
  2456. endif()
  2457. cotire_setup_clean_all_target()
  2458. add_dependencies(${_targetName} ${_pchTargetName})
  2459. endfunction()
  2460. function (cotire_add_to_unity_all_target _unityTargetName)
  2461. set (_targetName "${COTIRE_UNITY_BUILD_ALL_TARGET_NAME}")
  2462. if (NOT TARGET "${_targetName}")
  2463. add_custom_target("${_targetName}" WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" VERBATIM)
  2464. cotire_init_target("${_targetName}")
  2465. endif()
  2466. cotire_setup_clean_all_target()
  2467. add_dependencies(${_targetName} ${_unityTargetName})
  2468. endfunction()
  2469. function (cotire_setup_clean_all_target)
  2470. set (_targetName "${COTIRE_CLEAN_ALL_TARGET_NAME}")
  2471. if (NOT TARGET "${_targetName}")
  2472. cotire_set_cmd_to_prologue(_cmds)
  2473. list (APPEND _cmds -P "${COTIRE_CMAKE_MODULE_FILE}" "cleanup" "${CMAKE_BINARY_DIR}" "${COTIRE_INTDIR}")
  2474. add_custom_target(${_targetName} COMMAND ${_cmds}
  2475. WORKING_DIRECTORY "${CMAKE_BINARY_DIR}" COMMENT "Cleaning up all cotire generated files" VERBATIM)
  2476. cotire_init_target("${_targetName}")
  2477. endif()
  2478. endfunction()
  2479. function (cotire)
  2480. set(_options "")
  2481. set(_oneValueArgs SOURCE_DIR BINARY_DIR)
  2482. set(_multiValueArgs LANGUAGES CONFIGURATIONS)
  2483. cmake_parse_arguments(_option "${_options}" "${_oneValueArgs}" "${_multiValueArgs}" ${ARGN})
  2484. set (_targets ${_option_UNPARSED_ARGUMENTS})
  2485. if (NOT _option_SOURCE_DIR)
  2486. set (_option_SOURCE_DIR "..")
  2487. endif()
  2488. if (NOT _option_BINARY_DIR)
  2489. set (_option_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}")
  2490. endif()
  2491. foreach (_target ${_targets})
  2492. if (TARGET ${_target})
  2493. cotire_target(${_target} LANGUAGES ${_option_LANGUAGES} CONFIGURATIONS ${_option_CONFIGURATIONS}
  2494. SOURCE_DIR "${_option_SOURCE_DIR}" BINARY_DIR "${_option_BINARY_DIR}")
  2495. else()
  2496. message (WARNING "${_target} is not a target")
  2497. endif()
  2498. endforeach()
  2499. endfunction()
  2500. if (CMAKE_SCRIPT_MODE_FILE)
  2501. # cotire is being run in script mode
  2502. # locate -P on command args
  2503. set (COTIRE_ARGC -1)
  2504. foreach (_index RANGE ${CMAKE_ARGC})
  2505. if (COTIRE_ARGC GREATER -1)
  2506. set (COTIRE_ARGV${COTIRE_ARGC} "${CMAKE_ARGV${_index}}")
  2507. math (EXPR COTIRE_ARGC "${COTIRE_ARGC} + 1")
  2508. elseif ("${CMAKE_ARGV${_index}}" STREQUAL "-P")
  2509. set (COTIRE_ARGC 0)
  2510. endif()
  2511. endforeach()
  2512. # include target script if available
  2513. if ("${COTIRE_ARGV2}" MATCHES "\\.cmake$")
  2514. # the included target scripts sets up additional variables relating to the target (e.g., COTIRE_TARGET_SOURCES)
  2515. include("${COTIRE_ARGV2}")
  2516. endif()
  2517. if (COTIRE_DEBUG)
  2518. message (STATUS "${COTIRE_ARGV0} ${COTIRE_ARGV1} ${COTIRE_ARGV2} ${COTIRE_ARGV3} ${COTIRE_ARGV4} ${COTIRE_ARGV5}")
  2519. endif()
  2520. if (WIN32)
  2521. # for MSVC, compiler IDs may not always be set correctly
  2522. if (MSVC)
  2523. set (CMAKE_C_COMPILER_ID "MSVC")
  2524. set (CMAKE_CXX_COMPILER_ID "MSVC")
  2525. endif()
  2526. endif()
  2527. if (NOT COTIRE_BUILD_TYPE)
  2528. set (COTIRE_BUILD_TYPE "None")
  2529. endif()
  2530. string (TOUPPER "${COTIRE_BUILD_TYPE}" _upperConfig)
  2531. set (_includeDirs ${COTIRE_TARGET_INCLUDE_DIRECTORIES_${_upperConfig}})
  2532. set (_compileDefinitions ${COTIRE_TARGET_COMPILE_DEFINITIONS_${_upperConfig}})
  2533. set (_compileFlags ${COTIRE_TARGET_COMPILE_FLAGS_${_upperConfig}})
  2534. # check if target has been cotired for actual build type COTIRE_BUILD_TYPE
  2535. list (FIND COTIRE_TARGET_CONFIGURATION_TYPES "${COTIRE_BUILD_TYPE}" _index)
  2536. if (_index GREATER -1)
  2537. set (_sources ${COTIRE_TARGET_SOURCES})
  2538. set (_sourcesDefinitions ${COTIRE_TARGET_SOURCES_COMPILE_DEFINITIONS_${_upperConfig}})
  2539. else()
  2540. if (COTIRE_DEBUG)
  2541. message (STATUS "COTIRE_BUILD_TYPE=${COTIRE_BUILD_TYPE} not cotired (${COTIRE_TARGET_CONFIGURATION_TYPES})")
  2542. endif()
  2543. set (_sources "")
  2544. set (_sourcesDefinitions "")
  2545. endif()
  2546. set (_targetPreUndefs ${COTIRE_TARGET_PRE_UNDEFS})
  2547. set (_targetPostUndefs ${COTIRE_TARGET_POST_UNDEFS})
  2548. set (_sourcesPreUndefs ${COTIRE_TARGET_SOURCES_PRE_UNDEFS})
  2549. set (_sourcesPostUndefs ${COTIRE_TARGET_SOURCES_POST_UNDEFS})
  2550. if ("${COTIRE_ARGV1}" STREQUAL "unity")
  2551. cotire_select_unity_source_files("${COTIRE_ARGV3}" _sources ${_sources})
  2552. cotire_generate_unity_source(
  2553. "${COTIRE_ARGV3}" ${_sources}
  2554. LANGUAGE "${COTIRE_TARGET_LANGUAGE}"
  2555. DEPENDS "${COTIRE_ARGV0}" "${COTIRE_ARGV2}"
  2556. SOURCES_COMPILE_DEFINITIONS ${_sourcesDefinitions}
  2557. PRE_UNDEFS ${_targetPreUndefs}
  2558. POST_UNDEFS ${_targetPostUndefs}
  2559. SOURCES_PRE_UNDEFS ${_sourcesPreUndefs}
  2560. SOURCES_POST_UNDEFS ${_sourcesPostUndefs})
  2561. elseif ("${COTIRE_ARGV1}" STREQUAL "prefix")
  2562. set (_files "")
  2563. foreach (_index RANGE 4 ${COTIRE_ARGC})
  2564. if (COTIRE_ARGV${_index})
  2565. list (APPEND _files "${COTIRE_ARGV${_index}}")
  2566. endif()
  2567. endforeach()
  2568. cotire_generate_prefix_header(
  2569. "${COTIRE_ARGV3}" ${_files}
  2570. COMPILER_EXECUTABLE "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER}"
  2571. COMPILER_ARG1 ${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ARG1}
  2572. COMPILER_ID "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ID}"
  2573. COMPILER_VERSION "${COTIRE_${COTIRE_TARGET_LANGUAGE}_COMPILER_VERSION}"
  2574. LANGUAGE "${COTIRE_TARGET_LANGUAGE}"
  2575. DEPENDS "${COTIRE_ARGV0}" "${COTIRE_ARGV4}" ${COTIRE_TARGET_PREFIX_DEPENDS}
  2576. IGNORE_PATH "${COTIRE_TARGET_IGNORE_PATH};${COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_PATH}"
  2577. INCLUDE_PATH ${COTIRE_TARGET_INCLUDE_PATH}
  2578. IGNORE_EXTENSIONS "${CMAKE_${COTIRE_TARGET_LANGUAGE}_SOURCE_FILE_EXTENSIONS};${COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_EXTENSIONS}"
  2579. INCLUDE_DIRECTORIES ${_includeDirs}
  2580. COMPILE_DEFINITIONS ${_compileDefinitions}
  2581. COMPILE_FLAGS ${_compileFlags})
  2582. elseif ("${COTIRE_ARGV1}" STREQUAL "precompile")
  2583. set (_files "")
  2584. foreach (_index RANGE 5 ${COTIRE_ARGC})
  2585. if (COTIRE_ARGV${_index})
  2586. list (APPEND _files "${COTIRE_ARGV${_index}}")
  2587. endif()
  2588. endforeach()
  2589. cotire_precompile_prefix_header(
  2590. "${COTIRE_ARGV3}" "${COTIRE_ARGV4}" "${COTIRE_ARGV5}"
  2591. COMPILER_EXECUTABLE "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER}"
  2592. COMPILER_ARG1 ${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ARG1}
  2593. COMPILER_ID "${CMAKE_${COTIRE_TARGET_LANGUAGE}_COMPILER_ID}"
  2594. COMPILER_VERSION "${COTIRE_${COTIRE_TARGET_LANGUAGE}_COMPILER_VERSION}"
  2595. LANGUAGE "${COTIRE_TARGET_LANGUAGE}"
  2596. INCLUDE_DIRECTORIES ${_includeDirs}
  2597. COMPILE_DEFINITIONS ${_compileDefinitions}
  2598. COMPILE_FLAGS ${_compileFlags})
  2599. elseif ("${COTIRE_ARGV1}" STREQUAL "combine")
  2600. if (COTIRE_TARGET_LANGUAGE)
  2601. set (_startIndex 3)
  2602. else()
  2603. set (_startIndex 2)
  2604. endif()
  2605. set (_files "")
  2606. foreach (_index RANGE ${_startIndex} ${COTIRE_ARGC})
  2607. if (COTIRE_ARGV${_index})
  2608. list (APPEND _files "${COTIRE_ARGV${_index}}")
  2609. endif()
  2610. endforeach()
  2611. if (COTIRE_TARGET_LANGUAGE)
  2612. cotire_generate_unity_source(${_files} LANGUAGE "${COTIRE_TARGET_LANGUAGE}")
  2613. else()
  2614. cotire_generate_unity_source(${_files})
  2615. endif()
  2616. elseif ("${COTIRE_ARGV1}" STREQUAL "cleanup")
  2617. cotire_cleanup("${COTIRE_ARGV2}" "${COTIRE_ARGV3}" "${COTIRE_ARGV4}")
  2618. else()
  2619. message (FATAL_ERROR "Unknown cotire command \"${COTIRE_ARGV1}\".")
  2620. endif()
  2621. else()
  2622. # cotire is being run in include mode
  2623. # set up all variable and property definitions
  2624. unset (COTIRE_C_COMPILER_VERSION CACHE)
  2625. unset (COTIRE_CXX_COMPILER_VERSION CACHE)
  2626. if (NOT DEFINED COTIRE_DEBUG_INIT)
  2627. if (DEFINED COTIRE_DEBUG)
  2628. set (COTIRE_DEBUG_INIT ${COTIRE_DEBUG})
  2629. else()
  2630. set (COTIRE_DEBUG_INIT FALSE)
  2631. endif()
  2632. endif()
  2633. option (COTIRE_DEBUG "Enable cotire debugging output?" ${COTIRE_DEBUG_INIT})
  2634. if (NOT DEFINED COTIRE_VERBOSE_INIT)
  2635. if (DEFINED COTIRE_VERBOSE)
  2636. set (COTIRE_VERBOSE_INIT ${COTIRE_VERBOSE})
  2637. else()
  2638. set (COTIRE_VERBOSE_INIT FALSE)
  2639. endif()
  2640. endif()
  2641. option (COTIRE_VERBOSE "Enable cotire verbose output?" ${COTIRE_VERBOSE_INIT})
  2642. set (COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_EXTENSIONS "inc;inl;ipp" CACHE STRING
  2643. "Ignore headers with the listed file extensions from the generated prefix header.")
  2644. set (COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_PATH "" CACHE STRING
  2645. "Ignore headers from these directories when generating the prefix header.")
  2646. set (COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS "m;mm" CACHE STRING
  2647. "Ignore sources with the listed file extensions from the generated unity source.")
  2648. set (COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES "3" CACHE STRING
  2649. "Minimum number of sources in target required to enable use of precompiled header.")
  2650. set (COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES "" CACHE STRING
  2651. "Maximum number of source files to include in a single unity source file.")
  2652. if (NOT COTIRE_PREFIX_HEADER_FILENAME_SUFFIX)
  2653. set (COTIRE_PREFIX_HEADER_FILENAME_SUFFIX "_prefix")
  2654. endif()
  2655. if (NOT COTIRE_UNITY_SOURCE_FILENAME_SUFFIX)
  2656. set (COTIRE_UNITY_SOURCE_FILENAME_SUFFIX "_unity")
  2657. endif()
  2658. if (NOT COTIRE_INTDIR)
  2659. set (COTIRE_INTDIR "cotire")
  2660. endif()
  2661. if (NOT COTIRE_PCH_ALL_TARGET_NAME)
  2662. set (COTIRE_PCH_ALL_TARGET_NAME "all_pch")
  2663. endif()
  2664. if (NOT COTIRE_UNITY_BUILD_ALL_TARGET_NAME)
  2665. set (COTIRE_UNITY_BUILD_ALL_TARGET_NAME "all_unity")
  2666. endif()
  2667. if (NOT COTIRE_CLEAN_ALL_TARGET_NAME)
  2668. set (COTIRE_CLEAN_ALL_TARGET_NAME "clean_cotire")
  2669. endif()
  2670. if (NOT COTIRE_CLEAN_TARGET_SUFFIX)
  2671. set (COTIRE_CLEAN_TARGET_SUFFIX "_clean_cotire")
  2672. endif()
  2673. if (NOT COTIRE_PCH_TARGET_SUFFIX)
  2674. set (COTIRE_PCH_TARGET_SUFFIX "_pch")
  2675. endif()
  2676. if (NOT COTIRE_UNITY_BUILD_TARGET_SUFFIX)
  2677. set (COTIRE_UNITY_BUILD_TARGET_SUFFIX "_unity")
  2678. endif()
  2679. if (NOT DEFINED COTIRE_TARGETS_FOLDER)
  2680. set (COTIRE_TARGETS_FOLDER "cotire")
  2681. endif()
  2682. if (NOT DEFINED COTIRE_UNITY_OUTPUT_DIRECTORY)
  2683. if ("${CMAKE_GENERATOR}" MATCHES "Ninja")
  2684. # generated Ninja build files do not work if the unity target produces the same output file as the cotired target
  2685. set (COTIRE_UNITY_OUTPUT_DIRECTORY "unity")
  2686. else()
  2687. set (COTIRE_UNITY_OUTPUT_DIRECTORY "")
  2688. endif()
  2689. endif()
  2690. # define cotire cache variables
  2691. define_property(
  2692. CACHED_VARIABLE PROPERTY "COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_PATH"
  2693. BRIEF_DOCS "Ignore headers from these directories when generating the prefix header."
  2694. FULL_DOCS
  2695. "The variable can be set to a semicolon separated list of include directories."
  2696. "If a header file is found in one of these directories or sub-directories, it will be excluded from the generated prefix header."
  2697. "If not defined, defaults to empty list."
  2698. )
  2699. define_property(
  2700. CACHED_VARIABLE PROPERTY "COTIRE_ADDITIONAL_PREFIX_HEADER_IGNORE_EXTENSIONS"
  2701. BRIEF_DOCS "Ignore includes with the listed file extensions from the generated prefix header."
  2702. FULL_DOCS
  2703. "The variable can be set to a semicolon separated list of file extensions."
  2704. "If a header file extension matches one in the list, it will be excluded from the generated prefix header."
  2705. "Includes with an extension in CMAKE_<LANG>_SOURCE_FILE_EXTENSIONS are always ignored."
  2706. "If not defined, defaults to inc;inl;ipp."
  2707. )
  2708. define_property(
  2709. CACHED_VARIABLE PROPERTY "COTIRE_UNITY_SOURCE_EXCLUDE_EXTENSIONS"
  2710. BRIEF_DOCS "Exclude sources with the listed file extensions from the generated unity source."
  2711. FULL_DOCS
  2712. "The variable can be set to a semicolon separated list of file extensions."
  2713. "If a source file extension matches one in the list, it will be excluded from the generated unity source file."
  2714. "Source files with an extension in CMAKE_<LANG>_IGNORE_EXTENSIONS are always excluded."
  2715. "If not defined, defaults to m;mm."
  2716. )
  2717. define_property(
  2718. CACHED_VARIABLE PROPERTY "COTIRE_MINIMUM_NUMBER_OF_TARGET_SOURCES"
  2719. BRIEF_DOCS "Minimum number of sources in target required to enable use of precompiled header."
  2720. FULL_DOCS
  2721. "The variable can be set to an integer > 0."
  2722. "If a target contains less than that number of source files, cotire will not enable the use of the precompiled header for the target."
  2723. "If not defined, defaults to 3."
  2724. )
  2725. define_property(
  2726. CACHED_VARIABLE PROPERTY "COTIRE_MAXIMUM_NUMBER_OF_UNITY_INCLUDES"
  2727. BRIEF_DOCS "Maximum number of source files to include in a single unity source file."
  2728. FULL_DOCS
  2729. "This may be set to an integer > 0."
  2730. "If a target contains more than that number of source files, cotire will create multiple unity source files for it."
  2731. "If not set, cotire will only create a single unity source file."
  2732. "Is use to initialize the target property COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES."
  2733. "Defaults to empty."
  2734. )
  2735. # define cotire directory properties
  2736. define_property(
  2737. DIRECTORY PROPERTY "COTIRE_ENABLE_PRECOMPILED_HEADER"
  2738. BRIEF_DOCS "Modify build command of cotired targets added in this directory to make use of the generated precompiled header."
  2739. FULL_DOCS
  2740. "See target property COTIRE_ENABLE_PRECOMPILED_HEADER."
  2741. )
  2742. define_property(
  2743. DIRECTORY PROPERTY "COTIRE_ADD_UNITY_BUILD"
  2744. BRIEF_DOCS "Add a new target that performs a unity build for cotired targets added in this directory."
  2745. FULL_DOCS
  2746. "See target property COTIRE_ADD_UNITY_BUILD."
  2747. )
  2748. define_property(
  2749. DIRECTORY PROPERTY "COTIRE_ADD_CLEAN"
  2750. BRIEF_DOCS "Add a new target that cleans all cotire generated files for cotired targets added in this directory."
  2751. FULL_DOCS
  2752. "See target property COTIRE_ADD_CLEAN."
  2753. )
  2754. define_property(
  2755. DIRECTORY PROPERTY "COTIRE_PREFIX_HEADER_IGNORE_PATH"
  2756. BRIEF_DOCS "Ignore headers from these directories when generating the prefix header."
  2757. FULL_DOCS
  2758. "See target property COTIRE_PREFIX_HEADER_IGNORE_PATH."
  2759. )
  2760. define_property(
  2761. DIRECTORY PROPERTY "COTIRE_PREFIX_HEADER_INCLUDE_PATH"
  2762. BRIEF_DOCS "Honor headers from these directories when generating the prefix header."
  2763. FULL_DOCS
  2764. "See target property COTIRE_PREFIX_HEADER_INCLUDE_PATH."
  2765. )
  2766. define_property(
  2767. DIRECTORY PROPERTY "COTIRE_UNITY_SOURCE_PRE_UNDEFS"
  2768. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file before the inclusion of each source file."
  2769. FULL_DOCS
  2770. "See target property COTIRE_UNITY_SOURCE_PRE_UNDEFS."
  2771. )
  2772. define_property(
  2773. DIRECTORY PROPERTY "COTIRE_UNITY_SOURCE_POST_UNDEFS"
  2774. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file after the inclusion of each source file."
  2775. FULL_DOCS
  2776. "See target property COTIRE_UNITY_SOURCE_POST_UNDEFS."
  2777. )
  2778. define_property(
  2779. DIRECTORY PROPERTY "COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES"
  2780. BRIEF_DOCS "Maximum number of source files to include in a single unity source file."
  2781. FULL_DOCS
  2782. "See target property COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES."
  2783. )
  2784. # define cotire target properties
  2785. define_property(
  2786. TARGET PROPERTY "COTIRE_ENABLE_PRECOMPILED_HEADER" INHERITED
  2787. BRIEF_DOCS "Modify this target's build command to make use of the generated precompiled header."
  2788. FULL_DOCS
  2789. "If this property is set to TRUE, cotire will modify the build command to make use of the generated precompiled header."
  2790. "Irrespective of the value of this property, cotire will setup custom commands to generate the unity source and prefix header for the target."
  2791. "For makefile based generators cotire will also set up a custom target to manually invoke the generation of the precompiled header."
  2792. "The target name will be set to this target's name with the suffix _pch appended."
  2793. "Inherited from directory."
  2794. "Defaults to TRUE."
  2795. )
  2796. define_property(
  2797. TARGET PROPERTY "COTIRE_ADD_UNITY_BUILD" INHERITED
  2798. BRIEF_DOCS "Add a new target that performs a unity build for this target."
  2799. FULL_DOCS
  2800. "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."
  2801. "Most of the relevant target properties will be copied from this target to the new unity build target."
  2802. "Target dependencies and linked libraries have to be manually set up for the new unity build target."
  2803. "The unity target name will be set to this target's name with the suffix _unity appended."
  2804. "Inherited from directory."
  2805. "Defaults to TRUE."
  2806. )
  2807. define_property(
  2808. TARGET PROPERTY "COTIRE_ADD_CLEAN" INHERITED
  2809. BRIEF_DOCS "Add a new target that cleans all cotire generated files for this target."
  2810. FULL_DOCS
  2811. "If this property is set to TRUE, cotire creates a new target that clean all files (unity source, prefix header, precompiled header)."
  2812. "The clean target name will be set to this target's name with the suffix _clean_cotire appended."
  2813. "Inherited from directory."
  2814. "Defaults to FALSE."
  2815. )
  2816. define_property(
  2817. TARGET PROPERTY "COTIRE_PREFIX_HEADER_IGNORE_PATH" INHERITED
  2818. BRIEF_DOCS "Ignore headers from these directories when generating the prefix header."
  2819. FULL_DOCS
  2820. "The property can be set to a list of directories."
  2821. "If a header file is found in one of these directories or sub-directories, it will be excluded from the generated prefix header."
  2822. "Inherited from directory."
  2823. "If not set, this property is initialized to \${CMAKE_SOURCE_DIR};\${CMAKE_BINARY_DIR}."
  2824. )
  2825. define_property(
  2826. TARGET PROPERTY "COTIRE_PREFIX_HEADER_INCLUDE_PATH" INHERITED
  2827. BRIEF_DOCS "Honor headers from these directories when generating the prefix header."
  2828. FULL_DOCS
  2829. "The property can be set to a list of directories."
  2830. "If a header file is found in one of these directories or sub-directories, it will be included in the generated prefix header."
  2831. "If a header file is both selected by COTIRE_PREFIX_HEADER_IGNORE_PATH and COTIRE_PREFIX_HEADER_INCLUDE_PATH,"
  2832. "the option which yields the closer relative path match wins."
  2833. "Inherited from directory."
  2834. "If not set, this property is initialized to the empty list."
  2835. )
  2836. define_property(
  2837. TARGET PROPERTY "COTIRE_UNITY_SOURCE_PRE_UNDEFS" INHERITED
  2838. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file before the inclusion of each target source file."
  2839. FULL_DOCS
  2840. "This may be set to a semicolon-separated list of preprocessor symbols."
  2841. "cotire will add corresponding #undef directives to the generated unit source file before each target source file."
  2842. "Inherited from directory."
  2843. "Defaults to empty string."
  2844. )
  2845. define_property(
  2846. TARGET PROPERTY "COTIRE_UNITY_SOURCE_POST_UNDEFS" INHERITED
  2847. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file after the inclusion of each target source file."
  2848. FULL_DOCS
  2849. "This may be set to a semicolon-separated list of preprocessor symbols."
  2850. "cotire will add corresponding #undef directives to the generated unit source file after each target source file."
  2851. "Inherited from directory."
  2852. "Defaults to empty string."
  2853. )
  2854. define_property(
  2855. TARGET PROPERTY "COTIRE_UNITY_SOURCE_MAXIMUM_NUMBER_OF_INCLUDES" INHERITED
  2856. BRIEF_DOCS "Maximum number of source files to include in a single unity source file."
  2857. FULL_DOCS
  2858. "This may be set to an integer > 0."
  2859. "If a target contains more than that number of source files, cotire will create multiple unity build files for it."
  2860. "If not set, cotire will only create a single unity source file."
  2861. "Inherited from directory."
  2862. "Defaults to empty."
  2863. )
  2864. define_property(
  2865. TARGET PROPERTY "COTIRE_<LANG>_UNITY_SOURCE_INIT"
  2866. BRIEF_DOCS "User provided unity source file to be used instead of the automatically generated one."
  2867. FULL_DOCS
  2868. "If set, cotire will only add the given file(s) to the generated unity source file."
  2869. "If not set, cotire will add all the target source files to the generated unity source file."
  2870. "The property can be set to a user provided unity source file."
  2871. "Defaults to empty."
  2872. )
  2873. define_property(
  2874. TARGET PROPERTY "COTIRE_<LANG>_PREFIX_HEADER_INIT"
  2875. BRIEF_DOCS "User provided prefix header file to be used instead of the automatically generated one."
  2876. FULL_DOCS
  2877. "If set, cotire will add the given header file(s) to the generated prefix header file."
  2878. "If not set, cotire will generate a prefix header by tracking the header files included by the unity source file."
  2879. "The property can be set to a user provided prefix header file (e.g., stdafx.h)."
  2880. "Defaults to empty."
  2881. )
  2882. define_property(
  2883. TARGET PROPERTY "COTIRE_<LANG>_UNITY_SOURCE"
  2884. BRIEF_DOCS "Read-only property. The generated <LANG> unity source file(s)."
  2885. FULL_DOCS
  2886. "cotire sets this property to the path of the generated <LANG> single computation unit source file for the target."
  2887. "Defaults to empty string."
  2888. )
  2889. define_property(
  2890. TARGET PROPERTY "COTIRE_<LANG>_PREFIX_HEADER"
  2891. BRIEF_DOCS "Read-only property. The generated <LANG> prefix header file."
  2892. FULL_DOCS
  2893. "cotire sets this property to the full path of the generated <LANG> language prefix header for the target."
  2894. "Defaults to empty string."
  2895. )
  2896. define_property(
  2897. TARGET PROPERTY "COTIRE_<LANG>_PRECOMPILED_HEADER"
  2898. BRIEF_DOCS "Read-only property. The generated <LANG> precompiled header file."
  2899. FULL_DOCS
  2900. "cotire sets this property to the full path of the generated <LANG> language precompiled header binary for the target."
  2901. "Defaults to empty string."
  2902. )
  2903. define_property(
  2904. TARGET PROPERTY "COTIRE_UNITY_TARGET_NAME"
  2905. BRIEF_DOCS "The name of the generated unity build target corresponding to this target."
  2906. FULL_DOCS
  2907. "This property can be set to the desired name of the unity target that will be created by cotire."
  2908. "If not set, the unity target name will be set to this target's name with the suffix _unity appended."
  2909. "After this target has been processed by cotire, the property is set to the actual name of the generated unity target."
  2910. "Defaults to empty string."
  2911. )
  2912. # define cotire source properties
  2913. define_property(
  2914. SOURCE PROPERTY "COTIRE_EXCLUDED"
  2915. BRIEF_DOCS "Do not modify source file's build command."
  2916. FULL_DOCS
  2917. "If this property is set to TRUE, the source file's build command will not be modified to make use of the precompiled header."
  2918. "The source file will also be excluded from the generated unity source file."
  2919. "Source files that have their COMPILE_FLAGS property set will be excluded by default."
  2920. "Defaults to FALSE."
  2921. )
  2922. define_property(
  2923. SOURCE PROPERTY "COTIRE_DEPENDENCY"
  2924. BRIEF_DOCS "Add this source file to dependencies of the automatically generated prefix header file."
  2925. FULL_DOCS
  2926. "If this property is set to TRUE, the source file is added to dependencies of the generated prefix header file."
  2927. "If the file is modified, cotire will re-generate the prefix header source upon build."
  2928. "Defaults to FALSE."
  2929. )
  2930. define_property(
  2931. SOURCE PROPERTY "COTIRE_UNITY_SOURCE_PRE_UNDEFS"
  2932. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file before the inclusion of this source file."
  2933. FULL_DOCS
  2934. "This may be set to a semicolon-separated list of preprocessor symbols."
  2935. "cotire will add corresponding #undef directives to the generated unit source file before this file is included."
  2936. "Defaults to empty string."
  2937. )
  2938. define_property(
  2939. SOURCE PROPERTY "COTIRE_UNITY_SOURCE_POST_UNDEFS"
  2940. BRIEF_DOCS "Preprocessor undefs to place in the generated unity source file after the inclusion of this source file."
  2941. FULL_DOCS
  2942. "This may be set to a semicolon-separated list of preprocessor symbols."
  2943. "cotire will add corresponding #undef directives to the generated unit source file after this file is included."
  2944. "Defaults to empty string."
  2945. )
  2946. define_property(
  2947. SOURCE PROPERTY "COTIRE_START_NEW_UNITY_SOURCE"
  2948. BRIEF_DOCS "Start a new unity source file which includes this source file as the first one."
  2949. FULL_DOCS
  2950. "If this property is set to TRUE, cotire will complete the current unity file and start a new one."
  2951. "The new unity source file will include this source file as the first one."
  2952. "This property essentially works as a separator for unity source files."
  2953. "Defaults to FALSE."
  2954. )
  2955. define_property(
  2956. SOURCE PROPERTY "COTIRE_TARGET"
  2957. BRIEF_DOCS "Read-only property. Mark this source file as cotired for the given target."
  2958. FULL_DOCS
  2959. "cotire sets this property to the name of target, that the source file's build command has been altered for."
  2960. "Defaults to empty string."
  2961. )
  2962. endif()