cmMakefile.cxx 74 KB

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