cmMakefile.cxx 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmMakefile.h"
  11. #include "cmAlgorithms.h"
  12. #include "cmCommand.h"
  13. #include "cmCommandArgumentParserHelper.h"
  14. #include "cmCustomCommand.h"
  15. #include "cmCustomCommandLines.h"
  16. #include "cmExecutionStatus.h"
  17. #include "cmExpandedCommandArgument.h"
  18. #include "cmFileLockPool.h"
  19. #include "cmFunctionBlocker.h"
  20. #include "cmGeneratorExpression.h"
  21. #include "cmGeneratorExpressionEvaluationFile.h"
  22. #include "cmGlobalGenerator.h"
  23. #include "cmInstallGenerator.h"
  24. #include "cmListFileCache.h"
  25. #include "cmMessenger.h"
  26. #include "cmSourceFile.h"
  27. #include "cmSourceFileLocation.h"
  28. #include "cmState.h"
  29. #include "cmSystemTools.h"
  30. #include "cmTest.h"
  31. #include "cmTestGenerator.h"
  32. #include "cmVersion.h"
  33. #include "cmake.h"
  34. #ifdef CMAKE_BUILD_WITH_CMAKE
  35. #include "cmVariableWatch.h"
  36. #endif
  37. #include <cm_auto_ptr.hxx>
  38. #include <cmsys/FStream.hxx>
  39. #include <cmsys/RegularExpression.hxx>
  40. #include <algorithm>
  41. #include <assert.h>
  42. #include <ctype.h>
  43. #include <sstream>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <utility>
  47. // default is not to be building executables
  48. cmMakefile::cmMakefile(cmGlobalGenerator* globalGenerator,
  49. cmState::Snapshot const& snapshot)
  50. : GlobalGenerator(globalGenerator)
  51. , StateSnapshot(snapshot)
  52. , Backtrace(snapshot)
  53. {
  54. this->IsSourceFileTryCompile = false;
  55. this->WarnUnused = this->GetCMakeInstance()->GetWarnUnused();
  56. this->CheckSystemVars = this->GetCMakeInstance()->GetCheckSystemVars();
  57. this->SuppressWatches = false;
  58. // Setup the default include complaint regular expression (match nothing).
  59. this->ComplainFileRegularExpression = "^$";
  60. this->DefineFlags = " ";
  61. this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
  62. this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
  63. this->cmAtVarRegex.compile("(@[A-Za-z_0-9/.+-]+@)");
  64. this->cmNamedCurly.compile("^[A-Za-z0-9/_.+-]+{");
  65. this->StateSnapshot =
  66. this->StateSnapshot.GetState()->CreatePolicyScopeSnapshot(
  67. this->StateSnapshot);
  68. // Enter a policy level for this directory.
  69. this->PushPolicy();
  70. // push empty loop block
  71. this->PushLoopBlockBarrier();
  72. // By default the check is not done. It is enabled by
  73. // cmListFileCache in the top level if necessary.
  74. this->CheckCMP0000 = false;
  75. #if defined(CMAKE_BUILD_WITH_CMAKE)
  76. this->AddSourceGroup("", "^.*$");
  77. this->AddSourceGroup("Source Files",
  78. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|f|f90|for|fpp"
  79. "|ftn|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  80. this->AddSourceGroup("Header Files", CM_HEADER_REGEX);
  81. this->AddSourceGroup("CMake Rules", "\\.rule$");
  82. this->AddSourceGroup("Resources", "\\.plist$");
  83. this->AddSourceGroup("Object Files", "\\.(lo|o|obj)$");
  84. #endif
  85. }
  86. cmMakefile::~cmMakefile()
  87. {
  88. cmDeleteAll(this->InstallGenerators);
  89. cmDeleteAll(this->TestGenerators);
  90. cmDeleteAll(this->SourceFiles);
  91. cmDeleteAll(this->Tests);
  92. cmDeleteAll(this->ImportedTargetsOwned);
  93. cmDeleteAll(this->FinalPassCommands);
  94. cmDeleteAll(this->FunctionBlockers);
  95. cmDeleteAll(this->EvaluationFiles);
  96. }
  97. void cmMakefile::IssueMessage(cmake::MessageType t,
  98. std::string const& text) const
  99. {
  100. this->GetCMakeInstance()->IssueMessage(t, text, this->GetBacktrace());
  101. }
  102. cmStringRange cmMakefile::GetIncludeDirectoriesEntries() const
  103. {
  104. return this->StateSnapshot.GetDirectory().GetIncludeDirectoriesEntries();
  105. }
  106. cmBacktraceRange cmMakefile::GetIncludeDirectoriesBacktraces() const
  107. {
  108. return this->StateSnapshot.GetDirectory()
  109. .GetIncludeDirectoriesEntryBacktraces();
  110. }
  111. cmStringRange cmMakefile::GetCompileOptionsEntries() const
  112. {
  113. return this->StateSnapshot.GetDirectory().GetCompileOptionsEntries();
  114. }
  115. cmBacktraceRange cmMakefile::GetCompileOptionsBacktraces() const
  116. {
  117. return this->StateSnapshot.GetDirectory().GetCompileOptionsEntryBacktraces();
  118. }
  119. cmStringRange cmMakefile::GetCompileDefinitionsEntries() const
  120. {
  121. return this->StateSnapshot.GetDirectory().GetCompileDefinitionsEntries();
  122. }
  123. cmBacktraceRange cmMakefile::GetCompileDefinitionsBacktraces() const
  124. {
  125. return this->StateSnapshot.GetDirectory()
  126. .GetCompileDefinitionsEntryBacktraces();
  127. }
  128. cmListFileBacktrace cmMakefile::GetBacktrace() const
  129. {
  130. return this->Backtrace;
  131. }
  132. cmListFileBacktrace cmMakefile::GetBacktrace(cmCommandContext const& cc) const
  133. {
  134. cmListFileContext lfc;
  135. lfc.Name = cc.Name;
  136. lfc.Line = cc.Line;
  137. lfc.FilePath = this->StateSnapshot.GetExecutionListFile();
  138. return this->Backtrace.Push(lfc);
  139. }
  140. cmListFileContext cmMakefile::GetExecutionContext() const
  141. {
  142. cmListFileContext const& cur = this->Backtrace.Top();
  143. cmListFileContext lfc;
  144. lfc.Name = cur.Name;
  145. lfc.Line = cur.Line;
  146. lfc.FilePath = this->StateSnapshot.GetExecutionListFile();
  147. return lfc;
  148. }
  149. void cmMakefile::PrintCommandTrace(const cmListFileFunction& lff) const
  150. {
  151. // Check if current file in the list of requested to trace...
  152. std::vector<std::string> const& trace_only_this_files =
  153. this->GetCMakeInstance()->GetTraceSources();
  154. std::string const& full_path = this->GetExecutionFilePath();
  155. std::string const& only_filename = cmSystemTools::GetFilenameName(full_path);
  156. bool trace = trace_only_this_files.empty();
  157. if (!trace) {
  158. for (std::vector<std::string>::const_iterator i =
  159. trace_only_this_files.begin();
  160. !trace && i != trace_only_this_files.end(); ++i) {
  161. std::string::size_type const pos = full_path.rfind(*i);
  162. trace = (pos != std::string::npos) &&
  163. ((pos + i->size()) == full_path.size()) &&
  164. (only_filename == cmSystemTools::GetFilenameName(*i));
  165. }
  166. // Do nothing if current file wasn't requested for trace...
  167. if (!trace) {
  168. return;
  169. }
  170. }
  171. std::ostringstream msg;
  172. msg << full_path << "(" << lff.Line << "): ";
  173. msg << lff.Name << "(";
  174. bool expand = this->GetCMakeInstance()->GetTraceExpand();
  175. std::string temp;
  176. for (std::vector<cmListFileArgument>::const_iterator i =
  177. lff.Arguments.begin();
  178. i != lff.Arguments.end(); ++i) {
  179. if (expand) {
  180. temp = i->Value;
  181. this->ExpandVariablesInString(temp);
  182. msg << temp;
  183. } else {
  184. msg << i->Value;
  185. }
  186. msg << " ";
  187. }
  188. msg << ")";
  189. cmSystemTools::Message(msg.str().c_str());
  190. }
  191. // Helper class to make sure the call stack is valid.
  192. class cmMakefileCall
  193. {
  194. public:
  195. cmMakefileCall(cmMakefile* mf, cmCommandContext const& cc,
  196. cmExecutionStatus& status)
  197. : Makefile(mf)
  198. {
  199. cmListFileContext const& lfc = cmListFileContext::FromCommandContext(
  200. cc, this->Makefile->StateSnapshot.GetExecutionListFile());
  201. this->Makefile->Backtrace = this->Makefile->Backtrace.Push(lfc);
  202. this->Makefile->ExecutionStatusStack.push_back(&status);
  203. }
  204. ~cmMakefileCall()
  205. {
  206. this->Makefile->ExecutionStatusStack.pop_back();
  207. this->Makefile->Backtrace = this->Makefile->Backtrace.Pop();
  208. }
  209. private:
  210. cmMakefile* Makefile;
  211. };
  212. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff,
  213. cmExecutionStatus& status)
  214. {
  215. bool result = true;
  216. // quick return if blocked
  217. if (this->IsFunctionBlocked(lff, status)) {
  218. // No error.
  219. return result;
  220. }
  221. std::string name = lff.Name;
  222. // Place this call on the call stack.
  223. cmMakefileCall stack_manager(this, lff, status);
  224. static_cast<void>(stack_manager);
  225. // Lookup the command prototype.
  226. if (cmCommand* proto = this->GetState()->GetCommand(name)) {
  227. // Clone the prototype.
  228. CM_AUTO_PTR<cmCommand> pcmd(proto->Clone());
  229. pcmd->SetMakefile(this);
  230. // Decide whether to invoke the command.
  231. if (pcmd->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
  232. (this->GetCMakeInstance()->GetWorkingMode() != cmake::SCRIPT_MODE ||
  233. pcmd->IsScriptable()))
  234. {
  235. // if trace is enabled, print out invoke information
  236. if (this->GetCMakeInstance()->GetTrace()) {
  237. this->PrintCommandTrace(lff);
  238. }
  239. bool hadPreviousNonFatalError = cmSystemTools::GetErrorOccuredFlag() &&
  240. !cmSystemTools::GetFatalErrorOccured();
  241. cmSystemTools::ResetErrorOccuredFlag();
  242. bool invokeSucceeded = pcmd->InvokeInitialPass(lff.Arguments, status);
  243. bool hadNestedError = cmSystemTools::GetErrorOccuredFlag() &&
  244. !cmSystemTools::GetFatalErrorOccured();
  245. if (hadPreviousNonFatalError) {
  246. cmSystemTools::SetErrorOccured();
  247. }
  248. if (!invokeSucceeded || hadNestedError) {
  249. if (!hadNestedError && !cmSystemTools::GetFatalErrorOccured()) {
  250. // The command invocation requested that we report an error.
  251. this->IssueMessage(cmake::FATAL_ERROR, pcmd->GetError());
  252. }
  253. result = false;
  254. if (this->GetCMakeInstance()->GetWorkingMode() != cmake::NORMAL_MODE) {
  255. cmSystemTools::SetFatalErrorOccured();
  256. }
  257. } else if (pcmd->HasFinalPass()) {
  258. // use the command
  259. this->FinalPassCommands.push_back(pcmd.release());
  260. }
  261. } else if (this->GetCMakeInstance()->GetWorkingMode() ==
  262. cmake::SCRIPT_MODE &&
  263. !pcmd->IsScriptable()) {
  264. std::string error = "Command ";
  265. error += pcmd->GetName();
  266. error += "() is not scriptable";
  267. this->IssueMessage(cmake::FATAL_ERROR, error);
  268. result = false;
  269. cmSystemTools::SetFatalErrorOccured();
  270. }
  271. } else {
  272. if (!cmSystemTools::GetFatalErrorOccured()) {
  273. std::string error = "Unknown CMake command \"";
  274. error += lff.Name;
  275. error += "\".";
  276. this->IssueMessage(cmake::FATAL_ERROR, error);
  277. result = false;
  278. cmSystemTools::SetFatalErrorOccured();
  279. }
  280. }
  281. return result;
  282. }
  283. class cmMakefile::IncludeScope
  284. {
  285. public:
  286. IncludeScope(cmMakefile* mf, std::string const& filenametoread,
  287. bool noPolicyScope);
  288. ~IncludeScope();
  289. void Quiet() { this->ReportError = false; }
  290. private:
  291. cmMakefile* Makefile;
  292. bool NoPolicyScope;
  293. bool CheckCMP0011;
  294. bool ReportError;
  295. void EnforceCMP0011();
  296. };
  297. cmMakefile::IncludeScope::IncludeScope(cmMakefile* mf,
  298. std::string const& filenametoread,
  299. bool noPolicyScope)
  300. : Makefile(mf)
  301. , NoPolicyScope(noPolicyScope)
  302. , CheckCMP0011(false)
  303. , ReportError(true)
  304. {
  305. this->Makefile->Backtrace = this->Makefile->Backtrace.Push(filenametoread);
  306. this->Makefile->PushFunctionBlockerBarrier();
  307. this->Makefile->StateSnapshot =
  308. this->Makefile->GetState()->CreateIncludeFileSnapshot(
  309. this->Makefile->StateSnapshot, filenametoread);
  310. if (!this->NoPolicyScope) {
  311. // Check CMP0011 to determine the policy scope type.
  312. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011)) {
  313. case cmPolicies::WARN:
  314. // We need to push a scope to detect whether the script sets
  315. // any policies that would affect the includer and therefore
  316. // requires a warning. We use a weak scope to simulate OLD
  317. // behavior by allowing policy changes to affect the includer.
  318. this->Makefile->PushPolicy(true);
  319. this->CheckCMP0011 = true;
  320. break;
  321. case cmPolicies::OLD:
  322. // OLD behavior is to not push a scope at all.
  323. this->NoPolicyScope = true;
  324. break;
  325. case cmPolicies::REQUIRED_IF_USED:
  326. case cmPolicies::REQUIRED_ALWAYS:
  327. // We should never make this policy required, but we handle it
  328. // here just in case.
  329. this->CheckCMP0011 = true;
  330. case cmPolicies::NEW:
  331. // NEW behavior is to push a (strong) scope.
  332. this->Makefile->PushPolicy();
  333. break;
  334. }
  335. }
  336. }
  337. cmMakefile::IncludeScope::~IncludeScope()
  338. {
  339. if (!this->NoPolicyScope) {
  340. // If we need to enforce policy CMP0011 then the top entry is the
  341. // one we pushed above. If the entry is empty, then the included
  342. // script did not set any policies that might affect the includer so
  343. // we do not need to enforce the policy.
  344. if (this->CheckCMP0011 &&
  345. !this->Makefile->StateSnapshot.HasDefinedPolicyCMP0011()) {
  346. this->CheckCMP0011 = false;
  347. }
  348. // Pop the scope we pushed for the script.
  349. this->Makefile->PopPolicy();
  350. // We enforce the policy after the script's policy stack entry has
  351. // been removed.
  352. if (this->CheckCMP0011) {
  353. this->EnforceCMP0011();
  354. }
  355. }
  356. this->Makefile->PopSnapshot(this->ReportError);
  357. this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
  358. this->Makefile->Backtrace = this->Makefile->Backtrace.Pop();
  359. }
  360. void cmMakefile::IncludeScope::EnforceCMP0011()
  361. {
  362. // We check the setting of this policy again because the included
  363. // script might actually set this policy for its includer.
  364. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0011)) {
  365. case cmPolicies::WARN:
  366. // Warn because the user did not set this policy.
  367. {
  368. std::ostringstream w;
  369. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0011) << "\n"
  370. << "The included script\n "
  371. << this->Makefile->GetExecutionFilePath() << "\n"
  372. << "affects policy settings. "
  373. << "CMake is implying the NO_POLICY_SCOPE option for compatibility, "
  374. << "so the effects are applied to the including context.";
  375. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  376. }
  377. break;
  378. case cmPolicies::REQUIRED_IF_USED:
  379. case cmPolicies::REQUIRED_ALWAYS: {
  380. std::ostringstream e;
  381. /* clang-format off */
  382. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0011) << "\n"
  383. << "The included script\n "
  384. << this->Makefile->GetExecutionFilePath() << "\n"
  385. << "affects policy settings, so it requires this policy to be set.";
  386. /* clang-format on */
  387. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  388. } break;
  389. case cmPolicies::OLD:
  390. case cmPolicies::NEW:
  391. // The script set this policy. We assume the purpose of the
  392. // script is to initialize policies for its includer, and since
  393. // the policy is now set for later scripts, we do not warn.
  394. break;
  395. }
  396. }
  397. bool cmMakefile::ReadDependentFile(const char* filename, bool noPolicyScope)
  398. {
  399. this->AddDefinition("CMAKE_PARENT_LIST_FILE",
  400. this->GetDefinition("CMAKE_CURRENT_LIST_FILE"));
  401. std::string filenametoread = cmSystemTools::CollapseFullPath(
  402. filename, this->GetCurrentSourceDirectory());
  403. IncludeScope incScope(this, filenametoread, noPolicyScope);
  404. cmListFile listFile;
  405. if (!listFile.ParseFile(filenametoread.c_str(), this->GetMessenger(),
  406. this->Backtrace)) {
  407. return false;
  408. }
  409. this->ReadListFile(listFile, filenametoread);
  410. if (cmSystemTools::GetFatalErrorOccured()) {
  411. incScope.Quiet();
  412. }
  413. return true;
  414. }
  415. class cmMakefile::ListFileScope
  416. {
  417. public:
  418. ListFileScope(cmMakefile* mf, std::string const& filenametoread)
  419. : Makefile(mf)
  420. , ReportError(true)
  421. {
  422. this->Makefile->Backtrace = this->Makefile->Backtrace.Push(filenametoread);
  423. this->Makefile->StateSnapshot =
  424. this->Makefile->GetState()->CreateInlineListFileSnapshot(
  425. this->Makefile->StateSnapshot, filenametoread);
  426. assert(this->Makefile->StateSnapshot.IsValid());
  427. this->Makefile->PushFunctionBlockerBarrier();
  428. }
  429. ~ListFileScope()
  430. {
  431. this->Makefile->PopSnapshot(this->ReportError);
  432. this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
  433. this->Makefile->Backtrace = this->Makefile->Backtrace.Pop();
  434. }
  435. void Quiet() { this->ReportError = false; }
  436. private:
  437. cmMakefile* Makefile;
  438. bool ReportError;
  439. };
  440. bool cmMakefile::ReadListFile(const char* filename)
  441. {
  442. std::string filenametoread = cmSystemTools::CollapseFullPath(
  443. filename, this->GetCurrentSourceDirectory());
  444. ListFileScope scope(this, filenametoread);
  445. cmListFile listFile;
  446. if (!listFile.ParseFile(filenametoread.c_str(), this->GetMessenger(),
  447. this->Backtrace)) {
  448. return false;
  449. }
  450. this->ReadListFile(listFile, filenametoread);
  451. if (cmSystemTools::GetFatalErrorOccured()) {
  452. scope.Quiet();
  453. }
  454. return true;
  455. }
  456. void cmMakefile::ReadListFile(cmListFile const& listFile,
  457. std::string const& filenametoread)
  458. {
  459. // add this list file to the list of dependencies
  460. this->ListFiles.push_back(filenametoread);
  461. std::string currentParentFile =
  462. this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
  463. std::string currentFile = this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE");
  464. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread.c_str());
  465. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  466. cmSystemTools::GetFilenamePath(filenametoread).c_str());
  467. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  468. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  469. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  470. // Run the parsed commands.
  471. const size_t numberFunctions = listFile.Functions.size();
  472. for (size_t i = 0; i < numberFunctions; ++i) {
  473. cmExecutionStatus status;
  474. this->ExecuteCommand(listFile.Functions[i], status);
  475. if (cmSystemTools::GetFatalErrorOccured()) {
  476. break;
  477. }
  478. if (status.GetReturnInvoked()) {
  479. // Exit early due to return command.
  480. break;
  481. }
  482. }
  483. this->CheckForUnusedVariables();
  484. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  485. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  486. this->AddDefinition("CMAKE_CURRENT_LIST_DIR",
  487. cmSystemTools::GetFilenamePath(currentFile).c_str());
  488. this->MarkVariableAsUsed("CMAKE_PARENT_LIST_FILE");
  489. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_FILE");
  490. this->MarkVariableAsUsed("CMAKE_CURRENT_LIST_DIR");
  491. }
  492. void cmMakefile::EnforceDirectoryLevelRules() const
  493. {
  494. // Diagnose a violation of CMP0000 if necessary.
  495. if (this->CheckCMP0000) {
  496. std::ostringstream msg;
  497. msg << "No cmake_minimum_required command is present. "
  498. << "A line of code such as\n"
  499. << " cmake_minimum_required(VERSION " << cmVersion::GetMajorVersion()
  500. << "." << cmVersion::GetMinorVersion() << ")\n"
  501. << "should be added at the top of the file. "
  502. << "The version specified may be lower if you wish to "
  503. << "support older CMake versions for this project. "
  504. << "For more information run "
  505. << "\"cmake --help-policy CMP0000\".";
  506. switch (this->GetPolicyStatus(cmPolicies::CMP0000)) {
  507. case cmPolicies::WARN:
  508. // Warn because the user did not provide a mimimum required
  509. // version.
  510. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
  511. msg.str(), this->Backtrace);
  512. case cmPolicies::OLD:
  513. // OLD behavior is to use policy version 2.4 set in
  514. // cmListFileCache.
  515. break;
  516. case cmPolicies::REQUIRED_IF_USED:
  517. case cmPolicies::REQUIRED_ALWAYS:
  518. case cmPolicies::NEW:
  519. // NEW behavior is to issue an error.
  520. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, msg.str(),
  521. this->Backtrace);
  522. cmSystemTools::SetFatalErrorOccured();
  523. return;
  524. }
  525. }
  526. }
  527. void cmMakefile::AddEvaluationFile(
  528. const std::string& inputFile,
  529. CM_AUTO_PTR<cmCompiledGeneratorExpression> outputName,
  530. CM_AUTO_PTR<cmCompiledGeneratorExpression> condition, bool inputIsContent)
  531. {
  532. this->EvaluationFiles.push_back(new cmGeneratorExpressionEvaluationFile(
  533. inputFile, outputName, condition, inputIsContent));
  534. }
  535. std::vector<cmGeneratorExpressionEvaluationFile*>
  536. cmMakefile::GetEvaluationFiles() const
  537. {
  538. return this->EvaluationFiles;
  539. }
  540. std::vector<cmExportBuildFileGenerator*>
  541. cmMakefile::GetExportBuildFileGenerators() const
  542. {
  543. return this->ExportBuildFileGenerators;
  544. }
  545. void cmMakefile::RemoveExportBuildFileGeneratorCMP0024(
  546. cmExportBuildFileGenerator* gen)
  547. {
  548. std::vector<cmExportBuildFileGenerator*>::iterator it =
  549. std::find(this->ExportBuildFileGenerators.begin(),
  550. this->ExportBuildFileGenerators.end(), gen);
  551. if (it != this->ExportBuildFileGenerators.end()) {
  552. this->ExportBuildFileGenerators.erase(it);
  553. }
  554. }
  555. void cmMakefile::AddExportBuildFileGenerator(cmExportBuildFileGenerator* gen)
  556. {
  557. this->ExportBuildFileGenerators.push_back(gen);
  558. }
  559. namespace {
  560. struct file_not_persistent
  561. {
  562. bool operator()(const std::string& path) const
  563. {
  564. return !(path.find("CMakeTmp") == path.npos &&
  565. cmSystemTools::FileExists(path.c_str()));
  566. }
  567. };
  568. }
  569. void cmMakefile::FinalPass()
  570. {
  571. // do all the variable expansions here
  572. this->ExpandVariablesCMP0019();
  573. // give all the commands a chance to do something
  574. // after the file has been parsed before generation
  575. for (std::vector<cmCommand*>::iterator i = this->FinalPassCommands.begin();
  576. i != this->FinalPassCommands.end(); ++i) {
  577. (*i)->FinalPass();
  578. }
  579. // go through all configured files and see which ones still exist.
  580. // we don't want cmake to re-run if a configured file is created and deleted
  581. // during processing as that would make it a transient file that can't
  582. // influence the build process
  583. // remove_if will move all items that don't have a valid file name to the
  584. // back of the vector
  585. std::vector<std::string>::iterator new_output_files_end = std::remove_if(
  586. this->OutputFiles.begin(), this->OutputFiles.end(), file_not_persistent());
  587. // we just have to erase all items at the back
  588. this->OutputFiles.erase(new_output_files_end, this->OutputFiles.end());
  589. // if a configured file is used as input for another configured file,
  590. // and then deleted it will show up in the input list files so we
  591. // need to scan those too
  592. std::vector<std::string>::iterator new_list_files_end = std::remove_if(
  593. this->ListFiles.begin(), this->ListFiles.end(), file_not_persistent());
  594. this->ListFiles.erase(new_list_files_end, this->ListFiles.end());
  595. }
  596. // Generate the output file
  597. void cmMakefile::ConfigureFinalPass()
  598. {
  599. this->FinalPass();
  600. const char* oldValue = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  601. if (oldValue &&
  602. cmSystemTools::VersionCompare(cmSystemTools::OP_LESS, oldValue, "2.4")) {
  603. this->GetCMakeInstance()->IssueMessage(
  604. cmake::FATAL_ERROR,
  605. "You have set CMAKE_BACKWARDS_COMPATIBILITY to a CMake version less "
  606. "than 2.4. This version of CMake only supports backwards compatibility "
  607. "with CMake 2.4 or later. For compatibility with older versions please "
  608. "use any CMake 2.8.x release or lower.",
  609. this->Backtrace);
  610. }
  611. }
  612. void cmMakefile::AddCustomCommandToTarget(
  613. const std::string& target, const std::vector<std::string>& byproducts,
  614. const std::vector<std::string>& depends,
  615. const cmCustomCommandLines& commandLines, cmTarget::CustomCommandType type,
  616. const char* comment, const char* workingDir, bool escapeOldStyle,
  617. bool uses_terminal, const std::string& depfile)
  618. {
  619. // Find the target to which to add the custom command.
  620. cmTargets::iterator ti = this->Targets.find(target);
  621. if (ti == this->Targets.end()) {
  622. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  623. bool issueMessage = false;
  624. std::ostringstream e;
  625. switch (this->GetPolicyStatus(cmPolicies::CMP0040)) {
  626. case cmPolicies::WARN:
  627. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0040) << "\n";
  628. issueMessage = true;
  629. case cmPolicies::OLD:
  630. break;
  631. case cmPolicies::NEW:
  632. case cmPolicies::REQUIRED_IF_USED:
  633. case cmPolicies::REQUIRED_ALWAYS:
  634. issueMessage = true;
  635. messageType = cmake::FATAL_ERROR;
  636. }
  637. if (issueMessage) {
  638. if (cmTarget const* t = this->FindTargetToUse(target)) {
  639. if (t->IsImported()) {
  640. e << "TARGET '" << target
  641. << "' is IMPORTED and does not build here.";
  642. } else {
  643. e << "TARGET '" << target << "' was not created in this directory.";
  644. }
  645. } else {
  646. e << "No TARGET '" << target
  647. << "' has been created in this directory.";
  648. }
  649. IssueMessage(messageType, e.str());
  650. }
  651. return;
  652. }
  653. if (ti->second.GetType() == cmState::OBJECT_LIBRARY) {
  654. std::ostringstream e;
  655. e << "Target \"" << target
  656. << "\" is an OBJECT library "
  657. "that may not have PRE_BUILD, PRE_LINK, or POST_BUILD commands.";
  658. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  659. return;
  660. }
  661. if (ti->second.GetType() == cmState::INTERFACE_LIBRARY) {
  662. std::ostringstream e;
  663. e << "Target \"" << target
  664. << "\" is an INTERFACE library "
  665. "that may not have PRE_BUILD, PRE_LINK, or POST_BUILD commands.";
  666. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  667. return;
  668. }
  669. // Always create the byproduct sources and mark them generated.
  670. for (std::vector<std::string>::const_iterator o = byproducts.begin();
  671. o != byproducts.end(); ++o) {
  672. if (cmSourceFile* out = this->GetOrCreateSource(*o, true)) {
  673. out->SetProperty("GENERATED", "1");
  674. }
  675. }
  676. // Add the command to the appropriate build step for the target.
  677. std::vector<std::string> no_output;
  678. cmCustomCommand cc(this, no_output, byproducts, depends, commandLines,
  679. comment, workingDir);
  680. cc.SetEscapeOldStyle(escapeOldStyle);
  681. cc.SetEscapeAllowMakeVars(true);
  682. cc.SetUsesTerminal(uses_terminal);
  683. cc.SetDepfile(depfile);
  684. switch (type) {
  685. case cmTarget::PRE_BUILD:
  686. ti->second.AddPreBuildCommand(cc);
  687. break;
  688. case cmTarget::PRE_LINK:
  689. ti->second.AddPreLinkCommand(cc);
  690. break;
  691. case cmTarget::POST_BUILD:
  692. ti->second.AddPostBuildCommand(cc);
  693. break;
  694. }
  695. }
  696. cmSourceFile* cmMakefile::AddCustomCommandToOutput(
  697. const std::vector<std::string>& outputs,
  698. const std::vector<std::string>& byproducts,
  699. const std::vector<std::string>& depends, const std::string& main_dependency,
  700. const cmCustomCommandLines& commandLines, const char* comment,
  701. const char* workingDir, bool replace, bool escapeOldStyle,
  702. bool uses_terminal, const std::string& depfile)
  703. {
  704. // Make sure there is at least one output.
  705. if (outputs.empty()) {
  706. cmSystemTools::Error("Attempt to add a custom rule with no output!");
  707. return CM_NULLPTR;
  708. }
  709. // Validate custom commands. TODO: More strict?
  710. for (cmCustomCommandLines::const_iterator i = commandLines.begin();
  711. i != commandLines.end(); ++i) {
  712. cmCustomCommandLine const& cl = *i;
  713. if (!cl.empty() && !cl[0].empty() && cl[0][0] == '"') {
  714. std::ostringstream e;
  715. e << "COMMAND may not contain literal quotes:\n " << cl[0] << "\n";
  716. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  717. return CM_NULLPTR;
  718. }
  719. }
  720. // Choose a source file on which to store the custom command.
  721. cmSourceFile* file = CM_NULLPTR;
  722. if (!commandLines.empty() && !main_dependency.empty()) {
  723. // The main dependency was specified. Use it unless a different
  724. // custom command already used it.
  725. file = this->GetSource(main_dependency);
  726. if (file && file->GetCustomCommand() && !replace) {
  727. // The main dependency already has a custom command.
  728. if (commandLines == file->GetCustomCommand()->GetCommandLines()) {
  729. // The existing custom command is identical. Silently ignore
  730. // the duplicate.
  731. return file;
  732. }
  733. // The existing custom command is different. We need to
  734. // generate a rule file for this new command.
  735. file = CM_NULLPTR;
  736. } else if (!file) {
  737. file = this->CreateSource(main_dependency);
  738. }
  739. }
  740. // Generate a rule file if the main dependency is not available.
  741. if (!file) {
  742. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  743. // Construct a rule file associated with the first output produced.
  744. std::string outName = gg->GenerateRuleFile(outputs[0]);
  745. // Check if the rule file already exists.
  746. file = this->GetSource(outName);
  747. if (file && file->GetCustomCommand() && !replace) {
  748. // The rule file already exists.
  749. if (commandLines != file->GetCustomCommand()->GetCommandLines()) {
  750. cmSystemTools::Error("Attempt to add a custom rule to output \"",
  751. outName.c_str(),
  752. "\" which already has a custom rule.");
  753. }
  754. return file;
  755. }
  756. // Create a cmSourceFile for the rule file.
  757. if (!file) {
  758. file = this->CreateSource(outName, true);
  759. }
  760. file->SetProperty("__CMAKE_RULE", "1");
  761. }
  762. // Always create the output sources and mark them generated.
  763. for (std::vector<std::string>::const_iterator o = outputs.begin();
  764. o != outputs.end(); ++o) {
  765. if (cmSourceFile* out = this->GetOrCreateSource(*o, true)) {
  766. out->SetProperty("GENERATED", "1");
  767. }
  768. }
  769. for (std::vector<std::string>::const_iterator o = byproducts.begin();
  770. o != byproducts.end(); ++o) {
  771. if (cmSourceFile* out = this->GetOrCreateSource(*o, true)) {
  772. out->SetProperty("GENERATED", "1");
  773. }
  774. }
  775. // Attach the custom command to the file.
  776. if (file) {
  777. // Construct a complete list of dependencies.
  778. std::vector<std::string> depends2(depends);
  779. if (!main_dependency.empty()) {
  780. depends2.push_back(main_dependency);
  781. }
  782. cmCustomCommand* cc = new cmCustomCommand(
  783. this, outputs, byproducts, depends2, commandLines, comment, workingDir);
  784. cc->SetEscapeOldStyle(escapeOldStyle);
  785. cc->SetEscapeAllowMakeVars(true);
  786. cc->SetUsesTerminal(uses_terminal);
  787. cc->SetDepfile(depfile);
  788. file->SetCustomCommand(cc);
  789. this->UpdateOutputToSourceMap(outputs, file);
  790. }
  791. return file;
  792. }
  793. void cmMakefile::UpdateOutputToSourceMap(
  794. std::vector<std::string> const& outputs, cmSourceFile* source)
  795. {
  796. for (std::vector<std::string>::const_iterator o = outputs.begin();
  797. o != outputs.end(); ++o) {
  798. this->UpdateOutputToSourceMap(*o, source);
  799. }
  800. }
  801. void cmMakefile::UpdateOutputToSourceMap(std::string const& output,
  802. cmSourceFile* source)
  803. {
  804. OutputToSourceMap::iterator i = this->OutputToSource.find(output);
  805. if (i != this->OutputToSource.end()) {
  806. // Multiple custom commands produce the same output but may
  807. // be attached to a different source file (MAIN_DEPENDENCY).
  808. // LinearGetSourceFileWithOutput would return the first one,
  809. // so keep the mapping for the first one.
  810. //
  811. // TODO: Warn the user about this case. However, the VS 8 generator
  812. // triggers it for separate generate.stamp rules in ZERO_CHECK and
  813. // individual targets.
  814. return;
  815. }
  816. this->OutputToSource[output] = source;
  817. }
  818. cmSourceFile* cmMakefile::AddCustomCommandToOutput(
  819. const std::string& output, const std::vector<std::string>& depends,
  820. const std::string& main_dependency, const cmCustomCommandLines& commandLines,
  821. const char* comment, const char* workingDir, bool replace,
  822. bool escapeOldStyle, bool uses_terminal, const std::string& depfile)
  823. {
  824. std::vector<std::string> outputs;
  825. outputs.push_back(output);
  826. std::vector<std::string> no_byproducts;
  827. return this->AddCustomCommandToOutput(
  828. outputs, no_byproducts, depends, main_dependency, commandLines, comment,
  829. workingDir, replace, escapeOldStyle, uses_terminal, depfile);
  830. }
  831. void cmMakefile::AddCustomCommandOldStyle(
  832. const std::string& target, const std::vector<std::string>& outputs,
  833. const std::vector<std::string>& depends, const std::string& source,
  834. const cmCustomCommandLines& commandLines, const char* comment)
  835. {
  836. // Translate the old-style signature to one of the new-style
  837. // signatures.
  838. if (source == target) {
  839. // In the old-style signature if the source and target were the
  840. // same then it added a post-build rule to the target. Preserve
  841. // this behavior.
  842. std::vector<std::string> no_byproducts;
  843. this->AddCustomCommandToTarget(target, no_byproducts, depends,
  844. commandLines, cmTarget::POST_BUILD, comment,
  845. CM_NULLPTR);
  846. return;
  847. }
  848. // Each output must get its own copy of this rule.
  849. cmsys::RegularExpression sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
  850. "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
  851. "hm|hpp|hxx|in|txx|inl)$");
  852. for (std::vector<std::string>::const_iterator oi = outputs.begin();
  853. oi != outputs.end(); ++oi) {
  854. // Get the name of this output.
  855. const char* output = oi->c_str();
  856. cmSourceFile* sf;
  857. // Choose whether to use a main dependency.
  858. if (sourceFiles.find(source)) {
  859. // The source looks like a real file. Use it as the main dependency.
  860. sf = this->AddCustomCommandToOutput(output, depends, source,
  861. commandLines, comment, CM_NULLPTR);
  862. } else {
  863. // The source may not be a real file. Do not use a main dependency.
  864. std::string no_main_dependency = "";
  865. std::vector<std::string> depends2 = depends;
  866. depends2.push_back(source);
  867. sf = this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
  868. commandLines, comment, CM_NULLPTR);
  869. }
  870. // If the rule was added to the source (and not a .rule file),
  871. // then add the source to the target to make sure the rule is
  872. // included.
  873. if (sf && !sf->GetPropertyAsBool("__CMAKE_RULE")) {
  874. if (this->Targets.find(target) != this->Targets.end()) {
  875. this->Targets[target].AddSource(sf->GetFullPath());
  876. } else {
  877. cmSystemTools::Error("Attempt to add a custom rule to a target "
  878. "that does not exist yet for target ",
  879. target.c_str());
  880. return;
  881. }
  882. }
  883. }
  884. }
  885. cmTarget* cmMakefile::AddUtilityCommand(
  886. const std::string& utilityName, bool excludeFromAll,
  887. const std::vector<std::string>& depends, const char* workingDirectory,
  888. const char* command, const char* arg1, const char* arg2, const char* arg3,
  889. const char* arg4)
  890. {
  891. // Construct the command line for the custom command.
  892. cmCustomCommandLine commandLine;
  893. commandLine.push_back(command);
  894. if (arg1) {
  895. commandLine.push_back(arg1);
  896. }
  897. if (arg2) {
  898. commandLine.push_back(arg2);
  899. }
  900. if (arg3) {
  901. commandLine.push_back(arg3);
  902. }
  903. if (arg4) {
  904. commandLine.push_back(arg4);
  905. }
  906. cmCustomCommandLines commandLines;
  907. commandLines.push_back(commandLine);
  908. // Call the real signature of this method.
  909. return this->AddUtilityCommand(utilityName, excludeFromAll, workingDirectory,
  910. depends, commandLines);
  911. }
  912. cmTarget* cmMakefile::AddUtilityCommand(
  913. const std::string& utilityName, bool excludeFromAll,
  914. const char* workingDirectory, const std::vector<std::string>& depends,
  915. const cmCustomCommandLines& commandLines, bool escapeOldStyle,
  916. const char* comment, bool uses_terminal)
  917. {
  918. std::vector<std::string> no_byproducts;
  919. return this->AddUtilityCommand(utilityName, excludeFromAll, workingDirectory,
  920. no_byproducts, depends, commandLines,
  921. escapeOldStyle, comment, uses_terminal);
  922. }
  923. cmTarget* cmMakefile::AddUtilityCommand(
  924. const std::string& utilityName, bool excludeFromAll,
  925. const char* workingDirectory, const std::vector<std::string>& byproducts,
  926. const std::vector<std::string>& depends,
  927. const cmCustomCommandLines& commandLines, bool escapeOldStyle,
  928. const char* comment, bool uses_terminal)
  929. {
  930. // Create a target instance for this utility.
  931. cmTarget* target = this->AddNewTarget(cmState::UTILITY, utilityName);
  932. if (excludeFromAll) {
  933. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  934. }
  935. if (!comment) {
  936. // Use an empty comment to avoid generation of default comment.
  937. comment = "";
  938. }
  939. // Store the custom command in the target.
  940. if (!commandLines.empty() || !depends.empty()) {
  941. std::string force = this->GetCurrentBinaryDirectory();
  942. force += cmake::GetCMakeFilesDirectory();
  943. force += "/";
  944. force += utilityName;
  945. std::vector<std::string> forced;
  946. forced.push_back(force);
  947. std::string no_main_dependency = "";
  948. bool no_replace = false;
  949. this->AddCustomCommandToOutput(
  950. forced, byproducts, depends, no_main_dependency, commandLines, comment,
  951. workingDirectory, no_replace, escapeOldStyle, uses_terminal);
  952. cmSourceFile* sf = target->AddSourceCMP0049(force);
  953. // The output is not actually created so mark it symbolic.
  954. if (sf) {
  955. sf->SetProperty("SYMBOLIC", "1");
  956. } else {
  957. cmSystemTools::Error("Could not get source file entry for ",
  958. force.c_str());
  959. }
  960. // Always create the byproduct sources and mark them generated.
  961. for (std::vector<std::string>::const_iterator o = byproducts.begin();
  962. o != byproducts.end(); ++o) {
  963. if (cmSourceFile* out = this->GetOrCreateSource(*o, true)) {
  964. out->SetProperty("GENERATED", "1");
  965. }
  966. }
  967. }
  968. return target;
  969. }
  970. void cmMakefile::AddDefineFlag(const char* flag)
  971. {
  972. if (!flag) {
  973. return;
  974. }
  975. // Update the string used for the old DEFINITIONS property.
  976. this->AddDefineFlag(flag, this->DefineFlagsOrig);
  977. // If this is really a definition, update COMPILE_DEFINITIONS.
  978. if (this->ParseDefineFlag(flag, false)) {
  979. return;
  980. }
  981. // Add this flag that does not look like a definition.
  982. this->AddDefineFlag(flag, this->DefineFlags);
  983. }
  984. void cmMakefile::AddDefineFlag(const char* flag, std::string& dflags)
  985. {
  986. // remove any \n\r
  987. std::string::size_type initSize = dflags.size();
  988. dflags += std::string(" ") + flag;
  989. std::string::iterator flagStart = dflags.begin() + initSize + 1;
  990. std::replace(flagStart, dflags.end(), '\n', ' ');
  991. std::replace(flagStart, dflags.end(), '\r', ' ');
  992. }
  993. void cmMakefile::RemoveDefineFlag(const char* flag)
  994. {
  995. // Check the length of the flag to remove.
  996. std::string::size_type len = strlen(flag);
  997. if (len < 1) {
  998. return;
  999. }
  1000. // Update the string used for the old DEFINITIONS property.
  1001. this->RemoveDefineFlag(flag, len, this->DefineFlagsOrig);
  1002. // If this is really a definition, update COMPILE_DEFINITIONS.
  1003. if (this->ParseDefineFlag(flag, true)) {
  1004. return;
  1005. }
  1006. // Remove this flag that does not look like a definition.
  1007. this->RemoveDefineFlag(flag, len, this->DefineFlags);
  1008. }
  1009. void cmMakefile::RemoveDefineFlag(const char* flag, std::string::size_type len,
  1010. std::string& dflags)
  1011. {
  1012. // Remove all instances of the flag that are surrounded by
  1013. // whitespace or the beginning/end of the string.
  1014. for (std::string::size_type lpos = dflags.find(flag, 0);
  1015. lpos != std::string::npos; lpos = dflags.find(flag, lpos)) {
  1016. std::string::size_type rpos = lpos + len;
  1017. if ((lpos <= 0 || isspace(dflags[lpos - 1])) &&
  1018. (rpos >= dflags.size() || isspace(dflags[rpos]))) {
  1019. dflags.erase(lpos, len);
  1020. } else {
  1021. ++lpos;
  1022. }
  1023. }
  1024. }
  1025. void cmMakefile::AddCompileOption(const char* option)
  1026. {
  1027. this->AppendProperty("COMPILE_OPTIONS", option);
  1028. }
  1029. bool cmMakefile::ParseDefineFlag(std::string const& def, bool remove)
  1030. {
  1031. // Create a regular expression to match valid definitions.
  1032. static cmsys::RegularExpression valid("^[-/]D[A-Za-z_][A-Za-z0-9_]*(=.*)?$");
  1033. // Make sure the definition matches.
  1034. if (!valid.find(def.c_str())) {
  1035. return false;
  1036. }
  1037. // Definitions with non-trivial values require a policy check.
  1038. static cmsys::RegularExpression trivial(
  1039. "^[-/]D[A-Za-z_][A-Za-z0-9_]*(=[A-Za-z0-9_.]+)?$");
  1040. if (!trivial.find(def.c_str())) {
  1041. // This definition has a non-trivial value.
  1042. switch (this->GetPolicyStatus(cmPolicies::CMP0005)) {
  1043. case cmPolicies::WARN:
  1044. this->IssueMessage(cmake::AUTHOR_WARNING,
  1045. cmPolicies::GetPolicyWarning(cmPolicies::CMP0005));
  1046. case cmPolicies::OLD:
  1047. // OLD behavior is to not escape the value. We should not
  1048. // convert the definition to use the property.
  1049. return false;
  1050. case cmPolicies::REQUIRED_IF_USED:
  1051. case cmPolicies::REQUIRED_ALWAYS:
  1052. this->IssueMessage(
  1053. cmake::FATAL_ERROR,
  1054. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0005));
  1055. return false;
  1056. case cmPolicies::NEW:
  1057. // NEW behavior is to escape the value. Proceed to convert it
  1058. // to an entry in the property.
  1059. break;
  1060. }
  1061. }
  1062. // Get the definition part after the flag.
  1063. const char* define = def.c_str() + 2;
  1064. if (remove) {
  1065. if (const char* cdefs = this->GetProperty("COMPILE_DEFINITIONS")) {
  1066. // Expand the list.
  1067. std::vector<std::string> defs;
  1068. cmSystemTools::ExpandListArgument(cdefs, defs);
  1069. // Recompose the list without the definition.
  1070. std::vector<std::string>::const_iterator defEnd =
  1071. std::remove(defs.begin(), defs.end(), define);
  1072. std::vector<std::string>::const_iterator defBegin = defs.begin();
  1073. std::string ndefs = cmJoin(cmMakeRange(defBegin, defEnd), ";");
  1074. // Store the new list.
  1075. this->SetProperty("COMPILE_DEFINITIONS", ndefs.c_str());
  1076. }
  1077. } else {
  1078. // Append the definition to the directory property.
  1079. this->AppendProperty("COMPILE_DEFINITIONS", define);
  1080. }
  1081. return true;
  1082. }
  1083. void cmMakefile::AddLinkLibrary(const std::string& lib,
  1084. cmTargetLinkLibraryType llt)
  1085. {
  1086. cmTarget::LibraryID tmp;
  1087. tmp.first = lib;
  1088. tmp.second = llt;
  1089. this->LinkLibraries.push_back(tmp);
  1090. }
  1091. void cmMakefile::AddLinkLibraryForTarget(const std::string& target,
  1092. const std::string& lib,
  1093. cmTargetLinkLibraryType llt)
  1094. {
  1095. cmTargets::iterator i = this->Targets.find(target);
  1096. if (i != this->Targets.end()) {
  1097. cmTarget* tgt = this->GetGlobalGenerator()->FindTarget(lib);
  1098. if (tgt) {
  1099. // if it is not a static or shared library then you can not link to it
  1100. if (!((tgt->GetType() == cmState::STATIC_LIBRARY) ||
  1101. (tgt->GetType() == cmState::SHARED_LIBRARY) ||
  1102. (tgt->GetType() == cmState::INTERFACE_LIBRARY) ||
  1103. tgt->IsExecutableWithExports())) {
  1104. std::ostringstream e;
  1105. e << "Target \"" << lib << "\" of type "
  1106. << cmState::GetTargetTypeName(tgt->GetType())
  1107. << " may not be linked into another target. "
  1108. << "One may link only to STATIC or SHARED libraries, or "
  1109. << "to executables with the ENABLE_EXPORTS property set.";
  1110. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1111. }
  1112. }
  1113. i->second.AddLinkLibrary(*this, target, lib, llt);
  1114. } else {
  1115. std::ostringstream e;
  1116. e << "Attempt to add link library \"" << lib << "\" to target \"" << target
  1117. << "\" which is not built in this directory.";
  1118. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1119. }
  1120. }
  1121. void cmMakefile::AddLinkDirectoryForTarget(const std::string& target,
  1122. const std::string& d)
  1123. {
  1124. cmTargets::iterator i = this->Targets.find(target);
  1125. if (i != this->Targets.end()) {
  1126. if (this->IsAlias(target)) {
  1127. std::ostringstream e;
  1128. e << "ALIAS target \"" << target << "\" "
  1129. << "may not be linked into another target.";
  1130. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1131. return;
  1132. }
  1133. i->second.AddLinkDirectory(d);
  1134. } else {
  1135. cmSystemTools::Error(
  1136. "Attempt to add link directories to non-existent target: ",
  1137. target.c_str(), " for directory ", d.c_str());
  1138. }
  1139. }
  1140. void cmMakefile::AddLinkLibrary(const std::string& lib)
  1141. {
  1142. this->AddLinkLibrary(lib, GENERAL_LibraryType);
  1143. }
  1144. void cmMakefile::InitializeFromParent(cmMakefile* parent)
  1145. {
  1146. this->SystemIncludeDirectories = parent->SystemIncludeDirectories;
  1147. // define flags
  1148. this->DefineFlags = parent->DefineFlags;
  1149. this->DefineFlagsOrig = parent->DefineFlagsOrig;
  1150. // Include transform property. There is no per-config version.
  1151. {
  1152. const char* prop = "IMPLICIT_DEPENDS_INCLUDE_TRANSFORM";
  1153. this->SetProperty(prop, parent->GetProperty(prop));
  1154. }
  1155. // compile definitions property and per-config versions
  1156. cmPolicies::PolicyStatus polSt = this->GetPolicyStatus(cmPolicies::CMP0043);
  1157. if (polSt == cmPolicies::WARN || polSt == cmPolicies::OLD) {
  1158. this->SetProperty("COMPILE_DEFINITIONS",
  1159. parent->GetProperty("COMPILE_DEFINITIONS"));
  1160. std::vector<std::string> configs;
  1161. this->GetConfigurations(configs);
  1162. for (std::vector<std::string>::const_iterator ci = configs.begin();
  1163. ci != configs.end(); ++ci) {
  1164. std::string defPropName = "COMPILE_DEFINITIONS_";
  1165. defPropName += cmSystemTools::UpperCase(*ci);
  1166. const char* prop = parent->GetProperty(defPropName);
  1167. this->SetProperty(defPropName, prop);
  1168. }
  1169. }
  1170. // link libraries
  1171. this->LinkLibraries = parent->LinkLibraries;
  1172. // link directories
  1173. this->SetProperty("LINK_DIRECTORIES",
  1174. parent->GetProperty("LINK_DIRECTORIES"));
  1175. // the initial project name
  1176. this->StateSnapshot.SetProjectName(parent->StateSnapshot.GetProjectName());
  1177. // Copy include regular expressions.
  1178. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  1179. // Imported targets.
  1180. this->ImportedTargets = parent->ImportedTargets;
  1181. }
  1182. void cmMakefile::PushFunctionScope(std::string const& fileName,
  1183. const cmPolicies::PolicyMap& pm)
  1184. {
  1185. this->StateSnapshot = this->GetState()->CreateFunctionCallSnapshot(
  1186. this->StateSnapshot, fileName);
  1187. assert(this->StateSnapshot.IsValid());
  1188. this->PushLoopBlockBarrier();
  1189. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1190. this->GetGlobalGenerator()->GetFileLockPool().PushFunctionScope();
  1191. #endif
  1192. this->PushFunctionBlockerBarrier();
  1193. this->PushPolicy(true, pm);
  1194. }
  1195. void cmMakefile::PopFunctionScope(bool reportError)
  1196. {
  1197. this->PopPolicy();
  1198. this->PopSnapshot(reportError);
  1199. this->PopFunctionBlockerBarrier(reportError);
  1200. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1201. this->GetGlobalGenerator()->GetFileLockPool().PopFunctionScope();
  1202. #endif
  1203. this->PopLoopBlockBarrier();
  1204. this->CheckForUnusedVariables();
  1205. }
  1206. void cmMakefile::PushMacroScope(std::string const& fileName,
  1207. const cmPolicies::PolicyMap& pm)
  1208. {
  1209. this->StateSnapshot =
  1210. this->GetState()->CreateMacroCallSnapshot(this->StateSnapshot, fileName);
  1211. assert(this->StateSnapshot.IsValid());
  1212. this->PushFunctionBlockerBarrier();
  1213. this->PushPolicy(true, pm);
  1214. }
  1215. void cmMakefile::PopMacroScope(bool reportError)
  1216. {
  1217. this->PopPolicy();
  1218. this->PopSnapshot(reportError);
  1219. this->PopFunctionBlockerBarrier(reportError);
  1220. }
  1221. bool cmMakefile::IsRootMakefile() const
  1222. {
  1223. return !this->StateSnapshot.GetBuildsystemDirectoryParent().IsValid();
  1224. }
  1225. class cmMakefile::BuildsystemFileScope
  1226. {
  1227. public:
  1228. BuildsystemFileScope(cmMakefile* mf)
  1229. : Makefile(mf)
  1230. , ReportError(true)
  1231. {
  1232. std::string currentStart =
  1233. this->Makefile->StateSnapshot.GetDirectory().GetCurrentSource();
  1234. currentStart += "/CMakeLists.txt";
  1235. this->Makefile->StateSnapshot.SetListFile(currentStart);
  1236. this->Makefile->StateSnapshot =
  1237. this->Makefile->StateSnapshot.GetState()->CreatePolicyScopeSnapshot(
  1238. this->Makefile->StateSnapshot);
  1239. this->Makefile->PushFunctionBlockerBarrier();
  1240. this->GG = mf->GetGlobalGenerator();
  1241. this->CurrentMakefile = this->GG->GetCurrentMakefile();
  1242. this->Snapshot = this->GG->GetCMakeInstance()->GetCurrentSnapshot();
  1243. this->GG->GetCMakeInstance()->SetCurrentSnapshot(this->Snapshot);
  1244. this->GG->SetCurrentMakefile(mf);
  1245. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1246. this->GG->GetFileLockPool().PushFileScope();
  1247. #endif
  1248. }
  1249. ~BuildsystemFileScope()
  1250. {
  1251. this->Makefile->PopFunctionBlockerBarrier(this->ReportError);
  1252. this->Makefile->PopSnapshot(this->ReportError);
  1253. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1254. this->GG->GetFileLockPool().PopFileScope();
  1255. #endif
  1256. this->GG->SetCurrentMakefile(this->CurrentMakefile);
  1257. this->GG->GetCMakeInstance()->SetCurrentSnapshot(this->Snapshot);
  1258. }
  1259. void Quiet() { this->ReportError = false; }
  1260. private:
  1261. cmMakefile* Makefile;
  1262. cmGlobalGenerator* GG;
  1263. cmMakefile* CurrentMakefile;
  1264. cmState::Snapshot Snapshot;
  1265. bool ReportError;
  1266. };
  1267. void cmMakefile::Configure()
  1268. {
  1269. std::string currentStart =
  1270. this->StateSnapshot.GetDirectory().GetCurrentSource();
  1271. currentStart += "/CMakeLists.txt";
  1272. // Add the bottom of all backtraces within this directory.
  1273. // We will never pop this scope because it should be available
  1274. // for messages during the generate step too.
  1275. this->Backtrace = this->Backtrace.Push(currentStart);
  1276. BuildsystemFileScope scope(this);
  1277. // make sure the CMakeFiles dir is there
  1278. std::string filesDir = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  1279. filesDir += cmake::GetCMakeFilesDirectory();
  1280. cmSystemTools::MakeDirectory(filesDir.c_str());
  1281. assert(cmSystemTools::FileExists(currentStart.c_str(), true));
  1282. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentStart.c_str());
  1283. cmListFile listFile;
  1284. if (!listFile.ParseFile(currentStart.c_str(), this->GetMessenger(),
  1285. this->Backtrace)) {
  1286. return;
  1287. }
  1288. if (this->IsRootMakefile()) {
  1289. bool hasVersion = false;
  1290. // search for the right policy command
  1291. for (std::vector<cmListFileFunction>::iterator i =
  1292. listFile.Functions.begin();
  1293. i != listFile.Functions.end(); ++i) {
  1294. if (cmSystemTools::LowerCase(i->Name) == "cmake_minimum_required") {
  1295. hasVersion = true;
  1296. break;
  1297. }
  1298. }
  1299. // if no policy command is found this is an error if they use any
  1300. // non advanced functions or a lot of functions
  1301. if (!hasVersion) {
  1302. bool isProblem = true;
  1303. if (listFile.Functions.size() < 30) {
  1304. // the list of simple commands DO NOT ADD TO THIS LIST!!!!!
  1305. // these commands must have backwards compatibility forever and
  1306. // and that is a lot longer than your tiny mind can comprehend mortal
  1307. std::set<std::string> allowedCommands;
  1308. allowedCommands.insert("project");
  1309. allowedCommands.insert("set");
  1310. allowedCommands.insert("if");
  1311. allowedCommands.insert("endif");
  1312. allowedCommands.insert("else");
  1313. allowedCommands.insert("elseif");
  1314. allowedCommands.insert("add_executable");
  1315. allowedCommands.insert("add_library");
  1316. allowedCommands.insert("target_link_libraries");
  1317. allowedCommands.insert("option");
  1318. allowedCommands.insert("message");
  1319. isProblem = false;
  1320. for (std::vector<cmListFileFunction>::iterator i =
  1321. listFile.Functions.begin();
  1322. i != listFile.Functions.end(); ++i) {
  1323. std::string name = cmSystemTools::LowerCase(i->Name);
  1324. if (allowedCommands.find(name) == allowedCommands.end()) {
  1325. isProblem = true;
  1326. break;
  1327. }
  1328. }
  1329. }
  1330. if (isProblem) {
  1331. // Tell the top level cmMakefile to diagnose
  1332. // this violation of CMP0000.
  1333. this->SetCheckCMP0000(true);
  1334. // Implicitly set the version for the user.
  1335. this->SetPolicyVersion("2.4");
  1336. }
  1337. }
  1338. bool hasProject = false;
  1339. // search for a project command
  1340. for (std::vector<cmListFileFunction>::iterator i =
  1341. listFile.Functions.begin();
  1342. i != listFile.Functions.end(); ++i) {
  1343. if (cmSystemTools::LowerCase(i->Name) == "project") {
  1344. hasProject = true;
  1345. break;
  1346. }
  1347. }
  1348. // if no project command is found, add one
  1349. if (!hasProject) {
  1350. cmListFileFunction project;
  1351. project.Name = "PROJECT";
  1352. cmListFileArgument prj("Project", cmListFileArgument::Unquoted, 0);
  1353. project.Arguments.push_back(prj);
  1354. listFile.Functions.insert(listFile.Functions.begin(), project);
  1355. }
  1356. }
  1357. this->ReadListFile(listFile, currentStart);
  1358. if (cmSystemTools::GetFatalErrorOccured()) {
  1359. scope.Quiet();
  1360. }
  1361. // at the end handle any old style subdirs
  1362. std::vector<cmMakefile*> subdirs = this->UnConfiguredDirectories;
  1363. // for each subdir recurse
  1364. std::vector<cmMakefile*>::iterator sdi = subdirs.begin();
  1365. for (; sdi != subdirs.end(); ++sdi) {
  1366. (*sdi)->StateSnapshot.InitializeFromParent_ForSubdirsCommand();
  1367. this->ConfigureSubDirectory(*sdi);
  1368. }
  1369. this->AddCMakeDependFilesFromUser();
  1370. }
  1371. void cmMakefile::ConfigureSubDirectory(cmMakefile* mf)
  1372. {
  1373. mf->InitializeFromParent(this);
  1374. std::string currentStart = mf->GetCurrentSourceDirectory();
  1375. if (this->GetCMakeInstance()->GetDebugOutput()) {
  1376. std::string msg = " Entering ";
  1377. msg += currentStart;
  1378. cmSystemTools::Message(msg.c_str());
  1379. }
  1380. std::string const currentStartFile = currentStart + "/CMakeLists.txt";
  1381. if (!cmSystemTools::FileExists(currentStartFile, true)) {
  1382. // The file is missing. Check policy CMP0014.
  1383. std::ostringstream e;
  1384. /* clang-format off */
  1385. e << "The source directory\n"
  1386. << " " << currentStart << "\n"
  1387. << "does not contain a CMakeLists.txt file.";
  1388. /* clang-format on */
  1389. switch (this->GetPolicyStatus(cmPolicies::CMP0014)) {
  1390. case cmPolicies::WARN:
  1391. // Print the warning.
  1392. /* clang-format off */
  1393. e << "\n"
  1394. << "CMake does not support this case but it used "
  1395. << "to work accidentally and is being allowed for "
  1396. << "compatibility."
  1397. << "\n"
  1398. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0014);
  1399. /* clang-format on */
  1400. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  1401. case cmPolicies::OLD:
  1402. // OLD behavior does not warn.
  1403. break;
  1404. case cmPolicies::REQUIRED_IF_USED:
  1405. case cmPolicies::REQUIRED_ALWAYS:
  1406. e << "\n" << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0014);
  1407. case cmPolicies::NEW:
  1408. // NEW behavior prints the error.
  1409. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1410. }
  1411. return;
  1412. }
  1413. // finally configure the subdir
  1414. mf->Configure();
  1415. if (this->GetCMakeInstance()->GetDebugOutput()) {
  1416. std::string msg = " Returning to ";
  1417. msg += this->GetCurrentSourceDirectory();
  1418. cmSystemTools::Message(msg.c_str());
  1419. }
  1420. }
  1421. void cmMakefile::AddSubDirectory(const std::string& srcPath,
  1422. const std::string& binPath,
  1423. bool excludeFromAll, bool immediate)
  1424. {
  1425. // Make sure the binary directory is unique.
  1426. if (!this->EnforceUniqueDir(srcPath, binPath)) {
  1427. return;
  1428. }
  1429. cmState::Snapshot newSnapshot =
  1430. this->GetState()->CreateBuildsystemDirectorySnapshot(this->StateSnapshot);
  1431. newSnapshot.GetDirectory().SetCurrentSource(srcPath);
  1432. newSnapshot.GetDirectory().SetCurrentBinary(binPath);
  1433. cmSystemTools::MakeDirectory(binPath.c_str());
  1434. cmMakefile* subMf = new cmMakefile(this->GlobalGenerator, newSnapshot);
  1435. this->GetGlobalGenerator()->AddMakefile(subMf);
  1436. if (excludeFromAll) {
  1437. subMf->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1438. }
  1439. if (immediate) {
  1440. this->ConfigureSubDirectory(subMf);
  1441. } else {
  1442. this->UnConfiguredDirectories.push_back(subMf);
  1443. }
  1444. }
  1445. const char* cmMakefile::GetCurrentSourceDirectory() const
  1446. {
  1447. return this->StateSnapshot.GetDirectory().GetCurrentSource();
  1448. }
  1449. const char* cmMakefile::GetCurrentBinaryDirectory() const
  1450. {
  1451. return this->StateSnapshot.GetDirectory().GetCurrentBinary();
  1452. }
  1453. std::vector<cmTarget*> cmMakefile::GetImportedTargets() const
  1454. {
  1455. std::vector<cmTarget*> tgts;
  1456. tgts.reserve(this->ImportedTargets.size());
  1457. for (TargetMap::const_iterator it = this->ImportedTargets.begin();
  1458. it != this->ImportedTargets.end(); ++it) {
  1459. tgts.push_back(it->second);
  1460. }
  1461. return tgts;
  1462. }
  1463. void cmMakefile::AddIncludeDirectories(const std::vector<std::string>& incs,
  1464. bool before)
  1465. {
  1466. if (incs.empty()) {
  1467. return;
  1468. }
  1469. cmListFileBacktrace lfbt = this->GetBacktrace();
  1470. std::string entryString = cmJoin(incs, ";");
  1471. if (before) {
  1472. this->StateSnapshot.GetDirectory().PrependIncludeDirectoriesEntry(
  1473. entryString, lfbt);
  1474. } else {
  1475. this->StateSnapshot.GetDirectory().AppendIncludeDirectoriesEntry(
  1476. entryString, lfbt);
  1477. }
  1478. // Property on each target:
  1479. for (cmTargets::iterator l = this->Targets.begin(); l != this->Targets.end();
  1480. ++l) {
  1481. cmTarget& t = l->second;
  1482. t.InsertInclude(entryString, lfbt, before);
  1483. }
  1484. }
  1485. void cmMakefile::AddSystemIncludeDirectories(const std::set<std::string>& incs)
  1486. {
  1487. if (incs.empty()) {
  1488. return;
  1489. }
  1490. this->SystemIncludeDirectories.insert(incs.begin(), incs.end());
  1491. for (cmTargets::iterator l = this->Targets.begin(); l != this->Targets.end();
  1492. ++l) {
  1493. cmTarget& t = l->second;
  1494. t.AddSystemIncludeDirectories(incs);
  1495. }
  1496. }
  1497. void cmMakefile::AddDefinition(const std::string& name, const char* value)
  1498. {
  1499. if (!value) {
  1500. return;
  1501. }
  1502. if (this->VariableInitialized(name)) {
  1503. this->LogUnused("changing definition", name);
  1504. }
  1505. this->StateSnapshot.SetDefinition(name, value);
  1506. #ifdef CMAKE_BUILD_WITH_CMAKE
  1507. cmVariableWatch* vv = this->GetVariableWatch();
  1508. if (vv) {
  1509. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1510. value, this);
  1511. }
  1512. #endif
  1513. }
  1514. void cmMakefile::AddCacheDefinition(const std::string& name, const char* value,
  1515. const char* doc,
  1516. cmState::CacheEntryType type, bool force)
  1517. {
  1518. const char* existingValue = this->GetState()->GetInitializedCacheValue(name);
  1519. // must be outside the following if() to keep it alive long enough
  1520. std::string nvalue;
  1521. if (existingValue &&
  1522. (this->GetState()->GetCacheEntryType(name) == cmState::UNINITIALIZED)) {
  1523. // if this is not a force, then use the value from the cache
  1524. // if it is a force, then use the value being passed in
  1525. if (!force) {
  1526. value = existingValue;
  1527. }
  1528. if (type == cmState::PATH || type == cmState::FILEPATH) {
  1529. std::vector<std::string>::size_type cc;
  1530. std::vector<std::string> files;
  1531. nvalue = value ? value : "";
  1532. cmSystemTools::ExpandListArgument(nvalue, files);
  1533. nvalue = "";
  1534. for (cc = 0; cc < files.size(); cc++) {
  1535. if (!cmSystemTools::IsOff(files[cc].c_str())) {
  1536. files[cc] = cmSystemTools::CollapseFullPath(files[cc]);
  1537. }
  1538. if (cc > 0) {
  1539. nvalue += ";";
  1540. }
  1541. nvalue += files[cc];
  1542. }
  1543. this->GetCMakeInstance()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  1544. nvalue = this->GetState()->GetInitializedCacheValue(name);
  1545. value = nvalue.c_str();
  1546. }
  1547. }
  1548. this->GetCMakeInstance()->AddCacheEntry(name, value, doc, type);
  1549. // if there was a definition then remove it
  1550. this->StateSnapshot.RemoveDefinition(name);
  1551. }
  1552. void cmMakefile::AddDefinition(const std::string& name, bool value)
  1553. {
  1554. if (this->VariableInitialized(name)) {
  1555. this->LogUnused("changing definition", name);
  1556. }
  1557. this->StateSnapshot.SetDefinition(name, value ? "ON" : "OFF");
  1558. #ifdef CMAKE_BUILD_WITH_CMAKE
  1559. cmVariableWatch* vv = this->GetVariableWatch();
  1560. if (vv) {
  1561. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1562. value ? "ON" : "OFF", this);
  1563. }
  1564. #endif
  1565. }
  1566. void cmMakefile::CheckForUnusedVariables() const
  1567. {
  1568. if (!this->WarnUnused) {
  1569. return;
  1570. }
  1571. const std::vector<std::string>& unused = this->StateSnapshot.UnusedKeys();
  1572. std::vector<std::string>::const_iterator it = unused.begin();
  1573. for (; it != unused.end(); ++it) {
  1574. this->LogUnused("out of scope", *it);
  1575. }
  1576. }
  1577. void cmMakefile::MarkVariableAsUsed(const std::string& var)
  1578. {
  1579. this->StateSnapshot.GetDefinition(var);
  1580. }
  1581. bool cmMakefile::VariableInitialized(const std::string& var) const
  1582. {
  1583. return this->StateSnapshot.IsInitialized(var);
  1584. }
  1585. void cmMakefile::LogUnused(const char* reason, const std::string& name) const
  1586. {
  1587. if (this->WarnUnused) {
  1588. std::string path;
  1589. if (!this->ExecutionStatusStack.empty()) {
  1590. path = this->GetExecutionContext().FilePath;
  1591. } else {
  1592. path = this->GetCurrentSourceDirectory();
  1593. path += "/CMakeLists.txt";
  1594. }
  1595. if (this->CheckSystemVars ||
  1596. cmSystemTools::IsSubDirectory(path, this->GetHomeDirectory()) ||
  1597. (cmSystemTools::IsSubDirectory(path, this->GetHomeOutputDirectory()) &&
  1598. !cmSystemTools::IsSubDirectory(path,
  1599. cmake::GetCMakeFilesDirectory()))) {
  1600. std::ostringstream msg;
  1601. msg << "unused variable (" << reason << ") \'" << name << "\'";
  1602. this->IssueMessage(cmake::AUTHOR_WARNING, msg.str());
  1603. }
  1604. }
  1605. }
  1606. void cmMakefile::RemoveDefinition(const std::string& name)
  1607. {
  1608. if (this->VariableInitialized(name)) {
  1609. this->LogUnused("unsetting", name);
  1610. }
  1611. this->StateSnapshot.RemoveDefinition(name);
  1612. #ifdef CMAKE_BUILD_WITH_CMAKE
  1613. cmVariableWatch* vv = this->GetVariableWatch();
  1614. if (vv) {
  1615. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS,
  1616. CM_NULLPTR, this);
  1617. }
  1618. #endif
  1619. }
  1620. void cmMakefile::RemoveCacheDefinition(const std::string& name)
  1621. {
  1622. this->GetState()->RemoveCacheEntry(name);
  1623. }
  1624. void cmMakefile::SetProjectName(std::string const& p)
  1625. {
  1626. this->StateSnapshot.SetProjectName(p);
  1627. }
  1628. void cmMakefile::AddGlobalLinkInformation(const std::string& name,
  1629. cmTarget& target)
  1630. {
  1631. // for these targets do not add anything
  1632. switch (target.GetType()) {
  1633. case cmState::UTILITY:
  1634. case cmState::GLOBAL_TARGET:
  1635. case cmState::INTERFACE_LIBRARY:
  1636. return;
  1637. default:;
  1638. }
  1639. if (const char* linkDirsProp = this->GetProperty("LINK_DIRECTORIES")) {
  1640. std::vector<std::string> linkDirs;
  1641. cmSystemTools::ExpandListArgument(linkDirsProp, linkDirs);
  1642. for (std::vector<std::string>::iterator j = linkDirs.begin();
  1643. j != linkDirs.end(); ++j) {
  1644. std::string newdir = *j;
  1645. // remove trailing slashes
  1646. if (*j->rbegin() == '/') {
  1647. newdir = j->substr(0, j->size() - 1);
  1648. }
  1649. if (std::find(this->LinkDirectories.begin(), this->LinkDirectories.end(),
  1650. newdir) == this->LinkDirectories.end()) {
  1651. target.AddLinkDirectory(*j);
  1652. }
  1653. }
  1654. }
  1655. target.MergeLinkLibraries(*this, name, this->LinkLibraries);
  1656. }
  1657. void cmMakefile::AddAlias(const std::string& lname, std::string const& tgtName)
  1658. {
  1659. this->AliasTargets[lname] = tgtName;
  1660. this->GetGlobalGenerator()->AddAlias(lname, tgtName);
  1661. }
  1662. cmTarget* cmMakefile::AddLibrary(const std::string& lname,
  1663. cmState::TargetType type,
  1664. const std::vector<std::string>& srcs,
  1665. bool excludeFromAll)
  1666. {
  1667. // wrong type ? default to STATIC
  1668. if ((type != cmState::STATIC_LIBRARY) && (type != cmState::SHARED_LIBRARY) &&
  1669. (type != cmState::MODULE_LIBRARY) && (type != cmState::OBJECT_LIBRARY) &&
  1670. (type != cmState::INTERFACE_LIBRARY)) {
  1671. this->IssueMessage(cmake::INTERNAL_ERROR,
  1672. "cmMakefile::AddLibrary given invalid target type.");
  1673. type = cmState::STATIC_LIBRARY;
  1674. }
  1675. cmTarget* target = this->AddNewTarget(type, lname);
  1676. // Clear its dependencies. Otherwise, dependencies might persist
  1677. // over changes in CMakeLists.txt, making the information stale and
  1678. // hence useless.
  1679. target->ClearDependencyInformation(*this, lname);
  1680. if (excludeFromAll) {
  1681. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1682. }
  1683. target->AddSources(srcs);
  1684. this->AddGlobalLinkInformation(lname, *target);
  1685. return target;
  1686. }
  1687. cmTarget* cmMakefile::AddExecutable(const char* exeName,
  1688. const std::vector<std::string>& srcs,
  1689. bool excludeFromAll)
  1690. {
  1691. cmTarget* target = this->AddNewTarget(cmState::EXECUTABLE, exeName);
  1692. if (excludeFromAll) {
  1693. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1694. }
  1695. target->AddSources(srcs);
  1696. this->AddGlobalLinkInformation(exeName, *target);
  1697. return target;
  1698. }
  1699. cmTarget* cmMakefile::AddNewTarget(cmState::TargetType type,
  1700. const std::string& name)
  1701. {
  1702. cmTargets::iterator it =
  1703. this->Targets.insert(cmTargets::value_type(name, cmTarget())).first;
  1704. cmTarget& target = it->second;
  1705. target.SetType(type, name);
  1706. target.SetMakefile(this);
  1707. this->GetGlobalGenerator()->IndexTarget(&it->second);
  1708. return &it->second;
  1709. }
  1710. cmSourceFile* cmMakefile::LinearGetSourceFileWithOutput(
  1711. const std::string& name) const
  1712. {
  1713. std::string out;
  1714. // look through all the source files that have custom commands
  1715. // and see if the custom command has the passed source file as an output
  1716. for (std::vector<cmSourceFile*>::const_iterator i =
  1717. this->SourceFiles.begin();
  1718. i != this->SourceFiles.end(); ++i) {
  1719. // does this source file have a custom command?
  1720. if ((*i)->GetCustomCommand()) {
  1721. // Does the output of the custom command match the source file name?
  1722. const std::vector<std::string>& outputs =
  1723. (*i)->GetCustomCommand()->GetOutputs();
  1724. for (std::vector<std::string>::const_iterator o = outputs.begin();
  1725. o != outputs.end(); ++o) {
  1726. out = *o;
  1727. std::string::size_type pos = out.rfind(name);
  1728. // If the output matches exactly
  1729. if (pos != out.npos && pos == out.size() - name.size() &&
  1730. (pos == 0 || out[pos - 1] == '/')) {
  1731. return *i;
  1732. }
  1733. }
  1734. }
  1735. }
  1736. // otherwise return NULL
  1737. return CM_NULLPTR;
  1738. }
  1739. cmSourceFile* cmMakefile::GetSourceFileWithOutput(
  1740. const std::string& name) const
  1741. {
  1742. // If the queried path is not absolute we use the backward compatible
  1743. // linear-time search for an output with a matching suffix.
  1744. if (!cmSystemTools::FileIsFullPath(name.c_str())) {
  1745. return this->LinearGetSourceFileWithOutput(name);
  1746. }
  1747. // Otherwise we use an efficient lookup map.
  1748. OutputToSourceMap::const_iterator o = this->OutputToSource.find(name);
  1749. if (o != this->OutputToSource.end()) {
  1750. return (*o).second;
  1751. }
  1752. return CM_NULLPTR;
  1753. }
  1754. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1755. cmSourceGroup* cmMakefile::GetSourceGroup(
  1756. const std::vector<std::string>& name) const
  1757. {
  1758. cmSourceGroup* sg = CM_NULLPTR;
  1759. // first look for source group starting with the same as the one we want
  1760. for (std::vector<cmSourceGroup>::const_iterator sgIt =
  1761. this->SourceGroups.begin();
  1762. sgIt != this->SourceGroups.end(); ++sgIt) {
  1763. std::string sgName = sgIt->GetName();
  1764. if (sgName == name[0]) {
  1765. sg = const_cast<cmSourceGroup*>(&(*sgIt));
  1766. break;
  1767. }
  1768. }
  1769. if (sg != CM_NULLPTR) {
  1770. // iterate through its children to find match source group
  1771. for (unsigned int i = 1; i < name.size(); ++i) {
  1772. sg = sg->LookupChild(name[i].c_str());
  1773. if (sg == CM_NULLPTR) {
  1774. break;
  1775. }
  1776. }
  1777. }
  1778. return sg;
  1779. }
  1780. void cmMakefile::AddSourceGroup(const std::string& name, const char* regex)
  1781. {
  1782. std::vector<std::string> nameVector;
  1783. nameVector.push_back(name);
  1784. this->AddSourceGroup(nameVector, regex);
  1785. }
  1786. void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
  1787. const char* regex)
  1788. {
  1789. cmSourceGroup* sg = CM_NULLPTR;
  1790. std::vector<std::string> currentName;
  1791. int i = 0;
  1792. const int lastElement = static_cast<int>(name.size() - 1);
  1793. for (i = lastElement; i >= 0; --i) {
  1794. currentName.assign(name.begin(), name.begin() + i + 1);
  1795. sg = this->GetSourceGroup(currentName);
  1796. if (sg != CM_NULLPTR) {
  1797. break;
  1798. }
  1799. }
  1800. // i now contains the index of the last found component
  1801. if (i == lastElement) {
  1802. // group already exists, replace its regular expression
  1803. if (regex && sg) {
  1804. // We only want to set the regular expression. If there are already
  1805. // source files in the group, we don't want to remove them.
  1806. sg->SetGroupRegex(regex);
  1807. }
  1808. return;
  1809. }
  1810. if (i == -1) {
  1811. // group does not exist nor belong to any existing group
  1812. // add its first component
  1813. this->SourceGroups.push_back(cmSourceGroup(name[0].c_str(), regex));
  1814. sg = this->GetSourceGroup(currentName);
  1815. i = 0; // last component found
  1816. }
  1817. if (!sg) {
  1818. cmSystemTools::Error("Could not create source group ");
  1819. return;
  1820. }
  1821. // build the whole source group path
  1822. for (++i; i <= lastElement; ++i) {
  1823. sg->AddChild(
  1824. cmSourceGroup(name[i].c_str(), CM_NULLPTR, sg->GetFullName()));
  1825. sg = sg->LookupChild(name[i].c_str());
  1826. }
  1827. sg->SetGroupRegex(regex);
  1828. }
  1829. #endif
  1830. static bool mightExpandVariablesCMP0019(const char* s)
  1831. {
  1832. return s && *s && strstr(s, "${") && strchr(s, '}');
  1833. }
  1834. void cmMakefile::ExpandVariablesCMP0019()
  1835. {
  1836. // Drop this ancient compatibility behavior with a policy.
  1837. cmPolicies::PolicyStatus pol = this->GetPolicyStatus(cmPolicies::CMP0019);
  1838. if (pol != cmPolicies::OLD && pol != cmPolicies::WARN) {
  1839. return;
  1840. }
  1841. std::ostringstream w;
  1842. const char* includeDirs = this->GetProperty("INCLUDE_DIRECTORIES");
  1843. if (mightExpandVariablesCMP0019(includeDirs)) {
  1844. std::string dirs = includeDirs;
  1845. this->ExpandVariablesInString(dirs, true, true);
  1846. if (pol == cmPolicies::WARN && dirs != includeDirs) {
  1847. /* clang-format off */
  1848. w << "Evaluated directory INCLUDE_DIRECTORIES\n"
  1849. << " " << includeDirs << "\n"
  1850. << "as\n"
  1851. << " " << dirs << "\n";
  1852. /* clang-format on */
  1853. }
  1854. this->SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  1855. }
  1856. // Also for each target's INCLUDE_DIRECTORIES property:
  1857. for (cmTargets::iterator l = this->Targets.begin(); l != this->Targets.end();
  1858. ++l) {
  1859. cmTarget& t = l->second;
  1860. if (t.GetType() == cmState::INTERFACE_LIBRARY ||
  1861. t.GetType() == cmState::GLOBAL_TARGET) {
  1862. continue;
  1863. }
  1864. includeDirs = t.GetProperty("INCLUDE_DIRECTORIES");
  1865. if (mightExpandVariablesCMP0019(includeDirs)) {
  1866. std::string dirs = includeDirs;
  1867. this->ExpandVariablesInString(dirs, true, true);
  1868. if (pol == cmPolicies::WARN && dirs != includeDirs) {
  1869. /* clang-format off */
  1870. w << "Evaluated target " << t.GetName() << " INCLUDE_DIRECTORIES\n"
  1871. << " " << includeDirs << "\n"
  1872. << "as\n"
  1873. << " " << dirs << "\n";
  1874. /* clang-format on */
  1875. }
  1876. t.SetProperty("INCLUDE_DIRECTORIES", dirs.c_str());
  1877. }
  1878. }
  1879. if (const char* linkDirsProp = this->GetProperty("LINK_DIRECTORIES")) {
  1880. if (mightExpandVariablesCMP0019(linkDirsProp)) {
  1881. std::string d = linkDirsProp;
  1882. std::string orig = linkDirsProp;
  1883. this->ExpandVariablesInString(d, true, true);
  1884. if (pol == cmPolicies::WARN && d != orig) {
  1885. /* clang-format off */
  1886. w << "Evaluated link directories\n"
  1887. << " " << orig << "\n"
  1888. << "as\n"
  1889. << " " << d << "\n";
  1890. /* clang-format on */
  1891. }
  1892. }
  1893. }
  1894. for (cmTarget::LinkLibraryVectorType::iterator l =
  1895. this->LinkLibraries.begin();
  1896. l != this->LinkLibraries.end(); ++l) {
  1897. if (mightExpandVariablesCMP0019(l->first.c_str())) {
  1898. std::string orig = l->first;
  1899. this->ExpandVariablesInString(l->first, true, true);
  1900. if (pol == cmPolicies::WARN && l->first != orig) {
  1901. /* clang-format off */
  1902. w << "Evaluated link library\n"
  1903. << " " << orig << "\n"
  1904. << "as\n"
  1905. << " " << l->first << "\n";
  1906. /* clang-format on */
  1907. }
  1908. }
  1909. }
  1910. if (!w.str().empty()) {
  1911. std::ostringstream m;
  1912. /* clang-format off */
  1913. m << cmPolicies::GetPolicyWarning(cmPolicies::CMP0019)
  1914. << "\n"
  1915. << "The following variable evaluations were encountered:\n"
  1916. << w.str();
  1917. /* clang-format on */
  1918. this->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING, m.str(),
  1919. this->Backtrace);
  1920. }
  1921. }
  1922. bool cmMakefile::IsOn(const std::string& name) const
  1923. {
  1924. const char* value = this->GetDefinition(name);
  1925. return cmSystemTools::IsOn(value);
  1926. }
  1927. bool cmMakefile::IsSet(const std::string& name) const
  1928. {
  1929. const char* value = this->GetDefinition(name);
  1930. if (!value) {
  1931. return false;
  1932. }
  1933. if (!*value) {
  1934. return false;
  1935. }
  1936. if (cmSystemTools::IsNOTFOUND(value)) {
  1937. return false;
  1938. }
  1939. return true;
  1940. }
  1941. bool cmMakefile::PlatformIs32Bit() const
  1942. {
  1943. if (const char* sizeof_dptr = this->GetDefinition("CMAKE_SIZEOF_VOID_P")) {
  1944. return atoi(sizeof_dptr) == 4;
  1945. }
  1946. return false;
  1947. }
  1948. bool cmMakefile::PlatformIs64Bit() const
  1949. {
  1950. if (const char* sizeof_dptr = this->GetDefinition("CMAKE_SIZEOF_VOID_P")) {
  1951. return atoi(sizeof_dptr) == 8;
  1952. }
  1953. return false;
  1954. }
  1955. bool cmMakefile::PlatformIsAppleIos() const
  1956. {
  1957. std::string sdkRoot;
  1958. sdkRoot = this->GetSafeDefinition("CMAKE_OSX_SYSROOT");
  1959. sdkRoot = cmSystemTools::LowerCase(sdkRoot);
  1960. const std::string embedded[] = {
  1961. "appletvos", "appletvsimulator", "iphoneos",
  1962. "iphonesimulator", "watchos", "watchsimulator",
  1963. };
  1964. for (size_t i = 0; i < sizeof(embedded) / sizeof(embedded[0]); ++i) {
  1965. if (sdkRoot.find(embedded[i]) == 0 ||
  1966. sdkRoot.find(std::string("/") + embedded[i]) != std::string::npos) {
  1967. return true;
  1968. }
  1969. }
  1970. return false;
  1971. }
  1972. const char* cmMakefile::GetSONameFlag(const std::string& language) const
  1973. {
  1974. std::string name = "CMAKE_SHARED_LIBRARY_SONAME";
  1975. if (!language.empty()) {
  1976. name += "_";
  1977. name += language;
  1978. }
  1979. name += "_FLAG";
  1980. return GetDefinition(name);
  1981. }
  1982. bool cmMakefile::CanIWriteThisFile(const char* fileName) const
  1983. {
  1984. if (!this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES")) {
  1985. return true;
  1986. }
  1987. // If we are doing an in-source build, then the test will always fail
  1988. if (cmSystemTools::SameFile(this->GetHomeDirectory(),
  1989. this->GetHomeOutputDirectory())) {
  1990. return !this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD");
  1991. }
  1992. return !cmSystemTools::IsSubDirectory(fileName, this->GetHomeDirectory()) ||
  1993. cmSystemTools::IsSubDirectory(fileName, this->GetHomeOutputDirectory());
  1994. }
  1995. const char* cmMakefile::GetRequiredDefinition(const std::string& name) const
  1996. {
  1997. const char* ret = this->GetDefinition(name);
  1998. if (!ret) {
  1999. cmSystemTools::Error("Error required internal CMake variable not "
  2000. "set, cmake may be not be built correctly.\n",
  2001. "Missing variable is:\n", name.c_str());
  2002. return "";
  2003. }
  2004. return ret;
  2005. }
  2006. bool cmMakefile::IsDefinitionSet(const std::string& name) const
  2007. {
  2008. const char* def = this->StateSnapshot.GetDefinition(name);
  2009. if (!def) {
  2010. def = this->GetState()->GetInitializedCacheValue(name);
  2011. }
  2012. #ifdef CMAKE_BUILD_WITH_CMAKE
  2013. if (cmVariableWatch* vv = this->GetVariableWatch()) {
  2014. if (!def) {
  2015. vv->VariableAccessed(
  2016. name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS, def, this);
  2017. }
  2018. }
  2019. #endif
  2020. return def != CM_NULLPTR;
  2021. }
  2022. const char* cmMakefile::GetDefinition(const std::string& name) const
  2023. {
  2024. const char* def = this->StateSnapshot.GetDefinition(name);
  2025. if (!def) {
  2026. def = this->GetState()->GetInitializedCacheValue(name);
  2027. }
  2028. #ifdef CMAKE_BUILD_WITH_CMAKE
  2029. cmVariableWatch* vv = this->GetVariableWatch();
  2030. if (vv && !this->SuppressWatches) {
  2031. bool const watch_function_executed = vv->VariableAccessed(
  2032. name, def ? cmVariableWatch::VARIABLE_READ_ACCESS
  2033. : cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS,
  2034. def, this);
  2035. if (watch_function_executed) {
  2036. // A callback was executed and may have caused re-allocation of the
  2037. // variable storage. Look it up again for now.
  2038. // FIXME: Refactor variable storage to avoid this problem.
  2039. def = this->StateSnapshot.GetDefinition(name);
  2040. if (!def) {
  2041. def = this->GetState()->GetInitializedCacheValue(name);
  2042. }
  2043. }
  2044. }
  2045. #endif
  2046. return def;
  2047. }
  2048. const char* cmMakefile::GetSafeDefinition(const std::string& def) const
  2049. {
  2050. const char* ret = this->GetDefinition(def);
  2051. if (!ret) {
  2052. return "";
  2053. }
  2054. return ret;
  2055. }
  2056. std::vector<std::string> cmMakefile::GetDefinitions() const
  2057. {
  2058. std::vector<std::string> res = this->StateSnapshot.ClosureKeys();
  2059. std::vector<std::string> cacheKeys = this->GetState()->GetCacheEntryKeys();
  2060. res.insert(res.end(), cacheKeys.begin(), cacheKeys.end());
  2061. std::sort(res.begin(), res.end());
  2062. return res;
  2063. }
  2064. const char* cmMakefile::ExpandVariablesInString(std::string& source) const
  2065. {
  2066. return this->ExpandVariablesInString(source, false, false);
  2067. }
  2068. const char* cmMakefile::ExpandVariablesInString(
  2069. std::string& source, bool escapeQuotes, bool noEscapes, bool atOnly,
  2070. const char* filename, long line, bool removeEmpty, bool replaceAt) const
  2071. {
  2072. bool compareResults = false;
  2073. cmake::MessageType mtype = cmake::LOG;
  2074. std::string errorstr;
  2075. std::string original;
  2076. // Sanity check the @ONLY mode.
  2077. if (atOnly && (!noEscapes || !removeEmpty)) {
  2078. // This case should never be called. At-only is for
  2079. // configure-file/string which always does no escapes.
  2080. this->IssueMessage(cmake::INTERNAL_ERROR,
  2081. "ExpandVariablesInString @ONLY called "
  2082. "on something with escapes.");
  2083. return source.c_str();
  2084. }
  2085. // Variables used in the WARN case.
  2086. std::string newResult;
  2087. std::string newErrorstr;
  2088. cmake::MessageType newError = cmake::LOG;
  2089. switch (this->GetPolicyStatus(cmPolicies::CMP0053)) {
  2090. case cmPolicies::WARN: {
  2091. // Save the original string for the warning.
  2092. original = source;
  2093. newResult = source;
  2094. compareResults = true;
  2095. // Suppress variable watches to avoid calling hooks twice. Suppress new
  2096. // dereferences since the OLD behavior is still what is actually used.
  2097. this->SuppressWatches = true;
  2098. newError = ExpandVariablesInStringNew(
  2099. newErrorstr, newResult, escapeQuotes, noEscapes, atOnly, filename,
  2100. line, removeEmpty, replaceAt);
  2101. this->SuppressWatches = false;
  2102. }
  2103. case cmPolicies::OLD:
  2104. mtype =
  2105. ExpandVariablesInStringOld(errorstr, source, escapeQuotes, noEscapes,
  2106. atOnly, filename, line, removeEmpty, true);
  2107. break;
  2108. case cmPolicies::REQUIRED_IF_USED:
  2109. case cmPolicies::REQUIRED_ALWAYS:
  2110. // Messaging here would be *very* verbose.
  2111. case cmPolicies::NEW:
  2112. mtype = ExpandVariablesInStringNew(errorstr, source, escapeQuotes,
  2113. noEscapes, atOnly, filename, line,
  2114. removeEmpty, replaceAt);
  2115. break;
  2116. }
  2117. // If it's an error in either case, just report the error...
  2118. if (mtype != cmake::LOG) {
  2119. if (mtype == cmake::FATAL_ERROR) {
  2120. cmSystemTools::SetFatalErrorOccured();
  2121. }
  2122. this->IssueMessage(mtype, errorstr);
  2123. }
  2124. // ...otherwise, see if there's a difference that needs to be warned about.
  2125. else if (compareResults && (newResult != source || newError != mtype)) {
  2126. std::string msg = cmPolicies::GetPolicyWarning(cmPolicies::CMP0053);
  2127. msg += "\n";
  2128. std::string msg_input = original;
  2129. cmSystemTools::ReplaceString(msg_input, "\n", "\n ");
  2130. msg += "For input:\n '";
  2131. msg += msg_input;
  2132. msg += "'\n";
  2133. std::string msg_old = source;
  2134. cmSystemTools::ReplaceString(msg_old, "\n", "\n ");
  2135. msg += "the old evaluation rules produce:\n '";
  2136. msg += msg_old;
  2137. msg += "'\n";
  2138. if (newError == mtype) {
  2139. std::string msg_new = newResult;
  2140. cmSystemTools::ReplaceString(msg_new, "\n", "\n ");
  2141. msg += "but the new evaluation rules produce:\n '";
  2142. msg += msg_new;
  2143. msg += "'\n";
  2144. } else {
  2145. std::string msg_err = newErrorstr;
  2146. cmSystemTools::ReplaceString(msg_err, "\n", "\n ");
  2147. msg += "but the new evaluation rules produce an error:\n ";
  2148. msg += msg_err;
  2149. msg += "\n";
  2150. }
  2151. msg +=
  2152. "Using the old result for compatibility since the policy is not set.";
  2153. this->IssueMessage(cmake::AUTHOR_WARNING, msg);
  2154. }
  2155. return source.c_str();
  2156. }
  2157. cmake::MessageType cmMakefile::ExpandVariablesInStringOld(
  2158. std::string& errorstr, std::string& source, bool escapeQuotes,
  2159. bool noEscapes, bool atOnly, const char* filename, long line,
  2160. bool removeEmpty, bool replaceAt) const
  2161. {
  2162. // Fast path strings without any special characters.
  2163. if (source.find_first_of("$@\\") == source.npos) {
  2164. return cmake::LOG;
  2165. }
  2166. // Special-case the @ONLY mode.
  2167. if (atOnly) {
  2168. // Store an original copy of the input.
  2169. std::string input = source;
  2170. // Start with empty output.
  2171. source = "";
  2172. // Look for one @VAR@ at a time.
  2173. const char* in = input.c_str();
  2174. while (this->cmAtVarRegex.find(in)) {
  2175. // Get the range of the string to replace.
  2176. const char* first = in + this->cmAtVarRegex.start();
  2177. const char* last = in + this->cmAtVarRegex.end();
  2178. // Store the unchanged part of the string now.
  2179. source.append(in, first - in);
  2180. // Lookup the definition of VAR.
  2181. std::string var(first + 1, last - first - 2);
  2182. if (const char* val = this->GetDefinition(var)) {
  2183. // Store the value in the output escaping as requested.
  2184. if (escapeQuotes) {
  2185. source.append(cmSystemTools::EscapeQuotes(val));
  2186. } else {
  2187. source.append(val);
  2188. }
  2189. }
  2190. // Continue looking for @VAR@ further along the string.
  2191. in = last;
  2192. }
  2193. // Append the rest of the unchanged part of the string.
  2194. source.append(in);
  2195. return cmake::LOG;
  2196. }
  2197. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  2198. // with GetDefinition(), if not found in the map, nothing is expanded.
  2199. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  2200. // the current environment variables.
  2201. cmCommandArgumentParserHelper parser;
  2202. parser.SetMakefile(this);
  2203. parser.SetLineFile(line, filename);
  2204. parser.SetEscapeQuotes(escapeQuotes);
  2205. parser.SetNoEscapeMode(noEscapes);
  2206. parser.SetReplaceAtSyntax(replaceAt);
  2207. parser.SetRemoveEmpty(removeEmpty);
  2208. int res = parser.ParseString(source.c_str(), 0);
  2209. const char* emsg = parser.GetError();
  2210. cmake::MessageType mtype = cmake::LOG;
  2211. if (res && !emsg[0]) {
  2212. source = parser.GetResult();
  2213. } else {
  2214. // Construct the main error message.
  2215. std::ostringstream error;
  2216. error << "Syntax error in cmake code ";
  2217. if (filename && line > 0) {
  2218. // This filename and line number may be more specific than the
  2219. // command context because one command invocation can have
  2220. // arguments on multiple lines.
  2221. error << "at\n"
  2222. << " " << filename << ":" << line << "\n";
  2223. }
  2224. error << "when parsing string\n"
  2225. << " " << source << "\n";
  2226. error << emsg;
  2227. // If the parser failed ("res" is false) then this is a real
  2228. // argument parsing error, so the policy applies. Otherwise the
  2229. // parser reported an error message without failing because the
  2230. // helper implementation is unhappy, which has always reported an
  2231. // error.
  2232. mtype = cmake::FATAL_ERROR;
  2233. if (!res) {
  2234. // This is a real argument parsing error. Use policy CMP0010 to
  2235. // decide whether it is an error.
  2236. switch (this->GetPolicyStatus(cmPolicies::CMP0010)) {
  2237. case cmPolicies::WARN:
  2238. error << "\n" << cmPolicies::GetPolicyWarning(cmPolicies::CMP0010);
  2239. case cmPolicies::OLD:
  2240. // OLD behavior is to just warn and continue.
  2241. mtype = cmake::AUTHOR_WARNING;
  2242. break;
  2243. case cmPolicies::REQUIRED_IF_USED:
  2244. case cmPolicies::REQUIRED_ALWAYS:
  2245. error << "\n"
  2246. << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0010);
  2247. case cmPolicies::NEW:
  2248. // NEW behavior is to report the error.
  2249. break;
  2250. }
  2251. }
  2252. errorstr = error.str();
  2253. }
  2254. return mtype;
  2255. }
  2256. typedef enum { NORMAL, ENVIRONMENT, CACHE } t_domain;
  2257. struct t_lookup
  2258. {
  2259. t_lookup()
  2260. : domain(NORMAL)
  2261. , loc(0)
  2262. {
  2263. }
  2264. t_domain domain;
  2265. size_t loc;
  2266. };
  2267. cmake::MessageType cmMakefile::ExpandVariablesInStringNew(
  2268. std::string& errorstr, std::string& source, bool escapeQuotes,
  2269. bool noEscapes, bool atOnly, const char* filename, long line,
  2270. bool removeEmpty, bool replaceAt) const
  2271. {
  2272. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  2273. // with GetDefinition(), if not found in the map, nothing is expanded.
  2274. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  2275. // the current environment variables.
  2276. const char* in = source.c_str();
  2277. const char* last = in;
  2278. std::string result;
  2279. result.reserve(source.size());
  2280. std::vector<t_lookup> openstack;
  2281. bool error = false;
  2282. bool done = false;
  2283. cmake::MessageType mtype = cmake::LOG;
  2284. cmState* state = this->GetCMakeInstance()->GetState();
  2285. do {
  2286. char inc = *in;
  2287. switch (inc) {
  2288. case '}':
  2289. if (!openstack.empty()) {
  2290. t_lookup var = openstack.back();
  2291. openstack.pop_back();
  2292. result.append(last, in - last);
  2293. std::string const& lookup = result.substr(var.loc);
  2294. const char* value = CM_NULLPTR;
  2295. std::string varresult;
  2296. std::string svalue;
  2297. static const std::string lineVar = "CMAKE_CURRENT_LIST_LINE";
  2298. switch (var.domain) {
  2299. case NORMAL:
  2300. if (filename && lookup == lineVar) {
  2301. std::ostringstream ostr;
  2302. ostr << line;
  2303. varresult = ostr.str();
  2304. } else {
  2305. value = this->GetDefinition(lookup);
  2306. }
  2307. break;
  2308. case ENVIRONMENT:
  2309. if (cmSystemTools::GetEnv(lookup, svalue)) {
  2310. value = svalue.c_str();
  2311. }
  2312. break;
  2313. case CACHE:
  2314. value = state->GetCacheEntryValue(lookup);
  2315. break;
  2316. }
  2317. // Get the string we're meant to append to.
  2318. if (value) {
  2319. if (escapeQuotes) {
  2320. varresult = cmSystemTools::EscapeQuotes(value);
  2321. } else {
  2322. varresult = value;
  2323. }
  2324. } else if (!removeEmpty) {
  2325. // check to see if we need to print a warning
  2326. // if strict mode is on and the variable has
  2327. // not been "cleared"/initialized with a set(foo ) call
  2328. if (this->GetCMakeInstance()->GetWarnUninitialized() &&
  2329. !this->VariableInitialized(lookup)) {
  2330. if (this->CheckSystemVars ||
  2331. cmSystemTools::IsSubDirectory(filename,
  2332. this->GetHomeDirectory()) ||
  2333. cmSystemTools::IsSubDirectory(
  2334. filename, this->GetHomeOutputDirectory())) {
  2335. std::ostringstream msg;
  2336. msg << "uninitialized variable \'" << lookup << "\'";
  2337. this->IssueMessage(cmake::AUTHOR_WARNING, msg.str());
  2338. }
  2339. }
  2340. }
  2341. result.replace(var.loc, result.size() - var.loc, varresult);
  2342. // Start looking from here on out.
  2343. last = in + 1;
  2344. }
  2345. break;
  2346. case '$':
  2347. if (!atOnly) {
  2348. t_lookup lookup;
  2349. const char* next = in + 1;
  2350. const char* start = CM_NULLPTR;
  2351. char nextc = *next;
  2352. if (nextc == '{') {
  2353. // Looking for a variable.
  2354. start = in + 2;
  2355. lookup.domain = NORMAL;
  2356. } else if (nextc == '<') {
  2357. } else if (!nextc) {
  2358. result.append(last, next - last);
  2359. last = next;
  2360. } else if (cmHasLiteralPrefix(next, "ENV{")) {
  2361. // Looking for an environment variable.
  2362. start = in + 5;
  2363. lookup.domain = ENVIRONMENT;
  2364. } else if (cmHasLiteralPrefix(next, "CACHE{")) {
  2365. // Looking for a cache variable.
  2366. start = in + 7;
  2367. lookup.domain = CACHE;
  2368. } else {
  2369. if (this->cmNamedCurly.find(next)) {
  2370. errorstr = "Syntax $" +
  2371. std::string(next, this->cmNamedCurly.end()) +
  2372. "{} is not supported. Only ${}, $ENV{}, "
  2373. "and $CACHE{} are allowed.";
  2374. mtype = cmake::FATAL_ERROR;
  2375. error = true;
  2376. }
  2377. }
  2378. if (start) {
  2379. result.append(last, in - last);
  2380. last = start;
  2381. in = start - 1;
  2382. lookup.loc = result.size();
  2383. openstack.push_back(lookup);
  2384. }
  2385. break;
  2386. }
  2387. case '\\':
  2388. if (!noEscapes) {
  2389. const char* next = in + 1;
  2390. char nextc = *next;
  2391. if (nextc == 't') {
  2392. result.append(last, in - last);
  2393. result.append("\t");
  2394. last = next + 1;
  2395. } else if (nextc == 'n') {
  2396. result.append(last, in - last);
  2397. result.append("\n");
  2398. last = next + 1;
  2399. } else if (nextc == 'r') {
  2400. result.append(last, in - last);
  2401. result.append("\r");
  2402. last = next + 1;
  2403. } else if (nextc == ';' && openstack.empty()) {
  2404. // Handled in ExpandListArgument; pass the backslash literally.
  2405. } else if (isalnum(nextc) || nextc == '\0') {
  2406. errorstr += "Invalid character escape '\\";
  2407. if (nextc) {
  2408. errorstr += nextc;
  2409. errorstr += "'.";
  2410. } else {
  2411. errorstr += "' (at end of input).";
  2412. }
  2413. error = true;
  2414. } else {
  2415. // Take what we've found so far, skipping the escape character.
  2416. result.append(last, in - last);
  2417. // Start tracking from the next character.
  2418. last = in + 1;
  2419. }
  2420. // Skip the next character since it was escaped, but don't read past
  2421. // the end of the string.
  2422. if (*last) {
  2423. ++in;
  2424. }
  2425. }
  2426. break;
  2427. case '\n':
  2428. // Onto the next line.
  2429. ++line;
  2430. break;
  2431. case '\0':
  2432. done = true;
  2433. break;
  2434. case '@':
  2435. if (replaceAt) {
  2436. const char* nextAt = strchr(in + 1, '@');
  2437. if (nextAt && nextAt != in + 1 &&
  2438. nextAt ==
  2439. in + 1 + strspn(in + 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  2440. "abcdefghijklmnopqrstuvwxyz"
  2441. "0123456789/_.+-")) {
  2442. std::string variable(in + 1, nextAt - in - 1);
  2443. std::string varresult = this->GetSafeDefinition(variable);
  2444. if (escapeQuotes) {
  2445. varresult = cmSystemTools::EscapeQuotes(varresult);
  2446. }
  2447. // Skip over the variable.
  2448. result.append(last, in - last);
  2449. result.append(varresult);
  2450. in = nextAt;
  2451. last = in + 1;
  2452. break;
  2453. }
  2454. }
  2455. // Failed to find a valid @ expansion; treat it as literal.
  2456. /* FALLTHROUGH */
  2457. default: {
  2458. if (!openstack.empty() &&
  2459. !(isalnum(inc) || inc == '_' || inc == '/' || inc == '.' ||
  2460. inc == '+' || inc == '-')) {
  2461. errorstr += "Invalid character (\'";
  2462. errorstr += inc;
  2463. result.append(last, in - last);
  2464. errorstr += "\') in a variable name: "
  2465. "'" +
  2466. result.substr(openstack.back().loc) + "'";
  2467. mtype = cmake::FATAL_ERROR;
  2468. error = true;
  2469. }
  2470. break;
  2471. }
  2472. }
  2473. // Look at the next character.
  2474. } while (!error && !done && *++in);
  2475. // Check for open variable references yet.
  2476. if (!error && !openstack.empty()) {
  2477. // There's an open variable reference waiting. Policy CMP0010 flags
  2478. // whether this is an error or not. The new parser now enforces
  2479. // CMP0010 as well.
  2480. errorstr += "There is an unterminated variable reference.";
  2481. error = true;
  2482. }
  2483. if (error) {
  2484. std::ostringstream emsg;
  2485. emsg << "Syntax error in cmake code ";
  2486. if (filename) {
  2487. // This filename and line number may be more specific than the
  2488. // command context because one command invocation can have
  2489. // arguments on multiple lines.
  2490. emsg << "at\n"
  2491. << " " << filename << ":" << line << "\n";
  2492. }
  2493. emsg << "when parsing string\n"
  2494. << " " << source << "\n";
  2495. emsg << errorstr;
  2496. mtype = cmake::FATAL_ERROR;
  2497. errorstr = emsg.str();
  2498. } else {
  2499. // Append the rest of the unchanged part of the string.
  2500. result.append(last);
  2501. source = result;
  2502. }
  2503. return mtype;
  2504. }
  2505. void cmMakefile::RemoveVariablesInString(std::string& source,
  2506. bool atOnly) const
  2507. {
  2508. if (!atOnly) {
  2509. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  2510. while (var.find(source)) {
  2511. source.erase(var.start(), var.end() - var.start());
  2512. }
  2513. }
  2514. if (!atOnly) {
  2515. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  2516. while (varb.find(source)) {
  2517. source.erase(varb.start(), varb.end() - varb.start());
  2518. }
  2519. }
  2520. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  2521. while (var2.find(source)) {
  2522. source.erase(var2.start(), var2.end() - var2.start());
  2523. }
  2524. }
  2525. std::string cmMakefile::GetConfigurations(std::vector<std::string>& configs,
  2526. bool singleConfig) const
  2527. {
  2528. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  2529. if (const char* configTypes =
  2530. this->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
  2531. cmSystemTools::ExpandListArgument(configTypes, configs);
  2532. }
  2533. return "";
  2534. }
  2535. const std::string& buildType = this->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2536. if (singleConfig && !buildType.empty()) {
  2537. configs.push_back(buildType);
  2538. }
  2539. return buildType;
  2540. }
  2541. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2542. /**
  2543. * Find a source group whose regular expression matches the filename
  2544. * part of the given source name. Search backward through the list of
  2545. * source groups, and take the first matching group found. This way
  2546. * non-inherited SOURCE_GROUP commands will have precedence over
  2547. * inherited ones.
  2548. */
  2549. cmSourceGroup* cmMakefile::FindSourceGroup(
  2550. const char* source, std::vector<cmSourceGroup>& groups) const
  2551. {
  2552. // First search for a group that lists the file explicitly.
  2553. for (std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2554. sg != groups.rend(); ++sg) {
  2555. cmSourceGroup* result = sg->MatchChildrenFiles(source);
  2556. if (result) {
  2557. return result;
  2558. }
  2559. }
  2560. // Now search for a group whose regex matches the file.
  2561. for (std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  2562. sg != groups.rend(); ++sg) {
  2563. cmSourceGroup* result = sg->MatchChildrenRegex(source);
  2564. if (result) {
  2565. return result;
  2566. }
  2567. }
  2568. // Shouldn't get here, but just in case, return the default group.
  2569. return &groups.front();
  2570. }
  2571. #endif
  2572. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff,
  2573. cmExecutionStatus& status)
  2574. {
  2575. // if there are no blockers get out of here
  2576. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end()) {
  2577. return false;
  2578. }
  2579. // loop over all function blockers to see if any block this command
  2580. // evaluate in reverse, this is critical for balanced IF statements etc
  2581. std::vector<cmFunctionBlocker*>::reverse_iterator pos;
  2582. for (pos = this->FunctionBlockers.rbegin();
  2583. pos != this->FunctionBlockers.rend(); ++pos) {
  2584. if ((*pos)->IsFunctionBlocked(lff, *this, status)) {
  2585. return true;
  2586. }
  2587. }
  2588. return false;
  2589. }
  2590. void cmMakefile::PushFunctionBlockerBarrier()
  2591. {
  2592. this->FunctionBlockerBarriers.push_back(this->FunctionBlockers.size());
  2593. }
  2594. void cmMakefile::PopFunctionBlockerBarrier(bool reportError)
  2595. {
  2596. // Remove any extra entries pushed on the barrier.
  2597. FunctionBlockersType::size_type barrier =
  2598. this->FunctionBlockerBarriers.back();
  2599. while (this->FunctionBlockers.size() > barrier) {
  2600. CM_AUTO_PTR<cmFunctionBlocker> fb(this->FunctionBlockers.back());
  2601. this->FunctionBlockers.pop_back();
  2602. if (reportError) {
  2603. // Report the context in which the unclosed block was opened.
  2604. cmListFileContext const& lfc = fb->GetStartingContext();
  2605. std::ostringstream e;
  2606. /* clang-format off */
  2607. e << "A logical block opening on the line\n"
  2608. << " " << lfc << "\n"
  2609. << "is not closed.";
  2610. /* clang-format on */
  2611. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  2612. reportError = false;
  2613. }
  2614. }
  2615. // Remove the barrier.
  2616. this->FunctionBlockerBarriers.pop_back();
  2617. }
  2618. void cmMakefile::PushLoopBlock()
  2619. {
  2620. assert(!this->LoopBlockCounter.empty());
  2621. this->LoopBlockCounter.top()++;
  2622. }
  2623. void cmMakefile::PopLoopBlock()
  2624. {
  2625. assert(!this->LoopBlockCounter.empty());
  2626. assert(this->LoopBlockCounter.top() > 0);
  2627. this->LoopBlockCounter.top()--;
  2628. }
  2629. void cmMakefile::PushLoopBlockBarrier()
  2630. {
  2631. this->LoopBlockCounter.push(0);
  2632. }
  2633. void cmMakefile::PopLoopBlockBarrier()
  2634. {
  2635. assert(!this->LoopBlockCounter.empty());
  2636. assert(this->LoopBlockCounter.top() == 0);
  2637. this->LoopBlockCounter.pop();
  2638. }
  2639. bool cmMakefile::IsLoopBlock() const
  2640. {
  2641. assert(!this->LoopBlockCounter.empty());
  2642. return !this->LoopBlockCounter.empty() && this->LoopBlockCounter.top() > 0;
  2643. }
  2644. std::string cmMakefile::GetExecutionFilePath() const
  2645. {
  2646. assert(this->StateSnapshot.IsValid());
  2647. return this->StateSnapshot.GetExecutionListFile();
  2648. }
  2649. bool cmMakefile::ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  2650. std::vector<std::string>& outArgs,
  2651. const char* filename) const
  2652. {
  2653. std::string efp = this->GetExecutionFilePath();
  2654. if (!filename) {
  2655. filename = efp.c_str();
  2656. }
  2657. std::vector<cmListFileArgument>::const_iterator i;
  2658. std::string value;
  2659. outArgs.reserve(inArgs.size());
  2660. for (i = inArgs.begin(); i != inArgs.end(); ++i) {
  2661. // No expansion in a bracket argument.
  2662. if (i->Delim == cmListFileArgument::Bracket) {
  2663. outArgs.push_back(i->Value);
  2664. continue;
  2665. }
  2666. // Expand the variables in the argument.
  2667. value = i->Value;
  2668. this->ExpandVariablesInString(value, false, false, false, filename,
  2669. i->Line, false, false);
  2670. // If the argument is quoted, it should be one argument.
  2671. // Otherwise, it may be a list of arguments.
  2672. if (i->Delim == cmListFileArgument::Quoted) {
  2673. outArgs.push_back(value);
  2674. } else {
  2675. cmSystemTools::ExpandListArgument(value, outArgs);
  2676. }
  2677. }
  2678. return !cmSystemTools::GetFatalErrorOccured();
  2679. }
  2680. bool cmMakefile::ExpandArguments(
  2681. std::vector<cmListFileArgument> const& inArgs,
  2682. std::vector<cmExpandedCommandArgument>& outArgs, const char* filename) const
  2683. {
  2684. std::string efp = this->GetExecutionFilePath();
  2685. if (!filename) {
  2686. filename = efp.c_str();
  2687. }
  2688. std::vector<cmListFileArgument>::const_iterator i;
  2689. std::string value;
  2690. outArgs.reserve(inArgs.size());
  2691. for (i = inArgs.begin(); i != inArgs.end(); ++i) {
  2692. // No expansion in a bracket argument.
  2693. if (i->Delim == cmListFileArgument::Bracket) {
  2694. outArgs.push_back(cmExpandedCommandArgument(i->Value, true));
  2695. continue;
  2696. }
  2697. // Expand the variables in the argument.
  2698. value = i->Value;
  2699. this->ExpandVariablesInString(value, false, false, false, filename,
  2700. i->Line, false, false);
  2701. // If the argument is quoted, it should be one argument.
  2702. // Otherwise, it may be a list of arguments.
  2703. if (i->Delim == cmListFileArgument::Quoted) {
  2704. outArgs.push_back(cmExpandedCommandArgument(value, true));
  2705. } else {
  2706. std::vector<std::string> stringArgs;
  2707. cmSystemTools::ExpandListArgument(value, stringArgs);
  2708. for (size_t j = 0; j < stringArgs.size(); ++j) {
  2709. outArgs.push_back(cmExpandedCommandArgument(stringArgs[j], false));
  2710. }
  2711. }
  2712. }
  2713. return !cmSystemTools::GetFatalErrorOccured();
  2714. }
  2715. void cmMakefile::AddFunctionBlocker(cmFunctionBlocker* fb)
  2716. {
  2717. if (!this->ExecutionStatusStack.empty()) {
  2718. // Record the context in which the blocker is created.
  2719. fb->SetStartingContext(this->GetExecutionContext());
  2720. }
  2721. this->FunctionBlockers.push_back(fb);
  2722. }
  2723. CM_AUTO_PTR<cmFunctionBlocker> cmMakefile::RemoveFunctionBlocker(
  2724. cmFunctionBlocker* fb, const cmListFileFunction& lff)
  2725. {
  2726. // Find the function blocker stack barrier for the current scope.
  2727. // We only remove a blocker whose index is not less than the barrier.
  2728. FunctionBlockersType::size_type barrier = 0;
  2729. if (!this->FunctionBlockerBarriers.empty()) {
  2730. barrier = this->FunctionBlockerBarriers.back();
  2731. }
  2732. // Search for the function blocker whose scope this command ends.
  2733. for (FunctionBlockersType::size_type i = this->FunctionBlockers.size();
  2734. i > barrier; --i) {
  2735. std::vector<cmFunctionBlocker*>::iterator pos =
  2736. this->FunctionBlockers.begin() + (i - 1);
  2737. if (*pos == fb) {
  2738. // Warn if the arguments do not match, but always remove.
  2739. if (!(*pos)->ShouldRemove(lff, *this)) {
  2740. cmListFileContext const& lfc = fb->GetStartingContext();
  2741. cmListFileContext closingContext =
  2742. cmListFileContext::FromCommandContext(lff, lfc.FilePath);
  2743. std::ostringstream e;
  2744. /* clang-format off */
  2745. e << "A logical block opening on the line\n"
  2746. << " " << lfc << "\n"
  2747. << "closes on the line\n"
  2748. << " " << closingContext << "\n"
  2749. << "with mis-matching arguments.";
  2750. /* clang-format on */
  2751. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2752. }
  2753. cmFunctionBlocker* b = *pos;
  2754. this->FunctionBlockers.erase(pos);
  2755. return CM_AUTO_PTR<cmFunctionBlocker>(b);
  2756. }
  2757. }
  2758. return CM_AUTO_PTR<cmFunctionBlocker>();
  2759. }
  2760. const char* cmMakefile::GetHomeDirectory() const
  2761. {
  2762. return this->GetCMakeInstance()->GetHomeDirectory();
  2763. }
  2764. const char* cmMakefile::GetHomeOutputDirectory() const
  2765. {
  2766. return this->GetCMakeInstance()->GetHomeOutputDirectory();
  2767. }
  2768. void cmMakefile::SetScriptModeFile(const char* scriptfile)
  2769. {
  2770. this->AddDefinition("CMAKE_SCRIPT_MODE_FILE", scriptfile);
  2771. }
  2772. void cmMakefile::SetArgcArgv(const std::vector<std::string>& args)
  2773. {
  2774. std::ostringstream strStream;
  2775. strStream << args.size();
  2776. this->AddDefinition("CMAKE_ARGC", strStream.str().c_str());
  2777. // this->MarkVariableAsUsed("CMAKE_ARGC");
  2778. for (unsigned int t = 0; t < args.size(); ++t) {
  2779. std::ostringstream tmpStream;
  2780. tmpStream << "CMAKE_ARGV" << t;
  2781. this->AddDefinition(tmpStream.str(), args[t].c_str());
  2782. // this->MarkVariableAsUsed(tmpStream.str().c_str());
  2783. }
  2784. }
  2785. cmSourceFile* cmMakefile::GetSource(const std::string& sourceName) const
  2786. {
  2787. cmSourceFileLocation sfl(this, sourceName);
  2788. for (std::vector<cmSourceFile*>::const_iterator sfi =
  2789. this->SourceFiles.begin();
  2790. sfi != this->SourceFiles.end(); ++sfi) {
  2791. cmSourceFile* sf = *sfi;
  2792. if (sf->Matches(sfl)) {
  2793. return sf;
  2794. }
  2795. }
  2796. return CM_NULLPTR;
  2797. }
  2798. cmSourceFile* cmMakefile::CreateSource(const std::string& sourceName,
  2799. bool generated)
  2800. {
  2801. cmSourceFile* sf = new cmSourceFile(this, sourceName);
  2802. if (generated) {
  2803. sf->SetProperty("GENERATED", "1");
  2804. }
  2805. this->SourceFiles.push_back(sf);
  2806. return sf;
  2807. }
  2808. cmSourceFile* cmMakefile::GetOrCreateSource(const std::string& sourceName,
  2809. bool generated)
  2810. {
  2811. if (cmSourceFile* esf = this->GetSource(sourceName)) {
  2812. return esf;
  2813. }
  2814. return this->CreateSource(sourceName, generated);
  2815. }
  2816. void cmMakefile::EnableLanguage(std::vector<std::string> const& lang,
  2817. bool optional)
  2818. {
  2819. this->AddDefinition("CMAKE_CFG_INTDIR",
  2820. this->GetGlobalGenerator()->GetCMakeCFGIntDir());
  2821. this->GetGlobalGenerator()->EnableLanguage(lang, this, optional);
  2822. }
  2823. int cmMakefile::TryCompile(const std::string& srcdir,
  2824. const std::string& bindir,
  2825. const std::string& projectName,
  2826. const std::string& targetName, bool fast,
  2827. const std::vector<std::string>* cmakeArgs,
  2828. std::string& output)
  2829. {
  2830. this->IsSourceFileTryCompile = fast;
  2831. // does the binary directory exist ? If not create it...
  2832. if (!cmSystemTools::FileIsDirectory(bindir)) {
  2833. cmSystemTools::MakeDirectory(bindir.c_str());
  2834. }
  2835. // change to the tests directory and run cmake
  2836. // use the cmake object instead of calling cmake
  2837. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2838. cmSystemTools::ChangeDirectory(bindir);
  2839. // make sure the same generator is used
  2840. // use this program as the cmake to be run, it should not
  2841. // be run that way but the cmake object requires a vailid path
  2842. cmake cm;
  2843. cm.SetIsInTryCompile(true);
  2844. cmGlobalGenerator* gg =
  2845. cm.CreateGlobalGenerator(this->GetGlobalGenerator()->GetName());
  2846. if (!gg) {
  2847. cmSystemTools::Error(
  2848. "Internal CMake error, TryCompile bad GlobalGenerator");
  2849. // return to the original directory
  2850. cmSystemTools::ChangeDirectory(cwd);
  2851. this->IsSourceFileTryCompile = false;
  2852. return 1;
  2853. }
  2854. cm.SetGlobalGenerator(gg);
  2855. // do a configure
  2856. cm.SetHomeDirectory(srcdir);
  2857. cm.SetHomeOutputDirectory(bindir);
  2858. cm.SetGeneratorPlatform(this->GetCMakeInstance()->GetGeneratorPlatform());
  2859. cm.SetGeneratorToolset(this->GetCMakeInstance()->GetGeneratorToolset());
  2860. cm.LoadCache();
  2861. if (!gg->IsMultiConfig()) {
  2862. if (const char* config =
  2863. this->GetDefinition("CMAKE_TRY_COMPILE_CONFIGURATION")) {
  2864. // Tell the single-configuration generator which one to use.
  2865. // Add this before the user-provided CMake arguments in case
  2866. // one of the arguments is -DCMAKE_BUILD_TYPE=...
  2867. cm.AddCacheEntry("CMAKE_BUILD_TYPE", config, "Build configuration",
  2868. cmState::STRING);
  2869. }
  2870. }
  2871. // if cmake args were provided then pass them in
  2872. if (cmakeArgs) {
  2873. // FIXME: Workaround to ignore unused CLI variables in try-compile.
  2874. //
  2875. // Ideally we should use SetArgs to honor options like --warn-unused-vars.
  2876. // However, there is a subtle problem when certain arguments are passed to
  2877. // a macro wrapping around try_compile or try_run that does not escape
  2878. // semicolons in its parameters but just passes ${ARGV} or ${ARGN}. In
  2879. // this case a list argument like "-DVAR=a;b" gets split into multiple
  2880. // cmake arguments "-DVAR=a" and "b". Currently SetCacheArgs ignores
  2881. // argument "b" and uses just "-DVAR=a", leading to a subtle bug in that
  2882. // the try_compile or try_run does not get the proper value of VAR. If we
  2883. // call SetArgs here then it would treat "b" as the source directory and
  2884. // cause an error such as "The source directory .../CMakeFiles/CMakeTmp/b
  2885. // does not exist", thus breaking the try_compile or try_run completely.
  2886. //
  2887. // Strictly speaking the bug is in the wrapper macro because the CMake
  2888. // language has always flattened nested lists and the macro should escape
  2889. // the semicolons in its arguments before forwarding them. However, this
  2890. // bug is so subtle that projects typically work anyway, usually because
  2891. // the value VAR=a is sufficient for the try_compile or try_run to get the
  2892. // correct result. Calling SetArgs here would break such projects that
  2893. // previously built. Instead we work around the issue by never reporting
  2894. // unused arguments and ignoring options such as --warn-unused-vars.
  2895. cm.SetWarnUnusedCli(false);
  2896. // cm.SetArgs(*cmakeArgs, true);
  2897. cm.SetCacheArgs(*cmakeArgs);
  2898. }
  2899. // to save time we pass the EnableLanguage info directly
  2900. gg->EnableLanguagesFromGenerator(this->GetGlobalGenerator(), this);
  2901. if (this->IsOn("CMAKE_SUPPRESS_DEVELOPER_WARNINGS")) {
  2902. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "TRUE", "",
  2903. cmState::INTERNAL);
  2904. } else {
  2905. cm.AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "FALSE", "",
  2906. cmState::INTERNAL);
  2907. }
  2908. if (cm.Configure() != 0) {
  2909. cmSystemTools::Error(
  2910. "Internal CMake error, TryCompile configure of cmake failed");
  2911. // return to the original directory
  2912. cmSystemTools::ChangeDirectory(cwd);
  2913. this->IsSourceFileTryCompile = false;
  2914. return 1;
  2915. }
  2916. if (cm.Generate() != 0) {
  2917. cmSystemTools::Error(
  2918. "Internal CMake error, TryCompile generation of cmake failed");
  2919. // return to the original directory
  2920. cmSystemTools::ChangeDirectory(cwd);
  2921. this->IsSourceFileTryCompile = false;
  2922. return 1;
  2923. }
  2924. // finally call the generator to actually build the resulting project
  2925. int ret = this->GetGlobalGenerator()->TryCompile(
  2926. srcdir, bindir, projectName, targetName, fast, output, this);
  2927. cmSystemTools::ChangeDirectory(cwd);
  2928. this->IsSourceFileTryCompile = false;
  2929. return ret;
  2930. }
  2931. bool cmMakefile::GetIsSourceFileTryCompile() const
  2932. {
  2933. return this->IsSourceFileTryCompile;
  2934. }
  2935. cmake* cmMakefile::GetCMakeInstance() const
  2936. {
  2937. return this->GlobalGenerator->GetCMakeInstance();
  2938. }
  2939. cmMessenger* cmMakefile::GetMessenger() const
  2940. {
  2941. return this->GetCMakeInstance()->GetMessenger();
  2942. }
  2943. cmGlobalGenerator* cmMakefile::GetGlobalGenerator() const
  2944. {
  2945. return this->GlobalGenerator;
  2946. }
  2947. #ifdef CMAKE_BUILD_WITH_CMAKE
  2948. cmVariableWatch* cmMakefile::GetVariableWatch() const
  2949. {
  2950. if (this->GetCMakeInstance() &&
  2951. this->GetCMakeInstance()->GetVariableWatch()) {
  2952. return this->GetCMakeInstance()->GetVariableWatch();
  2953. }
  2954. return CM_NULLPTR;
  2955. }
  2956. #endif
  2957. cmState* cmMakefile::GetState() const
  2958. {
  2959. return this->GetCMakeInstance()->GetState();
  2960. }
  2961. void cmMakefile::DisplayStatus(const char* message, float s) const
  2962. {
  2963. cmake* cm = this->GetCMakeInstance();
  2964. if (cm->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) {
  2965. // don't output any STATUS message in FIND_PACKAGE_MODE, since they will
  2966. // directly be fed to the compiler, which will be confused.
  2967. return;
  2968. }
  2969. cm->UpdateProgress(message, s);
  2970. }
  2971. std::string cmMakefile::GetModulesFile(const char* filename) const
  2972. {
  2973. std::string result;
  2974. // We search the module always in CMAKE_ROOT and in CMAKE_MODULE_PATH,
  2975. // and then decide based on the policy setting which one to return.
  2976. // See CMP0017 for more details.
  2977. // The specific problem was that KDE 4.5.0 installs a
  2978. // FindPackageHandleStandardArgs.cmake which doesn't have the new features
  2979. // of FPHSA.cmake introduced in CMake 2.8.3 yet, and by setting
  2980. // CMAKE_MODULE_PATH also e.g. FindZLIB.cmake from cmake included
  2981. // FPHSA.cmake from kdelibs and not from CMake, and tried to use the
  2982. // new features, which were not there in the version from kdelibs, and so
  2983. // failed ("
  2984. std::string moduleInCMakeRoot;
  2985. std::string moduleInCMakeModulePath;
  2986. // Always search in CMAKE_MODULE_PATH:
  2987. const char* cmakeModulePath = this->GetDefinition("CMAKE_MODULE_PATH");
  2988. if (cmakeModulePath) {
  2989. std::vector<std::string> modulePath;
  2990. cmSystemTools::ExpandListArgument(cmakeModulePath, modulePath);
  2991. // Look through the possible module directories.
  2992. for (std::vector<std::string>::iterator i = modulePath.begin();
  2993. i != modulePath.end(); ++i) {
  2994. std::string itempl = *i;
  2995. cmSystemTools::ConvertToUnixSlashes(itempl);
  2996. itempl += "/";
  2997. itempl += filename;
  2998. if (cmSystemTools::FileExists(itempl.c_str())) {
  2999. moduleInCMakeModulePath = itempl;
  3000. break;
  3001. }
  3002. }
  3003. }
  3004. // Always search in the standard modules location.
  3005. moduleInCMakeRoot = cmSystemTools::GetCMakeRoot();
  3006. moduleInCMakeRoot += "/Modules/";
  3007. moduleInCMakeRoot += filename;
  3008. cmSystemTools::ConvertToUnixSlashes(moduleInCMakeRoot);
  3009. if (!cmSystemTools::FileExists(moduleInCMakeRoot.c_str())) {
  3010. moduleInCMakeRoot = "";
  3011. }
  3012. // Normally, prefer the files found in CMAKE_MODULE_PATH. Only when the file
  3013. // from which we are being called is located itself in CMAKE_ROOT, then
  3014. // prefer results from CMAKE_ROOT depending on the policy setting.
  3015. result = moduleInCMakeModulePath;
  3016. if (result.empty()) {
  3017. result = moduleInCMakeRoot;
  3018. }
  3019. if (!moduleInCMakeModulePath.empty() && !moduleInCMakeRoot.empty()) {
  3020. const char* currentFile = this->GetDefinition("CMAKE_CURRENT_LIST_FILE");
  3021. std::string mods = cmSystemTools::GetCMakeRoot() + "/Modules/";
  3022. if (currentFile && strncmp(currentFile, mods.c_str(), mods.size()) == 0) {
  3023. switch (this->GetPolicyStatus(cmPolicies::CMP0017)) {
  3024. case cmPolicies::WARN: {
  3025. std::ostringstream e;
  3026. /* clang-format off */
  3027. e << "File " << currentFile << " includes "
  3028. << moduleInCMakeModulePath
  3029. << " (found via CMAKE_MODULE_PATH) which shadows "
  3030. << moduleInCMakeRoot << ". This may cause errors later on .\n"
  3031. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0017);
  3032. /* clang-format on */
  3033. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3034. // break; // fall through to OLD behaviour
  3035. }
  3036. case cmPolicies::OLD:
  3037. result = moduleInCMakeModulePath;
  3038. break;
  3039. case cmPolicies::REQUIRED_IF_USED:
  3040. case cmPolicies::REQUIRED_ALWAYS:
  3041. case cmPolicies::NEW:
  3042. result = moduleInCMakeRoot;
  3043. break;
  3044. }
  3045. }
  3046. }
  3047. return result;
  3048. }
  3049. void cmMakefile::ConfigureString(const std::string& input, std::string& output,
  3050. bool atOnly, bool escapeQuotes) const
  3051. {
  3052. // Split input to handle one line at a time.
  3053. std::string::const_iterator lineStart = input.begin();
  3054. while (lineStart != input.end()) {
  3055. // Find the end of this line.
  3056. std::string::const_iterator lineEnd = lineStart;
  3057. while (lineEnd != input.end() && *lineEnd != '\n') {
  3058. ++lineEnd;
  3059. }
  3060. // Copy the line.
  3061. std::string line(lineStart, lineEnd);
  3062. // Skip the newline character.
  3063. bool haveNewline = (lineEnd != input.end());
  3064. if (haveNewline) {
  3065. ++lineEnd;
  3066. }
  3067. // Replace #cmakedefine instances.
  3068. if (this->cmDefineRegex.find(line)) {
  3069. const char* def = this->GetDefinition(this->cmDefineRegex.match(1));
  3070. if (!cmSystemTools::IsOff(def)) {
  3071. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  3072. output += line;
  3073. } else {
  3074. output += "/* #undef ";
  3075. output += this->cmDefineRegex.match(1);
  3076. output += " */";
  3077. }
  3078. } else if (this->cmDefine01Regex.find(line)) {
  3079. const char* def = this->GetDefinition(this->cmDefine01Regex.match(1));
  3080. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  3081. output += line;
  3082. if (!cmSystemTools::IsOff(def)) {
  3083. output += " 1";
  3084. } else {
  3085. output += " 0";
  3086. }
  3087. } else {
  3088. output += line;
  3089. }
  3090. if (haveNewline) {
  3091. output += "\n";
  3092. }
  3093. // Move to the next line.
  3094. lineStart = lineEnd;
  3095. }
  3096. // Perform variable replacements.
  3097. this->ExpandVariablesInString(output, escapeQuotes, true, atOnly, CM_NULLPTR,
  3098. -1, true, true);
  3099. }
  3100. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  3101. bool copyonly, bool atOnly, bool escapeQuotes,
  3102. const cmNewLineStyle& newLine)
  3103. {
  3104. int res = 1;
  3105. if (!this->CanIWriteThisFile(outfile)) {
  3106. cmSystemTools::Error("Attempt to write file: ", outfile,
  3107. " into a source directory.");
  3108. return 0;
  3109. }
  3110. if (!cmSystemTools::FileExists(infile)) {
  3111. cmSystemTools::Error("File ", infile, " does not exist.");
  3112. return 0;
  3113. }
  3114. std::string soutfile = outfile;
  3115. std::string sinfile = infile;
  3116. this->AddCMakeDependFile(sinfile);
  3117. cmSystemTools::ConvertToUnixSlashes(soutfile);
  3118. // Re-generate if non-temporary outputs are missing.
  3119. // when we finalize the configuration we will remove all
  3120. // output files that now don't exist.
  3121. this->AddCMakeOutputFile(soutfile);
  3122. mode_t perm = 0;
  3123. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  3124. std::string::size_type pos = soutfile.rfind('/');
  3125. if (pos != std::string::npos) {
  3126. std::string path = soutfile.substr(0, pos);
  3127. cmSystemTools::MakeDirectory(path.c_str());
  3128. }
  3129. if (copyonly) {
  3130. if (!cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  3131. soutfile.c_str())) {
  3132. return 0;
  3133. }
  3134. } else {
  3135. std::string newLineCharacters;
  3136. std::ios::openmode omode = std::ios::out | std::ios::trunc;
  3137. if (newLine.IsValid()) {
  3138. newLineCharacters = newLine.GetCharacters();
  3139. omode |= std::ios::binary;
  3140. } else {
  3141. newLineCharacters = "\n";
  3142. }
  3143. std::string tempOutputFile = soutfile;
  3144. tempOutputFile += ".tmp";
  3145. cmsys::ofstream fout(tempOutputFile.c_str(), omode);
  3146. if (!fout) {
  3147. cmSystemTools::Error("Could not open file for write in copy operation ",
  3148. tempOutputFile.c_str());
  3149. cmSystemTools::ReportLastSystemError("");
  3150. return 0;
  3151. }
  3152. cmsys::ifstream fin(sinfile.c_str());
  3153. if (!fin) {
  3154. cmSystemTools::Error("Could not open file for read in copy operation ",
  3155. sinfile.c_str());
  3156. return 0;
  3157. }
  3158. cmsys::FStream::BOM bom = cmsys::FStream::ReadBOM(fin);
  3159. if (bom != cmsys::FStream::BOM_None && bom != cmsys::FStream::BOM_UTF8) {
  3160. std::ostringstream e;
  3161. e << "File starts with a Byte-Order-Mark that is not UTF-8:\n "
  3162. << sinfile;
  3163. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3164. return 0;
  3165. }
  3166. // rewind to copy BOM to output file
  3167. fin.seekg(0);
  3168. // now copy input to output and expand variables in the
  3169. // input file at the same time
  3170. std::string inLine;
  3171. std::string outLine;
  3172. while (cmSystemTools::GetLineFromStream(fin, inLine)) {
  3173. outLine = "";
  3174. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  3175. fout << outLine << newLineCharacters;
  3176. }
  3177. // close the files before attempting to copy
  3178. fin.close();
  3179. fout.close();
  3180. if (!cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  3181. soutfile.c_str())) {
  3182. res = 0;
  3183. } else {
  3184. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  3185. }
  3186. cmSystemTools::RemoveFile(tempOutputFile);
  3187. }
  3188. return res;
  3189. }
  3190. void cmMakefile::SetProperty(const std::string& prop, const char* value)
  3191. {
  3192. cmListFileBacktrace lfbt = this->GetBacktrace();
  3193. this->StateSnapshot.GetDirectory().SetProperty(prop, value, lfbt);
  3194. }
  3195. void cmMakefile::AppendProperty(const std::string& prop, const char* value,
  3196. bool asString)
  3197. {
  3198. cmListFileBacktrace lfbt = this->GetBacktrace();
  3199. this->StateSnapshot.GetDirectory().AppendProperty(prop, value, asString,
  3200. lfbt);
  3201. }
  3202. const char* cmMakefile::GetProperty(const std::string& prop) const
  3203. {
  3204. return this->StateSnapshot.GetDirectory().GetProperty(prop);
  3205. }
  3206. const char* cmMakefile::GetProperty(const std::string& prop, bool chain) const
  3207. {
  3208. return this->StateSnapshot.GetDirectory().GetProperty(prop, chain);
  3209. }
  3210. bool cmMakefile::GetPropertyAsBool(const std::string& prop) const
  3211. {
  3212. return cmSystemTools::IsOn(this->GetProperty(prop));
  3213. }
  3214. std::vector<std::string> cmMakefile::GetPropertyKeys() const
  3215. {
  3216. return this->StateSnapshot.GetDirectory().GetPropertyKeys();
  3217. }
  3218. cmTarget* cmMakefile::FindLocalNonAliasTarget(const std::string& name) const
  3219. {
  3220. cmTargets::iterator i = this->Targets.find(name);
  3221. if (i != this->Targets.end()) {
  3222. return &i->second;
  3223. }
  3224. return CM_NULLPTR;
  3225. }
  3226. cmTest* cmMakefile::CreateTest(const std::string& testName)
  3227. {
  3228. cmTest* test = this->GetTest(testName);
  3229. if (test) {
  3230. return test;
  3231. }
  3232. test = new cmTest(this);
  3233. test->SetName(testName);
  3234. this->Tests[testName] = test;
  3235. return test;
  3236. }
  3237. cmTest* cmMakefile::GetTest(const std::string& testName) const
  3238. {
  3239. std::map<std::string, cmTest*>::const_iterator mi =
  3240. this->Tests.find(testName);
  3241. if (mi != this->Tests.end()) {
  3242. return mi->second;
  3243. }
  3244. return CM_NULLPTR;
  3245. }
  3246. void cmMakefile::AddCMakeDependFilesFromUser()
  3247. {
  3248. std::vector<std::string> deps;
  3249. if (const char* deps_str = this->GetProperty("CMAKE_CONFIGURE_DEPENDS")) {
  3250. cmSystemTools::ExpandListArgument(deps_str, deps);
  3251. }
  3252. for (std::vector<std::string>::iterator i = deps.begin(); i != deps.end();
  3253. ++i) {
  3254. if (cmSystemTools::FileIsFullPath(i->c_str())) {
  3255. this->AddCMakeDependFile(*i);
  3256. } else {
  3257. std::string f = this->GetCurrentSourceDirectory();
  3258. f += "/";
  3259. f += *i;
  3260. this->AddCMakeDependFile(f);
  3261. }
  3262. }
  3263. }
  3264. std::string cmMakefile::FormatListFileStack() const
  3265. {
  3266. std::vector<std::string> listFiles;
  3267. cmState::Snapshot snp = this->StateSnapshot;
  3268. while (snp.IsValid()) {
  3269. listFiles.push_back(snp.GetExecutionListFile());
  3270. snp = snp.GetCallStackParent();
  3271. }
  3272. std::reverse(listFiles.begin(), listFiles.end());
  3273. std::ostringstream tmp;
  3274. size_t depth = listFiles.size();
  3275. if (depth > 0) {
  3276. std::vector<std::string>::const_iterator it = listFiles.end();
  3277. do {
  3278. if (depth != listFiles.size()) {
  3279. tmp << "\n ";
  3280. }
  3281. --it;
  3282. tmp << "[";
  3283. tmp << depth;
  3284. tmp << "]\t";
  3285. tmp << *it;
  3286. depth--;
  3287. } while (it != listFiles.begin());
  3288. }
  3289. return tmp.str();
  3290. }
  3291. void cmMakefile::PushScope()
  3292. {
  3293. this->StateSnapshot =
  3294. this->GetState()->CreateVariableScopeSnapshot(this->StateSnapshot);
  3295. this->PushLoopBlockBarrier();
  3296. #if defined(CMAKE_BUILD_WITH_CMAKE)
  3297. this->GetGlobalGenerator()->GetFileLockPool().PushFunctionScope();
  3298. #endif
  3299. }
  3300. void cmMakefile::PopScope()
  3301. {
  3302. #if defined(CMAKE_BUILD_WITH_CMAKE)
  3303. this->GetGlobalGenerator()->GetFileLockPool().PopFunctionScope();
  3304. #endif
  3305. this->PopLoopBlockBarrier();
  3306. this->CheckForUnusedVariables();
  3307. this->PopSnapshot();
  3308. }
  3309. void cmMakefile::RaiseScope(const std::string& var, const char* varDef)
  3310. {
  3311. if (var.empty()) {
  3312. return;
  3313. }
  3314. if (!this->StateSnapshot.RaiseScope(var, varDef)) {
  3315. std::ostringstream m;
  3316. m << "Cannot set \"" << var << "\": current scope has no parent.";
  3317. this->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  3318. }
  3319. }
  3320. cmTarget* cmMakefile::AddImportedTarget(const std::string& name,
  3321. cmState::TargetType type, bool global)
  3322. {
  3323. // Create the target.
  3324. CM_AUTO_PTR<cmTarget> target(new cmTarget);
  3325. target->SetType(type, name);
  3326. target->MarkAsImported(global);
  3327. target->SetMakefile(this);
  3328. // Add to the set of available imported targets.
  3329. this->ImportedTargets[name] = target.get();
  3330. this->GetGlobalGenerator()->IndexTarget(target.get());
  3331. // Transfer ownership to this cmMakefile object.
  3332. this->ImportedTargetsOwned.push_back(target.get());
  3333. return target.release();
  3334. }
  3335. cmTarget* cmMakefile::FindTargetToUse(const std::string& name,
  3336. bool excludeAliases) const
  3337. {
  3338. // Look for an imported target. These take priority because they
  3339. // are more local in scope and do not have to be globally unique.
  3340. TargetMap::const_iterator imported = this->ImportedTargets.find(name);
  3341. if (imported != this->ImportedTargets.end()) {
  3342. return imported->second;
  3343. }
  3344. // Look for a target built in this directory.
  3345. if (cmTarget* t = this->FindLocalNonAliasTarget(name)) {
  3346. return t;
  3347. }
  3348. // Look for a target built in this project.
  3349. return this->GetGlobalGenerator()->FindTarget(name, excludeAliases);
  3350. }
  3351. bool cmMakefile::IsAlias(const std::string& name) const
  3352. {
  3353. if (this->AliasTargets.find(name) != this->AliasTargets.end()) {
  3354. return true;
  3355. }
  3356. return this->GetGlobalGenerator()->IsAlias(name);
  3357. }
  3358. bool cmMakefile::EnforceUniqueName(std::string const& name, std::string& msg,
  3359. bool isCustom) const
  3360. {
  3361. if (this->IsAlias(name)) {
  3362. std::ostringstream e;
  3363. e << "cannot create target \"" << name
  3364. << "\" because an alias with the same name already exists.";
  3365. msg = e.str();
  3366. return false;
  3367. }
  3368. if (cmTarget* existing = this->FindTargetToUse(name)) {
  3369. // The name given conflicts with an existing target. Produce an
  3370. // error in a compatible way.
  3371. if (existing->IsImported()) {
  3372. // Imported targets were not supported in previous versions.
  3373. // This is new code, so we can make it an error.
  3374. std::ostringstream e;
  3375. e << "cannot create target \"" << name
  3376. << "\" because an imported target with the same name already exists.";
  3377. msg = e.str();
  3378. return false;
  3379. }
  3380. // target names must be globally unique
  3381. switch (this->GetPolicyStatus(cmPolicies::CMP0002)) {
  3382. case cmPolicies::WARN:
  3383. this->IssueMessage(cmake::AUTHOR_WARNING,
  3384. cmPolicies::GetPolicyWarning(cmPolicies::CMP0002));
  3385. case cmPolicies::OLD:
  3386. return true;
  3387. case cmPolicies::REQUIRED_IF_USED:
  3388. case cmPolicies::REQUIRED_ALWAYS:
  3389. this->IssueMessage(
  3390. cmake::FATAL_ERROR,
  3391. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0002));
  3392. return true;
  3393. case cmPolicies::NEW:
  3394. break;
  3395. }
  3396. // The conflict is with a non-imported target.
  3397. // Allow this if the user has requested support.
  3398. cmake* cm = this->GetCMakeInstance();
  3399. if (isCustom && existing->GetType() == cmState::UTILITY &&
  3400. this != existing->GetMakefile() &&
  3401. cm->GetState()->GetGlobalPropertyAsBool(
  3402. "ALLOW_DUPLICATE_CUSTOM_TARGETS")) {
  3403. return true;
  3404. }
  3405. // Produce an error that tells the user how to work around the
  3406. // problem.
  3407. std::ostringstream e;
  3408. e << "cannot create target \"" << name
  3409. << "\" because another target with the same name already exists. "
  3410. << "The existing target is ";
  3411. switch (existing->GetType()) {
  3412. case cmState::EXECUTABLE:
  3413. e << "an executable ";
  3414. break;
  3415. case cmState::STATIC_LIBRARY:
  3416. e << "a static library ";
  3417. break;
  3418. case cmState::SHARED_LIBRARY:
  3419. e << "a shared library ";
  3420. break;
  3421. case cmState::MODULE_LIBRARY:
  3422. e << "a module library ";
  3423. break;
  3424. case cmState::UTILITY:
  3425. e << "a custom target ";
  3426. break;
  3427. case cmState::INTERFACE_LIBRARY:
  3428. e << "an interface library ";
  3429. break;
  3430. default:
  3431. break;
  3432. }
  3433. e << "created in source directory \""
  3434. << existing->GetMakefile()->GetCurrentSourceDirectory() << "\". "
  3435. << "See documentation for policy CMP0002 for more details.";
  3436. msg = e.str();
  3437. return false;
  3438. }
  3439. return true;
  3440. }
  3441. bool cmMakefile::EnforceUniqueDir(const std::string& srcPath,
  3442. const std::string& binPath) const
  3443. {
  3444. // Make sure the binary directory is unique.
  3445. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  3446. if (gg->BinaryDirectoryIsNew(binPath)) {
  3447. return true;
  3448. }
  3449. std::ostringstream e;
  3450. switch (this->GetPolicyStatus(cmPolicies::CMP0013)) {
  3451. case cmPolicies::WARN:
  3452. // Print the warning.
  3453. /* clang-format off */
  3454. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0013)
  3455. << "\n"
  3456. << "The binary directory\n"
  3457. << " " << binPath << "\n"
  3458. << "is already used to build a source directory. "
  3459. << "This command uses it to build source directory\n"
  3460. << " " << srcPath << "\n"
  3461. << "which can generate conflicting build files. "
  3462. << "CMake does not support this use case but it used "
  3463. << "to work accidentally and is being allowed for "
  3464. << "compatibility.";
  3465. /* clang-format on */
  3466. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  3467. case cmPolicies::OLD:
  3468. // OLD behavior does not warn.
  3469. return true;
  3470. case cmPolicies::REQUIRED_IF_USED:
  3471. case cmPolicies::REQUIRED_ALWAYS:
  3472. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0013) << "\n";
  3473. case cmPolicies::NEW:
  3474. // NEW behavior prints the error.
  3475. /* clang-format off */
  3476. e << "The binary directory\n"
  3477. << " " << binPath << "\n"
  3478. << "is already used to build a source directory. "
  3479. << "It cannot be used to build source directory\n"
  3480. << " " << srcPath << "\n"
  3481. << "Specify a unique binary directory name.";
  3482. /* clang-format on */
  3483. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3484. break;
  3485. }
  3486. return false;
  3487. }
  3488. void cmMakefile::AddQtUiFileWithOptions(cmSourceFile* sf)
  3489. {
  3490. this->QtUiFilesWithOptions.push_back(sf);
  3491. }
  3492. std::vector<cmSourceFile*> cmMakefile::GetQtUiFilesWithOptions() const
  3493. {
  3494. return this->QtUiFilesWithOptions;
  3495. }
  3496. static std::string const matchVariables[] = {
  3497. "CMAKE_MATCH_0", "CMAKE_MATCH_1", "CMAKE_MATCH_2", "CMAKE_MATCH_3",
  3498. "CMAKE_MATCH_4", "CMAKE_MATCH_5", "CMAKE_MATCH_6", "CMAKE_MATCH_7",
  3499. "CMAKE_MATCH_8", "CMAKE_MATCH_9"
  3500. };
  3501. static std::string const nMatchesVariable = "CMAKE_MATCH_COUNT";
  3502. void cmMakefile::ClearMatches()
  3503. {
  3504. const char* nMatchesStr = this->GetDefinition(nMatchesVariable);
  3505. if (!nMatchesStr) {
  3506. return;
  3507. }
  3508. int nMatches = atoi(nMatchesStr);
  3509. for (int i = 0; i <= nMatches; i++) {
  3510. std::string const& var = matchVariables[i];
  3511. std::string const& s = this->GetSafeDefinition(var);
  3512. if (!s.empty()) {
  3513. this->AddDefinition(var, "");
  3514. this->MarkVariableAsUsed(var);
  3515. }
  3516. }
  3517. this->AddDefinition(nMatchesVariable, "0");
  3518. this->MarkVariableAsUsed(nMatchesVariable);
  3519. }
  3520. void cmMakefile::StoreMatches(cmsys::RegularExpression& re)
  3521. {
  3522. char highest = 0;
  3523. for (int i = 0; i < 10; i++) {
  3524. std::string const& m = re.match(i);
  3525. if (!m.empty()) {
  3526. std::string const& var = matchVariables[i];
  3527. this->AddDefinition(var, m.c_str());
  3528. this->MarkVariableAsUsed(var);
  3529. highest = static_cast<char>('0' + i);
  3530. }
  3531. }
  3532. char nMatches[] = { highest, '\0' };
  3533. this->AddDefinition(nMatchesVariable, nMatches);
  3534. this->MarkVariableAsUsed(nMatchesVariable);
  3535. }
  3536. cmState::Snapshot cmMakefile::GetStateSnapshot() const
  3537. {
  3538. return this->StateSnapshot;
  3539. }
  3540. const char* cmMakefile::GetDefineFlagsCMP0059() const
  3541. {
  3542. return this->DefineFlagsOrig.c_str();
  3543. }
  3544. cmPolicies::PolicyStatus cmMakefile::GetPolicyStatus(
  3545. cmPolicies::PolicyID id) const
  3546. {
  3547. return this->StateSnapshot.GetPolicy(id);
  3548. }
  3549. bool cmMakefile::PolicyOptionalWarningEnabled(std::string const& var)
  3550. {
  3551. // Check for an explicit CMAKE_POLICY_WARNING_CMP<NNNN> setting.
  3552. if (!var.empty()) {
  3553. if (const char* val = this->GetDefinition(var)) {
  3554. return cmSystemTools::IsOn(val);
  3555. }
  3556. }
  3557. // Enable optional policy warnings with --debug-output, --trace,
  3558. // or --trace-expand.
  3559. cmake* cm = this->GetCMakeInstance();
  3560. return cm->GetDebugOutput() || cm->GetTrace();
  3561. }
  3562. bool cmMakefile::SetPolicy(const char* id, cmPolicies::PolicyStatus status)
  3563. {
  3564. cmPolicies::PolicyID pid;
  3565. if (!cmPolicies::GetPolicyID(id, /* out */ pid)) {
  3566. std::ostringstream e;
  3567. e << "Policy \"" << id << "\" is not known to this version of CMake.";
  3568. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3569. return false;
  3570. }
  3571. return this->SetPolicy(pid, status);
  3572. }
  3573. bool cmMakefile::SetPolicy(cmPolicies::PolicyID id,
  3574. cmPolicies::PolicyStatus status)
  3575. {
  3576. // A REQUIRED_ALWAYS policy may be set only to NEW.
  3577. if (status != cmPolicies::NEW &&
  3578. cmPolicies::GetPolicyStatus(id) == cmPolicies::REQUIRED_ALWAYS) {
  3579. std::string msg = cmPolicies::GetRequiredAlwaysPolicyError(id);
  3580. this->IssueMessage(cmake::FATAL_ERROR, msg);
  3581. return false;
  3582. }
  3583. this->StateSnapshot.SetPolicy(id, status);
  3584. return true;
  3585. }
  3586. cmMakefile::PolicyPushPop::PolicyPushPop(cmMakefile* m)
  3587. : Makefile(m)
  3588. {
  3589. this->Makefile->PushPolicy();
  3590. }
  3591. cmMakefile::PolicyPushPop::~PolicyPushPop()
  3592. {
  3593. this->Makefile->PopPolicy();
  3594. }
  3595. void cmMakefile::PushPolicy(bool weak, cmPolicies::PolicyMap const& pm)
  3596. {
  3597. this->StateSnapshot.PushPolicy(pm, weak);
  3598. }
  3599. void cmMakefile::PopPolicy()
  3600. {
  3601. if (!this->StateSnapshot.PopPolicy()) {
  3602. this->IssueMessage(cmake::FATAL_ERROR,
  3603. "cmake_policy POP without matching PUSH");
  3604. }
  3605. }
  3606. void cmMakefile::PopSnapshot(bool reportError)
  3607. {
  3608. // cmState::Snapshot manages nested policy scopes within it.
  3609. // Since the scope corresponding to the snapshot is closing,
  3610. // reject any still-open nested policy scopes with an error.
  3611. while (!this->StateSnapshot.CanPopPolicyScope()) {
  3612. if (reportError) {
  3613. this->IssueMessage(cmake::FATAL_ERROR,
  3614. "cmake_policy PUSH without matching POP");
  3615. reportError = false;
  3616. }
  3617. this->PopPolicy();
  3618. }
  3619. this->StateSnapshot = this->GetState()->Pop(this->StateSnapshot);
  3620. assert(this->StateSnapshot.IsValid());
  3621. }
  3622. bool cmMakefile::SetPolicyVersion(const char* version)
  3623. {
  3624. return cmPolicies::ApplyPolicyVersion(this, version);
  3625. }
  3626. bool cmMakefile::HasCMP0054AlreadyBeenReported(
  3627. cmListFileContext const& context) const
  3628. {
  3629. return !this->CMP0054ReportedIds.insert(context).second;
  3630. }
  3631. void cmMakefile::RecordPolicies(cmPolicies::PolicyMap& pm)
  3632. {
  3633. /* Record the setting of every policy. */
  3634. typedef cmPolicies::PolicyID PolicyID;
  3635. for (PolicyID pid = cmPolicies::CMP0000; pid != cmPolicies::CMPCOUNT;
  3636. pid = PolicyID(pid + 1)) {
  3637. pm.Set(pid, this->GetPolicyStatus(pid));
  3638. }
  3639. }
  3640. bool cmMakefile::IgnoreErrorsCMP0061() const
  3641. {
  3642. bool ignoreErrors = true;
  3643. switch (this->GetPolicyStatus(cmPolicies::CMP0061)) {
  3644. case cmPolicies::WARN:
  3645. // No warning for this policy!
  3646. case cmPolicies::OLD:
  3647. break;
  3648. case cmPolicies::REQUIRED_IF_USED:
  3649. case cmPolicies::REQUIRED_ALWAYS:
  3650. case cmPolicies::NEW:
  3651. ignoreErrors = false;
  3652. break;
  3653. }
  3654. return ignoreErrors;
  3655. }
  3656. #define FEATURE_STRING(F) , #F
  3657. static const char* const C_FEATURES[] = { CM_NULLPTR FOR_EACH_C_FEATURE(
  3658. FEATURE_STRING) };
  3659. static const char* const CXX_FEATURES[] = { CM_NULLPTR FOR_EACH_CXX_FEATURE(
  3660. FEATURE_STRING) };
  3661. #undef FEATURE_STRING
  3662. static const char* const C_STANDARDS[] = { "90", "99", "11" };
  3663. static const char* const CXX_STANDARDS[] = { "98", "11", "14" };
  3664. bool cmMakefile::AddRequiredTargetFeature(cmTarget* target,
  3665. const std::string& feature,
  3666. std::string* error) const
  3667. {
  3668. if (cmGeneratorExpression::Find(feature) != std::string::npos) {
  3669. target->AppendProperty("COMPILE_FEATURES", feature.c_str());
  3670. return true;
  3671. }
  3672. std::string lang;
  3673. if (!this->CompileFeatureKnown(target, feature, lang, error)) {
  3674. return false;
  3675. }
  3676. const char* features = this->CompileFeaturesAvailable(lang, error);
  3677. if (!features) {
  3678. return false;
  3679. }
  3680. std::vector<std::string> availableFeatures;
  3681. cmSystemTools::ExpandListArgument(features, availableFeatures);
  3682. if (std::find(availableFeatures.begin(), availableFeatures.end(), feature) ==
  3683. availableFeatures.end()) {
  3684. std::ostringstream e;
  3685. e << "The compiler feature \"" << feature << "\" is not known to " << lang
  3686. << " compiler\n\""
  3687. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_ID")
  3688. << "\"\nversion "
  3689. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_VERSION") << ".";
  3690. if (error) {
  3691. *error = e.str();
  3692. } else {
  3693. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  3694. this->Backtrace);
  3695. }
  3696. return false;
  3697. }
  3698. target->AppendProperty("COMPILE_FEATURES", feature.c_str());
  3699. return lang == "C"
  3700. ? this->AddRequiredTargetCFeature(target, feature, error)
  3701. : this->AddRequiredTargetCxxFeature(target, feature, error);
  3702. }
  3703. bool cmMakefile::CompileFeatureKnown(cmTarget const* target,
  3704. const std::string& feature,
  3705. std::string& lang,
  3706. std::string* error) const
  3707. {
  3708. assert(cmGeneratorExpression::Find(feature) == std::string::npos);
  3709. bool isCFeature =
  3710. std::find_if(cmArrayBegin(C_FEATURES) + 1, cmArrayEnd(C_FEATURES),
  3711. cmStrCmp(feature)) != cmArrayEnd(C_FEATURES);
  3712. if (isCFeature) {
  3713. lang = "C";
  3714. return true;
  3715. }
  3716. bool isCxxFeature =
  3717. std::find_if(cmArrayBegin(CXX_FEATURES) + 1, cmArrayEnd(CXX_FEATURES),
  3718. cmStrCmp(feature)) != cmArrayEnd(CXX_FEATURES);
  3719. if (isCxxFeature) {
  3720. lang = "CXX";
  3721. return true;
  3722. }
  3723. std::ostringstream e;
  3724. if (error) {
  3725. e << "specified";
  3726. } else {
  3727. e << "Specified";
  3728. }
  3729. e << " unknown feature \"" << feature << "\" for "
  3730. "target \""
  3731. << target->GetName() << "\".";
  3732. if (error) {
  3733. *error = e.str();
  3734. } else {
  3735. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  3736. this->Backtrace);
  3737. }
  3738. return false;
  3739. }
  3740. const char* cmMakefile::CompileFeaturesAvailable(const std::string& lang,
  3741. std::string* error) const
  3742. {
  3743. const char* featuresKnown =
  3744. this->GetDefinition("CMAKE_" + lang + "_COMPILE_FEATURES");
  3745. if (!featuresKnown || !*featuresKnown) {
  3746. std::ostringstream e;
  3747. if (error) {
  3748. e << "no";
  3749. } else {
  3750. e << "No";
  3751. }
  3752. e << " known features for " << lang << " compiler\n\""
  3753. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_ID")
  3754. << "\"\nversion "
  3755. << this->GetDefinition("CMAKE_" + lang + "_COMPILER_VERSION") << ".";
  3756. if (error) {
  3757. *error = e.str();
  3758. } else {
  3759. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  3760. this->Backtrace);
  3761. }
  3762. return CM_NULLPTR;
  3763. }
  3764. return featuresKnown;
  3765. }
  3766. bool cmMakefile::HaveStandardAvailable(cmTarget const* target,
  3767. std::string const& lang,
  3768. const std::string& feature) const
  3769. {
  3770. return lang == "C" ? this->HaveCStandardAvailable(target, feature)
  3771. : this->HaveCxxStandardAvailable(target, feature);
  3772. }
  3773. bool cmMakefile::HaveCStandardAvailable(cmTarget const* target,
  3774. const std::string& feature) const
  3775. {
  3776. const char* defaultCStandard =
  3777. this->GetDefinition("CMAKE_C_STANDARD_DEFAULT");
  3778. if (!defaultCStandard) {
  3779. std::ostringstream e;
  3780. e << "CMAKE_C_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
  3781. "not fully configured for this compiler.";
  3782. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  3783. // Return true so the caller does not try to lookup the default standard.
  3784. return true;
  3785. }
  3786. if (std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  3787. cmStrCmp(defaultCStandard)) == cmArrayEnd(C_STANDARDS)) {
  3788. std::ostringstream e;
  3789. e << "The CMAKE_C_STANDARD_DEFAULT variable contains an "
  3790. "invalid value: \""
  3791. << defaultCStandard << "\".";
  3792. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  3793. return false;
  3794. }
  3795. bool needC90 = false;
  3796. bool needC99 = false;
  3797. bool needC11 = false;
  3798. this->CheckNeededCLanguage(feature, needC90, needC99, needC11);
  3799. const char* existingCStandard = target->GetProperty("C_STANDARD");
  3800. if (!existingCStandard) {
  3801. existingCStandard = defaultCStandard;
  3802. }
  3803. if (std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  3804. cmStrCmp(existingCStandard)) == cmArrayEnd(C_STANDARDS)) {
  3805. std::ostringstream e;
  3806. e << "The C_STANDARD property on target \"" << target->GetName()
  3807. << "\" contained an invalid value: \"" << existingCStandard << "\".";
  3808. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3809. return false;
  3810. }
  3811. const char* const* existingCIt = existingCStandard
  3812. ? std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  3813. cmStrCmp(existingCStandard))
  3814. : cmArrayEnd(C_STANDARDS);
  3815. if (needC11 && existingCStandard &&
  3816. existingCIt < std::find_if(cmArrayBegin(C_STANDARDS),
  3817. cmArrayEnd(C_STANDARDS), cmStrCmp("11"))) {
  3818. return false;
  3819. }
  3820. if (needC99 && existingCStandard &&
  3821. existingCIt < std::find_if(cmArrayBegin(C_STANDARDS),
  3822. cmArrayEnd(C_STANDARDS), cmStrCmp("99"))) {
  3823. return false;
  3824. }
  3825. if (needC90 && existingCStandard &&
  3826. existingCIt < std::find_if(cmArrayBegin(C_STANDARDS),
  3827. cmArrayEnd(C_STANDARDS), cmStrCmp("90"))) {
  3828. return false;
  3829. }
  3830. return true;
  3831. }
  3832. bool cmMakefile::IsLaterStandard(std::string const& lang,
  3833. std::string const& lhs,
  3834. std::string const& rhs)
  3835. {
  3836. if (lang == "C") {
  3837. const char* const* rhsIt = std::find_if(
  3838. cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS), cmStrCmp(rhs));
  3839. return std::find_if(rhsIt, cmArrayEnd(C_STANDARDS), cmStrCmp(lhs)) !=
  3840. cmArrayEnd(C_STANDARDS);
  3841. }
  3842. const char* const* rhsIt = std::find_if(
  3843. cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS), cmStrCmp(rhs));
  3844. return std::find_if(rhsIt, cmArrayEnd(CXX_STANDARDS), cmStrCmp(lhs)) !=
  3845. cmArrayEnd(CXX_STANDARDS);
  3846. }
  3847. bool cmMakefile::HaveCxxStandardAvailable(cmTarget const* target,
  3848. const std::string& feature) const
  3849. {
  3850. const char* defaultCxxStandard =
  3851. this->GetDefinition("CMAKE_CXX_STANDARD_DEFAULT");
  3852. if (!defaultCxxStandard) {
  3853. std::ostringstream e;
  3854. e << "CMAKE_CXX_STANDARD_DEFAULT is not set. COMPILE_FEATURES support "
  3855. "not fully configured for this compiler.";
  3856. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  3857. // Return true so the caller does not try to lookup the default standard.
  3858. return true;
  3859. }
  3860. if (std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  3861. cmStrCmp(defaultCxxStandard)) ==
  3862. cmArrayEnd(CXX_STANDARDS)) {
  3863. std::ostringstream e;
  3864. e << "The CMAKE_CXX_STANDARD_DEFAULT variable contains an "
  3865. "invalid value: \""
  3866. << defaultCxxStandard << "\".";
  3867. this->IssueMessage(cmake::INTERNAL_ERROR, e.str());
  3868. return false;
  3869. }
  3870. bool needCxx98 = false;
  3871. bool needCxx11 = false;
  3872. bool needCxx14 = false;
  3873. this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11, needCxx14);
  3874. const char* existingCxxStandard = target->GetProperty("CXX_STANDARD");
  3875. if (!existingCxxStandard) {
  3876. existingCxxStandard = defaultCxxStandard;
  3877. }
  3878. if (std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  3879. cmStrCmp(existingCxxStandard)) ==
  3880. cmArrayEnd(CXX_STANDARDS)) {
  3881. std::ostringstream e;
  3882. e << "The CXX_STANDARD property on target \"" << target->GetName()
  3883. << "\" contained an invalid value: \"" << existingCxxStandard << "\".";
  3884. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  3885. return false;
  3886. }
  3887. const char* const* existingCxxIt = existingCxxStandard
  3888. ? std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  3889. cmStrCmp(existingCxxStandard))
  3890. : cmArrayEnd(CXX_STANDARDS);
  3891. if (needCxx11 &&
  3892. existingCxxIt < std::find_if(cmArrayBegin(CXX_STANDARDS),
  3893. cmArrayEnd(CXX_STANDARDS),
  3894. cmStrCmp("11"))) {
  3895. return false;
  3896. }
  3897. if (needCxx98 &&
  3898. existingCxxIt < std::find_if(cmArrayBegin(CXX_STANDARDS),
  3899. cmArrayEnd(CXX_STANDARDS),
  3900. cmStrCmp("98"))) {
  3901. return false;
  3902. }
  3903. return true;
  3904. }
  3905. void cmMakefile::CheckNeededCxxLanguage(const std::string& feature,
  3906. bool& needCxx98, bool& needCxx11,
  3907. bool& needCxx14) const
  3908. {
  3909. if (const char* propCxx98 =
  3910. this->GetDefinition("CMAKE_CXX98_COMPILE_FEATURES")) {
  3911. std::vector<std::string> props;
  3912. cmSystemTools::ExpandListArgument(propCxx98, props);
  3913. needCxx98 = std::find(props.begin(), props.end(), feature) != props.end();
  3914. }
  3915. if (const char* propCxx11 =
  3916. this->GetDefinition("CMAKE_CXX11_COMPILE_FEATURES")) {
  3917. std::vector<std::string> props;
  3918. cmSystemTools::ExpandListArgument(propCxx11, props);
  3919. needCxx11 = std::find(props.begin(), props.end(), feature) != props.end();
  3920. }
  3921. if (const char* propCxx14 =
  3922. this->GetDefinition("CMAKE_CXX14_COMPILE_FEATURES")) {
  3923. std::vector<std::string> props;
  3924. cmSystemTools::ExpandListArgument(propCxx14, props);
  3925. needCxx14 = std::find(props.begin(), props.end(), feature) != props.end();
  3926. }
  3927. }
  3928. bool cmMakefile::AddRequiredTargetCxxFeature(cmTarget* target,
  3929. const std::string& feature,
  3930. std::string* error) const
  3931. {
  3932. bool needCxx98 = false;
  3933. bool needCxx11 = false;
  3934. bool needCxx14 = false;
  3935. this->CheckNeededCxxLanguage(feature, needCxx98, needCxx11, needCxx14);
  3936. const char* existingCxxStandard = target->GetProperty("CXX_STANDARD");
  3937. if (existingCxxStandard) {
  3938. if (std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  3939. cmStrCmp(existingCxxStandard)) ==
  3940. cmArrayEnd(CXX_STANDARDS)) {
  3941. std::ostringstream e;
  3942. e << "The CXX_STANDARD property on target \"" << target->GetName()
  3943. << "\" contained an invalid value: \"" << existingCxxStandard << "\".";
  3944. if (error) {
  3945. *error = e.str();
  3946. } else {
  3947. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  3948. this->Backtrace);
  3949. }
  3950. return false;
  3951. }
  3952. }
  3953. const char* const* existingCxxIt = existingCxxStandard
  3954. ? std::find_if(cmArrayBegin(CXX_STANDARDS), cmArrayEnd(CXX_STANDARDS),
  3955. cmStrCmp(existingCxxStandard))
  3956. : cmArrayEnd(CXX_STANDARDS);
  3957. bool setCxx98 = needCxx98 && !existingCxxStandard;
  3958. bool setCxx11 = needCxx11 && !existingCxxStandard;
  3959. bool setCxx14 = needCxx14 && !existingCxxStandard;
  3960. if (needCxx14 && existingCxxStandard &&
  3961. existingCxxIt < std::find_if(cmArrayBegin(CXX_STANDARDS),
  3962. cmArrayEnd(CXX_STANDARDS),
  3963. cmStrCmp("14"))) {
  3964. setCxx14 = true;
  3965. } else if (needCxx11 && existingCxxStandard &&
  3966. existingCxxIt < std::find_if(cmArrayBegin(CXX_STANDARDS),
  3967. cmArrayEnd(CXX_STANDARDS),
  3968. cmStrCmp("11"))) {
  3969. setCxx11 = true;
  3970. } else if (needCxx98 && existingCxxStandard &&
  3971. existingCxxIt < std::find_if(cmArrayBegin(CXX_STANDARDS),
  3972. cmArrayEnd(CXX_STANDARDS),
  3973. cmStrCmp("98"))) {
  3974. setCxx98 = true;
  3975. }
  3976. if (setCxx14) {
  3977. target->SetProperty("CXX_STANDARD", "14");
  3978. } else if (setCxx11) {
  3979. target->SetProperty("CXX_STANDARD", "11");
  3980. } else if (setCxx98) {
  3981. target->SetProperty("CXX_STANDARD", "98");
  3982. }
  3983. return true;
  3984. }
  3985. void cmMakefile::CheckNeededCLanguage(const std::string& feature,
  3986. bool& needC90, bool& needC99,
  3987. bool& needC11) const
  3988. {
  3989. if (const char* propC90 =
  3990. this->GetDefinition("CMAKE_C90_COMPILE_FEATURES")) {
  3991. std::vector<std::string> props;
  3992. cmSystemTools::ExpandListArgument(propC90, props);
  3993. needC90 = std::find(props.begin(), props.end(), feature) != props.end();
  3994. }
  3995. if (const char* propC99 =
  3996. this->GetDefinition("CMAKE_C99_COMPILE_FEATURES")) {
  3997. std::vector<std::string> props;
  3998. cmSystemTools::ExpandListArgument(propC99, props);
  3999. needC99 = std::find(props.begin(), props.end(), feature) != props.end();
  4000. }
  4001. if (const char* propC11 =
  4002. this->GetDefinition("CMAKE_C11_COMPILE_FEATURES")) {
  4003. std::vector<std::string> props;
  4004. cmSystemTools::ExpandListArgument(propC11, props);
  4005. needC11 = std::find(props.begin(), props.end(), feature) != props.end();
  4006. }
  4007. }
  4008. bool cmMakefile::AddRequiredTargetCFeature(cmTarget* target,
  4009. const std::string& feature,
  4010. std::string* error) const
  4011. {
  4012. bool needC90 = false;
  4013. bool needC99 = false;
  4014. bool needC11 = false;
  4015. this->CheckNeededCLanguage(feature, needC90, needC99, needC11);
  4016. const char* existingCStandard = target->GetProperty("C_STANDARD");
  4017. if (existingCStandard) {
  4018. if (std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  4019. cmStrCmp(existingCStandard)) == cmArrayEnd(C_STANDARDS)) {
  4020. std::ostringstream e;
  4021. e << "The C_STANDARD property on target \"" << target->GetName()
  4022. << "\" contained an invalid value: \"" << existingCStandard << "\".";
  4023. if (error) {
  4024. *error = e.str();
  4025. } else {
  4026. this->GetCMakeInstance()->IssueMessage(cmake::FATAL_ERROR, e.str(),
  4027. this->Backtrace);
  4028. }
  4029. return false;
  4030. }
  4031. }
  4032. const char* const* existingCIt = existingCStandard
  4033. ? std::find_if(cmArrayBegin(C_STANDARDS), cmArrayEnd(C_STANDARDS),
  4034. cmStrCmp(existingCStandard))
  4035. : cmArrayEnd(C_STANDARDS);
  4036. bool setC90 = needC90 && !existingCStandard;
  4037. bool setC99 = needC99 && !existingCStandard;
  4038. bool setC11 = needC11 && !existingCStandard;
  4039. if (needC11 && existingCStandard &&
  4040. existingCIt < std::find_if(cmArrayBegin(C_STANDARDS),
  4041. cmArrayEnd(C_STANDARDS), cmStrCmp("11"))) {
  4042. setC11 = true;
  4043. } else if (needC99 && existingCStandard &&
  4044. existingCIt < std::find_if(cmArrayBegin(C_STANDARDS),
  4045. cmArrayEnd(C_STANDARDS),
  4046. cmStrCmp("99"))) {
  4047. setC99 = true;
  4048. } else if (needC90 && existingCStandard &&
  4049. existingCIt < std::find_if(cmArrayBegin(C_STANDARDS),
  4050. cmArrayEnd(C_STANDARDS),
  4051. cmStrCmp("90"))) {
  4052. setC90 = true;
  4053. }
  4054. if (setC11) {
  4055. target->SetProperty("C_STANDARD", "11");
  4056. } else if (setC99) {
  4057. target->SetProperty("C_STANDARD", "99");
  4058. } else if (setC90) {
  4059. target->SetProperty("C_STANDARD", "90");
  4060. }
  4061. return true;
  4062. }
  4063. cmMakefile::FunctionPushPop::FunctionPushPop(cmMakefile* mf,
  4064. const std::string& fileName,
  4065. cmPolicies::PolicyMap const& pm)
  4066. : Makefile(mf)
  4067. , ReportError(true)
  4068. {
  4069. this->Makefile->PushFunctionScope(fileName, pm);
  4070. }
  4071. cmMakefile::FunctionPushPop::~FunctionPushPop()
  4072. {
  4073. this->Makefile->PopFunctionScope(this->ReportError);
  4074. }
  4075. cmMakefile::MacroPushPop::MacroPushPop(cmMakefile* mf,
  4076. const std::string& fileName,
  4077. const cmPolicies::PolicyMap& pm)
  4078. : Makefile(mf)
  4079. , ReportError(true)
  4080. {
  4081. this->Makefile->PushMacroScope(fileName, pm);
  4082. }
  4083. cmMakefile::MacroPushPop::~MacroPushPop()
  4084. {
  4085. this->Makefile->PopMacroScope(this->ReportError);
  4086. }