cmMakefile.cxx 80 KB

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