cmMakefile.cxx 81 KB

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