cmMakefile.cxx 129 KB

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