cmMakefile.cxx 147 KB

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