cmMakefile.cxx 139 KB

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