cmMakefile.cxx 120 KB

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