cmMakefile.cxx 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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 "cmStandardIncludes.h"
  16. #include "cmSourceFile.h"
  17. #include "cmDirectory.h"
  18. #include "cmSystemTools.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmLocalGenerator.h"
  21. #include "cmCommands.h"
  22. #include "cmCacheManager.h"
  23. #include "cmFunctionBlocker.h"
  24. #include "cmListFileCache.h"
  25. #include "cmVariableWatch.h"
  26. #include "cmake.h"
  27. #include <stdio.h> // required for sprintf
  28. #include <stdlib.h> // required for atoi
  29. // default is not to be building executables
  30. cmMakefile::cmMakefile()
  31. {
  32. // Setup the default include file regular expression (match everything).
  33. m_IncludeFileRegularExpression = "^.*$";
  34. // Setup the default include complaint regular expression (match nothing).
  35. m_ComplainFileRegularExpression = "^$";
  36. // Source and header file extensions that we can handle
  37. // The "c" extension MUST precede the "C" extension.
  38. m_SourceFileExtensions.push_back( "c" );
  39. m_SourceFileExtensions.push_back( "C" );
  40. m_SourceFileExtensions.push_back( "c++" );
  41. m_SourceFileExtensions.push_back( "cc" );
  42. m_SourceFileExtensions.push_back( "cpp" );
  43. m_SourceFileExtensions.push_back( "cxx" );
  44. m_SourceFileExtensions.push_back( "m" );
  45. m_SourceFileExtensions.push_back( "M" );
  46. m_SourceFileExtensions.push_back( "mm" );
  47. m_HeaderFileExtensions.push_back( "h" );
  48. m_HeaderFileExtensions.push_back( "h++" );
  49. m_HeaderFileExtensions.push_back( "hm" );
  50. m_HeaderFileExtensions.push_back( "hpp" );
  51. m_HeaderFileExtensions.push_back( "hxx" );
  52. m_HeaderFileExtensions.push_back( "in" );
  53. m_HeaderFileExtensions.push_back( "txx" );
  54. m_DefineFlags = " ";
  55. m_LocalGenerator = 0;
  56. this->AddSourceGroup("", "^.*$");
  57. this->AddSourceGroup("Source Files",
  58. "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
  59. this->AddSourceGroup("Header Files", "\\.(h|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
  60. this->AddDefaultDefinitions();
  61. }
  62. const char* cmMakefile::GetReleaseVersion()
  63. {
  64. #if CMake_VERSION_MINOR & 1
  65. return "development";
  66. #else
  67. return "patch " CMAKE_TO_STRING(CMake_VERSION_PATCH);
  68. #endif
  69. }
  70. unsigned int cmMakefile::GetCacheMajorVersion()
  71. {
  72. if(!this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  73. {
  74. return 0;
  75. }
  76. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  77. }
  78. unsigned int cmMakefile::GetCacheMinorVersion()
  79. {
  80. if(!this->GetCacheManager()->GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  81. {
  82. return 0;
  83. }
  84. return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  85. }
  86. cmMakefile::~cmMakefile()
  87. {
  88. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  89. i != m_SourceFiles.end(); ++i)
  90. {
  91. delete *i;
  92. }
  93. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  94. {
  95. delete m_UsedCommands[i];
  96. }
  97. for(DataMap::const_iterator d = m_DataMap.begin();
  98. d != m_DataMap.end(); ++d)
  99. {
  100. if(d->second)
  101. {
  102. delete d->second;
  103. }
  104. }
  105. std::list<cmFunctionBlocker *>::iterator pos;
  106. for (pos = m_FunctionBlockers.begin();
  107. pos != m_FunctionBlockers.end(); ++pos)
  108. {
  109. cmFunctionBlocker* b = *pos;
  110. delete b;
  111. }
  112. m_FunctionBlockers.clear();
  113. }
  114. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  115. {
  116. std::cout << s << ": ( \n";
  117. for(std::vector<std::string>::const_iterator i = v.begin();
  118. i != v.end(); ++i)
  119. {
  120. std::cout << (*i).c_str() << " ";
  121. }
  122. std::cout << " )\n";
  123. }
  124. // call print on all the classes in the makefile
  125. void cmMakefile::Print() const
  126. {
  127. // print the class lists
  128. std::cout << "classes:\n";
  129. std::cout << " m_Targets: ";
  130. for (cmTargets::const_iterator l = m_Targets.begin();
  131. l != m_Targets.end(); l++)
  132. {
  133. std::cout << l->first << std::endl;
  134. }
  135. std::cout << " m_CurrentOutputDirectory; " <<
  136. m_CurrentOutputDirectory.c_str() << std::endl;
  137. std::cout << " m_StartOutputDirectory; " <<
  138. m_StartOutputDirectory.c_str() << std::endl;
  139. std::cout << " m_HomeOutputDirectory; " <<
  140. m_HomeOutputDirectory.c_str() << std::endl;
  141. std::cout << " m_cmCurrentDirectory; " <<
  142. m_cmCurrentDirectory.c_str() << std::endl;
  143. std::cout << " m_cmStartDirectory; " <<
  144. m_cmStartDirectory.c_str() << std::endl;
  145. std::cout << " m_cmHomeDirectory; " <<
  146. m_cmHomeDirectory.c_str() << std::endl;
  147. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  148. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  149. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  150. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  151. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  152. i != m_SourceGroups.end(); ++i)
  153. {
  154. i->Print();
  155. }
  156. }
  157. bool cmMakefile::CommandExists(const char* name) const
  158. {
  159. return this->GetCMakeInstance()->CommandExists(name);
  160. }
  161. bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff)
  162. {
  163. bool result = true;
  164. // quick return if blocked
  165. if(this->IsFunctionBlocked(lff))
  166. {
  167. // No error.
  168. return result;
  169. }
  170. std::string name = lff.m_Name;
  171. // execute the command
  172. cmCommand *rm =
  173. this->GetCMakeInstance()->GetCommand(name.c_str());
  174. if(rm)
  175. {
  176. cmCommand* usedCommand = rm->Clone();
  177. usedCommand->SetMakefile(this);
  178. bool keepCommand = false;
  179. if(usedCommand->GetEnabled() && !cmSystemTools::GetFatalErrorOccured())
  180. {
  181. // if not running in inherit mode or
  182. // if the command is inherited then InitialPass it.
  183. if(!m_Inheriting || usedCommand->IsInherited())
  184. {
  185. if(!usedCommand->InvokeInitialPass(lff.m_Arguments))
  186. {
  187. cmOStringStream error;
  188. error << "Error in cmake code at\n"
  189. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  190. << usedCommand->GetError();
  191. cmSystemTools::Error(error.str().c_str());
  192. result = false;
  193. }
  194. else
  195. {
  196. // use the command
  197. keepCommand = true;
  198. m_UsedCommands.push_back(usedCommand);
  199. }
  200. }
  201. }
  202. // if the Cloned command was not used
  203. // then delete it
  204. if(!keepCommand)
  205. {
  206. delete usedCommand;
  207. }
  208. }
  209. else
  210. {
  211. if(!cmSystemTools::GetFatalErrorOccured())
  212. {
  213. cmOStringStream error;
  214. error << "Error in cmake code at\n"
  215. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  216. << "Unknown CMake command \"" << lff.m_Name.c_str() << "\".";
  217. cmSystemTools::Error(error.str().c_str());
  218. result = false;
  219. }
  220. }
  221. return result;
  222. }
  223. // Parse the given CMakeLists.txt file into a list of classes.
  224. // Reads in current CMakeLists file and all parent CMakeLists files
  225. // executing all inherited commands in the parents
  226. //
  227. // if external is non-zero, this means that we have branched to grab some
  228. // commands from a remote list-file (that is, the equivalent of a
  229. // #include has been called). We DO NOT look at the parents of this
  230. // list-file, and for all other purposes, the name of this list-file
  231. // is "filename" and not "external".
  232. bool cmMakefile::ReadListFile(const char* filename_in, const char* external_in)
  233. {
  234. // used to watch for blockers going out of scope
  235. // e.g. mismatched IF statement
  236. std::set<cmFunctionBlocker *> originalBlockers;
  237. const char* external = 0;
  238. std::string external_abs;
  239. const char* filename = filename_in;
  240. std::string filename_abs;
  241. if (external_in)
  242. {
  243. external_abs =
  244. cmSystemTools::CollapseFullPath(external_in,
  245. m_cmCurrentDirectory.c_str());
  246. external = external_abs.c_str();
  247. if (filename_in)
  248. {
  249. filename_abs =
  250. cmSystemTools::CollapseFullPath(filename_in,
  251. m_cmCurrentDirectory.c_str());
  252. filename = filename_abs.c_str();
  253. }
  254. }
  255. // keep track of the current file being read
  256. if (filename)
  257. {
  258. if(m_cmCurrentListFile != filename)
  259. {
  260. m_cmCurrentListFile = filename;
  261. }
  262. // loop over current function blockers and record them
  263. std::list<cmFunctionBlocker *>::iterator pos;
  264. for (pos = m_FunctionBlockers.begin();
  265. pos != m_FunctionBlockers.end(); ++pos)
  266. {
  267. originalBlockers.insert(*pos);
  268. }
  269. }
  270. // if this is not a remote makefile
  271. // (if it were, this would be called from the "filename" call,
  272. // rather than the "external" call)
  273. if (!external)
  274. {
  275. // is there a parent CMakeLists file that does not go beyond the
  276. // Home directory? if so recurse and read in that List file
  277. std::string parentList = this->GetParentListFileName(filename);
  278. if (parentList != "")
  279. {
  280. std::string srcdir = m_cmCurrentDirectory;
  281. std::string bindir = m_CurrentOutputDirectory;
  282. std::string::size_type pos = parentList.rfind('/');
  283. m_cmCurrentDirectory = parentList.substr(0, pos);
  284. m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());
  285. // if not found, oops
  286. if(pos == std::string::npos)
  287. {
  288. cmSystemTools::Error("Trailing slash not found");
  289. }
  290. this->ReadListFile(parentList.c_str());
  291. // restore the current directory
  292. m_cmCurrentDirectory = srcdir;
  293. m_CurrentOutputDirectory = bindir;
  294. }
  295. }
  296. // are we at the start CMakeLists file or are we processing a parent
  297. // lists file
  298. //
  299. // this might, or might not be true, irrespective if we are
  300. // off looking at an external makefile.
  301. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  302. // Now read the input file
  303. const char *filenametoread= filename;
  304. if( external)
  305. {
  306. filenametoread= external;
  307. }
  308. // try to see if the list file is the top most
  309. // list file for a project, and if it is, then it
  310. // must have a project command. If there is not
  311. // one, then cmake will provide one via the
  312. // cmListFileCache class.
  313. bool requireProjectCommand = false;
  314. if(!external && m_cmCurrentDirectory == m_cmHomeDirectory)
  315. {
  316. if(cmSystemTools::LowerCase(
  317. cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
  318. {
  319. requireProjectCommand = true;
  320. }
  321. }
  322. cmListFile* lf =
  323. cmListFileCache::GetInstance()->GetFileCache(filenametoread,
  324. requireProjectCommand);
  325. if(!lf)
  326. {
  327. return false;
  328. }
  329. // add this list file to the list of dependencies
  330. m_ListFiles.push_back( filenametoread);
  331. const size_t numberFunctions = lf->m_Functions.size();
  332. for(size_t i =0; i < numberFunctions; ++i)
  333. {
  334. this->ExecuteCommand(lf->m_Functions[i]);
  335. }
  336. // send scope ended to and funciton blockers
  337. if (filename)
  338. {
  339. // loop over all function blockers to see if any block this command
  340. std::list<cmFunctionBlocker *>::iterator pos;
  341. for (pos = m_FunctionBlockers.begin();
  342. pos != m_FunctionBlockers.end(); ++pos)
  343. {
  344. // if this blocker was not in the original then send a
  345. // scope ended message
  346. if (originalBlockers.find(*pos) == originalBlockers.end())
  347. {
  348. (*pos)->ScopeEnded(*this);
  349. }
  350. }
  351. }
  352. return true;
  353. }
  354. void cmMakefile::AddCommand(cmCommand* wg)
  355. {
  356. this->GetCMakeInstance()->AddCommand(wg);
  357. }
  358. // Set the make file
  359. void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
  360. {
  361. m_LocalGenerator = lg;
  362. }
  363. void cmMakefile::FinalPass()
  364. {
  365. // do all the variable expansions here
  366. this->ExpandVariables();
  367. // give all the commands a chance to do something
  368. // after the file has been parsed before generation
  369. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  370. i != m_UsedCommands.end(); ++i)
  371. {
  372. (*i)->FinalPass();
  373. }
  374. }
  375. // Generate the output file
  376. void cmMakefile::ConfigureFinalPass()
  377. {
  378. this->FinalPass();
  379. const char* versionValue
  380. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  381. bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
  382. // merge libraries
  383. for (cmTargets::iterator l = m_Targets.begin();
  384. l != m_Targets.end(); l++)
  385. {
  386. l->second.GenerateSourceFilesFromSourceLists(*this);
  387. // pick up any LINK_LIBRARIES that were added after the target
  388. if(oldVersion)
  389. {
  390. this->AddGlobalLinkInformation(l->first.c_str(), l->second);
  391. }
  392. l->second.AnalyzeLibDependencies(*this);
  393. }
  394. }
  395. void cmMakefile::AddCustomCommand(const char* source,
  396. const char* command,
  397. const std::vector<std::string>& commandArgs,
  398. const std::vector<std::string>& depends,
  399. const std::vector<std::string>& outputs,
  400. const char *target,
  401. const char *comment)
  402. {
  403. // find the target,
  404. if (m_Targets.find(target) != m_Targets.end())
  405. {
  406. std::string expandC = command;
  407. this->ExpandVariablesInString(expandC);
  408. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  409. std::string combinedArgs;
  410. unsigned int i;
  411. for (i = 0; i < commandArgs.size(); ++i)
  412. {
  413. expandC = commandArgs[i].c_str();
  414. this->ExpandVariablesInString(expandC);
  415. combinedArgs += cmSystemTools::EscapeSpaces(expandC.c_str());
  416. combinedArgs += " ";
  417. }
  418. cmCustomCommand cc(source,c.c_str(),combinedArgs.c_str(),depends,outputs);
  419. if ( comment && comment[0] )
  420. {
  421. cc.SetComment(comment);
  422. }
  423. m_Targets[target].GetCustomCommands().push_back(cc);
  424. std::string cacheCommand = command;
  425. this->ExpandVariablesInString(cacheCommand);
  426. if(this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
  427. {
  428. m_Targets[target].AddUtility(
  429. this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()));
  430. }
  431. }
  432. }
  433. void cmMakefile::AddCustomCommand(const char* source,
  434. const char* command,
  435. const std::vector<std::string>& commandArgs,
  436. const std::vector<std::string>& depends,
  437. const char* output,
  438. const char *target)
  439. {
  440. std::vector<std::string> outputs;
  441. outputs.push_back(output);
  442. this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
  443. }
  444. void cmMakefile::AddDefineFlag(const char* flag)
  445. {
  446. m_DefineFlags += " ";
  447. m_DefineFlags += flag;
  448. }
  449. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  450. {
  451. m_LinkLibraries.push_back(
  452. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  453. }
  454. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  455. const char* lib,
  456. cmTarget::LinkLibraryType llt)
  457. {
  458. cmTargets::iterator i = m_Targets.find(target);
  459. if ( i != m_Targets.end())
  460. {
  461. i->second.AddLinkLibrary( *this, target, lib, llt );
  462. }
  463. else
  464. {
  465. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  466. }
  467. }
  468. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  469. const char* d)
  470. {
  471. cmTargets::iterator i = m_Targets.find(target);
  472. if ( i != m_Targets.end())
  473. {
  474. i->second.AddLinkDirectory( d );
  475. }
  476. else
  477. {
  478. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  479. target, " for directory ", d);
  480. }
  481. }
  482. void cmMakefile::AddLinkLibrary(const char* lib)
  483. {
  484. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  485. }
  486. void cmMakefile::AddLinkDirectory(const char* dir)
  487. {
  488. // Don't add a link directory that is already present. Yes, this
  489. // linear search results in n^2 behavior, but n won't be getting
  490. // much bigger than 20. We cannot use a set because of order
  491. // dependency of the link search path.
  492. // remove trailing slashes
  493. if(dir && dir[strlen(dir)-1] == '/')
  494. {
  495. std::string newdir = dir;
  496. newdir = newdir.substr(0, newdir.size()-1);
  497. if(std::find(m_LinkDirectories.begin(),
  498. m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
  499. {
  500. m_LinkDirectories.push_back(newdir);
  501. }
  502. }
  503. else
  504. {
  505. if(std::find(m_LinkDirectories.begin(),
  506. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  507. {
  508. m_LinkDirectories.push_back(dir);
  509. }
  510. }
  511. }
  512. void cmMakefile::AddSubDirectory(const char* sub)
  513. {
  514. // make sure it isn't already there
  515. if (std::find(m_SubDirectories.begin(),
  516. m_SubDirectories.end(), sub) == m_SubDirectories.end())
  517. {
  518. m_SubDirectories.push_back(sub);
  519. }
  520. }
  521. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  522. {
  523. // Don't add an include directory that is already present. Yes,
  524. // this linear search results in n^2 behavior, but n won't be
  525. // getting much bigger than 20. We cannot use a set because of
  526. // order dependency of the include path.
  527. if(std::find(m_IncludeDirectories.begin(),
  528. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  529. {
  530. if (before)
  531. {
  532. // WARNING: this *is* expensive (linear time) since it's a vector
  533. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  534. }
  535. else
  536. {
  537. m_IncludeDirectories.push_back(inc);
  538. }
  539. }
  540. }
  541. void cmMakefile::AddDefinition(const char* name, const char* value)
  542. {
  543. if (!value )
  544. {
  545. return;
  546. }
  547. m_Definitions.erase( DefinitionMap::key_type(name));
  548. m_Definitions.insert(DefinitionMap::value_type(name, value));
  549. cmVariableWatch* vv = this->GetVariableWatch();
  550. if ( vv )
  551. {
  552. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  553. }
  554. }
  555. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  556. const char* doc,
  557. cmCacheManager::CacheEntryType type)
  558. {
  559. this->GetCacheManager()->AddCacheEntry(name, value, doc, type);
  560. this->AddDefinition(name, value);
  561. }
  562. void cmMakefile::AddDefinition(const char* name, bool value)
  563. {
  564. if(value)
  565. {
  566. m_Definitions.erase( DefinitionMap::key_type(name));
  567. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  568. }
  569. else
  570. {
  571. m_Definitions.erase( DefinitionMap::key_type(name));
  572. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  573. }
  574. cmVariableWatch* vv = this->GetVariableWatch();
  575. if ( vv )
  576. {
  577. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_MODIFIED_ACCESS);
  578. }
  579. }
  580. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  581. {
  582. this->GetCacheManager()->AddCacheEntry(name, value, doc);
  583. this->AddDefinition(name, value);
  584. }
  585. void cmMakefile::RemoveDefinition(const char* name)
  586. {
  587. m_Definitions.erase(DefinitionMap::key_type(name));
  588. cmVariableWatch* vv = this->GetVariableWatch();
  589. if ( vv )
  590. {
  591. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_REMOVED_ACCESS);
  592. }
  593. }
  594. void cmMakefile::SetProjectName(const char* p)
  595. {
  596. m_ProjectName = p;
  597. }
  598. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  599. {
  600. // for these targets do not add anything
  601. switch(target.GetType())
  602. {
  603. case cmTarget::UTILITY:
  604. case cmTarget::INSTALL_FILES:
  605. case cmTarget::INSTALL_PROGRAMS:
  606. return;
  607. default:;
  608. }
  609. std::vector<std::string>::iterator j;
  610. for(j = m_LinkDirectories.begin();
  611. j != m_LinkDirectories.end(); ++j)
  612. {
  613. target.AddLinkDirectory(j->c_str());
  614. }
  615. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  616. }
  617. void cmMakefile::AddLibrary(const char* lname, int shared,
  618. const std::vector<std::string> &srcs)
  619. {
  620. cmTarget target;
  621. switch (shared)
  622. {
  623. case 0:
  624. target.SetType(cmTarget::STATIC_LIBRARY);
  625. break;
  626. case 1:
  627. target.SetType(cmTarget::SHARED_LIBRARY);
  628. break;
  629. case 2:
  630. target.SetType(cmTarget::MODULE_LIBRARY);
  631. break;
  632. default:
  633. target.SetType(cmTarget::STATIC_LIBRARY);
  634. }
  635. // Clear its dependencies. Otherwise, dependencies might persist
  636. // over changes in CMakeLists.txt, making the information stale and
  637. // hence useless.
  638. target.ClearDependencyInformation( *this, lname );
  639. target.SetInAll(true);
  640. target.GetSourceLists() = srcs;
  641. this->AddGlobalLinkInformation(lname, target);
  642. m_Targets.insert(cmTargets::value_type(lname,target));
  643. // Add an entry into the cache
  644. std::string libPath = lname;
  645. libPath += "_CMAKE_PATH";
  646. this->GetCacheManager()->
  647. AddCacheEntry(libPath.c_str(),
  648. this->GetCurrentOutputDirectory(),
  649. "Path to a library", cmCacheManager::INTERNAL);
  650. // Add an entry into the cache
  651. std::string ltname = lname;
  652. ltname += "_LIBRARY_TYPE";
  653. switch (shared)
  654. {
  655. case 0:
  656. this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
  657. "Whether a library is static, shared or module.",
  658. cmCacheManager::INTERNAL);
  659. break;
  660. case 1:
  661. this->GetCacheManager()->
  662. AddCacheEntry(ltname.c_str(),
  663. "SHARED",
  664. "Whether a library is static, shared or module.",
  665. cmCacheManager::INTERNAL);
  666. break;
  667. case 2:
  668. this->GetCacheManager()->
  669. AddCacheEntry(ltname.c_str(),
  670. "MODULE",
  671. "Whether a library is static, shared or module.",
  672. cmCacheManager::INTERNAL);
  673. break;
  674. default:
  675. this->GetCacheManager()->
  676. AddCacheEntry(ltname.c_str(),
  677. "STATIC",
  678. "Whether a library is static, shared or module.",
  679. cmCacheManager::INTERNAL);
  680. }
  681. }
  682. void cmMakefile::AddExecutable(const char *exeName,
  683. const std::vector<std::string> &srcs)
  684. {
  685. this->AddExecutable(exeName,srcs,false);
  686. }
  687. void cmMakefile::AddExecutable(const char *exeName,
  688. const std::vector<std::string> &srcs,
  689. bool win32)
  690. {
  691. cmTarget target;
  692. if (win32)
  693. {
  694. target.SetType(cmTarget::WIN32_EXECUTABLE);
  695. }
  696. else
  697. {
  698. target.SetType(cmTarget::EXECUTABLE);
  699. }
  700. target.SetInAll(true);
  701. target.GetSourceLists() = srcs;
  702. this->AddGlobalLinkInformation(exeName, target);
  703. m_Targets.insert(cmTargets::value_type(exeName,target));
  704. // Add an entry into the cache
  705. std::string exePath = exeName;
  706. exePath += "_CMAKE_PATH";
  707. this->GetCacheManager()->
  708. AddCacheEntry(exePath.c_str(),
  709. this->GetCurrentOutputDirectory(),
  710. "Path to an executable", cmCacheManager::INTERNAL);
  711. }
  712. void cmMakefile::AddUtilityCommand(const char* utilityName,
  713. const char* command,
  714. const char* arguments,
  715. bool all)
  716. {
  717. std::vector<std::string> empty;
  718. this->AddUtilityCommand(utilityName,command,arguments,all,
  719. empty,empty);
  720. }
  721. void cmMakefile::AddUtilityCommand(const char* utilityName,
  722. const char* command,
  723. const char* arguments,
  724. bool all,
  725. const std::vector<std::string> &dep,
  726. const std::vector<std::string> &out)
  727. {
  728. cmTarget target;
  729. target.SetType(cmTarget::UTILITY);
  730. target.SetInAll(all);
  731. cmCustomCommand cc(utilityName, command, arguments, dep, out);
  732. target.GetCustomCommands().push_back(cc);
  733. m_Targets.insert(cmTargets::value_type(utilityName,target));
  734. }
  735. cmSourceGroup* cmMakefile::GetSourceGroup(const char* name)
  736. {
  737. // First see if the group exists. If so, replace its regular expression.
  738. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  739. sg != m_SourceGroups.end(); ++sg)
  740. {
  741. std::string sgName = sg->GetName();
  742. if(sgName == name)
  743. {
  744. return &(*sg);
  745. }
  746. }
  747. return 0;
  748. }
  749. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  750. {
  751. // First see if the group exists. If so, replace its regular expression.
  752. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  753. sg != m_SourceGroups.end(); ++sg)
  754. {
  755. std::string sgName = sg->GetName();
  756. if(sgName == name)
  757. {
  758. if ( regex )
  759. {
  760. // We only want to set the regular expression. If there are already
  761. // source files in the group, we don't want to remove them.
  762. sg->SetGroupRegex(regex);
  763. }
  764. return;
  765. }
  766. }
  767. // The group doesn't exist. Add it.
  768. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  769. }
  770. void cmMakefile::AddExtraDirectory(const char* dir)
  771. {
  772. m_AuxSourceDirectories.push_back(dir);
  773. }
  774. // return the file name for the parent CMakeLists file to the
  775. // one passed in. Zero is returned if the CMakeLists file is the
  776. // one in the home directory or if for some reason a parent cmake lists
  777. // file cannot be found.
  778. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  779. {
  780. // extract the directory name
  781. std::string parentFile;
  782. std::string listsDir = currentFileName;
  783. std::string::size_type pos = listsDir.rfind('/');
  784. // if we could not find the directory return 0
  785. if(pos == std::string::npos)
  786. {
  787. return parentFile;
  788. }
  789. listsDir = listsDir.substr(0, pos);
  790. // if we are in the home directory then stop, return 0
  791. if(m_cmHomeDirectory == listsDir)
  792. {
  793. return parentFile;
  794. }
  795. // is there a parent directory we can check
  796. pos = listsDir.rfind('/');
  797. // if we could not find the directory return 0
  798. if(pos == std::string::npos)
  799. {
  800. return parentFile;
  801. }
  802. listsDir = listsDir.substr(0, pos);
  803. // is there a CMakeLists.txt file in the parent directory ?
  804. parentFile = listsDir;
  805. parentFile += "/CMakeLists.txt";
  806. while(!cmSystemTools::FileExists(parentFile.c_str()))
  807. {
  808. // There is no CMakeLists.txt file in the parent directory. This
  809. // can occur when coming out of a subdirectory resulting from a
  810. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  811. // walking up until a CMakeLists.txt is found or the home
  812. // directory is hit.
  813. // if we are in the home directory then stop, return 0
  814. if(m_cmHomeDirectory == listsDir) { return ""; }
  815. // is there a parent directory we can check
  816. pos = listsDir.rfind('/');
  817. // if we could not find the directory return 0
  818. if(pos == std::string::npos) { return ""; }
  819. listsDir = listsDir.substr(0, pos);
  820. parentFile = listsDir;
  821. parentFile += "/CMakeLists.txt";
  822. }
  823. return parentFile;
  824. }
  825. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  826. // include and library directories.
  827. void cmMakefile::ExpandVariables()
  828. {
  829. // Now expand variables in the include and link strings
  830. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  831. d != m_IncludeDirectories.end(); ++d)
  832. {
  833. this->ExpandVariablesInString(*d);
  834. }
  835. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  836. d != m_LinkDirectories.end(); ++d)
  837. {
  838. this->ExpandVariablesInString(*d);
  839. }
  840. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  841. l != m_LinkLibraries.end(); ++l)
  842. {
  843. this->ExpandVariablesInString(l->first);
  844. }
  845. }
  846. bool cmMakefile::IsOn(const char* name) const
  847. {
  848. const char* value = this->GetDefinition(name);
  849. return cmSystemTools::IsOn(value);
  850. }
  851. const char* cmMakefile::GetDefinition(const char* name) const
  852. {
  853. const char* def = 0;
  854. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  855. if(pos != m_Definitions.end())
  856. {
  857. def = (*pos).second.c_str();
  858. }
  859. else
  860. {
  861. def = this->GetCacheManager()->GetCacheValue(name);
  862. }
  863. cmVariableWatch* vv = this->GetVariableWatch();
  864. if ( vv )
  865. {
  866. if ( def )
  867. {
  868. vv->VariableAccessed(name, cmVariableWatch::VARIABLE_READ_ACCESS);
  869. }
  870. else
  871. {
  872. // are unknown access allowed
  873. DefinitionMap::const_iterator pos2 =
  874. m_Definitions.find("CMAKE_ALLOW_UNKNOWN_VARIABLE_READ_ACCESS");
  875. if (pos2 != m_Definitions.end() &&
  876. cmSystemTools::IsOn((*pos2).second.c_str()))
  877. {
  878. vv->VariableAccessed(name,
  879. cmVariableWatch::ALLOWED_UNKNOWN_VARIABLE_READ_ACCESS);
  880. }
  881. else
  882. {
  883. vv->VariableAccessed(name, cmVariableWatch::
  884. UNKNOWN_VARIABLE_READ_ACCESS);
  885. }
  886. }
  887. }
  888. return def;
  889. }
  890. std::vector<std::string> cmMakefile::GetDefinitions(int cacheonly /* = 0 */) const
  891. {
  892. std::map<std::string, int> definitions;
  893. if ( !cacheonly )
  894. {
  895. DefinitionMap::const_iterator it;
  896. for ( it = m_Definitions.begin(); it != m_Definitions.end(); it ++ )
  897. {
  898. definitions[it->first] = 1;
  899. }
  900. }
  901. cmCacheManager::CacheIterator cit = this->GetCacheManager()->GetCacheIterator();
  902. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  903. {
  904. definitions[cit.GetName()] = 1;
  905. }
  906. std::vector<std::string> res;
  907. std::map<std::string, int>::iterator fit;
  908. for ( fit = definitions.begin(); fit != definitions.end(); fit ++ )
  909. {
  910. res.push_back(fit->first);
  911. }
  912. return res;
  913. }
  914. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  915. {
  916. return this->ExpandVariablesInString(source, false);
  917. }
  918. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  919. bool escapeQuotes,
  920. bool atOnly) const
  921. {
  922. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  923. // with GetDefinition(), if not found in the map, nothing is expanded.
  924. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  925. // the current environment variables.
  926. // start by look for $ or @ in the string
  927. std::string::size_type markerPos;
  928. if(atOnly)
  929. {
  930. markerPos = source.find_first_of("@");
  931. }
  932. else
  933. {
  934. markerPos = source.find_first_of("$@");
  935. }
  936. // if not found, or found as the last character, then leave quickly as
  937. // nothing needs to be expanded
  938. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  939. {
  940. return source.c_str();
  941. }
  942. // current position
  943. std::string::size_type currentPos =0; // start at 0
  944. std::string result; // string with replacements
  945. // go until the the end of the string
  946. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  947. {
  948. // grab string from currentPos to the start of the variable
  949. // and add it to the result
  950. result += source.substr(currentPos, markerPos - currentPos);
  951. char endVariableMarker; // what is the end of the variable @ or }
  952. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  953. if(!atOnly && source[markerPos] == '$')
  954. {
  955. // ${var} case
  956. if(source[markerPos+1] == '{')
  957. {
  958. endVariableMarker = '}';
  959. markerStartSize = 2;
  960. }
  961. // $ENV{var} case
  962. else if(markerPos+4 < source.size() &&
  963. source[markerPos+4] == '{' &&
  964. !source.substr(markerPos+1, 3).compare("ENV"))
  965. {
  966. endVariableMarker = '}';
  967. markerStartSize = 5;
  968. }
  969. else
  970. {
  971. // bogus $ with no { so add $ to result and move on
  972. result += '$'; // add bogus $ back into string
  973. currentPos = markerPos+1; // move on
  974. endVariableMarker = ' '; // set end var to space so we can tell bogus
  975. }
  976. }
  977. else
  978. {
  979. // @VAR case
  980. endVariableMarker = '@';
  981. }
  982. // if it was a valid variable (started with @ or ${ or $ENV{ )
  983. if(endVariableMarker != ' ')
  984. {
  985. markerPos += markerStartSize; // move past marker
  986. // find the end variable marker starting at the markerPos
  987. std::string::size_type endVariablePos =
  988. source.find(endVariableMarker, markerPos);
  989. if(endVariablePos == std::string::npos)
  990. {
  991. // no end marker found so add the bogus start
  992. if(endVariableMarker == '@')
  993. {
  994. result += '@';
  995. }
  996. else
  997. {
  998. result += (markerStartSize == 5 ? "$ENV{" : "${");
  999. }
  1000. currentPos = markerPos;
  1001. }
  1002. else
  1003. {
  1004. // good variable remove it
  1005. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  1006. bool found = false;
  1007. if (markerStartSize == 5) // $ENV{
  1008. {
  1009. char *ptr = getenv(var.c_str());
  1010. if (ptr)
  1011. {
  1012. if (escapeQuotes)
  1013. {
  1014. result += cmSystemTools::EscapeQuotes(ptr);
  1015. }
  1016. else
  1017. {
  1018. result += ptr;
  1019. }
  1020. found = true;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. const char* lookup = this->GetDefinition(var.c_str());
  1026. if(lookup)
  1027. {
  1028. if (escapeQuotes)
  1029. {
  1030. result += cmSystemTools::EscapeQuotes(lookup);
  1031. }
  1032. else
  1033. {
  1034. result += lookup;
  1035. }
  1036. found = true;
  1037. }
  1038. }
  1039. // if found add to result, if not, then it gets blanked
  1040. if (!found)
  1041. {
  1042. // if no definition is found then add the var back
  1043. if(endVariableMarker == '@')
  1044. {
  1045. result += "@";
  1046. result += var;
  1047. result += "@";
  1048. }
  1049. // do nothing, we remove the variable
  1050. /* else
  1051. {
  1052. result += (markerStartSize == 5 ? "$ENV{" : "${");
  1053. result += var;
  1054. result += "}";
  1055. }
  1056. */
  1057. }
  1058. // lookup var, and replace it
  1059. currentPos = endVariablePos+1;
  1060. }
  1061. }
  1062. if(atOnly)
  1063. {
  1064. markerPos = source.find_first_of("@", currentPos);
  1065. }
  1066. else
  1067. {
  1068. markerPos = source.find_first_of("$@", currentPos);
  1069. }
  1070. }
  1071. result += source.substr(currentPos); // pick up the rest of the string
  1072. source = result;
  1073. return source.c_str();
  1074. }
  1075. void cmMakefile::RemoveVariablesInString(std::string& source,
  1076. bool atOnly) const
  1077. {
  1078. if(!atOnly)
  1079. {
  1080. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  1081. while (var.find(source))
  1082. {
  1083. source.erase(var.start(),var.end() - var.start());
  1084. }
  1085. }
  1086. if(!atOnly)
  1087. {
  1088. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1089. while (varb.find(source))
  1090. {
  1091. source.erase(varb.start(),varb.end() - varb.start());
  1092. }
  1093. }
  1094. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  1095. while (var2.find(source))
  1096. {
  1097. source.erase(var2.start(),var2.end() - var2.start());
  1098. }
  1099. }
  1100. /**
  1101. * Add the default definitions to the makefile. These values must not
  1102. * be dependent on anything that isn't known when this cmMakefile instance
  1103. * is constructed.
  1104. */
  1105. void cmMakefile::AddDefaultDefinitions()
  1106. {
  1107. #if defined(_WIN32) || defined(__CYGWIN__)
  1108. this->AddDefinition("WIN32", "1");
  1109. #else
  1110. this->AddDefinition("UNIX", "1");
  1111. #endif
  1112. // Cygwin is more like unix so enable the unix commands
  1113. #if defined(__CYGWIN__)
  1114. this->AddDefinition("UNIX", "1");
  1115. this->AddDefinition("CYGWIN", "1");
  1116. #endif
  1117. #if defined(__APPLE__)
  1118. this->AddDefinition("APPLE", "1");
  1119. #endif
  1120. char temp[1024];
  1121. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1122. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1123. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1124. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1125. }
  1126. /**
  1127. * Find a source group whose regular expression matches the filename
  1128. * part of the given source name. Search backward through the list of
  1129. * source groups, and take the first matching group found. This way
  1130. * non-inherited SOURCE_GROUP commands will have precedence over
  1131. * inherited ones.
  1132. */
  1133. cmSourceGroup&
  1134. cmMakefile::FindSourceGroup(const char* source,
  1135. std::vector<cmSourceGroup> &groups)
  1136. {
  1137. std::string file = source;
  1138. std::string::size_type pos = file.rfind('/');
  1139. if(pos != std::string::npos)
  1140. {
  1141. file = file.substr(pos, file.length()-pos);
  1142. }
  1143. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1144. sg != groups.rend(); ++sg)
  1145. {
  1146. if(sg->Matches(file.c_str()))
  1147. {
  1148. return *sg;
  1149. }
  1150. }
  1151. // Shouldn't get here, but just in case, return the default group.
  1152. return groups.front();
  1153. }
  1154. bool cmMakefile::IsFunctionBlocked(const cmListFileFunction& lff)
  1155. {
  1156. // if there are no blockers get out of here
  1157. if (m_FunctionBlockers.begin() == m_FunctionBlockers.end())
  1158. {
  1159. return false;
  1160. }
  1161. // loop over all function blockers to see if any block this command
  1162. // evaluate in reverse, this is critical for balanced IF statements etc
  1163. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1164. for (pos = m_FunctionBlockers.rbegin();
  1165. pos != m_FunctionBlockers.rend(); ++pos)
  1166. {
  1167. if((*pos)->IsFunctionBlocked(lff, *this))
  1168. {
  1169. return true;
  1170. }
  1171. }
  1172. return false;
  1173. }
  1174. void cmMakefile::ExpandArguments(
  1175. std::vector<cmListFileArgument> const& inArgs,
  1176. std::vector<std::string>& outArgs)
  1177. {
  1178. std::vector<cmListFileArgument>::const_iterator i;
  1179. for(i = inArgs.begin(); i != inArgs.end(); ++i)
  1180. {
  1181. // Expand the variables in the argument.
  1182. std::string value = i->Value;
  1183. this->ExpandVariablesInString(value);
  1184. // If the argument is quoted, it should be one argument.
  1185. // Otherwise, it may be a list of arguments.
  1186. if(i->Quoted)
  1187. {
  1188. outArgs.push_back(value);
  1189. }
  1190. else
  1191. {
  1192. cmSystemTools::ExpandListArgument(value, outArgs);
  1193. }
  1194. }
  1195. }
  1196. void cmMakefile::RemoveFunctionBlocker(const cmListFileFunction& lff)
  1197. {
  1198. // loop over all function blockers to see if any block this command
  1199. std::list<cmFunctionBlocker *>::reverse_iterator pos;
  1200. for (pos = m_FunctionBlockers.rbegin();
  1201. pos != m_FunctionBlockers.rend(); ++pos)
  1202. {
  1203. if ((*pos)->ShouldRemove(lff, *this))
  1204. {
  1205. cmFunctionBlocker* b = *pos;
  1206. m_FunctionBlockers.remove(b);
  1207. delete b;
  1208. break;
  1209. }
  1210. }
  1211. return;
  1212. }
  1213. void cmMakefile::SetHomeDirectory(const char* dir)
  1214. {
  1215. m_cmHomeDirectory = dir;
  1216. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1217. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1218. }
  1219. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1220. {
  1221. m_HomeOutputDirectory = lib;
  1222. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1223. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1224. }
  1225. /**
  1226. * Register the given cmData instance with its own name.
  1227. */
  1228. void cmMakefile::RegisterData(cmData* data)
  1229. {
  1230. std::string name = data->GetName();
  1231. DataMap::const_iterator d = m_DataMap.find(name);
  1232. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1233. {
  1234. delete d->second;
  1235. }
  1236. m_DataMap[name] = data;
  1237. }
  1238. /**
  1239. * Register the given cmData instance with the given name. This can be used
  1240. * to register a NULL pointer.
  1241. */
  1242. void cmMakefile::RegisterData(const char* name, cmData* data)
  1243. {
  1244. DataMap::const_iterator d = m_DataMap.find(name);
  1245. if((d != m_DataMap.end()) && (d->second != 0) && (d->second != data))
  1246. {
  1247. delete d->second;
  1248. }
  1249. m_DataMap[name] = data;
  1250. }
  1251. /**
  1252. * Lookup a cmData instance previously registered with the given name. If
  1253. * the instance cannot be found, return NULL.
  1254. */
  1255. cmData* cmMakefile::LookupData(const char* name) const
  1256. {
  1257. DataMap::const_iterator d = m_DataMap.find(name);
  1258. if(d != m_DataMap.end())
  1259. {
  1260. return d->second;
  1261. }
  1262. else
  1263. {
  1264. return 0;
  1265. }
  1266. }
  1267. cmSourceFile* cmMakefile::GetSource(const char* sourceName) const
  1268. {
  1269. std::string s = cmSystemTools::GetFilenameName(sourceName);
  1270. std::string ext;
  1271. std::string::size_type pos = s.rfind('.');
  1272. if(pos != std::string::npos)
  1273. {
  1274. ext = s.substr(pos+1, s.size() - pos-1);
  1275. s = s.substr(0, pos);
  1276. }
  1277. for(std::vector<cmSourceFile*>::const_iterator i = m_SourceFiles.begin();
  1278. i != m_SourceFiles.end(); ++i)
  1279. {
  1280. if ((*i)->GetSourceName() == s)
  1281. {
  1282. if ((ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1283. {
  1284. return *i;
  1285. }
  1286. }
  1287. }
  1288. return 0;
  1289. }
  1290. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1291. {
  1292. // check to see if it exists
  1293. cmSourceFile* ret = this->GetSource(sf.GetSourceName().c_str());
  1294. if(ret && ret->GetSourceExtension() == sf.GetSourceExtension())
  1295. {
  1296. return ret;
  1297. }
  1298. ret = new cmSourceFile(sf);
  1299. m_SourceFiles.push_back(ret);
  1300. return ret;
  1301. }
  1302. void cmMakefile::EnableLanguage(const char* lang)
  1303. {
  1304. m_LocalGenerator->GetGlobalGenerator()->EnableLanguage(lang, this);
  1305. }
  1306. void cmMakefile::ExpandSourceListArguments(
  1307. std::vector<std::string> const& arguments,
  1308. std::vector<std::string>& newargs, unsigned int start)
  1309. {
  1310. // first figure out if we need to handle version 1.2 style source lists
  1311. int oldVersion = 1;
  1312. const char* versionValue
  1313. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  1314. if (versionValue && atof(versionValue) > 1.2)
  1315. {
  1316. oldVersion = 0;
  1317. }
  1318. // now expand the args
  1319. unsigned int i;
  1320. for(i = 0; i < arguments.size(); ++i)
  1321. {
  1322. // is the arg defined ?, if so use the def
  1323. const char *def = this->GetDefinition(arguments[i].c_str());
  1324. if (def && oldVersion && i >= start)
  1325. {
  1326. // Definition lookup could result in a list that needs to be
  1327. // expanded.
  1328. cmSystemTools::ExpandListArgument(def, newargs);
  1329. }
  1330. else
  1331. {
  1332. // List expansion will have been done already.
  1333. newargs.push_back(arguments[i]);
  1334. }
  1335. }
  1336. }
  1337. int cmMakefile::TryCompile(const char *srcdir, const char *bindir,
  1338. const char *projectName, const char *targetName,
  1339. const std::vector<std::string> *cmakeArgs,
  1340. std::string *output)
  1341. {
  1342. // does the binary directory exist ? If not create it...
  1343. if (!cmSystemTools::FileIsDirectory(bindir))
  1344. {
  1345. cmSystemTools::MakeDirectory(bindir);
  1346. }
  1347. // change to the tests directory and run cmake
  1348. // use the cmake object instead of calling cmake
  1349. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1350. cmSystemTools::ChangeDirectory(bindir);
  1351. // make sure the same generator is used
  1352. // use this program as the cmake to be run, it should not
  1353. // be run that way but the cmake object requires a vailid path
  1354. std::string cmakeCommand = this->GetDefinition("CMAKE_COMMAND");
  1355. cmake cm;
  1356. cm.SetIsInTryCompile(true);
  1357. cmGlobalGenerator *gg =
  1358. cm.CreateGlobalGenerator(m_LocalGenerator->GetGlobalGenerator()->GetName());
  1359. if (!gg)
  1360. {
  1361. cmSystemTools::Error(
  1362. "Internal CMake error, TryCompile bad GlobalGenerator");
  1363. // return to the original directory
  1364. cmSystemTools::ChangeDirectory(cwd.c_str());
  1365. return 1;
  1366. }
  1367. cm.SetGlobalGenerator(gg);
  1368. // do a configure
  1369. cm.SetHomeDirectory(srcdir);
  1370. cm.SetHomeOutputDirectory(bindir);
  1371. cm.SetStartDirectory(srcdir);
  1372. cm.SetStartOutputDirectory(bindir);
  1373. cm.SetCMakeCommand(cmakeCommand.c_str());
  1374. cm.LoadCache();
  1375. // if cmake args were provided then pass them in
  1376. if (cmakeArgs)
  1377. {
  1378. cm.SetCacheArgs(*cmakeArgs);
  1379. }
  1380. // to save time we pass the EnableLanguage info directly
  1381. gg->EnableLanguagesFromGenerator(m_LocalGenerator->GetGlobalGenerator());
  1382. if (cm.Configure() != 0)
  1383. {
  1384. cmSystemTools::Error(
  1385. "Internal CMake error, TryCompile configure of cmake failed");
  1386. // return to the original directory
  1387. cmSystemTools::ChangeDirectory(cwd.c_str());
  1388. return 1;
  1389. }
  1390. if (cm.Generate() != 0)
  1391. {
  1392. cmSystemTools::Error(
  1393. "Internal CMake error, TryCompile generation of cmake failed");
  1394. // return to the original directory
  1395. cmSystemTools::ChangeDirectory(cwd.c_str());
  1396. return 1;
  1397. }
  1398. // finally call the generator to actually build the resulting project
  1399. int ret =
  1400. m_LocalGenerator->GetGlobalGenerator()->TryCompile(srcdir,bindir,
  1401. projectName,
  1402. targetName,
  1403. output);
  1404. cmSystemTools::ChangeDirectory(cwd.c_str());
  1405. return ret;
  1406. }
  1407. cmake *cmMakefile::GetCMakeInstance() const
  1408. {
  1409. if ( m_LocalGenerator && m_LocalGenerator->GetGlobalGenerator() )
  1410. {
  1411. return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance();
  1412. }
  1413. return 0;
  1414. }
  1415. cmVariableWatch *cmMakefile::GetVariableWatch() const
  1416. {
  1417. if ( this->GetCMakeInstance() &&
  1418. this->GetCMakeInstance()->GetVariableWatch() )
  1419. {
  1420. return this->GetCMakeInstance()->GetVariableWatch();
  1421. }
  1422. return 0;
  1423. }
  1424. cmCacheManager *cmMakefile::GetCacheManager() const
  1425. {
  1426. return this->GetCMakeInstance()->GetCacheManager();
  1427. }
  1428. bool cmMakefile::GetLocal() const
  1429. {
  1430. return this->GetCMakeInstance()->GetLocal();
  1431. }
  1432. void cmMakefile::DisplayStatus(const char* message, float s)
  1433. {
  1434. this->GetLocalGenerator()->GetGlobalGenerator()
  1435. ->GetCMakeInstance()->UpdateProgress(message, s);
  1436. }
  1437. /**
  1438. * Find the library with the given name. Searches the given path and then
  1439. * the system search path. Returns the full path to the library if it is
  1440. * found. Otherwise, the empty string is returned.
  1441. */
  1442. std::string cmMakefile::FindLibrary(const char* name,
  1443. const std::vector<std::string>& userPaths)
  1444. {
  1445. // See if the executable exists as written.
  1446. if(cmSystemTools::FileExists(name))
  1447. {
  1448. return cmSystemTools::CollapseFullPath(name);
  1449. }
  1450. // Add the system search path to our path.
  1451. std::vector<std::string> path = userPaths;
  1452. cmSystemTools::GetPath(path);
  1453. // Add some lib directories specific to compilers, depending on the
  1454. // current generator, so that library that might have been stored here
  1455. // can be found too.
  1456. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1457. // Borland: path to compiler/../Lib
  1458. const char* genName = this->GetDefinition("CMAKE_GENERATOR");
  1459. if (genName)
  1460. {
  1461. if (!strcmp(genName, "NMake Makefiles") ||
  1462. !strcmp(genName, "Visual Studio 6"))
  1463. {
  1464. const char* compiler = this->GetDefinition("CMAKE_CXX_COMPILER");
  1465. if (compiler)
  1466. {
  1467. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1468. if (compiler_path.size())
  1469. {
  1470. std::string lib_path =
  1471. cmSystemTools::GetFilenamePath(
  1472. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1473. path.push_back(lib_path);
  1474. }
  1475. }
  1476. }
  1477. else if (!strcmp(genName, "Visual Studio 7"))
  1478. {
  1479. // It is likely that the compiler won't be in the path for .Net, but
  1480. // we know where devenv is.
  1481. const char* devenv = this->GetDefinition("MICROSOFT_DEVENV");
  1482. if (devenv)
  1483. {
  1484. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1485. if (devenv_path.size())
  1486. {
  1487. std::string vc7_path =
  1488. cmSystemTools::GetFilenamePath(
  1489. cmSystemTools::GetFilenamePath(
  1490. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1491. path.push_back(vc7_path + "/lib");
  1492. path.push_back(vc7_path + "/PlatformSDK/lib");
  1493. }
  1494. }
  1495. }
  1496. else if (!strcmp(genName, "Borland Makefiles"))
  1497. {
  1498. const char* bcb_bin_path = this->GetDefinition("BCB_BIN_PATH");
  1499. if (bcb_bin_path)
  1500. {
  1501. std::string lib_path =
  1502. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1503. path.push_back(lib_path);
  1504. }
  1505. }
  1506. }
  1507. return cmSystemTools::FindLibrary(name, path);
  1508. }