cmMakefile.cxx 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616
  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. this->Targets.insert(cmTargets::value_type(utilityName, target));
  711. }
  712. void cmMakefile::AddDefineFlag(const char* flag)
  713. {
  714. this->DefineFlags += " ";
  715. this->DefineFlags += flag;
  716. }
  717. void cmMakefile::RemoveDefineFlag(const char* flag)
  718. {
  719. cmSystemTools::ReplaceString(this->DefineFlags, flag, " ");
  720. }
  721. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  722. {
  723. this->LinkLibraries.push_back(
  724. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  725. }
  726. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  727. const char* lib,
  728. cmTarget::LinkLibraryType llt)
  729. {
  730. cmTargets::iterator i = this->Targets.find(target);
  731. if ( i != this->Targets.end())
  732. {
  733. i->second.AddLinkLibrary( *this, target, lib, llt );
  734. }
  735. else
  736. {
  737. cmOStringStream e;
  738. e << "Attempt to add link library \""
  739. << lib << "\" to target \""
  740. << target << "\" which is not built by this project.";
  741. cmSystemTools::Error(e.str().c_str());
  742. }
  743. }
  744. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  745. const char* d)
  746. {
  747. cmTargets::iterator i = this->Targets.find(target);
  748. if ( i != this->Targets.end())
  749. {
  750. i->second.AddLinkDirectory( d );
  751. }
  752. else
  753. {
  754. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  755. target, " for directory ", d);
  756. }
  757. }
  758. void cmMakefile::AddLinkLibrary(const char* lib)
  759. {
  760. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  761. }
  762. void cmMakefile::AddLinkDirectory(const char* dir)
  763. {
  764. // Don't add a link directory that is already present. Yes, this
  765. // linear search results in n^2 behavior, but n won't be getting
  766. // much bigger than 20. We cannot use a set because of order
  767. // dependency of the link search path.
  768. // remove trailing slashes
  769. if(dir && dir[strlen(dir)-1] == '/')
  770. {
  771. std::string newdir = dir;
  772. newdir = newdir.substr(0, newdir.size()-1);
  773. if(std::find(this->LinkDirectories.begin(),
  774. this->LinkDirectories.end(), newdir.c_str()) == this->LinkDirectories.end())
  775. {
  776. this->LinkDirectories.push_back(newdir);
  777. }
  778. }
  779. else
  780. {
  781. if(std::find(this->LinkDirectories.begin(),
  782. this->LinkDirectories.end(), dir) == this->LinkDirectories.end())
  783. {
  784. this->LinkDirectories.push_back(dir);
  785. }
  786. }
  787. }
  788. void cmMakefile::InitializeFromParent()
  789. {
  790. cmMakefile *parent = this->LocalGenerator->GetParent()->GetMakefile();
  791. // copy the definitions
  792. this->Definitions = parent->Definitions;
  793. // copy include paths
  794. this->IncludeDirectories = parent->IncludeDirectories;
  795. // define flags
  796. this->DefineFlags = parent->DefineFlags;
  797. // link libraries
  798. this->LinkLibraries = parent->LinkLibraries;
  799. // link directories
  800. this->LinkDirectories = parent->LinkDirectories;
  801. // the initial project name
  802. this->ProjectName = parent->ProjectName;
  803. // Copy include regular expressions.
  804. this->IncludeFileRegularExpression = parent->IncludeFileRegularExpression;
  805. this->ComplainFileRegularExpression = parent->ComplainFileRegularExpression;
  806. }
  807. void cmMakefile::ConfigureSubDirectory(cmLocalGenerator *lg2)
  808. {
  809. // copy our variables from the child makefile
  810. lg2->GetMakefile()->InitializeFromParent();
  811. lg2->GetMakefile()->MakeStartDirectoriesCurrent();
  812. // finally configure the subdir
  813. lg2->Configure();
  814. }
  815. void cmMakefile::AddSubDirectory(const char* sub, bool topLevel, bool preorder)
  816. {
  817. // the source path must be made full if it isn't already
  818. std::string srcPath = sub;
  819. if (!cmSystemTools::FileIsFullPath(srcPath.c_str()))
  820. {
  821. srcPath = this->GetCurrentDirectory();
  822. srcPath += "/";
  823. srcPath += sub;
  824. }
  825. // binary path must be made full if it isn't already
  826. std::string binPath = sub;
  827. if (!cmSystemTools::FileIsFullPath(binPath.c_str()))
  828. {
  829. binPath = this->GetCurrentOutputDirectory();
  830. binPath += "/";
  831. binPath += sub;
  832. }
  833. this->AddSubDirectory(srcPath.c_str(), binPath.c_str(),
  834. topLevel, preorder, false);
  835. }
  836. void cmMakefile::AddSubDirectory(const char* srcPath, const char *binPath,
  837. bool topLevel, bool preorder,
  838. bool immediate)
  839. {
  840. std::vector<cmLocalGenerator *>& children = this->LocalGenerator->GetChildren();
  841. // has this directory already been added? If so error
  842. unsigned int i;
  843. for (i = 0; i < children.size(); ++i)
  844. {
  845. if (srcPath == children[i]->GetMakefile()->GetStartDirectory())
  846. {
  847. cmSystemTools::Error("Attempt to add subdirectory multiple times for directory.\n", srcPath);
  848. return;
  849. }
  850. }
  851. // create a new local generator and set its parent
  852. cmLocalGenerator *lg2 =
  853. this->LocalGenerator->GetGlobalGenerator()->CreateLocalGenerator();
  854. lg2->SetParent(this->LocalGenerator);
  855. this->LocalGenerator->GetGlobalGenerator()->AddLocalGenerator(lg2);
  856. // set the subdirs start dirs
  857. lg2->GetMakefile()->SetStartDirectory(srcPath);
  858. lg2->GetMakefile()->SetStartOutputDirectory(binPath);
  859. lg2->SetExcludeAll(!topLevel);
  860. lg2->GetMakefile()->SetPreOrder(preorder);
  861. if (immediate)
  862. {
  863. this->ConfigureSubDirectory(lg2);
  864. }
  865. }
  866. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  867. {
  868. // Don't add an include directory that is already present. Yes,
  869. // this linear search results in n^2 behavior, but n won't be
  870. // getting much bigger than 20. We cannot use a set because of
  871. // order dependency of the include path.
  872. std::vector<std::string>::iterator i =
  873. std::find(this->IncludeDirectories.begin(),
  874. this->IncludeDirectories.end(), inc);
  875. if(i == this->IncludeDirectories.end())
  876. {
  877. if (before)
  878. {
  879. // WARNING: this *is* expensive (linear time) since it's a vector
  880. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  881. }
  882. else
  883. {
  884. this->IncludeDirectories.push_back(inc);
  885. }
  886. }
  887. else
  888. {
  889. if(before)
  890. {
  891. // if this before and already in the path then remove it
  892. this->IncludeDirectories.erase(i);
  893. // WARNING: this *is* expensive (linear time) since it's a vector
  894. this->IncludeDirectories.insert(this->IncludeDirectories.begin(), inc);
  895. }
  896. }
  897. }
  898. void cmMakefile::AddDefinition(const char* name, const char* value)
  899. {
  900. if (!value )
  901. {
  902. return;
  903. }
  904. this->TemporaryDefinitionKey = name;
  905. this->Definitions[this->TemporaryDefinitionKey] = value;
  906. #ifdef CMAKE_BUILD_WITH_CMAKE
  907. cmVariableWatch* vv = this->GetVariableWatch();
  908. if ( vv )
  909. {
  910. vv->VariableAccessed(this->TemporaryDefinitionKey,
  911. cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  912. }
  913. #endif
  914. }
  915. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  916. const char* doc,
  917. cmCacheManager::CacheEntryType type)
  918. {
  919. const char* val = value;
  920. cmCacheManager::CacheIterator it =
  921. this->GetCacheManager()->GetCacheIterator(name);
  922. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  923. it.Initialized())
  924. {
  925. val = it.GetValue();
  926. if ( type == cmCacheManager::PATH || type == cmCacheManager::FILEPATH )
  927. {
  928. std::vector<std::string>::size_type cc;
  929. std::vector<std::string> files;
  930. std::string nvalue = "";
  931. cmSystemTools::ExpandListArgument(val, files);
  932. for ( cc = 0; cc < files.size(); cc ++ )
  933. {
  934. files[cc] = cmSystemTools::CollapseFullPath(files[cc].c_str());
  935. if ( cc > 0 )
  936. {
  937. nvalue += ";";
  938. }
  939. nvalue += files[cc];
  940. }
  941. this->GetCacheManager()->AddCacheEntry(name, nvalue.c_str(), doc, type);
  942. val = it.GetValue();
  943. }
  944. }
  945. this->GetCacheManager()->AddCacheEntry(name, val, doc, type);
  946. this->AddDefinition(name, val);
  947. }
  948. void cmMakefile::AddDefinition(const char* name, bool value)
  949. {
  950. if(value)
  951. {
  952. this->Definitions.erase( DefinitionMap::key_type(name));
  953. this->Definitions.insert(DefinitionMap::value_type(name, "ON"));
  954. }
  955. else
  956. {
  957. this->Definitions.erase( DefinitionMap::key_type(name));
  958. this->Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  959. }
  960. #ifdef CMAKE_BUILD_WITH_CMAKE
  961. cmVariableWatch* vv = this->GetVariableWatch();
  962. if ( vv )
  963. {
  964. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  965. }
  966. #endif
  967. }
  968. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  969. {
  970. bool val = value;
  971. cmCacheManager::CacheIterator it =
  972. this->GetCacheManager()->GetCacheIterator(name);
  973. if(!it.IsAtEnd() && (it.GetType() == cmCacheManager::UNINITIALIZED) &&
  974. it.Initialized())
  975. {
  976. val = it.GetValueAsBool();
  977. }
  978. this->GetCacheManager()->AddCacheEntry(name, val, doc);
  979. this->AddDefinition(name, val);
  980. }
  981. void cmMakefile::RemoveDefinition(const char* name)
  982. {
  983. this->Definitions.erase(DefinitionMap::key_type(name));
  984. #ifdef CMAKE_BUILD_WITH_CMAKE
  985. cmVariableWatch* vv = this->GetVariableWatch();
  986. if ( vv )
  987. {
  988. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  989. }
  990. #endif
  991. }
  992. void cmMakefile::SetProjectName(const char* p)
  993. {
  994. this->ProjectName = p;
  995. }
  996. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  997. {
  998. // for these targets do not add anything
  999. switch(target.GetType())
  1000. {
  1001. case cmTarget::UTILITY:
  1002. case cmTarget::GLOBAL_TARGET:
  1003. case cmTarget::INSTALL_FILES:
  1004. case cmTarget::INSTALL_PROGRAMS:
  1005. return;
  1006. default:;
  1007. }
  1008. std::vector<std::string>::iterator j;
  1009. for(j = this->LinkDirectories.begin();
  1010. j != this->LinkDirectories.end(); ++j)
  1011. {
  1012. target.AddLinkDirectory(j->c_str());
  1013. }
  1014. target.MergeLinkLibraries( *this, name, this->LinkLibraries );
  1015. }
  1016. void cmMakefile::AddLibrary(const char* lname, int shared,
  1017. const std::vector<std::string> &srcs)
  1018. {
  1019. cmTarget target;
  1020. switch (shared)
  1021. {
  1022. case 0:
  1023. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  1024. break;
  1025. case 1:
  1026. target.SetType(cmTarget::SHARED_LIBRARY, lname);
  1027. break;
  1028. case 2:
  1029. target.SetType(cmTarget::MODULE_LIBRARY, lname);
  1030. break;
  1031. default:
  1032. target.SetType(cmTarget::STATIC_LIBRARY, lname);
  1033. }
  1034. // Clear its dependencies. Otherwise, dependencies might persist
  1035. // over changes in CMakeLists.txt, making the information stale and
  1036. // hence useless.
  1037. target.ClearDependencyInformation( *this, lname );
  1038. target.SetInAll(true);
  1039. target.GetSourceLists() = srcs;
  1040. target.SetMakefile(this);
  1041. this->AddGlobalLinkInformation(lname, target);
  1042. this->Targets.insert(cmTargets::value_type(lname,target));
  1043. }
  1044. cmTarget* cmMakefile::AddExecutable(const char *exeName,
  1045. const std::vector<std::string> &srcs)
  1046. {
  1047. cmTarget target;
  1048. target.SetType(cmTarget::EXECUTABLE, exeName);
  1049. target.SetInAll(true);
  1050. target.GetSourceLists() = srcs;
  1051. target.SetMakefile(this);
  1052. this->AddGlobalLinkInformation(exeName, target);
  1053. cmTargets::iterator it =
  1054. this->Targets.insert(cmTargets::value_type(exeName,target)).first;
  1055. return &it->second;
  1056. }
  1057. cmSourceFile *cmMakefile::GetSourceFileWithOutput(const char *cname)
  1058. {
  1059. std::string name = cname;
  1060. std::string out;
  1061. // look through all the source files that have custom commands
  1062. // and see if the custom command has the passed source file as an output
  1063. // keep in mind the possible .rule extension that may be tacked on
  1064. for(std::vector<cmSourceFile*>::const_iterator i = this->SourceFiles.begin();
  1065. i != this->SourceFiles.end(); ++i)
  1066. {
  1067. // does this source file have a custom command?
  1068. if ((*i)->GetCustomCommand())
  1069. {
  1070. // is the output of the custom command match the source files name
  1071. const std::vector<std::string>& outputs =
  1072. (*i)->GetCustomCommand()->GetOutputs();
  1073. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1074. o != outputs.end(); ++o)
  1075. {
  1076. out = *o;
  1077. std::string::size_type pos = out.rfind(name);
  1078. // If the output matches exactly
  1079. if (pos != out.npos &&
  1080. pos == out.size() - name.size() &&
  1081. (pos ==0 || out[pos-1] == '/'))
  1082. {
  1083. return *i;
  1084. }
  1085. }
  1086. }
  1087. }
  1088. // otherwise return NULL
  1089. return 0;
  1090. }
  1091. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1092. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  1093. {
  1094. // First see if the group exists. If so, replace its regular expression.
  1095. for(std::vector<cmSourceGroup>::iterator sg = this->SourceGroups.begin();
  1096. sg != this->SourceGroups.end(); ++sg)
  1097. {
  1098. std::string sgName = sg->GetName();
  1099. if(sgName == name)
  1100. {
  1101. return &(*sg);
  1102. }
  1103. else
  1104. {
  1105. cmSourceGroup *target = sg->lookupChild(name);
  1106. if(target)
  1107. {
  1108. return target;
  1109. }
  1110. }
  1111. }
  1112. return 0;
  1113. }
  1114. void cmMakefile::AddSourceGroup(const char* name, const char* regex, const char *parent)
  1115. {
  1116. // First see if the group exists. If so, replace its regular expression.
  1117. for(unsigned int i=0;i<this->SourceGroups.size();++i)
  1118. {
  1119. cmSourceGroup *sg = &this->SourceGroups[i];
  1120. std::string sgName = sg->GetName();
  1121. if(!parent)
  1122. {
  1123. if(sgName == name)
  1124. {
  1125. if ( regex )
  1126. {
  1127. // We only want to set the regular expression. If there are already
  1128. // source files in the group, we don't want to remove them.
  1129. sg->SetGroupRegex(regex);
  1130. }
  1131. return;
  1132. }
  1133. }
  1134. else
  1135. {
  1136. if(sgName == parent)
  1137. {
  1138. cmSourceGroup *localtarget = sg->lookupChild(name);
  1139. if(localtarget)
  1140. {
  1141. if ( regex )
  1142. {
  1143. // We only want to set the regular expression. If there are already
  1144. // source files in the group, we don't want to remove them.
  1145. localtarget->SetGroupRegex(regex);
  1146. }
  1147. }
  1148. else
  1149. {
  1150. sg->AddChild(cmSourceGroup(name, regex));
  1151. }
  1152. return;
  1153. }
  1154. else
  1155. {
  1156. cmSourceGroup *localtarget = sg->lookupChild(parent);
  1157. if(localtarget)
  1158. {
  1159. cmSourceGroup *addtarget = localtarget->lookupChild(name);
  1160. if(addtarget)
  1161. {
  1162. if ( regex )
  1163. {
  1164. // We only want to set the regular expression. If there are already
  1165. // source files in the group, we don't want to remove them.
  1166. addtarget->SetGroupRegex(regex);
  1167. }
  1168. }
  1169. else
  1170. {
  1171. localtarget->AddChild(cmSourceGroup(name, regex));
  1172. }
  1173. return;
  1174. }
  1175. }
  1176. }
  1177. }
  1178. // The group doesn't exist. Add it.
  1179. this->SourceGroups.push_back(cmSourceGroup(name, regex));
  1180. }
  1181. #endif
  1182. void cmMakefile::AddExtraDirectory(const char* dir)
  1183. {
  1184. this->AuxSourceDirectories.push_back(dir);
  1185. }
  1186. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  1187. // include and library directories.
  1188. void cmMakefile::ExpandVariables()
  1189. {
  1190. // Now expand variables in the include and link strings
  1191. for(std::vector<std::string>::iterator d = this->IncludeDirectories.begin();
  1192. d != this->IncludeDirectories.end(); ++d)
  1193. {
  1194. this->ExpandVariablesInString(*d, true, true);
  1195. }
  1196. for(std::vector<std::string>::iterator d = this->LinkDirectories.begin();
  1197. d != this->LinkDirectories.end(); ++d)
  1198. {
  1199. this->ExpandVariablesInString(*d, true, true);
  1200. }
  1201. for(cmTarget::LinkLibraryVectorType::iterator l =
  1202. this->LinkLibraries.begin();
  1203. l != this->LinkLibraries.end(); ++l)
  1204. {
  1205. this->ExpandVariablesInString(l->first, true, true);
  1206. }
  1207. }
  1208. bool cmMakefile::IsOn(const char* name) const
  1209. {
  1210. const char* value = this->GetDefinition(name);
  1211. return cmSystemTools::IsOn(value);
  1212. }
  1213. bool cmMakefile::IsSet(const char* name) const
  1214. {
  1215. const char* value = this->GetDefinition(name);
  1216. if ( !value )
  1217. {
  1218. return false;
  1219. }
  1220. if ( ! *value )
  1221. {
  1222. return false;
  1223. }
  1224. if ( cmSystemTools::IsNOTFOUND(value) )
  1225. {
  1226. return false;
  1227. }
  1228. return true;
  1229. }
  1230. bool cmMakefile::CanIWriteThisFile(const char* fileName)
  1231. {
  1232. if ( !this->IsOn("CMAKE_DISABLE_SOURCE_CHANGES") )
  1233. {
  1234. return true;
  1235. }
  1236. // If we are doing an in-source build, than the test will always fail
  1237. if ( cmSystemTools::SameFile(this->GetHomeDirectory(), this->GetHomeOutputDirectory()) )
  1238. {
  1239. if ( this->IsOn("CMAKE_DISABLE_IN_SOURCE_BUILD") )
  1240. {
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. // Check if this is subdirectory of the source tree but not a subdirectory of a build tree
  1246. if ( cmSystemTools::IsSubDirectory(fileName,
  1247. this->GetHomeDirectory()) &&
  1248. !cmSystemTools::IsSubDirectory(fileName,
  1249. this->GetHomeOutputDirectory()) )
  1250. {
  1251. return false;
  1252. }
  1253. return true;
  1254. }
  1255. const char* cmMakefile::GetRequiredDefinition(const char* name) const
  1256. {
  1257. const char* ret = this->GetDefinition(name);
  1258. if(!ret)
  1259. {
  1260. cmSystemTools::Error("Error required internal CMake variable not set, cmake may be not be built correctly.\n",
  1261. "Missing variable is:\n",
  1262. name);
  1263. return "";
  1264. }
  1265. return ret;
  1266. }
  1267. const char* cmMakefile::GetDefinition(const char* name) const
  1268. {
  1269. const char* def = 0;
  1270. DefinitionMap::const_iterator pos = this->Definitions.find(name);
  1271. if(pos != this->Definitions.end())
  1272. {
  1273. def = (*pos).second.c_str();
  1274. }
  1275. else
  1276. {
  1277. def = this->GetCacheManager()->GetCacheValue(name);
  1278. }
  1279. #ifdef CMAKE_BUILD_WITH_CMAKE
  1280. cmVariableWatch* vv = this->GetVariableWatch();
  1281. if ( vv )
  1282. {
  1283. if ( def )
  1284. {
  1285. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  1286. }
  1287. else
  1288. {
  1289. // are unknown access allowed
  1290. DefinitionMap::const_iterator pos2 =
  1291. this->Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  1292. if (pos2 != this->Definitions.end() &&
  1293. cmSystemTools::IsOn((*pos2).second.c_str()))
  1294. {
  1295. vv->VariableAccessed(name,
  1296. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  1297. }
  1298. else
  1299. {
  1300. vv->VariableAccessed(name, cmVariableWatch::
  1301. UNKNOWN_VARIABLE_READ_ACCESS);
  1302. }
  1303. }
  1304. }
  1305. #endif
  1306. return def;
  1307. }
  1308. const char* cmMakefile::GetSafeDefinition(const char* def) const
  1309. {
  1310. const char* ret = this->GetDefinition(def);
  1311. if(!ret)
  1312. {
  1313. return "";
  1314. }
  1315. return ret;
  1316. }
  1317. std::vector<std::string> cmMakefile::GetDefinitions(int cacheonly /* = 0 */) const
  1318. {
  1319. std::map<cmStdString, int> definitions;
  1320. if ( !cacheonly )
  1321. {
  1322. DefinitionMap::const_iterator it;
  1323. for ( it = this->Definitions.begin(); it != this->Definitions.end(); it ++ )
  1324. {
  1325. definitions[it->first] = 1;
  1326. }
  1327. }
  1328. cmCacheManager::CacheIterator cit = this->GetCacheManager()->GetCacheIterator();
  1329. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  1330. {
  1331. definitions[cit.GetName()] = 1;
  1332. }
  1333. std::vector<std::string> res;
  1334. std::map<cmStdString, int>::iterator fit;
  1335. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  1336. {
  1337. res.push_back(fit->first);
  1338. }
  1339. return res;
  1340. }
  1341. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  1342. {
  1343. return this->ExpandVariablesInString(source, false, false);
  1344. }
  1345. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  1346. bool escapeQuotes,
  1347. bool noEscapes,
  1348. bool atOnly,
  1349. const char* filename,
  1350. long line,
  1351. bool removeEmpty) const
  1352. {
  1353. if ( source.empty() || source.find_first_of("$@\\") == source.npos)
  1354. {
  1355. return source.c_str();
  1356. }
  1357. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  1358. // with GetDefinition(), if not found in the map, nothing is expanded.
  1359. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  1360. // the current environment variables.
  1361. bool notParsed = true;
  1362. if ( !atOnly )
  1363. {
  1364. cmCommandArgumentParserHelper parser;
  1365. parser.SetMakefile(this);
  1366. parser.SetLineFile(line, filename);
  1367. parser.SetEscapeQuotes(escapeQuotes);
  1368. parser.SetNoEscapeMode(noEscapes);
  1369. int res = parser.ParseString(source.c_str(), 0);
  1370. if ( res )
  1371. {
  1372. source = parser.GetResult();
  1373. notParsed = false;
  1374. }
  1375. else
  1376. {
  1377. cmOStringStream error;
  1378. error << "Syntax error in cmake code at\n"
  1379. << (filename?filename:"(no filename given)")
  1380. << ":" << line << ":\n"
  1381. << parser.GetError() << ", when parsing string \""
  1382. << source.c_str() << "\"";
  1383. const char* versionValue
  1384. = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1385. int major = 0;
  1386. int minor = 0;
  1387. if ( versionValue )
  1388. {
  1389. sscanf(versionValue, "%d.%d", &major, &minor);
  1390. }
  1391. if ( major < 2 || major == 2 && minor < 1 )
  1392. {
  1393. cmSystemTools::Error(error.str().c_str());
  1394. cmSystemTools::SetFatalErrorOccured();
  1395. return source.c_str();
  1396. }
  1397. else
  1398. {
  1399. cmSystemTools::Message(error.str().c_str());
  1400. }
  1401. }
  1402. }
  1403. if ( notParsed )
  1404. {
  1405. // start by look for $ or @ in the string
  1406. std::string::size_type markerPos;
  1407. if(atOnly)
  1408. {
  1409. markerPos = source.find_first_of("@");
  1410. }
  1411. else
  1412. {
  1413. markerPos = source.find_first_of("$@");
  1414. }
  1415. // if not found, or found as the last character, then leave quickly as
  1416. // nothing needs to be expanded
  1417. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  1418. {
  1419. return source.c_str();
  1420. }
  1421. // current position
  1422. std::string::size_type currentPos =0; // start at 0
  1423. std::string result; // string with replacements
  1424. // go until the the end of the string
  1425. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  1426. {
  1427. // grab string from currentPos to the start of the variable
  1428. // and add it to the result
  1429. result += source.substr(currentPos, markerPos - currentPos);
  1430. char endVariableMarker; // what is the end of the variable @ or }
  1431. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  1432. if(!atOnly && source[markerPos] == '$')
  1433. {
  1434. // ${var} case
  1435. if(source[markerPos+1] == '{')
  1436. {
  1437. endVariableMarker = '}';
  1438. markerStartSize = 2;
  1439. }
  1440. // $ENV{var} case
  1441. else if(markerPos+4 < source.size() &&
  1442. source[markerPos+4] == '{' &&
  1443. !source.substr(markerPos+1, 3).compare("ENV"))
  1444. {
  1445. endVariableMarker = '}';
  1446. markerStartSize = 5;
  1447. }
  1448. else
  1449. {
  1450. // bogus $ with no { so add $ to result and move on
  1451. result += '$'; // add bogus $ back into string
  1452. currentPos = markerPos+1; // move on
  1453. endVariableMarker = ' '; // set end var to space so we can tell bogus
  1454. }
  1455. }
  1456. else
  1457. {
  1458. // @VAR case
  1459. endVariableMarker = '@';
  1460. }
  1461. // if it was a valid variable (started with @ or ${ or $ENV{ )
  1462. if(endVariableMarker != ' ')
  1463. {
  1464. markerPos += markerStartSize; // move past marker
  1465. // find the end variable marker starting at the markerPos
  1466. std::string::size_type endVariablePos =
  1467. source.find(endVariableMarker, markerPos);
  1468. if(endVariablePos == std::string::npos)
  1469. {
  1470. // no end marker found so add the bogus start
  1471. if(endVariableMarker == '@')
  1472. {
  1473. result += '@';
  1474. }
  1475. else
  1476. {
  1477. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1478. }
  1479. currentPos = markerPos;
  1480. }
  1481. else
  1482. {
  1483. // good variable remove it
  1484. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  1485. bool found = false;
  1486. if (markerStartSize == 5) // $ENV{
  1487. {
  1488. char *ptr = getenv(var.c_str());
  1489. if (ptr)
  1490. {
  1491. if (escapeQuotes)
  1492. {
  1493. result += cmSystemTools::EscapeQuotes(ptr);
  1494. }
  1495. else
  1496. {
  1497. result += ptr;
  1498. }
  1499. found = true;
  1500. }
  1501. }
  1502. else
  1503. {
  1504. const char* lookup = this->GetDefinition(var.c_str());
  1505. if(lookup)
  1506. {
  1507. if (escapeQuotes)
  1508. {
  1509. result += cmSystemTools::EscapeQuotes(lookup);
  1510. }
  1511. else
  1512. {
  1513. result += lookup;
  1514. }
  1515. found = true;
  1516. }
  1517. else if(filename && (var == "CMAKE_CURRENT_LIST_FILE"))
  1518. {
  1519. result += filename;
  1520. found = true;
  1521. }
  1522. else if(line >= 0 && (var == "CMAKE_CURRENT_LIST_LINE"))
  1523. {
  1524. cmOStringStream ostr;
  1525. ostr << line;
  1526. result += ostr.str();
  1527. found = true;
  1528. }
  1529. }
  1530. // if found add to result, if not, then it gets blanked
  1531. if (!found)
  1532. {
  1533. // if no definition is found then add the var back
  1534. if(!removeEmpty && endVariableMarker == '@')
  1535. {
  1536. result += "@";
  1537. result += var;
  1538. result += "@";
  1539. }
  1540. }
  1541. // lookup var, and replace it
  1542. currentPos = endVariablePos+1;
  1543. }
  1544. }
  1545. if(atOnly)
  1546. {
  1547. markerPos = source.find_first_of("@", currentPos);
  1548. }
  1549. else
  1550. {
  1551. markerPos = source.find_first_of("$@", currentPos);
  1552. }
  1553. }
  1554. result += source.substr(currentPos); // pick up the rest of the string
  1555. source = result;
  1556. }
  1557. return source.c_str();
  1558. }
  1559. void cmMakefile::RemoveVariablesInString(std::string& source,
  1560. bool atOnly) const
  1561. {
  1562. if(!atOnly)
  1563. {
  1564. cmsys::RegularExpression var("(\\${[A-Za-z_0-9]*})");
  1565. while (var.find(source))
  1566. {
  1567. source.erase(var.start(),var.end() - var.start());
  1568. }
  1569. }
  1570. if(!atOnly)
  1571. {
  1572. cmsys::RegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1573. while (varb.find(source))
  1574. {
  1575. source.erase(varb.start(),varb.end() - varb.start());
  1576. }
  1577. }
  1578. cmsys::RegularExpression var2("(@[A-Za-z_0-9]*@)");
  1579. while (var2.find(source))
  1580. {
  1581. source.erase(var2.start(),var2.end() - var2.start());
  1582. }
  1583. }
  1584. /**
  1585. * Add the default definitions to the makefile. These values must not
  1586. * be dependent on anything that isn't known when this cmMakefile instance
  1587. * is constructed.
  1588. */
  1589. void cmMakefile::AddDefaultDefinitions()
  1590. {
  1591. #if defined(_WIN32) || defined(__CYGWIN__)
  1592. this->AddDefinition("WIN32", "1");
  1593. #else
  1594. this->AddDefinition("UNIX", "1");
  1595. #endif
  1596. // Cygwin is more like unix so enable the unix commands
  1597. #if defined(__CYGWIN__)
  1598. this->AddDefinition("UNIX", "1");
  1599. this->AddDefinition("CYGWIN", "1");
  1600. #endif
  1601. #if defined(__APPLE__)
  1602. this->AddDefinition("APPLE", "1");
  1603. #endif
  1604. char temp[1024];
  1605. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1606. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1607. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1608. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1609. }
  1610. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1611. /**
  1612. * Find a source group whose regular expression matches the filename
  1613. * part of the given source name. Search backward through the list of
  1614. * source groups, and take the first matching group found. This way
  1615. * non-inherited SOURCE_GROUP commands will have precedence over
  1616. * inherited ones.
  1617. */
  1618. cmSourceGroup&
  1619. cmMakefile::FindSourceGroup(const char* source,
  1620. std::vector<cmSourceGroup> &groups)
  1621. {
  1622. // First search for a group that lists the file explicitly.
  1623. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1624. sg != groups.rend(); ++sg)
  1625. {
  1626. cmSourceGroup *result = sg->MatchChildrenFiles(source);
  1627. if(result)
  1628. {
  1629. return *result;
  1630. }
  1631. }
  1632. // Now search for a group whose regex matches the file.
  1633. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1634. sg != groups.rend(); ++sg)
  1635. {
  1636. cmSourceGroup *result = sg->MatchChildrenRegex(source);
  1637. if(result)
  1638. {
  1639. return *result;
  1640. }
  1641. }
  1642. // Shouldn't get here, but just in case, return the default group.
  1643. return groups.front();
  1644. }
  1645. #endif
  1646. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1647. {
  1648. // if there are no blockers get out of here
  1649. if (this->FunctionBlockers.begin() == this->FunctionBlockers.end())
  1650. {
  1651. return false;
  1652. }
  1653. // loop over all function blockers to see if any block this command
  1654. // evaluate in reverse, this is critical for balanced IF statements etc
  1655. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1656. for (pos = this->FunctionBlockers.rbegin();
  1657. pos != this->FunctionBlockers.rend(); ++pos)
  1658. {
  1659. if((*pos)->IsFunctionBlocked(lff, *this))
  1660. {
  1661. return true;
  1662. }
  1663. }
  1664. return false;
  1665. }
  1666. void cmMakefile::ExpandArguments(
  1667. std::vector<cmListFileArgument> const& inArgs,
  1668. std::vector<std::string>& outArgs)
  1669. {
  1670. std::vector<cmListFileArgument>::const_iterator i;
  1671. std::string value;
  1672. outArgs.reserve(inArgs.size());
  1673. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1674. {
  1675. // Expand the variables in the argument.
  1676. value = i->Value;
  1677. this->ExpandVariablesInString(value, false, false, false, i->FilePath, i->Line);
  1678. // If the argument is quoted, it should be one argument.
  1679. // Otherwise, it may be a list of arguments.
  1680. if(i->Quoted)
  1681. {
  1682. outArgs.push_back(value);
  1683. }
  1684. else
  1685. {
  1686. cmSystemTools::ExpandListArgument(value, outArgs);
  1687. }
  1688. }
  1689. }
  1690. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1691. {
  1692. // loop over all function blockers to see if any block this command
  1693. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1694. for (pos = this->FunctionBlockers.rbegin();
  1695. pos != this->FunctionBlockers.rend(); ++pos)
  1696. {
  1697. if ((*pos)->ShouldRemove(lff, *this))
  1698. {
  1699. cmFunctionBlocker* b = *pos;
  1700. this->FunctionBlockers.remove(b);
  1701. delete b;
  1702. break;
  1703. }
  1704. }
  1705. return;
  1706. }
  1707. void cmMakefile::SetHomeDirectory(const char* dir)
  1708. {
  1709. this->cmHomeDirectory = dir;
  1710. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1711. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1712. if ( !this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR") )
  1713. {
  1714. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", this->GetHomeDirectory());
  1715. }
  1716. }
  1717. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1718. {
  1719. this->HomeOutputDirectory = lib;
  1720. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1721. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1722. if ( !this->GetDefinition("CMAKE_CURRENT_BINARY_DIR") )
  1723. {
  1724. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR", this->GetHomeOutputDirectory());
  1725. }
  1726. }
  1727. /**
  1728. * Register the given cmData instance with its own name.
  1729. */
  1730. void cmMakefile::RegisterData(cmData* data)
  1731. {
  1732. std::string name = data->GetName();
  1733. DataMapType::const_iterator d = this->DataMap.find(name);
  1734. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1735. {
  1736. delete d->second;
  1737. }
  1738. this->DataMap[name] = data;
  1739. }
  1740. /**
  1741. * Register the given cmData instance with the given name. This can be used
  1742. * to register a NULL pointer.
  1743. */
  1744. void cmMakefile::RegisterData(const char* name, cmData* data)
  1745. {
  1746. DataMapType::const_iterator d = this->DataMap.find(name);
  1747. if((d != this->DataMap.end()) && (d->second != 0) && (d->second != data))
  1748. {
  1749. delete d->second;
  1750. }
  1751. this->DataMap[name] = data;
  1752. }
  1753. /**
  1754. * Lookup a cmData instance previously registered with the given name. If
  1755. * the instance cannot be found, return NULL.
  1756. */
  1757. cmData* cmMakefile::LookupData(const char* name) const
  1758. {
  1759. DataMapType::const_iterator d = this->DataMap.find(name);
  1760. if(d != this->DataMap.end())
  1761. {
  1762. return d->second;
  1763. }
  1764. else
  1765. {
  1766. return 0;
  1767. }
  1768. }
  1769. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1770. {
  1771. // if the source is provided with a full path use it, otherwise
  1772. // by default it is in the current source dir
  1773. std::string path = cmSystemTools::GetFilenamePath(sourceName);
  1774. if (path.empty())
  1775. {
  1776. path = this->GetCurrentDirectory();
  1777. }
  1778. std::string sname =
  1779. cmSystemTools::GetFilenameWithoutLastExtension(sourceName);
  1780. // compute the extension
  1781. std::string ext
  1782. = cmSystemTools::GetFilenameLastExtension(sourceName);
  1783. if ( ext.length() && ext[0] == '.' )
  1784. {
  1785. ext = ext.substr(1);
  1786. }
  1787. for(std::vector<cmSourceFile*>::const_iterator i = this->SourceFiles.begin();
  1788. i != this->SourceFiles.end(); ++i)
  1789. {
  1790. if ((*i)->GetSourceNameWithoutLastExtension() == sname &&
  1791. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1792. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1793. {
  1794. return *i;
  1795. }
  1796. }
  1797. // geeze, if it wasn't found maybe it is listed under the output dir
  1798. if (!cmSystemTools::GetFilenamePath(sourceName).empty())
  1799. {
  1800. return 0;
  1801. }
  1802. path = this->GetCurrentOutputDirectory();
  1803. for(std::vector<cmSourceFile*>::const_iterator i = this->SourceFiles.begin();
  1804. i != this->SourceFiles.end(); ++i)
  1805. {
  1806. if ((*i)->GetSourceName() == sname &&
  1807. cmSystemTools::GetFilenamePath((*i)->GetFullPath()) == path &&
  1808. (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1809. {
  1810. return *i;
  1811. }
  1812. }
  1813. return 0;
  1814. }
  1815. cmSourceFile* cmMakefile::GetOrCreateSource(const char* sourceName,
  1816. bool generated)
  1817. {
  1818. // make it a full path first
  1819. std::string src = sourceName;
  1820. bool relative = !cmSystemTools::FileIsFullPath(sourceName);
  1821. std::string srcTreeFile = this->GetCurrentDirectory();
  1822. srcTreeFile += "/";
  1823. srcTreeFile += sourceName;
  1824. if(relative)
  1825. {
  1826. src = srcTreeFile;
  1827. }
  1828. // check to see if it exists
  1829. cmSourceFile* ret = this->GetSource(src.c_str());
  1830. if (ret)
  1831. {
  1832. return ret;
  1833. }
  1834. // OK a source file object doesn't exist for the source
  1835. // maybe we made a bad call on assuming it was in the src tree
  1836. std::string buildTreeFile = this->GetCurrentOutputDirectory();
  1837. buildTreeFile += "/";
  1838. buildTreeFile += sourceName;
  1839. if (relative)
  1840. {
  1841. src = buildTreeFile;
  1842. ret = this->GetSource(src.c_str());
  1843. if (ret)
  1844. {
  1845. return ret;
  1846. }
  1847. // if it has not been marked generated check to see if it exists in the
  1848. // src tree
  1849. if(!generated)
  1850. {
  1851. // see if the file is in the source tree, otherwise assume it
  1852. // is in the binary tree
  1853. if (cmSystemTools::FileExists(srcTreeFile.c_str()) &&
  1854. !cmSystemTools::FileIsDirectory(srcTreeFile.c_str()))
  1855. {
  1856. src = srcTreeFile;
  1857. }
  1858. else
  1859. {
  1860. if ( cmSystemTools::GetFilenameLastExtension(srcTreeFile.c_str()).size() == 0)
  1861. {
  1862. if (cmSystemTools::DoesFileExistWithExtensions(
  1863. srcTreeFile.c_str(), this->GetSourceExtensions()))
  1864. {
  1865. src = srcTreeFile;
  1866. }
  1867. else if (cmSystemTools::DoesFileExistWithExtensions(
  1868. srcTreeFile.c_str(), this->GetHeaderExtensions()))
  1869. {
  1870. src = srcTreeFile;
  1871. }
  1872. }
  1873. }
  1874. }
  1875. }
  1876. // a cmSourceFile instance does not exist yet so we must create one
  1877. // go back to looking in the source directory for it
  1878. // we must create one
  1879. cmSourceFile file;
  1880. std::string path = cmSystemTools::GetFilenamePath(src);
  1881. if(generated)
  1882. {
  1883. std::string ext = cmSystemTools::GetFilenameLastExtension(src);
  1884. std::string name_no_ext = cmSystemTools::GetFilenameName(src.c_str());
  1885. name_no_ext = name_no_ext.substr(0, name_no_ext.length()-ext.length());
  1886. if ( ext.length() && ext[0] == '.' )
  1887. {
  1888. ext = ext.substr(1);
  1889. }
  1890. bool headerFile = !(std::find( this->HeaderFileExtensions.begin(), this->HeaderFileExtensions.end(), ext ) ==
  1891. this->HeaderFileExtensions.end());
  1892. file.SetName(name_no_ext.c_str(), path.c_str(), ext.c_str(), headerFile);
  1893. }
  1894. else
  1895. {
  1896. std::string relPath = cmSystemTools::GetFilenamePath(sourceName);
  1897. if (relative && relPath.size())
  1898. {
  1899. // we need to keep the relative part of the filename
  1900. std::string fullPathLessRel = path;
  1901. std::string::size_type pos = fullPathLessRel.rfind(relPath);
  1902. if (pos == std::string::npos)
  1903. {
  1904. cmSystemTools::Error(
  1905. "CMake failed to properly look up relative cmSourceFile: ",
  1906. sourceName);
  1907. }
  1908. fullPathLessRel.erase(pos-1);
  1909. file.SetName(sourceName, fullPathLessRel.c_str(),
  1910. this->GetSourceExtensions(),
  1911. this->GetHeaderExtensions());
  1912. }
  1913. else
  1914. {
  1915. file.SetName(cmSystemTools::GetFilenameName(src.c_str()).c_str(),
  1916. path.c_str(),
  1917. this->GetSourceExtensions(),
  1918. this->GetHeaderExtensions());
  1919. }
  1920. }
  1921. // add the source file to the makefile
  1922. this->AddSource(file);
  1923. src = file.GetFullPath();
  1924. ret = this->GetSource(src.c_str());
  1925. if (!ret)
  1926. {
  1927. cmSystemTools::Error(
  1928. "CMake failed to properly look up cmSourceFile: ", sourceName);
  1929. }
  1930. return ret;
  1931. }
  1932. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1933. {
  1934. // check to see if it exists
  1935. cmSourceFile* ret = this->GetSource(sf.GetFullPath().c_str());
  1936. if(ret)
  1937. {
  1938. return ret;
  1939. }
  1940. ret = new cmSourceFile(sf);
  1941. this->SourceFiles.push_back(ret);
  1942. return ret;
  1943. }
  1944. void cmMakefile::EnableLanguage(std::vector<std::string> const & lang)
  1945. {
  1946. this->AddDefinition("CMAKE_CFG_INTDIR",
  1947. this->LocalGenerator->GetGlobalGenerator()->GetCMakeCFGInitDirectory());
  1948. this->LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1949. }
  1950. void cmMakefile::ExpandSourceListArguments(
  1951. std::vector<std::string> const& arguments,
  1952. std::vector<std::string>& newargs, unsigned int /* start */)
  1953. {
  1954. // now expand the args
  1955. unsigned int i;
  1956. for(i = 0; i < arguments.size(); ++i)
  1957. {
  1958. // List expansion will have been done already.
  1959. newargs.push_back(arguments[i]);
  1960. }
  1961. }
  1962. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1963. const char *projectName, const char *targetName,
  1964. const std::vector<std::string> *cmakeArgs,
  1965. std::string *output)
  1966. {
  1967. // does the binary directory exist ? If not create it...
  1968. if (!cmSystemTools::FileIsDirectory(bindir))
  1969. {
  1970. cmSystemTools::MakeDirectory(bindir);
  1971. }
  1972. // change to the tests directory and run cmake
  1973. // use the cmake object instead of calling cmake
  1974. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1975. cmSystemTools::ChangeDirectory(bindir);
  1976. // make sure the same generator is used
  1977. // use this program as the cmake to be run, it should not
  1978. // be run that way but the cmake object requires a vailid path
  1979. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1980. cmake cm;
  1981. cm.SetIsInTryCompile(true);
  1982. cmGlobalGenerator *gg =
  1983. cm.CreateGlobalGenerator(this->LocalGenerator->GetGlobalGenerator()->GetName());
  1984. if (!gg)
  1985. {
  1986. cmSystemTools::Error(
  1987. "Internal CMake error, TryCompile bad GlobalGenerator");
  1988. // return to the original directory
  1989. cmSystemTools::ChangeDirectory(cwd.c_str());
  1990. return 1;
  1991. }
  1992. cm.SetGlobalGenerator(gg);
  1993. // do a configure
  1994. cm.SetHomeDirectory(srcdir);
  1995. cm.SetHomeOutputDirectory(bindir);
  1996. cm.SetStartDirectory(srcdir);
  1997. cm.SetStartOutputDirectory(bindir);
  1998. cm.SetCMakeCommand(cmakeCommand.c_str());
  1999. cm.LoadCache();
  2000. // if cmake args were provided then pass them in
  2001. if (cmakeArgs)
  2002. {
  2003. cm.SetCacheArgs(*cmakeArgs);
  2004. }
  2005. // to save time we pass the EnableLanguage info directly
  2006. gg->EnableLanguagesFromGenerator(this->LocalGenerator->GetGlobalGenerator());
  2007. if (cm.Configure() != 0)
  2008. {
  2009. cmSystemTools::Error(
  2010. "Internal CMake error, TryCompile configure of cmake failed");
  2011. // return to the original directory
  2012. cmSystemTools::ChangeDirectory(cwd.c_str());
  2013. return 1;
  2014. }
  2015. if (cm.Generate() != 0)
  2016. {
  2017. cmSystemTools::Error(
  2018. "Internal CMake error, TryCompile generation of cmake failed");
  2019. // return to the original directory
  2020. cmSystemTools::ChangeDirectory(cwd.c_str());
  2021. return 1;
  2022. }
  2023. // finally call the generator to actually build the resulting project
  2024. int ret =
  2025. this->LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  2026. projectName,
  2027. targetName,
  2028. output,
  2029. this);
  2030. cmSystemTools::ChangeDirectory(cwd.c_str());
  2031. return ret;
  2032. }
  2033. cmake *cmMakefile::GetCMakeInstance() const
  2034. {
  2035. if ( this->LocalGenerator && this->LocalGenerator->GetGlobalGenerator() )
  2036. {
  2037. return this->LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  2038. }
  2039. return 0;
  2040. }
  2041. #ifdef CMAKE_BUILD_WITH_CMAKE
  2042. cmVariableWatch *cmMakefile::GetVariableWatch() const
  2043. {
  2044. if ( this->GetCMakeInstance() &&
  2045. this->GetCMakeInstance()->GetVariableWatch() )
  2046. {
  2047. return this->GetCMakeInstance()->GetVariableWatch();
  2048. }
  2049. return 0;
  2050. }
  2051. #endif
  2052. void cmMakefile::AddMacro(const char* name, const char* signature)
  2053. {
  2054. if ( !name || !signature )
  2055. {
  2056. return;
  2057. }
  2058. this->MacrosMap[name] = signature;
  2059. }
  2060. void cmMakefile::GetListOfMacros(std::string& macros)
  2061. {
  2062. StringStringMap::iterator it;
  2063. macros = "";
  2064. int cc = 0;
  2065. for ( it = this->MacrosMap.begin(); it != this->MacrosMap.end(); ++it )
  2066. {
  2067. if ( cc > 0 )
  2068. {
  2069. macros += ";";
  2070. }
  2071. macros += it->first;
  2072. cc ++;
  2073. }
  2074. }
  2075. cmCacheManager *cmMakefile::GetCacheManager() const
  2076. {
  2077. return this->GetCMakeInstance()->GetCacheManager();
  2078. }
  2079. void cmMakefile::DisplayStatus(const char* message, float s)
  2080. {
  2081. this->GetLocalGenerator()->GetGlobalGenerator()
  2082. ->GetCMakeInstance()->UpdateProgress(message, s);
  2083. }
  2084. std::string cmMakefile::GetModulesFile(const char* filename)
  2085. {
  2086. std::vector<std::string> modulePath;
  2087. const char* def = this->GetDefinition("CMAKE_MODULE_PATH");
  2088. if(def)
  2089. {
  2090. cmSystemTools::ExpandListArgument(def, modulePath);
  2091. }
  2092. // Also search in the standard modules location.
  2093. def = this->GetDefinition("CMAKE_ROOT");
  2094. if(def)
  2095. {
  2096. std::string rootModules = def;
  2097. rootModules += "/Modules";
  2098. modulePath.push_back(rootModules);
  2099. }
  2100. //std::string Look through the possible module directories.
  2101. for(std::vector<std::string>::iterator i = modulePath.begin();
  2102. i != modulePath.end(); ++i)
  2103. {
  2104. std::string itempl = *i;
  2105. cmSystemTools::ConvertToUnixSlashes(itempl);
  2106. itempl += "/";
  2107. itempl += filename;
  2108. if(cmSystemTools::FileExists(itempl.c_str()))
  2109. {
  2110. return itempl;
  2111. }
  2112. }
  2113. return "";
  2114. }
  2115. void cmMakefile::ConfigureString(const std::string& input,
  2116. std::string& output, bool atOnly,
  2117. bool escapeQuotes)
  2118. {
  2119. // Split input to handle one line at a time.
  2120. std::string::const_iterator lineStart = input.begin();
  2121. while(lineStart != input.end())
  2122. {
  2123. // Find the end of this line.
  2124. std::string::const_iterator lineEnd = lineStart;
  2125. while(lineEnd != input.end() && *lineEnd != '\n')
  2126. {
  2127. ++lineEnd;
  2128. }
  2129. // Copy the line.
  2130. std::string line(lineStart, lineEnd);
  2131. // Skip the newline character.
  2132. bool haveNewline = (lineEnd != input.end());
  2133. if(haveNewline)
  2134. {
  2135. ++lineEnd;
  2136. }
  2137. // Replace #cmakedefine instances.
  2138. if(this->cmDefineRegex.find(line))
  2139. {
  2140. const char* def = this->GetDefinition(this->cmDefineRegex.match(1).c_str());
  2141. if(!cmSystemTools::IsOff(def))
  2142. {
  2143. cmSystemTools::ReplaceString(line, "#cmakedefine", "#define");
  2144. output += line;
  2145. }
  2146. else
  2147. {
  2148. cmSystemTools::ReplaceString(line, "#cmakedefine", "#undef");
  2149. output += "/* ";
  2150. output += line;
  2151. output += " */";
  2152. }
  2153. }
  2154. else if(this->cmDefine01Regex.find(line))
  2155. {
  2156. const char* def = this->GetDefinition(this->cmDefine01Regex.match(1).c_str());
  2157. cmSystemTools::ReplaceString(line, "#cmakedefine01", "#define");
  2158. output += line;
  2159. if(!cmSystemTools::IsOff(def))
  2160. {
  2161. output += " 1";
  2162. }
  2163. else
  2164. {
  2165. output += " 0";
  2166. }
  2167. }
  2168. else
  2169. {
  2170. output += line;
  2171. }
  2172. if(haveNewline)
  2173. {
  2174. output += "\n";
  2175. }
  2176. // Move to the next line.
  2177. lineStart = lineEnd;
  2178. }
  2179. // Perform variable replacements.
  2180. this->ExpandVariablesInString(output, escapeQuotes, true, atOnly, 0, -1, true);
  2181. // this->RemoveVariablesInString(output, atOnly);
  2182. }
  2183. int cmMakefile::ConfigureFile(const char* infile, const char* outfile,
  2184. bool copyonly, bool atOnly, bool escapeQuotes)
  2185. {
  2186. int res = 1;
  2187. if ( !this->CanIWriteThisFile(outfile) )
  2188. {
  2189. cmSystemTools::Error("Attempt to write file: ", outfile, " into a source directory.");
  2190. return 0;
  2191. }
  2192. if ( !cmSystemTools::FileExists(infile) )
  2193. {
  2194. cmSystemTools::Error("File ", infile, " does not exist.");
  2195. return 0;
  2196. }
  2197. std::string soutfile = outfile;
  2198. std::string sinfile = infile;
  2199. this->AddCMakeDependFile(infile);
  2200. this->AddCMakeOutputFile(outfile);
  2201. cmSystemTools::ConvertToUnixSlashes(soutfile);
  2202. mode_t perm = 0;
  2203. cmSystemTools::GetPermissions(sinfile.c_str(), perm);
  2204. std::string::size_type pos = soutfile.rfind('/');
  2205. if(pos != std::string::npos)
  2206. {
  2207. std::string path = soutfile.substr(0, pos);
  2208. cmSystemTools::MakeDirectory(path.c_str());
  2209. }
  2210. if(copyonly)
  2211. {
  2212. if ( !cmSystemTools::CopyFileIfDifferent(sinfile.c_str(),
  2213. soutfile.c_str()))
  2214. {
  2215. return 0;
  2216. }
  2217. }
  2218. else
  2219. {
  2220. std::string tempOutputFile = soutfile;
  2221. tempOutputFile += ".tmp";
  2222. std::ofstream fout(tempOutputFile.c_str());
  2223. if(!fout)
  2224. {
  2225. cmSystemTools::Error(
  2226. "Could not open file for write in copy operation ",
  2227. tempOutputFile.c_str());
  2228. cmSystemTools::ReportLastSystemError("");
  2229. return 0;
  2230. }
  2231. std::ifstream fin(sinfile.c_str());
  2232. if(!fin)
  2233. {
  2234. cmSystemTools::Error("Could not open file for read in copy operation ",
  2235. sinfile.c_str());
  2236. return 0;
  2237. }
  2238. // now copy input to output and expand variables in the
  2239. // input file at the same time
  2240. std::string inLine;
  2241. std::string outLine;
  2242. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  2243. {
  2244. outLine = "";
  2245. this->ConfigureString(inLine, outLine, atOnly, escapeQuotes);
  2246. fout << outLine.c_str() << "\n";
  2247. }
  2248. // close the files before attempting to copy
  2249. fin.close();
  2250. fout.close();
  2251. if ( !cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  2252. soutfile.c_str()) )
  2253. {
  2254. res = 0;
  2255. }
  2256. else
  2257. {
  2258. cmSystemTools::SetPermissions(soutfile.c_str(), perm);
  2259. }
  2260. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  2261. }
  2262. return res;
  2263. }
  2264. void cmMakefile::AddWrittenFile(const char* file)
  2265. { this->GetCMakeInstance()->AddWrittenFile(file); }
  2266. bool cmMakefile::HasWrittenFile(const char* file)
  2267. { return this->GetCMakeInstance()->HasWrittenFile(file); }
  2268. bool cmMakefile::CheckInfiniteLoops()
  2269. {
  2270. std::vector<std::string>::iterator it;
  2271. for ( it = this->ListFiles.begin();
  2272. it != this->ListFiles.end();
  2273. ++ it )
  2274. {
  2275. if ( this->HasWrittenFile(it->c_str()) )
  2276. {
  2277. cmOStringStream str;
  2278. 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.";
  2279. cmSystemTools::Error(str.str().c_str());
  2280. return false;
  2281. }
  2282. }
  2283. return true;
  2284. }
  2285. void cmMakefile::SetProperty(const char* prop, const char* value)
  2286. {
  2287. if (!prop)
  2288. {
  2289. return;
  2290. }
  2291. if (!value)
  2292. {
  2293. value = "NOTFOUND";
  2294. }
  2295. this->Properties[prop] = value;
  2296. }
  2297. const char *cmMakefile::GetProperty(const char* prop)
  2298. {
  2299. // watch for specific properties
  2300. if (!strcmp("PARENT_DIRECTORY",prop))
  2301. {
  2302. return this->LocalGenerator->GetParent()
  2303. ->GetMakefile()->GetStartDirectory();
  2304. }
  2305. // watch for specific properties
  2306. if (!strcmp("LISTFILE_STACK",prop))
  2307. {
  2308. std::string tmp;
  2309. for (std::deque<cmStdString>::iterator i = this->ListFileStack.begin();
  2310. i != this->ListFileStack.end(); ++i)
  2311. {
  2312. if (i != this->ListFileStack.begin())
  2313. {
  2314. tmp += ";";
  2315. }
  2316. tmp += *i;
  2317. }
  2318. this->SetProperty("LISTFILE_STACK",tmp.c_str());
  2319. }
  2320. std::map<cmStdString,cmStdString>::const_iterator i =
  2321. this->Properties.find(prop);
  2322. if (i != this->Properties.end())
  2323. {
  2324. return i->second.c_str();
  2325. }
  2326. return 0;
  2327. }
  2328. bool cmMakefile::GetPropertyAsBool(const char* prop) const
  2329. {
  2330. std::map<cmStdString,cmStdString>::const_iterator i =
  2331. this->Properties.find(prop);
  2332. if (i != this->Properties.end())
  2333. {
  2334. return cmSystemTools::IsOn(i->second.c_str());
  2335. }
  2336. return false;
  2337. }
  2338. cmTarget* cmMakefile::FindTarget(const char* name)
  2339. {
  2340. cmTargets& tgts = this->GetTargets();
  2341. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  2342. {
  2343. if(l->first == name)
  2344. {
  2345. return &l->second;
  2346. }
  2347. }
  2348. return 0;
  2349. }
  2350. cmTest* cmMakefile::CreateTest(const char* testName)
  2351. {
  2352. if ( !testName )
  2353. {
  2354. return 0;
  2355. }
  2356. cmTest* test = this->GetTest(testName);
  2357. if ( test )
  2358. {
  2359. return test;
  2360. }
  2361. test = new cmTest;
  2362. test->SetName(testName);
  2363. this->Tests.push_back(test);
  2364. return test;
  2365. }
  2366. cmTest* cmMakefile::GetTest(const char* testName) const
  2367. {
  2368. if ( !testName )
  2369. {
  2370. return 0;
  2371. }
  2372. std::vector<cmTest*>::const_iterator it;
  2373. for ( it = this->Tests.begin(); it != this->Tests.end(); ++ it )
  2374. {
  2375. if ( strcmp((*it)->GetName(), testName) == 0 )
  2376. {
  2377. return *it;
  2378. }
  2379. }
  2380. return 0;
  2381. }
  2382. const std::vector<cmTest*> *cmMakefile::GetTests() const
  2383. {
  2384. return &this->Tests;
  2385. }
  2386. std::vector<cmTest*> *cmMakefile::GetTests()
  2387. {
  2388. return &this->Tests;
  2389. }