cmMakefile.cxx 161 KB

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