cotire.cmake 146 KB

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