cmMakefile.cxx 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931
  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 <ctype.h> // for isspace
  35. // default is not to be building executables
  36. cmMakefile::cmMakefile()
  37. {
  38. this->DefinitionStack.push_back(DefinitionMap());
  39. // Setup the default include file regular expression (match everything).
  40. this->IncludeFileRegularExpression = "^.*$";
  41. // Setup the default include complaint regular expression (match nothing).
  42. this->ComplainFileRegularExpression = "^$";
  43. // Source and header file extensions that we can handle
  44. // Set up a list of source and header extensions
  45. // these are used to find files when the extension
  46. // is not given
  47. // The "c" extension MUST precede the "C" extension.
  48. this->SourceFileExtensions.push_back( "c" );
  49. this->SourceFileExtensions.push_back( "C" );
  50. this->SourceFileExtensions.push_back( "c++" );
  51. this->SourceFileExtensions.push_back( "cc" );
  52. this->SourceFileExtensions.push_back( "cpp" );
  53. this->SourceFileExtensions.push_back( "cxx" );
  54. this->SourceFileExtensions.push_back( "m" );
  55. this->SourceFileExtensions.push_back( "M" );
  56. this->SourceFileExtensions.push_back( "mm" );
  57. this->HeaderFileExtensions.push_back( "h" );
  58. this->HeaderFileExtensions.push_back( "hh" );
  59. this->HeaderFileExtensions.push_back( "h++" );
  60. this->HeaderFileExtensions.push_back( "hm" );
  61. this->HeaderFileExtensions.push_back( "hpp" );
  62. this->HeaderFileExtensions.push_back( "hxx" );
  63. this->HeaderFileExtensions.push_back( "in" );
  64. this->HeaderFileExtensions.push_back( "txx" );
  65. this->DefineFlags = " ";
  66. this->LocalGenerator = 0;
  67. #if defined(CMAKE_BUILD_WITH_CMAKE)
  68. this->AddSourceGroup("", "^.*$");
  69. this->AddSourceGroup
  70. ("Source Files",
  71. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  72. this->AddSourceGroup("Header Files",
  73. "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  74. this->AddSourceGroup("CMake Rules", "\\.rule$");
  75. this->AddSourceGroup("Resources", "\\.plist$");
  76. #endif
  77. this->AddDefaultDefinitions();
  78. this->Initialize();
  79. this->PreOrder = false;
  80. }
  81. cmMakefile::cmMakefile(const cmMakefile& mf)
  82. {
  83. this->Prefix = mf.Prefix;
  84. this->AuxSourceDirectories = mf.AuxSourceDirectories;
  85. this->cmStartDirectory = mf.cmStartDirectory;
  86. this->StartOutputDirectory = mf.StartOutputDirectory;
  87. this->cmHomeDirectory = mf.cmHomeDirectory;
  88. this->HomeOutputDirectory = mf.HomeOutputDirectory;
  89. this->cmCurrentListFile = mf.cmCurrentListFile;
  90. this->ProjectName = mf.ProjectName;
  91. this->Targets = mf.Targets;
  92. this->SourceFiles = mf.SourceFiles;
  93. this->Tests = mf.Tests;
  94. this->IncludeDirectories = mf.IncludeDirectories;
  95. this->LinkDirectories = mf.LinkDirectories;
  96. this->SystemIncludeDirectories = mf.SystemIncludeDirectories;
  97. this->ListFiles = mf.ListFiles;
  98. this->OutputFiles = mf.OutputFiles;
  99. this->LinkLibraries = mf.LinkLibraries;
  100. this->InstallGenerators = mf.InstallGenerators;
  101. this->IncludeFileRegularExpression = mf.IncludeFileRegularExpression;
  102. this->ComplainFileRegularExpression = mf.ComplainFileRegularExpression;
  103. this->SourceFileExtensions = mf.SourceFileExtensions;
  104. this->HeaderFileExtensions = mf.HeaderFileExtensions;
  105. this->DefineFlags = mf.DefineFlags;
  106. #if defined(CMAKE_BUILD_WITH_CMAKE)
  107. this->SourceGroups = mf.SourceGroups;
  108. #endif
  109. this->DefinitionStack.push_back(mf.DefinitionStack.back());
  110. this->LocalGenerator = mf.LocalGenerator;
  111. this->FunctionBlockers = mf.FunctionBlockers;
  112. this->DataMap = mf.DataMap;
  113. this->MacrosMap = mf.MacrosMap;
  114. this->SubDirectoryOrder = mf.SubDirectoryOrder;
  115. this->TemporaryDefinitionKey = mf.TemporaryDefinitionKey;
  116. this->Properties = mf.Properties;
  117. this->PreOrder = mf.PreOrder;
  118. this->ListFileStack = mf.ListFileStack;
  119. this->Initialize();
  120. }
  121. //----------------------------------------------------------------------------
  122. void cmMakefile::Initialize()
  123. {
  124. this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
  125. this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
  126. this->cmAtVarRegex.compile("(@[A-Za-z_0-9/.+-]+@)");
  127. }
  128. unsigned int cmMakefile::GetCacheMajorVersion()
  129. {
  130. if(const char* vstr =
  131. this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  132. {
  133. unsigned int v=0;
  134. if(sscanf(vstr, "%u", &v) == 1)
  135. {
  136. return v;
  137. }
  138. }
  139. return 0;
  140. }
  141. unsigned int cmMakefile::GetCacheMinorVersion()
  142. {
  143. if(const char* vstr =
  144. this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"))
  145. {
  146. unsigned int v=0;
  147. if(sscanf(vstr, "%u", &v) == 1)
  148. {
  149. return v;
  150. }
  151. }
  152. return 0;
  153. }
  154. cmMakefile::~cmMakefile()
  155. {
  156. for(std::vector<cmInstallGenerator*>::iterator
  157. i = this->InstallGenerators.begin();
  158. i != this->InstallGenerators.end(); ++i)
  159. {
  160. delete *i;
  161. }
  162. for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
  163. i != this->SourceFiles.end(); ++i)
  164. {
  165. delete *i;
  166. }
  167. for(std::vector<cmTest*>::iterator i = this->Tests.begin();
  168. i != this->Tests.end(); ++i)
  169. {
  170. delete *i;
  171. }
  172. for(unsigned int i=0; i < this->UsedCommands.size(); i++)
  173. {
  174. delete this->UsedCommands[i];
  175. }
  176. for(DataMapType::const_iterator d = this->DataMap.begin();
  177. d != this->DataMap.end(); ++d)
  178. {
  179. if(d->second)
  180. {
  181. delete d->second;
  182. }
  183. }
  184. std::list<cmFunctionBlocker *>::iterator pos;
  185. for (pos = this->FunctionBlockers.begin();
  186. pos != this->FunctionBlockers.end(); ++pos)
  187. {
  188. cmFunctionBlocker* b = *pos;
  189. delete b;
  190. }
  191. this->FunctionBlockers.clear();
  192. }
  193. void cmMakefile::PrintStringVector(const char* s,
  194. const std::vector<std::string>& v) const
  195. {
  196. std::cout << s << ": ( \n";
  197. for(std::vector<std::string>::const_iterator i = v.begin();
  198. i != v.end(); ++i)
  199. {
  200. std::cout << (*i).c_str() << " ";
  201. }
  202. std::cout << " )\n";
  203. }
  204. void cmMakefile
  205. ::PrintStringVector(const char* s,
  206. const std::vector<std::pair<cmStdString, bool> >& v) const
  207. {
  208. std::cout << s << ": ( \n";
  209. for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
  210. = v.begin(); i != v.end(); ++i)
  211. {
  212. std::cout << i->first.c_str() << " " << i->second;
  213. }
  214. std::cout << " )\n";
  215. }
  216. // call print on all the classes in the makefile
  217. void cmMakefile::Print()
  218. {
  219. // print the class lists
  220. std::cout << "classes:\n";
  221. std::cout << " this->Targets: ";
  222. for (cmTargets::iterator l = this->Targets.begin();
  223. l != this->Targets.end(); l++)
  224. {
  225. std::cout << l->first << std::endl;
  226. }
  227. std::cout << " this->StartOutputDirectory; " <<
  228. this->StartOutputDirectory.c_str() << std::endl;
  229. std::cout << " this->HomeOutputDirectory; " <<
  230. this->HomeOutputDirectory.c_str() << std::endl;
  231. std::cout << " this->cmStartDirectory; " <<
  232. this->cmStartDirectory.c_str() << std::endl;
  233. std::cout << " this->cmHomeDirectory; " <<
  234. this->cmHomeDirectory.c_str() << std::endl;
  235. std::cout << " this->ProjectName; "
  236. << this->ProjectName.c_str() << std::endl;
  237. this->PrintStringVector("this->IncludeDirectories;",
  238. this->IncludeDirectories);
  239. this->PrintStringVector("this->LinkDirectories", this->LinkDirectories);
  240. #if defined(CMAKE_BUILD_WITH_CMAKE)
  241. for( std::vector<cmSourceGroup>::const_iterator i =
  242. this->SourceGroups.begin(); i != this->SourceGroups.end(); ++i)
  243. {
  244. std::cout << "Source Group: " << i->GetName() << std::endl;
  245. }
  246. #endif
  247. }
  248. bool cmMakefile::CommandExists(const char* name) const
  249. {
  250. return this->GetCMakeInstance()->CommandExists(name);
  251. }
  252. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  253. {
  254. bool result = true;
  255. // quick return if blocked
  256. if(this->IsFunctionBlocked(lff))
  257. {
  258. // No error.
  259. return result;
  260. }
  261. std::string name = lff.Name;
  262. // execute the command
  263. cmCommand *rm =
  264. this->GetCMakeInstance()->GetCommand(name.c_str());
  265. if(rm)
  266. {
  267. const char* versionValue
  268. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  269. int major = 0;
  270. int minor = 0;
  271. if ( versionValue )
  272. {
  273. sscanf(versionValue, "%d.%d", &major, &minor);
  274. }
  275. if ( rm->IsDeprecated(major, minor) )
  276. {
  277. cmOStringStream error;
  278. error << "Error in cmake code at\n"
  279. << lff.FilePath << ":" << lff.Line << ":\n"
  280. << rm->GetError() << std::endl
  281. << " Called from: " << this->GetListFileStack().c_str();
  282. cmSystemTools::Error(error.str().c_str());
  283. return false;
  284. }
  285. cmCommand* usedCommand = rm->Clone();
  286. usedCommand->SetMakefile(this);
  287. bool keepCommand = false;
  288. if(usedCommand->GetEnabled() && !cmSystemTools::GetFatalErrorOccured() &&
  289. (!this->GetCMakeInstance()->GetScriptMode() ||
  290. usedCommand->IsScriptable()))
  291. {
  292. if(!usedCommand->InvokeInitialPass(lff.Arguments))
  293. {
  294. cmOStringStream error;
  295. error << "Error in cmake code at\n"
  296. << lff.FilePath << ":" << lff.Line << ":\n"
  297. << usedCommand->GetError() << std::endl
  298. << " Called from: " << this->GetListFileStack().c_str();
  299. cmSystemTools::Error(error.str().c_str());
  300. result = false;
  301. if ( this->GetCMakeInstance()->GetScriptMode() )
  302. {
  303. cmSystemTools::SetFatalErrorOccured();
  304. }
  305. }
  306. else
  307. {
  308. // use the command
  309. keepCommand = true;
  310. this->UsedCommands.push_back(usedCommand);
  311. }
  312. }
  313. else if ( this->GetCMakeInstance()->GetScriptMode()
  314. && !usedCommand->IsScriptable() )
  315. {
  316. cmOStringStream error;
  317. error << "Error in cmake code at\n"
  318. << lff.FilePath << ":" << lff.Line << ":\n"
  319. << "Command " << usedCommand->GetName()
  320. << "() is not scriptable" << std::endl;
  321. cmSystemTools::Error(error.str().c_str());
  322. result = false;
  323. cmSystemTools::SetFatalErrorOccured();
  324. }
  325. // if the Cloned command was not used
  326. // then delete it
  327. if(!keepCommand)
  328. {
  329. delete usedCommand;
  330. }
  331. }
  332. else
  333. {
  334. if(!cmSystemTools::GetFatalErrorOccured())
  335. {
  336. cmOStringStream error;
  337. error << "Error in cmake code at\n"
  338. << lff.FilePath << ":" << lff.Line << ":\n"
  339. << "Unknown CMake command \"" << lff.Name.c_str() << "\".";
  340. cmSystemTools::Error(error.str().c_str());
  341. result = false;
  342. cmSystemTools::SetFatalErrorOccured();
  343. }
  344. }
  345. return result;
  346. }
  347. // Parse the given CMakeLists.txt file executing all commands
  348. //
  349. bool cmMakefile::ReadListFile(const char* filename_in,
  350. const char *external_in,
  351. std::string* fullPath)
  352. {
  353. std::string currentParentFile
  354. = this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
  355. std::string currentFile
  356. = this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE");
  357. this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in);
  358. // used to watch for blockers going out of scope
  359. // e.g. mismatched IF statement
  360. std::set<cmFunctionBlocker *> originalBlockers;
  361. const char* external = 0;
  362. std::string external_abs;
  363. const char* filename = filename_in;
  364. std::string filename_abs;
  365. if (external_in)
  366. {
  367. external_abs =
  368. cmSystemTools::CollapseFullPath(external_in,
  369. this->cmStartDirectory.c_str());
  370. external = external_abs.c_str();
  371. if (filename_in)
  372. {
  373. filename_abs =
  374. cmSystemTools::CollapseFullPath(filename_in,
  375. this->cmStartDirectory.c_str());
  376. filename = filename_abs.c_str();
  377. }
  378. }
  379. // keep track of the current file being read
  380. if (filename)
  381. {
  382. if(this->cmCurrentListFile != filename)
  383. {
  384. this->cmCurrentListFile = filename;
  385. }
  386. // loop over current function blockers and record them
  387. std::list<cmFunctionBlocker *>::iterator pos;
  388. for (pos = this->FunctionBlockers.begin();
  389. pos != this->FunctionBlockers.end(); ++pos)
  390. {
  391. originalBlockers.insert(*pos);
  392. }
  393. }
  394. // Now read the input file
  395. const char *filenametoread= filename;
  396. if( external)
  397. {
  398. filenametoread= external;
  399. }
  400. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread);
  401. // try to see if the list file is the top most
  402. // list file for a project, and if it is, then it
  403. // must have a project command. If there is not
  404. // one, then cmake will provide one via the
  405. // cmListFileCache class.
  406. bool requireProjectCommand = false;
  407. if(!external && this->cmStartDirectory == this->cmHomeDirectory)
  408. {
  409. if(cmSystemTools::LowerCase(
  410. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  411. {
  412. requireProjectCommand = true;
  413. }
  414. }
  415. // push the listfile onto the stack
  416. this->ListFileStack.push_back(filenametoread);
  417. if(fullPath!=0)
  418. {
  419. *fullPath=filenametoread;
  420. }
  421. cmListFile cacheFile;
  422. if( !cacheFile.ParseFile(filenametoread, requireProjectCommand) )
  423. {
  424. // pop the listfile off the stack
  425. this->ListFileStack.pop_back();
  426. if(fullPath!=0)
  427. {
  428. *fullPath = "";
  429. }
  430. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  431. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  432. return false;
  433. }
  434. // add this list file to the list of dependencies
  435. this->ListFiles.push_back( filenametoread);
  436. const size_t numberFunctions = cacheFile.Functions.size();
  437. for(size_t i =0; i < numberFunctions; ++i)
  438. {
  439. this->ExecuteCommand(cacheFile.Functions[i]);
  440. if ( cmSystemTools::GetFatalErrorOccured() )
  441. {
  442. // pop the listfile off the stack
  443. this->ListFileStack.pop_back();
  444. this->AddDefinition("CMAKE_PARENT_LIST_FILE",
  445. currentParentFile.c_str());
  446. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  447. return true;
  448. }
  449. }
  450. // send scope ended to and function blockers
  451. if (filename)
  452. {
  453. // loop over all function blockers to see if any block this command
  454. std::list<cmFunctionBlocker *>::iterator pos;
  455. for (pos = this->FunctionBlockers.begin();
  456. pos != this->FunctionBlockers.end(); ++pos)
  457. {
  458. // if this blocker was not in the original then send a
  459. // scope ended message
  460. if (originalBlockers.find(*pos) == originalBlockers.end())
  461. {
  462. (*pos)->ScopeEnded(*this);
  463. }
  464. }
  465. }
  466. this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  467. this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
  468. // pop the listfile off the stack
  469. this->ListFileStack.pop_back();
  470. return true;
  471. }
  472. void cmMakefile::AddCommand(cmCommand* wg)
  473. {
  474. this->GetCMakeInstance()->AddCommand(wg);
  475. }
  476. // Set the make file
  477. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  478. {
  479. this->LocalGenerator = lg;
  480. }
  481. void cmMakefile::FinalPass()
  482. {
  483. // do all the variable expansions here
  484. this->ExpandVariables();
  485. // give all the commands a chance to do something
  486. // after the file has been parsed before generation
  487. for(std::vector<cmCommand*>::iterator i = this->UsedCommands.begin();
  488. i != this->UsedCommands.end(); ++i)
  489. {
  490. (*i)->FinalPass();
  491. }
  492. }
  493. // Generate the output file
  494. void cmMakefile::ConfigureFinalPass()
  495. {
  496. this->FinalPass();
  497. const char* oldValue
  498. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  499. if (oldValue && atof(oldValue) <= 1.2)
  500. {
  501. cmSystemTools::Error("You have requested backwards compatibility "
  502. "with CMake version 1.2 or earlier. This version "
  503. "of CMake only supports backwards compatibility "
  504. "with CMake 1.4 or later. For compatibility with "
  505. "1.2 or earlier please use CMake 2.0");
  506. }
  507. for (cmTargets::iterator l = this->Targets.begin();
  508. l != this->Targets.end(); l++)
  509. {
  510. l->second.AnalyzeLibDependencies(*this);
  511. }
  512. }
  513. //----------------------------------------------------------------------------
  514. void
  515. cmMakefile::AddCustomCommandToTarget(const char* target,
  516. const std::vector<std::string>& depends,
  517. const cmCustomCommandLines& commandLines,
  518. cmTarget::CustomCommandType type,
  519. const char* comment,
  520. const char* workingDir,
  521. bool escapeOldStyle)
  522. {
  523. // Find the target to which to add the custom command.
  524. cmTargets::iterator ti = this->Targets.find(target);
  525. if(ti != this->Targets.end())
  526. {
  527. // Add the command to the appropriate build step for the target.
  528. std::vector<std::string> no_output;
  529. cmCustomCommand cc(no_output, depends, commandLines, comment, workingDir);
  530. cc.SetEscapeOldStyle(escapeOldStyle);
  531. switch(type)
  532. {
  533. case cmTarget::PRE_BUILD:
  534. ti->second.GetPreBuildCommands().push_back(cc);
  535. break;
  536. case cmTarget::PRE_LINK:
  537. ti->second.GetPreLinkCommands().push_back(cc);
  538. break;
  539. case cmTarget::POST_BUILD:
  540. ti->second.GetPostBuildCommands().push_back(cc);
  541. break;
  542. }
  543. }
  544. }
  545. //----------------------------------------------------------------------------
  546. void
  547. cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
  548. const std::vector<std::string>& depends,
  549. const char* main_dependency,
  550. const cmCustomCommandLines& commandLines,
  551. const char* comment,
  552. const char* workingDir,
  553. bool replace,
  554. bool escapeOldStyle)
  555. {
  556. // Make sure there is at least one output.
  557. if(outputs.empty())
  558. {
  559. cmSystemTools::Error("Attempt to add a custom rule with no output!");
  560. return;
  561. }
  562. // Choose a source file on which to store the custom command.
  563. cmSourceFile* file = 0;
  564. if(main_dependency && main_dependency[0])
  565. {
  566. // The main dependency was specified. Use it unless a different
  567. // custom command already used it.
  568. file = this->GetSource(main_dependency);
  569. if(file && file->GetCustomCommand() && !replace)
  570. {
  571. // The main dependency already has a custom command.
  572. if(commandLines == file->GetCustomCommand()->GetCommandLines())
  573. {
  574. // The existing custom command is identical. Silently ignore
  575. // the duplicate.
  576. return;
  577. }
  578. else
  579. {
  580. // The existing custom command is different. We need to
  581. // generate a rule file for this new command.
  582. file = 0;
  583. }
  584. }
  585. else
  586. {
  587. // The main dependency does not have a custom command or we are
  588. // allowed to replace it. Use it to store the command.
  589. file = this->GetOrCreateSource(main_dependency);
  590. }
  591. }
  592. // Generate a rule file if the main dependency is not available.
  593. if(!file)
  594. {
  595. // Construct a rule file associated with the first output produced.
  596. std::string outName = outputs[0];
  597. outName += ".rule";
  598. // Check if the rule file already exists.
  599. file = this->GetSource(outName.c_str());
  600. if(file && file->GetCustomCommand() && !replace)
  601. {
  602. // The rule file already exists.
  603. if(commandLines != file->GetCustomCommand()->GetCommandLines())
  604. {
  605. cmSystemTools::Error("Attempt to add a custom rule to output \"",
  606. outName.c_str(),
  607. "\" which already has a custom rule.");
  608. }
  609. return;
  610. }
  611. // Create a cmSourceFile for the rule file.
  612. file = this->GetOrCreateSource(outName.c_str(), true);
  613. }
  614. // Always create the output sources and mark them generated.
  615. for(std::vector<std::string>::const_iterator o = outputs.begin();
  616. o != outputs.end(); ++o)
  617. {
  618. if(cmSourceFile* out = this->GetOrCreateSource(o->c_str(), true))
  619. {
  620. out->SetProperty("GENERATED", "1");
  621. }
  622. }
  623. // Construct a complete list of dependencies.
  624. std::vector<std::string> depends2(depends);
  625. if(main_dependency && main_dependency[0])
  626. {
  627. depends2.push_back(main_dependency);
  628. }
  629. // Attach the custom command to the file.
  630. if(file)
  631. {
  632. cmCustomCommand* cc =
  633. new cmCustomCommand(outputs, depends2, commandLines,
  634. comment, workingDir);
  635. cc->SetEscapeOldStyle(escapeOldStyle);
  636. file->SetCustomCommand(cc);
  637. }
  638. }
  639. //----------------------------------------------------------------------------
  640. void
  641. cmMakefile::AddCustomCommandToOutput(const char* output,
  642. const std::vector<std::string>& depends,
  643. const char* main_dependency,
  644. const cmCustomCommandLines& commandLines,
  645. const char* comment,
  646. const char* workingDir,
  647. bool replace,
  648. bool escapeOldStyle)
  649. {
  650. std::vector<std::string> outputs;
  651. outputs.push_back(output);
  652. this->AddCustomCommandToOutput(outputs, depends, main_dependency,
  653. commandLines, comment, workingDir,
  654. replace, escapeOldStyle);
  655. }
  656. //----------------------------------------------------------------------------
  657. void
  658. cmMakefile::AddCustomCommandOldStyle(const char* target,
  659. const std::vector<std::string>& outputs,
  660. const std::vector<std::string>& depends,
  661. const char* source,
  662. const cmCustomCommandLines& commandLines,
  663. const char* comment)
  664. {
  665. // Translate the old-style signature to one of the new-style
  666. // signatures.
  667. if(strcmp(source, target) == 0)
  668. {
  669. // In the old-style signature if the source and target were the
  670. // same then it added a post-build rule to the target. Preserve
  671. // this behavior.
  672. this->AddCustomCommandToTarget(target, depends, commandLines,
  673. cmTarget::POST_BUILD, comment, 0);
  674. return;
  675. }
  676. // Each output must get its own copy of this rule.
  677. cmsys::RegularExpression sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
  678. "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
  679. "hm|hpp|hxx|in|txx|inl)$");
  680. for(std::vector<std::string>::const_iterator oi = outputs.begin();
  681. oi != outputs.end(); ++oi)
  682. {
  683. // Get the name of this output.
  684. const char* output = oi->c_str();
  685. // Choose whether to use a main dependency.
  686. if(sourceFiles.find(source))
  687. {
  688. // The source looks like a real file. Use it as the main dependency.
  689. this->AddCustomCommandToOutput(output, depends, source,
  690. commandLines, comment, 0);
  691. }
  692. else
  693. {
  694. // The source may not be a real file. Do not use a main dependency.
  695. const char* no_main_dependency = 0;
  696. std::vector<std::string> depends2 = depends;
  697. depends2.push_back(source);
  698. this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
  699. commandLines, comment, 0);
  700. }
  701. // If the rule was added to the source (and not a .rule file),
  702. // then add the source to the target to make sure the rule is
  703. // included.
  704. std::string sname = output;
  705. sname += ".rule";
  706. if(!this->GetSource(sname.c_str()))
  707. {
  708. if (this->Targets.find(target) != this->Targets.end())
  709. {
  710. this->Targets[target].AddSource(source);
  711. }
  712. else
  713. {
  714. cmSystemTools::Error("Attempt to add a custom rule to a target "
  715. "that does not exist yet for target ", target);
  716. return;
  717. }
  718. }
  719. }
  720. }
  721. //----------------------------------------------------------------------------
  722. void cmMakefile::AddUtilityCommand(const char* utilityName,
  723. bool excludeFromAll,
  724. const std::vector<std::string>& depends,
  725. const char* workingDirectory,
  726. const char* command,
  727. const char* arg1,
  728. const char* arg2,
  729. const char* arg3,
  730. const char* arg4)
  731. {
  732. // Construct the command line for the custom command.
  733. cmCustomCommandLine commandLine;
  734. commandLine.push_back(command);
  735. if(arg1)
  736. {
  737. commandLine.push_back(arg1);
  738. }
  739. if(arg2)
  740. {
  741. commandLine.push_back(arg2);
  742. }
  743. if(arg3)
  744. {
  745. commandLine.push_back(arg3);
  746. }
  747. if(arg4)
  748. {
  749. commandLine.push_back(arg4);
  750. }
  751. cmCustomCommandLines commandLines;
  752. commandLines.push_back(commandLine);
  753. // Call the real signature of this method.
  754. this->AddUtilityCommand(utilityName, excludeFromAll, workingDirectory,
  755. depends, commandLines);
  756. }
  757. //----------------------------------------------------------------------------
  758. void cmMakefile::AddUtilityCommand(const char* utilityName,
  759. bool excludeFromAll,
  760. const char* workingDirectory,
  761. const std::vector<std::string>& depends,
  762. const cmCustomCommandLines& commandLines,
  763. bool escapeOldStyle, const char* comment)
  764. {
  765. // Create a target instance for this utility.
  766. cmTarget* target = this->AddNewTarget(cmTarget::UTILITY, utilityName, false);
  767. if (excludeFromAll)
  768. {
  769. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  770. }
  771. if(!comment)
  772. {
  773. // Use an empty comment to avoid generation of default comment.
  774. comment = "";
  775. }
  776. // Store the custom command in the target.
  777. std::string force = this->GetStartOutputDirectory();
  778. force += cmake::GetCMakeFilesDirectory();
  779. force += "/";
  780. force += utilityName;
  781. const char* no_main_dependency = 0;
  782. bool no_replace = false;
  783. this->AddCustomCommandToOutput(force.c_str(), depends,
  784. no_main_dependency,
  785. commandLines, comment,
  786. workingDirectory, no_replace,
  787. escapeOldStyle);
  788. cmSourceFile* sf = target->AddSource(force.c_str());
  789. // The output is not actually created so mark it symbolic.
  790. if(sf)
  791. {
  792. sf->SetProperty("SYMBOLIC", "1");
  793. }
  794. else
  795. {
  796. cmSystemTools::Error("Could not get source file entry for ",
  797. force.c_str());
  798. }
  799. }
  800. void cmMakefile::AddDefineFlag(const char* flag)
  801. {
  802. if (!flag)
  803. {
  804. return;
  805. }
  806. // remove any \n\r
  807. std::string ret = flag;
  808. std::string::size_type pos = 0;
  809. while((pos = ret.find('\n', pos)) != std::string::npos)
  810. {
  811. ret[pos] = ' ';
  812. pos++;
  813. }
  814. pos = 0;
  815. while((pos = ret.find('\r', pos)) != std::string::npos)
  816. {
  817. ret[pos] = ' ';
  818. pos++;
  819. }
  820. this->DefineFlags += " ";
  821. this->DefineFlags += ret;
  822. }
  823. void cmMakefile::RemoveDefineFlag(const char* flag)
  824. {
  825. // Check the length of the flag to remove.
  826. std::string::size_type len = strlen(flag);
  827. if(len < 1)
  828. {
  829. return;
  830. }
  831. // Remove all instances of the flag that are surrounded by
  832. // whitespace or the beginning/end of the string.
  833. for(std::string::size_type lpos = this->DefineFlags.find(flag, 0);
  834. lpos != std::string::npos; lpos = this->DefineFlags.find(flag, lpos))
  835. {
  836. std::string::size_type rpos = lpos + len;
  837. if((lpos <= 0 || isspace(this->DefineFlags[lpos-1])) &&
  838. (rpos >= this->DefineFlags.size() || isspace(this->DefineFlags[rpos])))
  839. {
  840. this->DefineFlags.erase(lpos, len);
  841. }
  842. else
  843. {
  844. ++lpos;
  845. }
  846. }
  847. }
  848. void cmMakefile::AddLinkLibrary(const char* lib,
  849. cmTarget::LinkLibraryType llt)
  850. {
  851. cmTarget::LibraryID tmp;
  852. tmp.first = lib;
  853. tmp.second = llt;
  854. this->LinkLibraries.push_back(tmp);
  855. }
  856. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  857. const char* lib,
  858. cmTarget::LinkLibraryType llt)
  859. {
  860. cmTargets::iterator i = this->Targets.find(target);
  861. if ( i != this->Targets.end())
  862. {
  863. cmTarget* tgt =
  864. this->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(0,lib,false);
  865. if(tgt)
  866. {
  867. bool allowModules = true;
  868. const char* versionValue
  869. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  870. if (versionValue && (atof(versionValue) >= 2.4) )
  871. {
  872. allowModules = false;
  873. }
  874. // if it is not a static or shared library then you can not link to it
  875. if(!((tgt->GetType() == cmTarget::STATIC_LIBRARY) ||
  876. (tgt->GetType() == cmTarget::SHARED_LIBRARY) ||
  877. (tgt->GetType() == cmTarget::EXECUTABLE &&
  878. tgt->GetPropertyAsBool("ENABLE_EXPORTS"))))
  879. {
  880. cmOStringStream e;
  881. e << "Attempt to add link target " << lib << " of type: "
  882. << cmTarget::TargetTypeNames[static_cast<int>(tgt->GetType())]
  883. << "\nto target " << target
  884. << ". One can only link to STATIC or SHARED libraries, or "
  885. << "to executables with the ENABLE_EXPORTS property set.";
  886. // in older versions of cmake linking to modules was allowed
  887. if( tgt->GetType() == cmTarget::MODULE_LIBRARY )
  888. {
  889. e <<
  890. "\nTo allow linking of modules set "
  891. "CMAKE_BACKWARDS_COMPATIBILITY to 2.2 or lower\n";
  892. }
  893. // if no modules are allowed then this is always an error
  894. if(!allowModules ||
  895. // if we allow modules but the type is not a module then it is
  896. // still an error
  897. (allowModules && tgt->GetType() != cmTarget::MODULE_LIBRARY))
  898. {
  899. cmSystemTools::Error(e.str().c_str());
  900. }
  901. }
  902. }
  903. i->second.AddLinkLibrary( *this, target, lib, llt );
  904. }
  905. else
  906. {
  907. cmOStringStream e;
  908. e << "Attempt to add link library \""
  909. << lib << "\" to target \""
  910. << target << "\" which is not built by this project.";
  911. cmSystemTools::Error(e.str().c_str());
  912. }
  913. }
  914. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  915. const char* d)
  916. {
  917. cmTargets::iterator i = this->Targets.find(target);
  918. if ( i != this->Targets.end())
  919. {
  920. i->second.AddLinkDirectory( d );
  921. }
  922. else
  923. {
  924. cmSystemTools::Error
  925. ("Attempt to add link directories to non-existant target: ",
  926. target, " for directory ", d);
  927. }
  928. }
  929. void cmMakefile::AddLinkLibrary(const char* lib)
  930. {
  931. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  932. }
  933. void cmMakefile::AddLinkDirectory(const char* dir)
  934. {
  935. // Don't add a link directory that is already present. Yes, this
  936. // linear search results in n^2 behavior, but n won't be getting
  937. // much bigger than 20. We cannot use a set because of order
  938. // dependency of the link search path.
  939. if(!dir)
  940. {
  941. return;
  942. }
  943. // remove trailing slashes
  944. if(dir[strlen(dir)-1] == '/')
  945. {
  946. std::string newdir = dir;
  947. newdir = newdir.substr(0, newdir.size()-1);
  948. if(std::find(this->LinkDirectories.begin(),
  949. this->LinkDirectories.end(),
  950. newdir.c_str()) == this->LinkDirectories.end())
  951. {
  952. this->LinkDirectories.push_back(newdir);
  953. }
  954. }
  955. else
  956. {
  957. if(std::find(this->LinkDirectories.begin(),
  958. this->LinkDirectories.end(), dir)
  959. == this->LinkDirectories.end())
  960. {
  961. this->LinkDirectories.push_back(dir);
  962. }
  963. }
  964. }
  965. void cmMakefile::InitializeFromParent()
  966. {
  967. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  968. // copy the definitions
  969. this->DefinitionStack.front() = parent->DefinitionStack.back();
  970. // copy include paths
  971. this->IncludeDirectories = parent->IncludeDirectories;
  972. this->SystemIncludeDirectories = parent->SystemIncludeDirectories;
  973. // define flags
  974. this->DefineFlags = parent->DefineFlags;
  975. // link libraries
  976. this->LinkLibraries = parent->LinkLibraries;
  977. // link directories
  978. this->LinkDirectories = parent->LinkDirectories;
  979. // the initial project name
  980. this->ProjectName = parent->ProjectName;
  981. // Copy include regular expressions.
  982. this->IncludeFileRegularExpression = parent->IncludeFileRegularExpression;
  983. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  984. }
  985. void cmMakefile::ConfigureSubDirectory(cmLocalGenerator *lg2)
  986. {
  987. // copy our variables from the child makefile
  988. lg2->GetMakefile()->InitializeFromParent();
  989. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  990. if (this->GetCMakeInstance()->GetDebugOutput())
  991. {
  992. std::string msg=" Entering ";
  993. msg += lg2->GetMakefile()->GetCurrentDirectory();
  994. cmSystemTools::Message(msg.c_str());
  995. }
  996. // finally configure the subdir
  997. lg2->Configure();
  998. if (this->GetCMakeInstance()->GetDebugOutput())
  999. {
  1000. std::string msg=" Returning to ";
  1001. msg += this->GetCurrentDirectory();
  1002. cmSystemTools::Message(msg.c_str());
  1003. }
  1004. }
  1005. void cmMakefile::AddSubDirectory(const char* sub,
  1006. bool excludeFromAll, bool preorder)
  1007. {
  1008. // the source path must be made full if it isn't already
  1009. std::string srcPath = sub;
  1010. if (!cmSystemTools::FileIsFullPath(srcPath.c_str()))
  1011. {
  1012. srcPath = this->GetCurrentDirectory();
  1013. srcPath += "/";
  1014. srcPath += sub;
  1015. }
  1016. // binary path must be made full if it isn't already
  1017. std::string binPath = sub;
  1018. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  1019. {
  1020. binPath = this->GetCurrentOutputDirectory();
  1021. binPath += "/";
  1022. binPath += sub;
  1023. }
  1024. this->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  1025. excludeFromAll, preorder, false);
  1026. }
  1027. void cmMakefile::AddSubDirectory(const char* srcPath, const char *binPath,
  1028. bool excludeFromAll, bool preorder,
  1029. bool immediate)
  1030. {
  1031. std::vector<cmLocalGenerator *>& children =
  1032. this->LocalGenerator->GetChildren();
  1033. // has this directory already been added? If so error
  1034. unsigned int i;
  1035. for (i = 0; i < children.size(); ++i)
  1036. {
  1037. if (srcPath == children[i]->GetMakefile()->GetStartDirectory())
  1038. {
  1039. cmSystemTools::Error
  1040. ("Attempt to add subdirectory multiple times for directory.\n",
  1041. srcPath);
  1042. return;
  1043. }
  1044. }
  1045. // create a new local generator and set its parent
  1046. cmLocalGenerator *lg2 =
  1047. this->LocalGenerator->GetGlobalGenerator()->CreateLocalGenerator();
  1048. lg2->SetParent(this->LocalGenerator);
  1049. this->LocalGenerator->GetGlobalGenerator()->AddLocalGenerator(lg2);
  1050. // set the subdirs start dirs
  1051. lg2->GetMakefile()->SetStartDirectory(srcPath);
  1052. lg2->GetMakefile()->SetStartOutputDirectory(binPath);
  1053. if(excludeFromAll)
  1054. {
  1055. lg2->GetMakefile()->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1056. }
  1057. lg2->GetMakefile()->SetPreOrder(preorder);
  1058. if (immediate)
  1059. {
  1060. this->ConfigureSubDirectory(lg2);
  1061. }
  1062. }
  1063. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  1064. {
  1065. // if there is a newline then break it into multiple arguments
  1066. if (!inc)
  1067. {
  1068. return;
  1069. }
  1070. // Don't add an include directory that is already present. Yes,
  1071. // this linear search results in n^2 behavior, but n won't be
  1072. // getting much bigger than 20. We cannot use a set because of
  1073. // order dependency of the include path.
  1074. std::vector<std::string>::iterator i =
  1075. std::find(this->IncludeDirectories.begin(),
  1076. this->IncludeDirectories.end(), inc);
  1077. if(i == this->IncludeDirectories.end())
  1078. {
  1079. if (before)
  1080. {
  1081. // WARNING: this *is* expensive (linear time) since it's a vector
  1082. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  1083. }
  1084. else
  1085. {
  1086. this->IncludeDirectories.push_back(inc);
  1087. }
  1088. }
  1089. else
  1090. {
  1091. if(before)
  1092. {
  1093. // if this before and already in the path then remove it
  1094. this->IncludeDirectories.erase(i);
  1095. // WARNING: this *is* expensive (linear time) since it's a vector
  1096. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  1097. }
  1098. }
  1099. }
  1100. //----------------------------------------------------------------------------
  1101. void cmMakefile::AddSystemIncludeDirectory(const char* dir)
  1102. {
  1103. this->SystemIncludeDirectories.insert(dir);
  1104. }
  1105. //----------------------------------------------------------------------------
  1106. bool cmMakefile::IsSystemIncludeDirectory(const char* dir)
  1107. {
  1108. return (this->SystemIncludeDirectories.find(dir) !=
  1109. this->SystemIncludeDirectories.end());
  1110. }
  1111. void cmMakefile::AddDefinition(const char* name, const char* value)
  1112. {
  1113. if (!value )
  1114. {
  1115. return;
  1116. }
  1117. #ifdef CMAKE_STRICT
  1118. if (this->GetCMakeInstance())
  1119. {
  1120. this->GetCMakeInstance()->
  1121. RecordPropertyAccess(name,cmProperty::VARIABLE);
  1122. }
  1123. #endif
  1124. this->TemporaryDefinitionKey = name;
  1125. this->DefinitionStack.back()[this->TemporaryDefinitionKey] = value;
  1126. #ifdef CMAKE_BUILD_WITH_CMAKE
  1127. cmVariableWatch* vv = this->GetVariableWatch();
  1128. if ( vv )
  1129. {
  1130. vv->VariableAccessed(this->TemporaryDefinitionKey,
  1131. cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1132. value,
  1133. this);
  1134. }
  1135. #endif
  1136. }
  1137. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  1138. const char* doc,
  1139. cmCacheManager::CacheEntryType type)
  1140. {
  1141. const char* val = value;
  1142. cmCacheManager::CacheIterator it =
  1143. this->GetCacheManager()->GetCacheIterator(name);
  1144. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1145. it.Initialized())
  1146. {
  1147. val = it.GetValue();
  1148. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  1149. {
  1150. std::vector<std::string>::size_type cc;
  1151. std::vector<std::string> files;
  1152. std::string nvalue = "";
  1153. cmSystemTools::ExpandListArgument(val, files);
  1154. for ( cc = 0; cc < files.size(); cc ++ )
  1155. {
  1156. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  1157. if ( cc > 0 )
  1158. {
  1159. nvalue += ";";
  1160. }
  1161. nvalue += files[cc];
  1162. }
  1163. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  1164. val = it.GetValue();
  1165. }
  1166. }
  1167. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  1168. // if there was a definition then remove it
  1169. this->DefinitionStack.back().erase( DefinitionMap::key_type(name));
  1170. }
  1171. void cmMakefile::AddDefinition(const char* name, bool value)
  1172. {
  1173. if(value)
  1174. {
  1175. this->DefinitionStack.back()
  1176. .erase( DefinitionMap::key_type(name));
  1177. this->DefinitionStack.back()
  1178. .insert(DefinitionMap::value_type(name, "ON"));
  1179. }
  1180. else
  1181. {
  1182. this->DefinitionStack.back()
  1183. .erase( DefinitionMap::key_type(name));
  1184. this->DefinitionStack.back()
  1185. .insert(DefinitionMap::value_type(name, "OFF"));
  1186. }
  1187. #ifdef CMAKE_BUILD_WITH_CMAKE
  1188. cmVariableWatch* vv = this->GetVariableWatch();
  1189. if ( vv )
  1190. {
  1191. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS,
  1192. value?"ON":"OFF", this);
  1193. }
  1194. #endif
  1195. }
  1196. void cmMakefile::AddCacheDefinition(const char* name,
  1197. bool value,
  1198. const char* doc)
  1199. {
  1200. bool val = value;
  1201. cmCacheManager::CacheIterator it =
  1202. this->GetCacheManager()->GetCacheIterator(name);
  1203. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  1204. it.Initialized())
  1205. {
  1206. val = it.GetValueAsBool();
  1207. }
  1208. this->GetCacheManager()->AddCacheEntry(name, val, doc);
  1209. this->AddDefinition(name, val);
  1210. }
  1211. void cmMakefile::RemoveDefinition(const char* name)
  1212. {
  1213. this->DefinitionStack.back().erase(DefinitionMap::key_type(name));
  1214. #ifdef CMAKE_BUILD_WITH_CMAKE
  1215. cmVariableWatch* vv = this->GetVariableWatch();
  1216. if ( vv )
  1217. {
  1218. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS,
  1219. 0, this);
  1220. }
  1221. #endif
  1222. }
  1223. void cmMakefile::SetProjectName(const char* p)
  1224. {
  1225. this->ProjectName = p;
  1226. }
  1227. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  1228. {
  1229. // for these targets do not add anything
  1230. switch(target.GetType())
  1231. {
  1232. case cmTarget::UTILITY:
  1233. case cmTarget::GLOBAL_TARGET:
  1234. return;
  1235. default:;
  1236. }
  1237. std::vector<std::string>::iterator j;
  1238. for(j = this->LinkDirectories.begin();
  1239. j != this->LinkDirectories.end(); ++j)
  1240. {
  1241. target.AddLinkDirectory(j->c_str());
  1242. }
  1243. target.MergeLinkLibraries( *this, name, this->LinkLibraries );
  1244. }
  1245. void cmMakefile::AddLibrary(const char* lname, cmTarget::TargetType type,
  1246. const std::vector<std::string> &srcs,
  1247. bool excludeFromAll)
  1248. {
  1249. // wrong type ? default to STATIC
  1250. if ( (type != cmTarget::STATIC_LIBRARY)
  1251. && (type != cmTarget::SHARED_LIBRARY)
  1252. && (type != cmTarget::MODULE_LIBRARY))
  1253. {
  1254. type = cmTarget::STATIC_LIBRARY;
  1255. }
  1256. cmTarget* target = this->AddNewTarget(type, lname, false);
  1257. // Clear its dependencies. Otherwise, dependencies might persist
  1258. // over changes in CMakeLists.txt, making the information stale and
  1259. // hence useless.
  1260. target->ClearDependencyInformation( *this, lname );
  1261. if(excludeFromAll)
  1262. {
  1263. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1264. }
  1265. target->AddSources(srcs);
  1266. this->AddGlobalLinkInformation(lname, *target);
  1267. }
  1268. cmTarget* cmMakefile::AddExecutable(const char *exeName,
  1269. const std::vector<std::string> &srcs,
  1270. bool excludeFromAll)
  1271. {
  1272. cmTarget* target = this->AddNewTarget(cmTarget::EXECUTABLE, exeName, false);
  1273. if(excludeFromAll)
  1274. {
  1275. target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
  1276. }
  1277. target->AddSources(srcs);
  1278. this->AddGlobalLinkInformation(exeName, *target);
  1279. return target;
  1280. }
  1281. cmTarget* cmMakefile::AddNewTarget(cmTarget::TargetType type,
  1282. const char* name,
  1283. bool isImported)
  1284. {
  1285. cmTargets::iterator it;
  1286. cmTarget target;
  1287. target.SetType(type, name);
  1288. target.SetMakefile(this);
  1289. if (isImported)
  1290. {
  1291. target.MarkAsImported();
  1292. it=this->ImportedTargets.insert(
  1293. cmTargets::value_type(target.GetName(), target)).first;
  1294. }
  1295. else
  1296. {
  1297. it=this->Targets.insert(
  1298. cmTargets::value_type(target.GetName(), target)).first;
  1299. }
  1300. this->LocalGenerator->GetGlobalGenerator()->AddTarget(*it);
  1301. return &it->second;
  1302. }
  1303. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1304. {
  1305. std::string name = cname;
  1306. std::string out;
  1307. // look through all the source files that have custom commands
  1308. // and see if the custom command has the passed source file as an output
  1309. // keep in mind the possible .rule extension that may be tacked on
  1310. for(std::vector<cmSourceFile*>::const_iterator i =
  1311. this->SourceFiles.begin(); i != this->SourceFiles.end(); ++i)
  1312. {
  1313. // does this source file have a custom command?
  1314. if ((*i)->GetCustomCommand())
  1315. {
  1316. // is the output of the custom command match the source files name
  1317. const std::vector<std::string>& outputs =
  1318. (*i)->GetCustomCommand()->GetOutputs();
  1319. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1320. o != outputs.end(); ++o)
  1321. {
  1322. out = *o;
  1323. std::string::size_type pos = out.rfind(name);
  1324. // If the output matches exactly
  1325. if (pos != out.npos &&
  1326. pos == out.size() - name.size() &&
  1327. (pos ==0 || out[pos-1] == '/'))
  1328. {
  1329. return *i;
  1330. }
  1331. }
  1332. }
  1333. }
  1334. // otherwise return NULL
  1335. return 0;
  1336. }
  1337. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1338. cmSourceGroup* cmMakefile::GetSourceGroup(const std::vector<std::string>&name)
  1339. {
  1340. cmSourceGroup* sg = 0;
  1341. // first look for source group starting with the same as the one we wants
  1342. for (std::vector<cmSourceGroup>::iterator sgIt = this->SourceGroups.begin();
  1343. sgIt != this->SourceGroups.end(); ++sgIt)
  1344. {
  1345. std::string sgName = sgIt->GetName();
  1346. if(sgName == name[0])
  1347. {
  1348. sg = &(*sgIt);
  1349. break;
  1350. }
  1351. }
  1352. if(sg != 0)
  1353. {
  1354. // iterate through its children to find match source group
  1355. for(unsigned int i=1; i<name.size(); ++i)
  1356. {
  1357. sg = sg->lookupChild(name[i].c_str());
  1358. if(sg == 0)
  1359. {
  1360. break;
  1361. }
  1362. }
  1363. }
  1364. return sg;
  1365. }
  1366. void cmMakefile::AddSourceGroup(const char* name,
  1367. const char* regex)
  1368. {
  1369. if (name)
  1370. {
  1371. std::vector<std::string> nameVector;
  1372. nameVector.push_back(name);
  1373. AddSourceGroup(nameVector, regex);
  1374. }
  1375. }
  1376. void cmMakefile::AddSourceGroup(const std::vector<std::string>& name,
  1377. const char* regex)
  1378. {
  1379. cmSourceGroup* sg = 0;
  1380. std::vector<std::string> currentName;
  1381. int i = 0;
  1382. const int lastElement = static_cast<int>(name.size()-1);
  1383. for(i=lastElement; i>=0; --i)
  1384. {
  1385. currentName.assign(name.begin(), name.begin()+i+1);
  1386. sg = this->GetSourceGroup(currentName);
  1387. if(sg != 0)
  1388. {
  1389. break;
  1390. }
  1391. }
  1392. // i now contains the index of the last found component
  1393. if(i==lastElement)
  1394. {
  1395. // group already exists, replace its regular expression
  1396. if ( regex )
  1397. {
  1398. // We only want to set the regular expression. If there are already
  1399. // source files in the group, we don't want to remove them.
  1400. sg->SetGroupRegex(regex);
  1401. }
  1402. return;
  1403. }
  1404. else if(i==-1)
  1405. {
  1406. // group does not exists nor belong to any existing group
  1407. // add its first component
  1408. this->SourceGroups.push_back(cmSourceGroup(name[0].c_str(), regex));
  1409. sg = this->GetSourceGroup(currentName);
  1410. i = 0; // last component found
  1411. }
  1412. // build the whole source group path
  1413. for(++i; i<=lastElement; ++i)
  1414. {
  1415. sg->AddChild(cmSourceGroup(name[i].c_str(), 0));
  1416. sg = sg->lookupChild(name[i].c_str());
  1417. }
  1418. sg->SetGroupRegex(regex);
  1419. }
  1420. #endif
  1421. void cmMakefile::AddExtraDirectory(const char* dir)
  1422. {
  1423. this->AuxSourceDirectories.push_back(dir);
  1424. }
  1425. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1426. // include and library directories.
  1427. void cmMakefile::ExpandVariables()
  1428. {
  1429. // Now expand variables in the include and link strings
  1430. for(std::vector<std::string>::iterator d = this->IncludeDirectories.begin();
  1431. d != this->IncludeDirectories.end(); ++d)
  1432. {
  1433. this->ExpandVariablesInString(*d, true, true);
  1434. }
  1435. for(std::vector<std::string>::iterator d = this->LinkDirectories.begin();
  1436. d != this->LinkDirectories.end(); ++d)
  1437. {
  1438. this->ExpandVariablesInString(*d, true, true);
  1439. }
  1440. for(cmTarget::LinkLibraryVectorType::iterator l =
  1441. this->LinkLibraries.begin();
  1442. l != this->LinkLibraries.end(); ++l)
  1443. {
  1444. this->ExpandVariablesInString(l->first, true, true);
  1445. }
  1446. }
  1447. bool cmMakefile::IsOn(const char* name) const
  1448. {
  1449. const char* value = this->GetDefinition(name);
  1450. return cmSystemTools::IsOn(value);
  1451. }
  1452. bool cmMakefile::IsSet(const char* name) const
  1453. {
  1454. const char* value = this->GetDefinition(name);
  1455. if ( !value )
  1456. {
  1457. return false;
  1458. }
  1459. if ( ! *value )
  1460. {
  1461. return false;
  1462. }
  1463. if ( cmSystemTools::IsNOTFOUND(value) )
  1464. {
  1465. return false;
  1466. }
  1467. return true;
  1468. }
  1469. bool cmMakefile::CanIWriteThisFile(const char* fileName)
  1470. {
  1471. if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
  1472. {
  1473. return true;
  1474. }
  1475. // If we are doing an in-source build, than the test will always fail
  1476. if ( cmSystemTools::SameFile(this->GetHomeDirectory(),
  1477. this->GetHomeOutputDirectory()) )
  1478. {
  1479. if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
  1480. {
  1481. return false;
  1482. }
  1483. return true;
  1484. }
  1485. // Check if this is subdirectory of the source tree but not a
  1486. // subdirectory of a build tree
  1487. if ( cmSystemTools::IsSubDirectory(fileName,
  1488. this->GetHomeDirectory()) &&
  1489. !cmSystemTools::IsSubDirectory(fileName,
  1490. this->GetHomeOutputDirectory()) )
  1491. {
  1492. return false;
  1493. }
  1494. return true;
  1495. }
  1496. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  1497. {
  1498. const char* ret = this->GetDefinition(name);
  1499. if(!ret)
  1500. {
  1501. cmSystemTools::Error("Error required internal CMake variable not "
  1502. "set, cmake may be not be built correctly.\n",
  1503. "Missing variable is:\n",
  1504. name);
  1505. return "";
  1506. }
  1507. return ret;
  1508. }
  1509. bool cmMakefile::IsDefinitionSet(const char* name) const
  1510. {
  1511. const char* def = 0;
  1512. DefinitionMap::const_iterator pos =
  1513. this->DefinitionStack.back().find(name);
  1514. if(pos != this->DefinitionStack.back().end())
  1515. {
  1516. def = (*pos).second.c_str();
  1517. }
  1518. else
  1519. {
  1520. def = this->GetCacheManager()->GetCacheValue(name);
  1521. }
  1522. #ifdef CMAKE_BUILD_WITH_CMAKE
  1523. if(cmVariableWatch* vv = this->GetVariableWatch())
  1524. {
  1525. if(!def)
  1526. {
  1527. vv->VariableAccessed
  1528. (name, cmVariableWatch::UNKNOWN_VARIABLE_DEFINED_ACCESS,
  1529. def, this);
  1530. }
  1531. }
  1532. #endif
  1533. return def?true:false;
  1534. }
  1535. const char* cmMakefile::GetDefinition(const char* name) const
  1536. {
  1537. #ifdef CMAKE_STRICT
  1538. if (this->GetCMakeInstance())
  1539. {
  1540. this->GetCMakeInstance()->
  1541. RecordPropertyAccess(name,cmProperty::VARIABLE);
  1542. }
  1543. #endif
  1544. const char* def = 0;
  1545. DefinitionMap::const_iterator pos =
  1546. this->DefinitionStack.back().find(name);
  1547. if(pos != this->DefinitionStack.back().end())
  1548. {
  1549. def = (*pos).second.c_str();
  1550. }
  1551. else
  1552. {
  1553. def = this->GetCacheManager()->GetCacheValue(name);
  1554. }
  1555. #ifdef CMAKE_BUILD_WITH_CMAKE
  1556. cmVariableWatch* vv = this->GetVariableWatch();
  1557. if ( vv )
  1558. {
  1559. if ( def )
  1560. {
  1561. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS,
  1562. def, this);
  1563. }
  1564. else
  1565. {
  1566. // are unknown access allowed
  1567. DefinitionMap::const_iterator pos2 =
  1568. this->DefinitionStack.back()
  1569. .find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1570. if (pos2 != this->DefinitionStack.back().end() &&
  1571. cmSystemTools::IsOn((*pos2).second.c_str()))
  1572. {
  1573. vv->VariableAccessed(name,
  1574. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  1575. }
  1576. else
  1577. {
  1578. vv->VariableAccessed(name,
  1579. cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS, def, this);
  1580. }
  1581. }
  1582. }
  1583. #endif
  1584. return def;
  1585. }
  1586. const char* cmMakefile::GetSafeDefinition(const char* def) const
  1587. {
  1588. const char* ret = this->GetDefinition(def);
  1589. if(!ret)
  1590. {
  1591. return "";
  1592. }
  1593. return ret;
  1594. }
  1595. std::vector<std::string> cmMakefile
  1596. ::GetDefinitions(int cacheonly /* = 0 */) const
  1597. {
  1598. std::map<cmStdString, int> definitions;
  1599. if ( !cacheonly )
  1600. {
  1601. DefinitionMap::const_iterator it;
  1602. for ( it = this->DefinitionStack.back().begin();
  1603. it != this->DefinitionStack.back().end(); it ++ )
  1604. {
  1605. definitions[it->first] = 1;
  1606. }
  1607. }
  1608. cmCacheManager::CacheIterator cit =
  1609. this->GetCacheManager()->GetCacheIterator();
  1610. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1611. {
  1612. definitions[cit.GetName()] = 1;
  1613. }
  1614. std::vector<std::string> res;
  1615. std::map<cmStdString, int>::iterator fit;
  1616. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1617. {
  1618. res.push_back(fit->first);
  1619. }
  1620. return res;
  1621. }
  1622. const char *cmMakefile::ExpandVariablesInString(std::string& source)
  1623. {
  1624. return this->ExpandVariablesInString(source, false, false);
  1625. }
  1626. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1627. bool escapeQuotes,
  1628. bool noEscapes,
  1629. bool atOnly,
  1630. const char* filename,
  1631. long line,
  1632. bool removeEmpty,
  1633. bool replaceAt)
  1634. {
  1635. if ( source.empty() || source.find_first_of("$@\\") == source.npos)
  1636. {
  1637. return source.c_str();
  1638. }
  1639. // Special-case the @ONLY mode.
  1640. if(atOnly)
  1641. {
  1642. if(!noEscapes || !removeEmpty || !replaceAt)
  1643. {
  1644. // This case should never be called. At-only is for
  1645. // configure-file/string which always does no escapes.
  1646. abort();
  1647. }
  1648. // Store an original copy of the input.
  1649. std::string input = source;
  1650. // Start with empty output.
  1651. source = "";
  1652. // Look for one @VAR@ at a time.
  1653. const char* in = input.c_str();
  1654. while(this->cmAtVarRegex.find(in))
  1655. {
  1656. // Get the range of the string to replace.
  1657. const char* first = in + this->cmAtVarRegex.start();
  1658. const char* last = in + this->cmAtVarRegex.end();
  1659. // Store the unchanged part of the string now.
  1660. source.append(in, first-in);
  1661. // Lookup the definition of VAR.
  1662. std::string var(first+1, last-first-2);
  1663. if(const char* val = this->GetDefinition(var.c_str()))
  1664. {
  1665. // Store the value in the output escaping as requested.
  1666. if(escapeQuotes)
  1667. {
  1668. source.append(cmSystemTools::EscapeQuotes(val));
  1669. }
  1670. else
  1671. {
  1672. source.append(val);
  1673. }
  1674. }
  1675. // Continue looking for @VAR@ further along the string.
  1676. in = last;
  1677. }
  1678. // Append the rest of the unchanged part of the string.
  1679. source.append(in);
  1680. return source.c_str();
  1681. }
  1682. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1683. // with GetDefinition(), if not found in the map, nothing is expanded.
  1684. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1685. // the current environment variables.
  1686. cmCommandArgumentParserHelper parser;
  1687. parser.SetMakefile(this);
  1688. parser.SetLineFile(line, filename);
  1689. parser.SetEscapeQuotes(escapeQuotes);
  1690. parser.SetNoEscapeMode(noEscapes);
  1691. parser.SetReplaceAtSyntax(replaceAt);
  1692. parser.SetRemoveEmpty(removeEmpty);
  1693. int res = parser.ParseString(source.c_str(), 0);
  1694. if ( res )
  1695. {
  1696. source = parser.GetResult();
  1697. }
  1698. else
  1699. {
  1700. cmOStringStream error;
  1701. error << "Syntax error in cmake code at\n"
  1702. << (filename?filename:"(no filename given)")
  1703. << ":" << line << ":\n"
  1704. << parser.GetError() << ", when parsing string \""
  1705. << source.c_str() << "\"";
  1706. const char* versionValue
  1707. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1708. int major = 0;
  1709. int minor = 0;
  1710. if ( versionValue )
  1711. {
  1712. sscanf(versionValue, "%d.%d", &major, &minor);
  1713. }
  1714. if ( major < 2 || major == 2 && minor < 1 )
  1715. {
  1716. cmSystemTools::Error(error.str().c_str());
  1717. cmSystemTools::SetFatalErrorOccured();
  1718. return source.c_str();
  1719. }
  1720. else
  1721. {
  1722. cmSystemTools::Message(error.str().c_str());
  1723. }
  1724. }
  1725. return source.c_str();
  1726. }
  1727. void cmMakefile::RemoveVariablesInString(std::string& source,
  1728. bool atOnly) const
  1729. {
  1730. if(!atOnly)
  1731. {
  1732. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  1733. while (var.find(source))
  1734. {
  1735. source.erase(var.start(),var.end() - var.start());
  1736. }
  1737. }
  1738. if(!atOnly)
  1739. {
  1740. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1741. while (varb.find(source))
  1742. {
  1743. source.erase(varb.start(),varb.end() - varb.start());
  1744. }
  1745. }
  1746. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  1747. while (var2.find(source))
  1748. {
  1749. source.erase(var2.start(),var2.end() - var2.start());
  1750. }
  1751. }
  1752. /**
  1753. * Add the default definitions to the makefile. These values must not
  1754. * be dependent on anything that isn't known when this cmMakefile instance
  1755. * is constructed.
  1756. */
  1757. void cmMakefile::AddDefaultDefinitions()
  1758. {
  1759. /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
  1760. With CMake must separate between target and host platform. In most cases
  1761. the tests for WIN32, UNIX and APPLE will be for the target system, so an
  1762. additional set of variables for the host system is required ->
  1763. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
  1764. WIN32, UNIX and APPLE are now set in the platform files in
  1765. Modules/Platforms/.
  1766. To keep cmake scripts (-P) and custom language and compiler modules
  1767. working, these variables are still also set here in this place, but they
  1768. will be reset in CMakeSystemSpecificInformation.cmake before the platform
  1769. files are executed. */
  1770. #if defined(_WIN32) || defined(__CYGWIN__)
  1771. this->AddDefinition("WIN32", "1");
  1772. this->AddDefinition("CMAKE_HOST_WIN32", "1");
  1773. #else
  1774. this->AddDefinition("UNIX", "1");
  1775. this->AddDefinition("CMAKE_HOST_UNIX", "1");
  1776. #endif
  1777. // Cygwin is more like unix so enable the unix commands
  1778. #if defined(__CYGWIN__)
  1779. this->AddDefinition("UNIX", "1");
  1780. this->AddDefinition("CMAKE_HOST_UNIX", "1");
  1781. #endif
  1782. #if defined(__APPLE__)
  1783. this->AddDefinition("APPLE", "1");
  1784. this->AddDefinition("CMAKE_HOST_APPLE", "1");
  1785. #endif
  1786. char temp[1024];
  1787. sprintf(temp, "%d", cmVersion::GetMinorVersion());
  1788. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1789. sprintf(temp, "%d", cmVersion::GetMajorVersion());
  1790. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1791. sprintf(temp, "%d", cmVersion::GetPatchVersion());
  1792. this->AddDefinition("CMAKE_PATCH_VERSION", temp);
  1793. this->AddDefinition("CMAKE_FILES_DIRECTORY",
  1794. cmake::GetCMakeFilesDirectory());
  1795. }
  1796. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1797. /**
  1798. * Find a source group whose regular expression matches the filename
  1799. * part of the given source name. Search backward through the list of
  1800. * source groups, and take the first matching group found. This way
  1801. * non-inherited SOURCE_GROUP commands will have precedence over
  1802. * inherited ones.
  1803. */
  1804. cmSourceGroup&
  1805. cmMakefile::FindSourceGroup(const char* source,
  1806. std::vector<cmSourceGroup> &groups)
  1807. {
  1808. // First search for a group that lists the file explicitly.
  1809. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1810. sg != groups.rend(); ++sg)
  1811. {
  1812. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  1813. if(result)
  1814. {
  1815. return *result;
  1816. }
  1817. }
  1818. // Now search for a group whose regex matches the file.
  1819. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1820. sg != groups.rend(); ++sg)
  1821. {
  1822. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  1823. if(result)
  1824. {
  1825. return *result;
  1826. }
  1827. }
  1828. // Shouldn't get here, but just in case, return the default group.
  1829. return groups.front();
  1830. }
  1831. #endif
  1832. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1833. {
  1834. // if there are no blockers get out of here
  1835. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  1836. {
  1837. return false;
  1838. }
  1839. // loop over all function blockers to see if any block this command
  1840. // evaluate in reverse, this is critical for balanced IF statements etc
  1841. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1842. for (pos = this->FunctionBlockers.rbegin();
  1843. pos != this->FunctionBlockers.rend(); ++pos)
  1844. {
  1845. if((*pos)->IsFunctionBlocked(lff, *this))
  1846. {
  1847. return true;
  1848. }
  1849. }
  1850. return false;
  1851. }
  1852. void cmMakefile::ExpandArguments(
  1853. std::vector<cmListFileArgument> const& inArgs,
  1854. std::vector<std::string>& outArgs)
  1855. {
  1856. std::vector<cmListFileArgument>::const_iterator i;
  1857. std::string value;
  1858. outArgs.reserve(inArgs.size());
  1859. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1860. {
  1861. // Expand the variables in the argument.
  1862. value = i->Value;
  1863. this->ExpandVariablesInString(value, false, false, false,
  1864. i->FilePath, i->Line,
  1865. false, true);
  1866. // If the argument is quoted, it should be one argument.
  1867. // Otherwise, it may be a list of arguments.
  1868. if(i->Quoted)
  1869. {
  1870. outArgs.push_back(value);
  1871. }
  1872. else
  1873. {
  1874. cmSystemTools::ExpandListArgument(value, outArgs);
  1875. }
  1876. }
  1877. }
  1878. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1879. {
  1880. // loop over all function blockers to see if any block this command
  1881. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1882. for (pos = this->FunctionBlockers.rbegin();
  1883. pos != this->FunctionBlockers.rend(); ++pos)
  1884. {
  1885. if ((*pos)->ShouldRemove(lff, *this))
  1886. {
  1887. cmFunctionBlocker* b = *pos;
  1888. this->FunctionBlockers.remove(b);
  1889. delete b;
  1890. break;
  1891. }
  1892. }
  1893. return;
  1894. }
  1895. void cmMakefile::SetHomeDirectory(const char* dir)
  1896. {
  1897. this->cmHomeDirectory = dir;
  1898. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1899. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1900. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  1901. {
  1902. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  1903. }
  1904. }
  1905. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1906. {
  1907. this->HomeOutputDirectory = lib;
  1908. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1909. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1910. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  1911. {
  1912. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR",
  1913. this->GetHomeOutputDirectory());
  1914. }
  1915. }
  1916. /**
  1917. * Register the given cmData instance with its own name.
  1918. */
  1919. void cmMakefile::RegisterData(cmData* data)
  1920. {
  1921. std::string name = data->GetName();
  1922. DataMapType::const_iterator d = this->DataMap.find(name);
  1923. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1924. {
  1925. delete d->second;
  1926. }
  1927. this->DataMap[name] = data;
  1928. }
  1929. /**
  1930. * Register the given cmData instance with the given name. This can be used
  1931. * to register a NULL pointer.
  1932. */
  1933. void cmMakefile::RegisterData(const char* name, cmData* data)
  1934. {
  1935. DataMapType::const_iterator d = this->DataMap.find(name);
  1936. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1937. {
  1938. delete d->second;
  1939. }
  1940. this->DataMap[name] = data;
  1941. }
  1942. /**
  1943. * Lookup a cmData instance previously registered with the given name. If
  1944. * the instance cannot be found, return NULL.
  1945. */
  1946. cmData* cmMakefile::LookupData(const char* name) const
  1947. {
  1948. DataMapType::const_iterator d = this->DataMap.find(name);
  1949. if(d != this->DataMap.end())
  1950. {
  1951. return d->second;
  1952. }
  1953. else
  1954. {
  1955. return 0;
  1956. }
  1957. }
  1958. //----------------------------------------------------------------------------
  1959. cmSourceFile* cmMakefile::GetSource(const char* sourceName)
  1960. {
  1961. cmSourceFileLocation sfl(this, sourceName);
  1962. for(std::vector<cmSourceFile*>::const_iterator
  1963. sfi = this->SourceFiles.begin();
  1964. sfi != this->SourceFiles.end(); ++sfi)
  1965. {
  1966. cmSourceFile* sf = *sfi;
  1967. if(sf->Matches(sfl))
  1968. {
  1969. return sf;
  1970. }
  1971. }
  1972. return 0;
  1973. }
  1974. //----------------------------------------------------------------------------
  1975. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  1976. bool generated)
  1977. {
  1978. if(cmSourceFile* esf = this->GetSource(sourceName))
  1979. {
  1980. return esf;
  1981. }
  1982. else
  1983. {
  1984. cmSourceFile* sf = new cmSourceFile(this, sourceName);
  1985. if(generated)
  1986. {
  1987. sf->SetProperty("GENERATED", "1");
  1988. }
  1989. this->SourceFiles.push_back(sf);
  1990. return sf;
  1991. }
  1992. }
  1993. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang,
  1994. bool optional)
  1995. {
  1996. this->AddDefinition("CMAKE_CFG_INTDIR",
  1997. this->LocalGenerator->GetGlobalGenerator()->GetCMakeCFGInitDirectory());
  1998. this->LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this,
  1999. optional);
  2000. }
  2001. void cmMakefile::ExpandSourceListArguments(
  2002. std::vector<std::string> const& arguments,
  2003. std::vector<std::string>& newargs, unsigned int /* start */)
  2004. {
  2005. // now expand the args
  2006. unsigned int i;
  2007. for(i = 0; i < arguments.size(); ++i)
  2008. {
  2009. // List expansion will have been done already.
  2010. newargs.push_back(arguments[i]);
  2011. }
  2012. }
  2013. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  2014. const char *projectName, const char *targetName,
  2015. const std::vector<std::string> *cmakeArgs,
  2016. std::string *output)
  2017. {
  2018. // does the binary directory exist ? If not create it...
  2019. if (!cmSystemTools::FileIsDirectory(bindir))
  2020. {
  2021. cmSystemTools::MakeDirectory(bindir);
  2022. }
  2023. // change to the tests directory and run cmake
  2024. // use the cmake object instead of calling cmake
  2025. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2026. cmSystemTools::ChangeDirectory(bindir);
  2027. // make sure the same generator is used
  2028. // use this program as the cmake to be run, it should not
  2029. // be run that way but the cmake object requires a vailid path
  2030. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  2031. cmake cm;
  2032. cm.SetIsInTryCompile(true);
  2033. cmGlobalGenerator *gg = cm.CreateGlobalGenerator
  2034. (this->LocalGenerator->GetGlobalGenerator()->GetName());
  2035. if (!gg)
  2036. {
  2037. cmSystemTools::Error(
  2038. "Internal CMake error, TryCompile bad GlobalGenerator");
  2039. // return to the original directory
  2040. cmSystemTools::ChangeDirectory(cwd.c_str());
  2041. return 1;
  2042. }
  2043. cm.SetGlobalGenerator(gg);
  2044. // do a configure
  2045. cm.SetHomeDirectory(srcdir);
  2046. cm.SetHomeOutputDirectory(bindir);
  2047. cm.SetStartDirectory(srcdir);
  2048. cm.SetStartOutputDirectory(bindir);
  2049. cm.SetCMakeCommand(cmakeCommand.c_str());
  2050. cm.LoadCache();
  2051. // if cmake args were provided then pass them in
  2052. if (cmakeArgs)
  2053. {
  2054. cm.SetCacheArgs(*cmakeArgs);
  2055. }
  2056. // to save time we pass the EnableLanguage info directly
  2057. gg->EnableLanguagesFromGenerator
  2058. (this->LocalGenerator->GetGlobalGenerator());
  2059. if (cm.Configure() != 0)
  2060. {
  2061. cmSystemTools::Error(
  2062. "Internal CMake error, TryCompile configure of cmake failed");
  2063. // return to the original directory
  2064. cmSystemTools::ChangeDirectory(cwd.c_str());
  2065. return 1;
  2066. }
  2067. if (cm.Generate() != 0)
  2068. {
  2069. cmSystemTools::Error(
  2070. "Internal CMake error, TryCompile generation of cmake failed");
  2071. // return to the original directory
  2072. cmSystemTools::ChangeDirectory(cwd.c_str());
  2073. return 1;
  2074. }
  2075. // finally call the generator to actually build the resulting project
  2076. int ret =
  2077. this->LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  2078. projectName,
  2079. targetName,
  2080. output,
  2081. this);
  2082. cmSystemTools::ChangeDirectory(cwd.c_str());
  2083. return ret;
  2084. }
  2085. cmake *cmMakefile::GetCMakeInstance() const
  2086. {
  2087. if ( this->LocalGenerator && this->LocalGenerator->GetGlobalGenerator() )
  2088. {
  2089. return this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  2090. }
  2091. return 0;
  2092. }
  2093. #ifdef CMAKE_BUILD_WITH_CMAKE
  2094. cmVariableWatch *cmMakefile::GetVariableWatch() const
  2095. {
  2096. if ( this->GetCMakeInstance() &&
  2097. this->GetCMakeInstance()->GetVariableWatch() )
  2098. {
  2099. return this->GetCMakeInstance()->GetVariableWatch();
  2100. }
  2101. return 0;
  2102. }
  2103. #endif
  2104. void cmMakefile::AddMacro(const char* name, const char* signature)
  2105. {
  2106. if ( !name || !signature )
  2107. {
  2108. return;
  2109. }
  2110. this->MacrosMap[name] = signature;
  2111. }
  2112. void cmMakefile::GetListOfMacros(std::string& macros)
  2113. {
  2114. StringStringMap::iterator it;
  2115. macros = "";
  2116. int cc = 0;
  2117. for ( it = this->MacrosMap.begin(); it != this->MacrosMap.end(); ++it )
  2118. {
  2119. if ( cc > 0 )
  2120. {
  2121. macros += ";";
  2122. }
  2123. macros += it->first;
  2124. cc ++;
  2125. }
  2126. }
  2127. cmCacheManager *cmMakefile::GetCacheManager() const
  2128. {
  2129. return this->GetCMakeInstance()->GetCacheManager();
  2130. }
  2131. void cmMakefile::DisplayStatus(const char* message, float s)
  2132. {
  2133. this->GetLocalGenerator()->GetGlobalGenerator()
  2134. ->GetCMakeInstance()->UpdateProgress(message, s);
  2135. }
  2136. std::string cmMakefile::GetModulesFile(const char* filename)
  2137. {
  2138. std::vector<std::string> modulePath;
  2139. const char* def = this->GetDefinition("CMAKE_MODULE_PATH");
  2140. if(def)
  2141. {
  2142. cmSystemTools::ExpandListArgument(def, modulePath);
  2143. }
  2144. // Also search in the standard modules location.
  2145. def = this->GetDefinition("CMAKE_ROOT");
  2146. if(def)
  2147. {
  2148. std::string rootModules = def;
  2149. rootModules += "/Modules";
  2150. modulePath.push_back(rootModules);
  2151. }
  2152. //std::string Look through the possible module directories.
  2153. for(std::vector<std::string>::iterator i = modulePath.begin();
  2154. i != modulePath.end(); ++i)
  2155. {
  2156. std::string itempl = *i;
  2157. cmSystemTools::ConvertToUnixSlashes(itempl);
  2158. itempl += "/";
  2159. itempl += filename;
  2160. if(cmSystemTools::FileExists(itempl.c_str()))
  2161. {
  2162. return itempl;
  2163. }
  2164. }
  2165. return "";
  2166. }
  2167. void cmMakefile::ConfigureString(const std::string& input,
  2168. std::string& output, bool atOnly,
  2169. bool escapeQuotes)
  2170. {
  2171. // Split input to handle one line at a time.
  2172. std::string::const_iterator lineStart = input.begin();
  2173. while(lineStart != input.end())
  2174. {
  2175. // Find the end of this line.
  2176. std::string::const_iterator lineEnd = lineStart;
  2177. while(lineEnd != input.end() && *lineEnd != '\n')
  2178. {
  2179. ++lineEnd;
  2180. }
  2181. // Copy the line.
  2182. std::string line(lineStart, lineEnd);
  2183. // Skip the newline character.
  2184. bool haveNewline = (lineEnd != input.end());
  2185. if(haveNewline)
  2186. {
  2187. ++lineEnd;
  2188. }
  2189. // Replace #cmakedefine instances.
  2190. if(this->cmDefineRegex.find(line))
  2191. {
  2192. const char* def =
  2193. this->GetDefinition(this->cmDefineRegex.match(1).c_str());
  2194. if(!cmSystemTools::IsOff(def))
  2195. {
  2196. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  2197. output += line;
  2198. }
  2199. else
  2200. {
  2201. output += "/* #undef ";
  2202. output += this->cmDefineRegex.match(1);
  2203. output += " */";
  2204. }
  2205. }
  2206. else if(this->cmDefine01Regex.find(line))
  2207. {
  2208. const char* def =
  2209. this->GetDefinition(this->cmDefine01Regex.match(1).c_str());
  2210. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  2211. output += line;
  2212. if(!cmSystemTools::IsOff(def))
  2213. {
  2214. output += " 1";
  2215. }
  2216. else
  2217. {
  2218. output += " 0";
  2219. }
  2220. }
  2221. else
  2222. {
  2223. output += line;
  2224. }
  2225. if(haveNewline)
  2226. {
  2227. output += "\n";
  2228. }
  2229. // Move to the next line.
  2230. lineStart = lineEnd;
  2231. }
  2232. // Perform variable replacements.
  2233. this->ExpandVariablesInString(output, escapeQuotes, true,
  2234. atOnly, 0, -1, true);
  2235. }
  2236. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  2237. bool copyonly, bool atOnly, bool escapeQuotes)
  2238. {
  2239. int res = 1;
  2240. if ( !this->CanIWriteThisFile(outfile) )
  2241. {
  2242. cmSystemTools::Error("Attempt to write file: ",
  2243. outfile, " into a source directory.");
  2244. return 0;
  2245. }
  2246. if ( !cmSystemTools::FileExists(infile) )
  2247. {
  2248. cmSystemTools::Error("File ", infile, " does not exist.");
  2249. return 0;
  2250. }
  2251. std::string soutfile = outfile;
  2252. std::string sinfile = infile;
  2253. this->AddCMakeDependFile(infile);
  2254. cmSystemTools::ConvertToUnixSlashes(soutfile);
  2255. mode_t perm = 0;
  2256. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  2257. std::string::size_type pos = soutfile.rfind('/');
  2258. if(pos != std::string::npos)
  2259. {
  2260. std::string path = soutfile.substr(0, pos);
  2261. cmSystemTools::MakeDirectory(path.c_str());
  2262. }
  2263. if(copyonly)
  2264. {
  2265. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  2266. soutfile.c_str()))
  2267. {
  2268. return 0;
  2269. }
  2270. }
  2271. else
  2272. {
  2273. std::string tempOutputFile = soutfile;
  2274. tempOutputFile += ".tmp";
  2275. std::ofstream fout(tempOutputFile.c_str());
  2276. if(!fout)
  2277. {
  2278. cmSystemTools::Error(
  2279. "Could not open file for write in copy operation ",
  2280. tempOutputFile.c_str());
  2281. cmSystemTools::ReportLastSystemError("");
  2282. return 0;
  2283. }
  2284. std::ifstream fin(sinfile.c_str());
  2285. if(!fin)
  2286. {
  2287. cmSystemTools::Error("Could not open file for read in copy operation ",
  2288. sinfile.c_str());
  2289. return 0;
  2290. }
  2291. // now copy input to output and expand variables in the
  2292. // input file at the same time
  2293. std::string inLine;
  2294. std::string outLine;
  2295. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  2296. {
  2297. outLine = "";
  2298. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  2299. fout << outLine.c_str() << "\n";
  2300. }
  2301. // close the files before attempting to copy
  2302. fin.close();
  2303. fout.close();
  2304. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  2305. soutfile.c_str()) )
  2306. {
  2307. res = 0;
  2308. }
  2309. else
  2310. {
  2311. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  2312. }
  2313. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  2314. }
  2315. return res;
  2316. }
  2317. void cmMakefile::AddWrittenFile(const char* file)
  2318. { this->GetCMakeInstance()->AddWrittenFile(file); }
  2319. bool cmMakefile::HasWrittenFile(const char* file)
  2320. { return this->GetCMakeInstance()->HasWrittenFile(file); }
  2321. bool cmMakefile::CheckInfiniteLoops()
  2322. {
  2323. std::vector<std::string>::iterator it;
  2324. for ( it = this->ListFiles.begin();
  2325. it != this->ListFiles.end();
  2326. ++ it )
  2327. {
  2328. if ( this->HasWrittenFile(it->c_str()) )
  2329. {
  2330. cmOStringStream str;
  2331. str << "File " << it->c_str() <<
  2332. " is written by WRITE_FILE (or FILE WRITE) command and should "
  2333. "not be used as input to CMake. Please use CONFIGURE_FILE to "
  2334. "be safe. Refer to the note next to FILE WRITE command.";
  2335. cmSystemTools::Error(str.str().c_str());
  2336. return false;
  2337. }
  2338. }
  2339. return true;
  2340. }
  2341. void cmMakefile::SetProperty(const char* prop, const char* value)
  2342. {
  2343. if (!prop)
  2344. {
  2345. return;
  2346. }
  2347. // handle special props
  2348. std::string propname = prop;
  2349. if ( propname == "INCLUDE_DIRECTORIES" )
  2350. {
  2351. std::vector<std::string> varArgsExpanded;
  2352. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  2353. this->SetIncludeDirectories(varArgsExpanded);
  2354. return;
  2355. }
  2356. if ( propname == "LINK_DIRECTORIES" )
  2357. {
  2358. std::vector<std::string> varArgsExpanded;
  2359. cmSystemTools::ExpandListArgument(value, varArgsExpanded);
  2360. this->SetLinkDirectories(varArgsExpanded);
  2361. return;
  2362. }
  2363. if ( propname == "INCLUDE_REGULAR_EXPRESSION" )
  2364. {
  2365. this->SetIncludeRegularExpression(value);
  2366. return;
  2367. }
  2368. if ( propname == "ADDITIONAL_MAKE_CLEAN_FILES" )
  2369. {
  2370. // This property is not inherrited
  2371. if ( strcmp(this->GetCurrentDirectory(),
  2372. this->GetStartDirectory()) != 0 )
  2373. {
  2374. return;
  2375. }
  2376. }
  2377. this->Properties.SetProperty(prop,value,cmProperty::DIRECTORY);
  2378. }
  2379. const char *cmMakefile::GetPropertyOrDefinition(const char* prop)
  2380. {
  2381. const char *ret = this->GetProperty(prop, cmProperty::DIRECTORY);
  2382. if (!ret)
  2383. {
  2384. ret = this->GetDefinition(prop);
  2385. }
  2386. return ret;
  2387. }
  2388. const char *cmMakefile::GetProperty(const char* prop)
  2389. {
  2390. return this->GetProperty(prop, cmProperty::DIRECTORY);
  2391. }
  2392. const char *cmMakefile::GetProperty(const char* prop,
  2393. cmProperty::ScopeType scope)
  2394. {
  2395. // watch for specific properties
  2396. static std::string output;
  2397. output = "";
  2398. if (!strcmp("PARENT_DIRECTORY",prop))
  2399. {
  2400. output = this->LocalGenerator->GetParent()
  2401. ->GetMakefile()->GetStartDirectory();
  2402. return output.c_str();
  2403. }
  2404. else if (!strcmp("INCLUDE_REGULAR_EXPRESSION",prop) )
  2405. {
  2406. output = this->GetIncludeRegularExpression();
  2407. return output.c_str();
  2408. }
  2409. else if (!strcmp("LISTFILE_STACK",prop))
  2410. {
  2411. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  2412. i != this->ListFileStack.end(); ++i)
  2413. {
  2414. if (i != this->ListFileStack.begin())
  2415. {
  2416. output += ";";
  2417. }
  2418. output += *i;
  2419. }
  2420. return output.c_str();
  2421. }
  2422. else if (!strcmp("VARIABLES",prop) || !strcmp("CACHE_VARIABLES",prop))
  2423. {
  2424. int cacheonly = 0;
  2425. if ( !strcmp("CACHE_VARIABLES",prop) )
  2426. {
  2427. cacheonly = 1;
  2428. }
  2429. std::vector<std::string> vars = this->GetDefinitions(cacheonly);
  2430. for (unsigned int cc = 0; cc < vars.size(); cc ++ )
  2431. {
  2432. if ( cc > 0 )
  2433. {
  2434. output += ";";
  2435. }
  2436. output += vars[cc];
  2437. }
  2438. return output.c_str();
  2439. }
  2440. else if (!strcmp("MACROS",prop))
  2441. {
  2442. this->GetListOfMacros(output);
  2443. return output.c_str();
  2444. }
  2445. else if (!strcmp("DEFINITIONS",prop))
  2446. {
  2447. output = this->GetDefineFlags();
  2448. return output.c_str();
  2449. }
  2450. else if (!strcmp("INCLUDE_DIRECTORIES",prop) )
  2451. {
  2452. cmOStringStream str;
  2453. for (std::vector<std::string>::const_iterator
  2454. it = this->GetIncludeDirectories().begin();
  2455. it != this->GetIncludeDirectories().end();
  2456. ++ it )
  2457. {
  2458. if ( it != this->GetIncludeDirectories().begin())
  2459. {
  2460. str << ";";
  2461. }
  2462. str << it->c_str();
  2463. }
  2464. output = str.str();
  2465. return output.c_str();
  2466. }
  2467. else if (!strcmp("LINK_DIRECTORIES",prop))
  2468. {
  2469. cmOStringStream str;
  2470. for (std::vector<std::string>::const_iterator
  2471. it = this->GetLinkDirectories().begin();
  2472. it != this->GetLinkDirectories().end();
  2473. ++ it )
  2474. {
  2475. if ( it != this->GetLinkDirectories().begin())
  2476. {
  2477. str << ";";
  2478. }
  2479. str << it->c_str();
  2480. }
  2481. output = str.str();
  2482. return output.c_str();
  2483. }
  2484. bool chain = false;
  2485. const char *retVal =
  2486. this->Properties.GetPropertyValue(prop, scope, chain);
  2487. if (chain)
  2488. {
  2489. if(this->LocalGenerator->GetParent())
  2490. {
  2491. return this->LocalGenerator->GetParent()->GetMakefile()->
  2492. GetProperty(prop, scope);
  2493. }
  2494. return this->GetCMakeInstance()->GetProperty(prop,scope);
  2495. }
  2496. return retVal;
  2497. }
  2498. bool cmMakefile::GetPropertyAsBool(const char* prop)
  2499. {
  2500. return cmSystemTools::IsOn(this->GetProperty(prop));
  2501. }
  2502. cmTarget* cmMakefile::FindTarget(const char* name, bool useImportedTargets)
  2503. {
  2504. cmTargets& tgts = this->GetTargets();
  2505. cmTargets::iterator i = tgts.find ( name );
  2506. if ( i != tgts.end() )
  2507. {
  2508. return &i->second;
  2509. }
  2510. if (useImportedTargets)
  2511. {
  2512. cmTargets::iterator impTarget = this->ImportedTargets.find(name);
  2513. if (impTarget != this->ImportedTargets.end())
  2514. {
  2515. return &impTarget->second;
  2516. }
  2517. }
  2518. return 0;
  2519. }
  2520. cmTest* cmMakefile::CreateTest(const char* testName)
  2521. {
  2522. if ( !testName )
  2523. {
  2524. return 0;
  2525. }
  2526. cmTest* test = this->GetTest(testName);
  2527. if ( test )
  2528. {
  2529. return test;
  2530. }
  2531. test = new cmTest;
  2532. test->SetName(testName);
  2533. test->SetMakefile(this);
  2534. this->Tests.push_back(test);
  2535. return test;
  2536. }
  2537. cmTest* cmMakefile::GetTest(const char* testName) const
  2538. {
  2539. if ( !testName )
  2540. {
  2541. return 0;
  2542. }
  2543. std::vector<cmTest*>::const_iterator it;
  2544. for ( it = this->Tests.begin(); it != this->Tests.end(); ++ it )
  2545. {
  2546. if ( strcmp((*it)->GetName(), testName) == 0 )
  2547. {
  2548. return *it;
  2549. }
  2550. }
  2551. return 0;
  2552. }
  2553. const std::vector<cmTest*> *cmMakefile::GetTests() const
  2554. {
  2555. return &this->Tests;
  2556. }
  2557. std::vector<cmTest*> *cmMakefile::GetTests()
  2558. {
  2559. return &this->Tests;
  2560. }
  2561. std::string cmMakefile::GetListFileStack()
  2562. {
  2563. cmOStringStream tmp;
  2564. size_t depth = this->ListFileStack.size();
  2565. if (depth > 0)
  2566. {
  2567. std::deque<cmStdString>::iterator it = this->ListFileStack.end();
  2568. do
  2569. {
  2570. if (depth != this->ListFileStack.size())
  2571. {
  2572. tmp << "\n ";
  2573. }
  2574. --it;
  2575. tmp << "[";
  2576. tmp << depth;
  2577. tmp << "]\t";
  2578. tmp << *it;
  2579. depth--;
  2580. }
  2581. while (it != this->ListFileStack.begin());
  2582. }
  2583. return tmp.str();
  2584. }
  2585. void cmMakefile::PushScope()
  2586. {
  2587. this->DefinitionStack.push_back(this->DefinitionStack.back());
  2588. }
  2589. void cmMakefile::PopScope()
  2590. {
  2591. this->DefinitionStack.pop_back();
  2592. }
  2593. void cmMakefile::RaiseScope(const char *var)
  2594. {
  2595. const char *varDef = this->GetDefinition(var);
  2596. // multiple scopes in this directory?
  2597. if (this->DefinitionStack.size() > 1)
  2598. {
  2599. if(varDef)
  2600. {
  2601. this->DefinitionStack[this->DefinitionStack.size()-2][var] = varDef;
  2602. }
  2603. else
  2604. {
  2605. this->DefinitionStack[this->DefinitionStack.size()-2].erase(var);
  2606. }
  2607. }
  2608. // otherwise do the parent
  2609. else
  2610. {
  2611. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  2612. if (parent)
  2613. {
  2614. if(varDef)
  2615. {
  2616. parent->AddDefinition(var,varDef);
  2617. }
  2618. else
  2619. {
  2620. parent->RemoveDefinition(var);
  2621. }
  2622. }
  2623. }
  2624. }
  2625. // define properties
  2626. void cmMakefile::DefineProperties(cmake *cm)
  2627. {
  2628. cm->DefineProperty
  2629. ("ADDITIONAL_MAKE_CLEAN_FILES", cmProperty::DIRECTORY,
  2630. "Addditional files to clean during the make clean stage.",
  2631. "A list of files that will be cleaned as a part of the "
  2632. "\"make clean\" stage. ");
  2633. cm->DefineProperty
  2634. ("CLEAN_NO_CUSTOM", cmProperty::DIRECTORY,
  2635. "Should the output of custom commands be left.",
  2636. "If this is true then the outputs of custom commands for this "
  2637. "directory will not be removed during the \"make clean\" stage. ");
  2638. cm->DefineProperty
  2639. ("CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS", cmProperty::DIRECTORY,
  2640. "Allow loops to have non-matching closing statements.",
  2641. "If this is set then the closing statement of control "
  2642. "structures in CMake will not require an exact match to the "
  2643. "opening statement. For example IF(foo) will not require "
  2644. "ENDIF(foo) but simple ENDIF() will work.",
  2645. true);
  2646. cm->DefineProperty
  2647. ("LISTFILE_STACK", cmProperty::DIRECTORY,
  2648. "The current stack of listfiles being processed.",
  2649. "This property is mainly useful when trying to debug errors "
  2650. "in your CMake scripts. It returns a list of what list files "
  2651. "are currently being processed, in order. So if one listfile "
  2652. "does an INCLUDE command then that is effectively pushing "
  2653. "the included listfile onto the stack.");
  2654. cm->DefineProperty
  2655. ("TEST_INCLUDE_FILE", cmProperty::DIRECTORY,
  2656. "A cmake file that will be included when ctest is run.",
  2657. "If you specify TEST_INCLUDE_FILE, that file will be "
  2658. "included and processed when ctest is run on the directory.");
  2659. cm->DefineProperty
  2660. ("EXCLUDE_FROM_ALL", cmProperty::DIRECTORY,
  2661. "Exclude the directory from the all target of its parent.",
  2662. "A property on a directory that indicates if its targets are excluded "
  2663. "from the default build target. If it is not, then with a Makefile "
  2664. "for example typing make will cause the targets to be built. "
  2665. "The same concept applies to the default build of other generators.",
  2666. false);
  2667. }