cmMakefile.cxx 156 KB

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