cotire.cmake 139 KB

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