cmMakefile.cxx 107 KB

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