cmMakefile.cxx 168 KB

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