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.

3186 lines
130 KiB

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