The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

3334 lines
136 KiB

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