cmMakefile.cxx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmMakefileGenerator.h"
  20. #include "cmCommands.h"
  21. #include "cmCacheManager.h"
  22. #include "cmFunctionBlocker.h"
  23. #include "cmListFileCache.h"
  24. #include <stdio.h> // required for sprintf
  25. // default is not to be building executables
  26. cmMakefile::cmMakefile()
  27. {
  28. // Setup the default include file regular expression (match everything).
  29. m_IncludeFileRegularExpression = "^.*$";
  30. // Setup the default include complaint regular expression (match nothing).
  31. m_ComplainFileRegularExpression = "^$";
  32. // Source and header file extensions that we can handle
  33. m_SourceFileExtensions.push_back( "cxx" );
  34. m_SourceFileExtensions.push_back( "cpp" );
  35. m_SourceFileExtensions.push_back( "c" );
  36. m_SourceFileExtensions.push_back( "M" );
  37. m_SourceFileExtensions.push_back( "m" );
  38. m_SourceFileExtensions.push_back( "mm" );
  39. m_HeaderFileExtensions.push_back( "h" );
  40. m_HeaderFileExtensions.push_back( "txx" );
  41. m_HeaderFileExtensions.push_back( "in" );
  42. m_DefineFlags = " ";
  43. m_MakefileGenerator = 0;
  44. this->AddSourceGroup("", "^.*$");
  45. this->AddSourceGroup("Source Files", "\\.(cpp|C|c|cxx|rc|def|r|odl|idl|hpj|bat)$");
  46. this->AddSourceGroup("Header Files", "\\.(h|hh|hpp|hxx|hm|inl)$");
  47. this->AddDefaultCommands();
  48. this->AddDefaultDefinitions();
  49. }
  50. unsigned int cmMakefile::GetCacheMajorVersion()
  51. {
  52. if(!cmCacheManager::GetInstance()->
  53. GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  54. {
  55. return 0;
  56. }
  57. return atoi(cmCacheManager::GetInstance()->
  58. GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  59. }
  60. unsigned int cmMakefile::GetCacheMinorVersion()
  61. {
  62. if(!cmCacheManager::GetInstance()->
  63. GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  64. {
  65. return 0;
  66. }
  67. return atoi(cmCacheManager::GetInstance()->
  68. GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  69. }
  70. void cmMakefile::AddDefaultCommands()
  71. {
  72. std::list<cmCommand*> commands;
  73. GetPredefinedCommands(commands);
  74. for(std::list<cmCommand*>::iterator i = commands.begin();
  75. i != commands.end(); ++i)
  76. {
  77. this->AddCommand(*i);
  78. }
  79. #if defined(_WIN32) || defined(__CYGWIN__)
  80. this->AddDefinition("WIN32", "1");
  81. #else
  82. this->AddDefinition("UNIX", "1");
  83. #endif
  84. // Cygwin is more like unix so enable the unix commands
  85. #if defined(__CYGWIN__)
  86. this->AddDefinition("UNIX", "1");
  87. this->AddDefinition("CYGWIN", "1");
  88. #endif
  89. #if defined(__APPLE__)
  90. this->AddDefinition("APPLE", "1");
  91. #endif
  92. }
  93. cmMakefile::~cmMakefile()
  94. {
  95. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  96. i != m_SourceFiles.end(); ++i)
  97. {
  98. delete *i;
  99. }
  100. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  101. {
  102. delete m_UsedCommands[i];
  103. }
  104. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  105. j != m_Commands.end(); ++j)
  106. {
  107. delete (*j).second;
  108. }
  109. for(DataMap::const_iterator d = m_DataMap.begin();
  110. d != m_DataMap.end(); ++d)
  111. {
  112. if(d->second)
  113. {
  114. delete d->second;
  115. }
  116. }
  117. std::set<cmFunctionBlocker *>::const_iterator pos;
  118. for (pos = m_FunctionBlockers.begin();
  119. pos != m_FunctionBlockers.end(); pos = m_FunctionBlockers.begin())
  120. {
  121. cmFunctionBlocker* b = *pos;
  122. m_FunctionBlockers.erase(*pos);
  123. delete b;
  124. }
  125. delete m_MakefileGenerator;
  126. }
  127. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  128. {
  129. std::cout << s << ": ( \n";
  130. for(std::vector<std::string>::const_iterator i = v.begin();
  131. i != v.end(); ++i)
  132. {
  133. std::cout << (*i).c_str() << " ";
  134. }
  135. std::cout << " )\n";
  136. }
  137. // call print on all the classes in the makefile
  138. void cmMakefile::Print() const
  139. {
  140. // print the class lists
  141. std::cout << "classes:\n";
  142. for(SourceMap::const_iterator l = m_Sources.begin();
  143. l != m_Sources.end(); l++)
  144. {
  145. std::cout << " Class list named: " << l->first << std::endl;
  146. for(std::vector<cmSourceFile*>::const_iterator i = l->second.begin();
  147. i != l->second.end(); i++)
  148. {
  149. (*i)->Print();
  150. }
  151. }
  152. std::cout << " m_Targets: ";
  153. for (cmTargets::const_iterator l = m_Targets.begin();
  154. l != m_Targets.end(); l++)
  155. {
  156. std::cout << l->first << std::endl;
  157. }
  158. std::cout << " m_CurrentOutputDirectory; " <<
  159. m_CurrentOutputDirectory.c_str() << std::endl;
  160. std::cout << " m_StartOutputDirectory; " <<
  161. m_StartOutputDirectory.c_str() << std::endl;
  162. std::cout << " m_HomeOutputDirectory; " <<
  163. m_HomeOutputDirectory.c_str() << std::endl;
  164. std::cout << " m_cmCurrentDirectory; " <<
  165. m_cmCurrentDirectory.c_str() << std::endl;
  166. std::cout << " m_cmStartDirectory; " <<
  167. m_cmStartDirectory.c_str() << std::endl;
  168. std::cout << " m_cmHomeDirectory; " <<
  169. m_cmHomeDirectory.c_str() << std::endl;
  170. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  171. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  172. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  173. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  174. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  175. i != m_SourceGroups.end(); ++i)
  176. {
  177. i->Print();
  178. }
  179. }
  180. bool cmMakefile::CommandExists(const char* name) const
  181. {
  182. return (m_Commands.find(name) != m_Commands.end());
  183. }
  184. void cmMakefile::ExecuteCommand(std::string &name,
  185. std::vector<std::string> const& arguments)
  186. {
  187. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  188. if(pos != m_Commands.end())
  189. {
  190. cmCommand* rm = (*pos).second;
  191. cmCommand* usedCommand = rm->Clone();
  192. usedCommand->SetMakefile(this);
  193. bool keepCommand = false;
  194. if(usedCommand->GetEnabled())
  195. {
  196. // if not running in inherit mode or
  197. // if the command is inherited then InitialPass it.
  198. if(!m_Inheriting || usedCommand->IsInherited())
  199. {
  200. std::vector<std::string> expandedArguments = arguments;
  201. for(std::vector<std::string>::iterator i = expandedArguments.begin();
  202. i != expandedArguments.end(); ++i)
  203. {
  204. this->ExpandVariablesInString(*i);
  205. }
  206. if(!usedCommand->InitialPass(expandedArguments))
  207. {
  208. std::string error;
  209. error = usedCommand->GetName();
  210. error += ": Error : \n";
  211. error += usedCommand->GetError();
  212. error += " from CMakeLists.txt file in directory: ";
  213. error += m_cmCurrentDirectory;
  214. cmSystemTools::Error(error.c_str());
  215. }
  216. else
  217. {
  218. // use the command
  219. keepCommand = true;
  220. m_UsedCommands.push_back(usedCommand);
  221. }
  222. }
  223. }
  224. // if the Cloned command was not used
  225. // then delete it
  226. if(!keepCommand)
  227. {
  228. delete usedCommand;
  229. }
  230. }
  231. else if((name == "CABLE_WRAP_TCL") || (name == "CABLE_CLASS_SET") ||
  232. (name == "CONFIGURE_GCCXML"))
  233. {
  234. cmSystemTools::Error("The command ", name.c_str(),
  235. " is not implemented in this version of CMake.\n"
  236. "Contact [email protected] for more information.");
  237. }
  238. else
  239. {
  240. cmSystemTools::Error("unknown CMake command:", name.c_str(),
  241. "\nReading cmake file in directory:" ,
  242. m_cmCurrentDirectory.c_str());
  243. }
  244. }
  245. // Parse the given CMakeLists.txt file into a list of classes.
  246. // Reads in current CMakeLists file and all parent CMakeLists files
  247. // executing all inherited commands in the parents
  248. //
  249. // if external is non-zero, this means that we have branched to grab some
  250. // commands from a remote list-file (that is, the equivalent of a
  251. // #include has been called). We DO NOT look at the parents of this
  252. // list-file, and for all other purposes, the name of this list-file
  253. // is "filename" and not "external".
  254. bool cmMakefile::ReadListFile(const char* filename, const char* external)
  255. {
  256. // keep track of the current file being read
  257. if (filename)
  258. {
  259. if(m_cmCurrentListFile != filename)
  260. {
  261. m_cmCurrentListFile = filename;
  262. }
  263. }
  264. // if this is not a remote makefile
  265. // (if it were, this would be called from the "filename" call,
  266. // rather than the "external" call)
  267. if (!external)
  268. {
  269. // is there a parent CMakeLists file that does not go beyond the
  270. // Home directory? if so recurse and read in that List file
  271. std::string parentList = this->GetParentListFileName(filename);
  272. if (parentList != "")
  273. {
  274. std::string srcdir = m_cmCurrentDirectory;
  275. std::string bindir = m_CurrentOutputDirectory;
  276. std::string::size_type pos = parentList.rfind('/');
  277. m_cmCurrentDirectory = parentList.substr(0, pos);
  278. m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());
  279. // if not found, oops
  280. if(pos == std::string::npos)
  281. {
  282. cmSystemTools::Error("Trailing slash not found");
  283. }
  284. this->ReadListFile(parentList.c_str());
  285. // restore the current directory
  286. m_cmCurrentDirectory = srcdir;
  287. m_CurrentOutputDirectory = bindir;
  288. }
  289. }
  290. // are we at the start CMakeLists file or are we processing a parent
  291. // lists file
  292. //
  293. // this might, or might not be true, irrespective if we are
  294. // off looking at an external makefile.
  295. m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  296. // Now read the input file
  297. const char *filenametoread= filename;
  298. if( external)
  299. {
  300. filenametoread= external;
  301. }
  302. cmListFile* lf =
  303. cmListFileCache::GetInstance()->GetFileCache(filenametoread);
  304. if(!lf)
  305. {
  306. return false;
  307. }
  308. // add this list file to the list of dependencies
  309. m_ListFiles.push_back( filenametoread);
  310. const size_t numberFunctions = lf->m_Functions.size();
  311. for(size_t i =0; i < numberFunctions; ++i)
  312. {
  313. cmListFileFunction& curFunction = lf->m_Functions[i];
  314. if(!this->IsFunctionBlocked(curFunction.m_Name.c_str(),
  315. curFunction.m_Arguments))
  316. {
  317. this->ExecuteCommand(curFunction.m_Name,
  318. curFunction.m_Arguments);
  319. }
  320. }
  321. // send scope ended to and funciton blockers
  322. if (filename)
  323. {
  324. // loop over all function blockers to see if any block this command
  325. std::set<cmFunctionBlocker *>::const_iterator pos;
  326. for (pos = m_FunctionBlockers.begin();
  327. pos != m_FunctionBlockers.end(); ++pos)
  328. {
  329. (*pos)->ScopeEnded(*this);
  330. }
  331. }
  332. return true;
  333. }
  334. cmSourceFile *cmMakefile::GetSource(const char *srclist, const char *cname)
  335. {
  336. SourceMap::iterator sl = m_Sources.find(srclist);
  337. // find the src list
  338. if (sl == m_Sources.end())
  339. {
  340. return 0;
  341. }
  342. // find the class
  343. for (std::vector<cmSourceFile*>::iterator i = sl->second.begin();
  344. i != sl->second.end(); ++i)
  345. {
  346. if ((*i)->GetSourceName() == cname)
  347. {
  348. return *i;
  349. }
  350. }
  351. return 0;
  352. }
  353. void cmMakefile::AddCommand(cmCommand* wg)
  354. {
  355. std::string name = wg->GetName();
  356. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  357. }
  358. // Set the make file
  359. void cmMakefile::SetMakefileGenerator(cmMakefileGenerator* mf)
  360. {
  361. if(mf == m_MakefileGenerator)
  362. {
  363. return;
  364. }
  365. delete m_MakefileGenerator;
  366. m_MakefileGenerator = mf;
  367. mf->SetMakefile(this);
  368. }
  369. void cmMakefile::FinalPass()
  370. {
  371. // do all the variable expansions here
  372. this->ExpandVariables();
  373. // give all the commands a chance to do something
  374. // after the file has been parsed before generation
  375. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  376. i != m_UsedCommands.end(); ++i)
  377. {
  378. (*i)->FinalPass();
  379. }
  380. }
  381. // Generate the output file
  382. void cmMakefile::GenerateMakefile()
  383. {
  384. this->FinalPass();
  385. const char* versionValue
  386. = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  387. bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
  388. // merge libraries
  389. for (cmTargets::iterator l = m_Targets.begin();
  390. l != m_Targets.end(); l++)
  391. {
  392. l->second.GenerateSourceFilesFromSourceLists(*this);
  393. // pick up any LINK_LIBRARIES that were added after the target
  394. if(oldVersion)
  395. {
  396. this->AddGlobalLinkInformation(l->first.c_str(), l->second);
  397. }
  398. l->second.AnalyzeLibDependencies(*this);
  399. }
  400. // now do the generation
  401. m_MakefileGenerator->GenerateMakefile();
  402. }
  403. void cmMakefile::AddSource(cmSourceFile& cmfile, const char *srclist)
  404. {
  405. m_Sources[srclist].push_back(this->AddSource(cmfile));
  406. }
  407. void cmMakefile::RemoveSource(cmSourceFile& cmfile, const char *srclist)
  408. {
  409. std::vector<cmSourceFile*> &maplist = m_Sources[srclist];
  410. for( std::vector<cmSourceFile*>::iterator f = maplist.begin();
  411. f != maplist.end(); ++f)
  412. {
  413. if((*f)->GetSourceName() == cmfile.GetSourceName())
  414. {
  415. maplist.erase(f);
  416. return;
  417. }
  418. }
  419. }
  420. void cmMakefile::AddCustomCommand(const char* source,
  421. const char* command,
  422. const std::vector<std::string>& commandArgs,
  423. const std::vector<std::string>& depends,
  424. const std::vector<std::string>& outputs,
  425. const char *target)
  426. {
  427. // find the target,
  428. if (m_Targets.find(target) != m_Targets.end())
  429. {
  430. std::string expandC = command;
  431. this->ExpandVariablesInString(expandC);
  432. std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
  433. std::string combinedArgs;
  434. unsigned int i;
  435. for (i = 0; i < commandArgs.size(); ++i)
  436. {
  437. combinedArgs += cmSystemTools::EscapeSpaces(commandArgs[i].c_str());
  438. combinedArgs += " ";
  439. }
  440. cmCustomCommand cc(source,c.c_str(),combinedArgs.c_str(),depends,outputs);
  441. m_Targets[target].GetCustomCommands().push_back(cc);
  442. std::string cacheCommand = command;
  443. this->ExpandVariablesInString(cacheCommand);
  444. if(cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()))
  445. {
  446. m_Targets[target].AddUtility(
  447. cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()));
  448. }
  449. }
  450. }
  451. void cmMakefile::AddCustomCommand(const char* source,
  452. const char* command,
  453. const std::vector<std::string>& commandArgs,
  454. const std::vector<std::string>& depends,
  455. const char* output,
  456. const char *target)
  457. {
  458. std::vector<std::string> outputs;
  459. outputs.push_back(output);
  460. this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
  461. }
  462. void cmMakefile::AddDefineFlag(const char* flag)
  463. {
  464. m_DefineFlags += " ";
  465. m_DefineFlags += flag;
  466. }
  467. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  468. {
  469. m_LinkLibraries.push_back(
  470. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  471. }
  472. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  473. const char* lib,
  474. cmTarget::LinkLibraryType llt)
  475. {
  476. cmTargets::iterator i = m_Targets.find(target);
  477. if ( i != m_Targets.end())
  478. {
  479. i->second.AddLinkLibrary( *this, target, lib, llt );
  480. }
  481. else
  482. {
  483. cmSystemTools::Error("Attempt to add link libraries to non-existant target: ", target, " for lib ", lib);
  484. }
  485. }
  486. void cmMakefile::AddLinkDirectoryForTarget(const char *target,
  487. const char* d)
  488. {
  489. cmTargets::iterator i = m_Targets.find(target);
  490. if ( i != m_Targets.end())
  491. {
  492. i->second.AddLinkDirectory( d );
  493. }
  494. else
  495. {
  496. cmSystemTools::Error("Attempt to add link directories to non-existant target: ",
  497. target, " for directory ", d);
  498. }
  499. }
  500. void cmMakefile::AddLinkLibrary(const char* lib)
  501. {
  502. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  503. }
  504. void cmMakefile::AddLinkDirectory(const char* dir)
  505. {
  506. // Don't add a link directory that is already present. Yes, this
  507. // linear search results in n^2 behavior, but n won't be getting
  508. // much bigger than 20. We cannot use a set because of order
  509. // dependency of the link search path.
  510. // remove trailing slashes
  511. if(dir && dir[strlen(dir)-1] == '/')
  512. {
  513. std::string newdir = dir;
  514. newdir = newdir.substr(0, newdir.size()-1);
  515. if(std::find(m_LinkDirectories.begin(),
  516. m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
  517. {
  518. m_LinkDirectories.push_back(newdir);
  519. }
  520. }
  521. else
  522. {
  523. if(std::find(m_LinkDirectories.begin(),
  524. m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
  525. {
  526. m_LinkDirectories.push_back(dir);
  527. }
  528. }
  529. }
  530. void cmMakefile::AddSubDirectory(const char* sub)
  531. {
  532. m_SubDirectories.push_back(sub);
  533. }
  534. void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
  535. {
  536. // Don't add an include directory that is already present. Yes,
  537. // this linear search results in n^2 behavior, but n won't be
  538. // getting much bigger than 20. We cannot use a set because of
  539. // order dependency of the include path.
  540. if(std::find(m_IncludeDirectories.begin(),
  541. m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
  542. {
  543. if (before)
  544. {
  545. // WARNING: this *is* expensive (linear time) since it's a vector
  546. m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
  547. }
  548. else
  549. {
  550. m_IncludeDirectories.push_back(inc);
  551. }
  552. }
  553. }
  554. void cmMakefile::AddDefinition(const char* name, const char* value)
  555. {
  556. m_Definitions.erase( DefinitionMap::key_type(name));
  557. m_Definitions.insert(DefinitionMap::value_type(name, value));
  558. }
  559. void cmMakefile::AddCacheDefinition(const char* name, const char* value,
  560. const char* doc,
  561. cmCacheManager::CacheEntryType type)
  562. {
  563. cmCacheManager::GetInstance()->AddCacheEntry(name, value, doc, type);
  564. this->AddDefinition(name, value);
  565. }
  566. void cmMakefile::AddDefinition(const char* name, bool value)
  567. {
  568. if(value)
  569. {
  570. m_Definitions.erase( DefinitionMap::key_type(name));
  571. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  572. }
  573. else
  574. {
  575. m_Definitions.erase( DefinitionMap::key_type(name));
  576. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  577. }
  578. }
  579. void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
  580. {
  581. cmCacheManager::GetInstance()->AddCacheEntry(name, value, doc);
  582. this->AddDefinition(name, value);
  583. }
  584. void cmMakefile::SetProjectName(const char* p)
  585. {
  586. m_ProjectName = p;
  587. }
  588. void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
  589. {
  590. // for these targets do not add anything
  591. switch(target.GetType())
  592. {
  593. case cmTarget::UTILITY: return;
  594. case cmTarget::INSTALL_FILES: return;
  595. case cmTarget::INSTALL_PROGRAMS: return;
  596. default:;
  597. }
  598. std::vector<std::string>::iterator j;
  599. for(j = m_LinkDirectories.begin();
  600. j != m_LinkDirectories.end(); ++j)
  601. {
  602. target.AddLinkDirectory(j->c_str());
  603. }
  604. target.MergeLinkLibraries( *this, name, m_LinkLibraries );
  605. }
  606. void cmMakefile::AddLibrary(const char* lname, int shared,
  607. const std::vector<std::string> &srcs)
  608. {
  609. cmTarget target;
  610. switch (shared)
  611. {
  612. case 0:
  613. target.SetType(cmTarget::STATIC_LIBRARY);
  614. break;
  615. case 1:
  616. target.SetType(cmTarget::SHARED_LIBRARY);
  617. break;
  618. case 2:
  619. target.SetType(cmTarget::MODULE_LIBRARY);
  620. break;
  621. default:
  622. target.SetType(cmTarget::STATIC_LIBRARY);
  623. }
  624. // Clear its dependencies. Otherwise, dependencies might persist
  625. // over changes in CMakeLists.txt, making the information stale and
  626. // hence useless.
  627. std::string depname = lname;
  628. depname += "_LIB_DEPENDS";
  629. cmCacheManager::GetInstance()->
  630. AddCacheEntry(depname.c_str(), "",
  631. "Dependencies for target", cmCacheManager::STATIC);
  632. target.SetInAll(true);
  633. target.GetSourceLists() = srcs;
  634. this->AddGlobalLinkInformation(lname, target);
  635. m_Targets.insert(cmTargets::value_type(lname,target));
  636. // Add an entry into the cache
  637. cmCacheManager::GetInstance()->
  638. AddCacheEntry(lname,
  639. this->GetCurrentOutputDirectory(),
  640. "Path to a library", cmCacheManager::INTERNAL);
  641. // Add an entry into the cache
  642. std::string ltname = lname;
  643. ltname += "_LIBRARY_TYPE";
  644. switch (shared)
  645. {
  646. case 0:
  647. cmCacheManager::GetInstance()->
  648. AddCacheEntry(ltname.c_str(),
  649. "STATIC",
  650. "Whether a library is static, shared or module.",
  651. cmCacheManager::INTERNAL);
  652. break;
  653. case 1:
  654. cmCacheManager::GetInstance()->
  655. AddCacheEntry(ltname.c_str(),
  656. "SHARED",
  657. "Whether a library is static, shared or module.",
  658. cmCacheManager::INTERNAL);
  659. break;
  660. case 2:
  661. cmCacheManager::GetInstance()->
  662. AddCacheEntry(ltname.c_str(),
  663. "MODULE",
  664. "Whether a library is static, shared or module.",
  665. cmCacheManager::INTERNAL);
  666. break;
  667. default:
  668. cmCacheManager::GetInstance()->
  669. AddCacheEntry(ltname.c_str(),
  670. "STATIC",
  671. "Whether a library is static, shared or module.",
  672. cmCacheManager::INTERNAL);
  673. }
  674. }
  675. void cmMakefile::AddExecutable(const char *exeName,
  676. const std::vector<std::string> &srcs)
  677. {
  678. this->AddExecutable(exeName,srcs,false);
  679. }
  680. void cmMakefile::AddExecutable(const char *exeName,
  681. const std::vector<std::string> &srcs,
  682. bool win32)
  683. {
  684. cmTarget target;
  685. if (win32)
  686. {
  687. target.SetType(cmTarget::WIN32_EXECUTABLE);
  688. }
  689. else
  690. {
  691. target.SetType(cmTarget::EXECUTABLE);
  692. }
  693. target.SetInAll(true);
  694. target.GetSourceLists() = srcs;
  695. this->AddGlobalLinkInformation(exeName, target);
  696. m_Targets.insert(cmTargets::value_type(exeName,target));
  697. // Add an entry into the cache
  698. cmCacheManager::GetInstance()->
  699. AddCacheEntry(exeName,
  700. this->GetCurrentOutputDirectory(),
  701. "Path to an executable", cmCacheManager::INTERNAL);
  702. }
  703. void cmMakefile::AddUtilityCommand(const char* utilityName,
  704. const char* command,
  705. const char* arguments,
  706. bool all)
  707. {
  708. std::vector<std::string> empty;
  709. this->AddUtilityCommand(utilityName,command,arguments,all,
  710. empty,empty);
  711. }
  712. void cmMakefile::AddUtilityCommand(const char* utilityName,
  713. const char* command,
  714. const char* arguments,
  715. bool all,
  716. const std::vector<std::string> &dep,
  717. const std::vector<std::string> &out)
  718. {
  719. cmTarget target;
  720. target.SetType(cmTarget::UTILITY);
  721. target.SetInAll(all);
  722. cmCustomCommand cc(utilityName, command, arguments, dep, out);
  723. target.GetCustomCommands().push_back(cc);
  724. m_Targets.insert(cmTargets::value_type(utilityName,target));
  725. }
  726. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  727. {
  728. // First see if the group exists. If so, replace its regular expression.
  729. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  730. sg != m_SourceGroups.end(); ++sg)
  731. {
  732. std::string sgName = sg->GetName();
  733. if(sgName == name)
  734. {
  735. // We only want to set the regular expression. If there are already
  736. // source files in the group, we don't want to remove them.
  737. sg->SetGroupRegex(regex);
  738. return;
  739. }
  740. }
  741. // The group doesn't exist. Add it.
  742. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  743. }
  744. void cmMakefile::AddExtraDirectory(const char* dir)
  745. {
  746. m_AuxSourceDirectories.push_back(dir);
  747. }
  748. // return the file name for the parent CMakeLists file to the
  749. // one passed in. Zero is returned if the CMakeLists file is the
  750. // one in the home directory or if for some reason a parent cmake lists
  751. // file cannot be found.
  752. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  753. {
  754. // extract the directory name
  755. std::string parentFile;
  756. std::string listsDir = currentFileName;
  757. std::string::size_type pos = listsDir.rfind('/');
  758. // if we could not find the directory return 0
  759. if(pos == std::string::npos)
  760. {
  761. return parentFile;
  762. }
  763. listsDir = listsDir.substr(0, pos);
  764. // if we are in the home directory then stop, return 0
  765. if(m_cmHomeDirectory == listsDir)
  766. {
  767. return parentFile;
  768. }
  769. // is there a parent directory we can check
  770. pos = listsDir.rfind('/');
  771. // if we could not find the directory return 0
  772. if(pos == std::string::npos)
  773. {
  774. return parentFile;
  775. }
  776. listsDir = listsDir.substr(0, pos);
  777. // is there a CMakeLists.txt file in the parent directory ?
  778. parentFile = listsDir;
  779. parentFile += "/CMakeLists.txt";
  780. while(!cmSystemTools::FileExists(parentFile.c_str()))
  781. {
  782. // There is no CMakeLists.txt file in the parent directory. This
  783. // can occur when coming out of a subdirectory resulting from a
  784. // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo). Try
  785. // walking up until a CMakeLists.txt is found or the home
  786. // directory is hit.
  787. // if we are in the home directory then stop, return 0
  788. if(m_cmHomeDirectory == listsDir) { return ""; }
  789. // is there a parent directory we can check
  790. pos = listsDir.rfind('/');
  791. // if we could not find the directory return 0
  792. if(pos == std::string::npos) { return ""; }
  793. listsDir = listsDir.substr(0, pos);
  794. parentFile = listsDir;
  795. parentFile += "/CMakeLists.txt";
  796. }
  797. return parentFile;
  798. }
  799. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  800. // include and library directories.
  801. void cmMakefile::ExpandVariables()
  802. {
  803. // Now expand varibles in the include and link strings
  804. for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
  805. d != m_IncludeDirectories.end(); ++d)
  806. {
  807. this->ExpandVariablesInString(*d);
  808. }
  809. for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
  810. d != m_LinkDirectories.end(); ++d)
  811. {
  812. this->ExpandVariablesInString(*d);
  813. }
  814. for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
  815. l != m_LinkLibraries.end(); ++l)
  816. {
  817. this->ExpandVariablesInString(l->first);
  818. }
  819. }
  820. bool cmMakefile::IsOn(const char* name) const
  821. {
  822. const char* value = this->GetDefinition(name);
  823. return cmSystemTools::IsOn(value);
  824. }
  825. const char* cmMakefile::GetDefinition(const char* name) const
  826. {
  827. DefinitionMap::const_iterator pos = m_Definitions.find(name);
  828. if(pos != m_Definitions.end())
  829. {
  830. return (*pos).second.c_str();
  831. }
  832. return cmCacheManager::GetInstance()->GetCacheValue(name);
  833. }
  834. int cmMakefile::DumpDocumentationToFile(std::ostream& f)
  835. {
  836. // Open the supplied filename
  837. // Loop over all registered commands and print out documentation
  838. const char *name;
  839. const char *terse;
  840. const char *full;
  841. char tmp[1024];
  842. sprintf(tmp,"Version %d.%d", cmMakefile::GetMajorVersion(),
  843. cmMakefile::GetMinorVersion());
  844. f << "<html>\n";
  845. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  846. f << "<ul>\n";
  847. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  848. j != m_Commands.end(); ++j)
  849. {
  850. name = (*j).second->GetName();
  851. terse = (*j).second->GetTerseDocumentation();
  852. full = (*j).second->GetFullDocumentation();
  853. f << "<li><b>" << name << "</b> - " << terse << std::endl
  854. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  855. }
  856. f << "</ul></html>\n";
  857. return 1;
  858. }
  859. const char *cmMakefile::ExpandVariablesInString(std::string& source) const
  860. {
  861. return this->ExpandVariablesInString(source, false);
  862. }
  863. const char *cmMakefile::ExpandVariablesInString(std::string& source,
  864. bool escapeQuotes,
  865. bool atOnly) const
  866. {
  867. // This method replaces ${VAR} and @VAR@ where VAR is looked up
  868. // with GetDefinition(), if not found in the map, nothing is expanded.
  869. // It also supports the $ENV{VAR} syntax where VAR is looked up in
  870. // the current environment variables.
  871. // start by look for $ or @ in the string
  872. std::string::size_type markerPos;
  873. if(atOnly)
  874. {
  875. markerPos = source.find_first_of("@");
  876. }
  877. else
  878. {
  879. markerPos = source.find_first_of("$@");
  880. }
  881. // if not found, or found as the last character, then leave quickly as
  882. // nothing needs to be expanded
  883. if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
  884. {
  885. return source.c_str();
  886. }
  887. // current position
  888. std::string::size_type currentPos =0; // start at 0
  889. std::string result; // string with replacements
  890. // go until the the end of the string
  891. while((markerPos != std::string::npos) && (markerPos < source.size()-1))
  892. {
  893. // grab string from currentPos to the start of the variable
  894. // and add it to the result
  895. result += source.substr(currentPos, markerPos - currentPos);
  896. char endVariableMarker; // what is the end of the variable @ or }
  897. int markerStartSize = 1; // size of the start marker 1 or 2 or 5
  898. if(!atOnly && source[markerPos] == '$')
  899. {
  900. // ${var} case
  901. if(source[markerPos+1] == '{')
  902. {
  903. endVariableMarker = '}';
  904. markerStartSize = 2;
  905. }
  906. // $ENV{var} case
  907. else if(markerPos+4 < source.size() &&
  908. source[markerPos+4] == '{' &&
  909. !source.substr(markerPos+1, 3).compare("ENV"))
  910. {
  911. endVariableMarker = '}';
  912. markerStartSize = 5;
  913. }
  914. else
  915. {
  916. // bogus $ with no { so add $ to result and move on
  917. result += '$'; // add bogus $ back into string
  918. currentPos = markerPos+1; // move on
  919. endVariableMarker = ' '; // set end var to space so we can tell bogus
  920. }
  921. }
  922. else
  923. {
  924. // @VAR case
  925. endVariableMarker = '@';
  926. }
  927. // if it was a valid variable (started with @ or ${ or $ENV{ )
  928. if(endVariableMarker != ' ')
  929. {
  930. markerPos += markerStartSize; // move past marker
  931. // find the end variable marker starting at the markerPos
  932. std::string::size_type endVariablePos =
  933. source.find(endVariableMarker, markerPos);
  934. if(endVariablePos == std::string::npos)
  935. {
  936. // no end marker found so add the bogus start
  937. if(endVariableMarker == '@')
  938. {
  939. result += '@';
  940. }
  941. else
  942. {
  943. result += (markerStartSize == 5 ? "$ENV{" : "${");
  944. }
  945. currentPos = markerPos;
  946. }
  947. else
  948. {
  949. // good variable remove it
  950. std::string var = source.substr(markerPos, endVariablePos - markerPos);
  951. bool found = false;
  952. if (markerStartSize == 5) // $ENV{
  953. {
  954. char *ptr = getenv(var.c_str());
  955. if (ptr)
  956. {
  957. if (escapeQuotes)
  958. {
  959. result += cmSystemTools::EscapeQuotes(ptr);
  960. }
  961. else
  962. {
  963. result += ptr;
  964. }
  965. found = true;
  966. }
  967. }
  968. else
  969. {
  970. const char* lookup = this->GetDefinition(var.c_str());
  971. if(lookup)
  972. {
  973. if (escapeQuotes)
  974. {
  975. result += cmSystemTools::EscapeQuotes(lookup);
  976. }
  977. else
  978. {
  979. result += lookup;
  980. }
  981. found = true;
  982. }
  983. }
  984. // if found add to result, if not, then it gets blanked
  985. if (!found)
  986. {
  987. // if no definition is found then add the var back
  988. if(endVariableMarker == '@')
  989. {
  990. result += "@";
  991. result += var;
  992. result += "@";
  993. }
  994. else
  995. {
  996. result += (markerStartSize == 5 ? "$ENV{" : "${");
  997. result += var;
  998. result += "}";
  999. }
  1000. }
  1001. // lookup var, and replace it
  1002. currentPos = endVariablePos+1;
  1003. }
  1004. }
  1005. if(atOnly)
  1006. {
  1007. markerPos = source.find_first_of("@", currentPos);
  1008. }
  1009. else
  1010. {
  1011. markerPos = source.find_first_of("$@", currentPos);
  1012. }
  1013. }
  1014. result += source.substr(currentPos); // pick up the rest of the string
  1015. source = result;
  1016. return source.c_str();
  1017. }
  1018. void cmMakefile::RemoveVariablesInString(std::string& source,
  1019. bool atOnly) const
  1020. {
  1021. if(!atOnly)
  1022. {
  1023. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  1024. while (var.find(source))
  1025. {
  1026. source.erase(var.start(),var.end() - var.start());
  1027. }
  1028. }
  1029. if(!atOnly)
  1030. {
  1031. cmRegularExpression varb("(\\$ENV{[A-Za-z_0-9]*})");
  1032. while (varb.find(source))
  1033. {
  1034. source.erase(varb.start(),varb.end() - varb.start());
  1035. }
  1036. }
  1037. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  1038. while (var2.find(source))
  1039. {
  1040. source.erase(var2.start(),var2.end() - var2.start());
  1041. }
  1042. }
  1043. // recursive function to create a vector of cmMakefile objects
  1044. // This is done by reading the sub directory CMakeLists.txt files,
  1045. // then calling this function with the new cmMakefile object
  1046. void
  1047. cmMakefile::FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles)
  1048. {
  1049. // loop over all the sub directories of this makefile
  1050. const std::vector<std::string>& subdirs = this->GetSubDirectories();
  1051. for(std::vector<std::string>::const_iterator i = subdirs.begin();
  1052. i != subdirs.end(); ++i)
  1053. {
  1054. std::string subdir = *i;
  1055. // Create a path to the list file in the sub directory
  1056. std::string listFile = this->GetCurrentDirectory();
  1057. listFile += "/";
  1058. listFile += subdir;
  1059. listFile += "/CMakeLists.txt";
  1060. // if there is a CMakeLists.txt file read it
  1061. if(!cmSystemTools::FileExists(listFile.c_str()))
  1062. {
  1063. cmSystemTools::Error("CMakeLists.txt file missing from sub directory:",
  1064. listFile.c_str());
  1065. }
  1066. else
  1067. {
  1068. cmMakefile* mf = new cmMakefile;
  1069. mf->SetMakefileGenerator(m_MakefileGenerator->CreateObject());
  1070. makefiles.push_back(mf);
  1071. // initialize new makefile
  1072. mf->SetHomeOutputDirectory(this->GetHomeOutputDirectory());
  1073. mf->SetHomeDirectory(this->GetHomeDirectory());
  1074. // add the subdir to the start output directory
  1075. std::string outdir = this->GetStartOutputDirectory();
  1076. outdir += "/";
  1077. outdir += subdir;
  1078. mf->SetStartOutputDirectory(outdir.c_str());
  1079. // add the subdir to the start source directory
  1080. std::string currentDir = this->GetStartDirectory();
  1081. currentDir += "/";
  1082. currentDir += subdir;
  1083. mf->SetStartDirectory(currentDir.c_str());
  1084. // Parse the CMakeLists.txt file
  1085. currentDir += "/CMakeLists.txt";
  1086. mf->MakeStartDirectoriesCurrent();
  1087. mf->ReadListFile(currentDir.c_str());
  1088. // recurse into nextDir
  1089. mf->FindSubDirectoryCMakeListsFiles(makefiles);
  1090. }
  1091. }
  1092. }
  1093. /**
  1094. * Add the default definitions to the makefile. These values must not
  1095. * be dependent on anything that isn't known when this cmMakefile instance
  1096. * is constructed.
  1097. */
  1098. void cmMakefile::AddDefaultDefinitions()
  1099. {
  1100. #if defined(_WIN32) && !defined(__CYGWIN__)
  1101. this->AddDefinition("CMAKE_CFG_INTDIR","$(IntDir)");
  1102. #else
  1103. this->AddDefinition("CMAKE_CFG_INTDIR",".");
  1104. #endif
  1105. char temp[1024];
  1106. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  1107. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  1108. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  1109. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  1110. }
  1111. /**
  1112. * Find a source group whose regular expression matches the filename
  1113. * part of the given source name. Search backward through the list of
  1114. * source groups, and take the first matching group found. This way
  1115. * non-inherited SOURCE_GROUP commands will have precedence over
  1116. * inherited ones.
  1117. */
  1118. cmSourceGroup&
  1119. cmMakefile::FindSourceGroup(const char* source,
  1120. std::vector<cmSourceGroup> &groups)
  1121. {
  1122. std::string file = source;
  1123. std::string::size_type pos = file.rfind('/');
  1124. if(pos != std::string::npos)
  1125. {
  1126. file = file.substr(pos, file.length()-pos);
  1127. }
  1128. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  1129. sg != groups.rend(); ++sg)
  1130. {
  1131. if(sg->Matches(file.c_str()))
  1132. {
  1133. return *sg;
  1134. }
  1135. }
  1136. // Shouldn't get here, but just in case, return the default group.
  1137. return groups.front();
  1138. }
  1139. bool cmMakefile::IsFunctionBlocked(const char *name,
  1140. std::vector<std::string> const&args)
  1141. {
  1142. // loop over all function blockers to see if any block this command
  1143. std::set<cmFunctionBlocker *>::const_iterator pos;
  1144. std::vector<std::string> expandedArguments = args;
  1145. for(std::vector<std::string>::iterator i = expandedArguments.begin();
  1146. i != expandedArguments.end(); ++i)
  1147. {
  1148. this->ExpandVariablesInString(*i);
  1149. }
  1150. for (pos = m_FunctionBlockers.begin();
  1151. pos != m_FunctionBlockers.end(); ++pos)
  1152. {
  1153. if ((*pos)->NeedExpandedVariables())
  1154. {
  1155. if ((*pos)->IsFunctionBlocked(name, expandedArguments, *this))
  1156. {
  1157. return true;
  1158. }
  1159. }
  1160. else
  1161. {
  1162. if ((*pos)->IsFunctionBlocked(name, args, *this))
  1163. {
  1164. return true;
  1165. }
  1166. }
  1167. }
  1168. return false;
  1169. }
  1170. void cmMakefile::RemoveFunctionBlocker(const char *name,
  1171. const std::vector<std::string> &args)
  1172. {
  1173. // loop over all function blockers to see if any block this command
  1174. std::set<cmFunctionBlocker *>::const_iterator pos;
  1175. for (pos = m_FunctionBlockers.begin();
  1176. pos != m_FunctionBlockers.end(); ++pos)
  1177. {
  1178. if ((*pos)->ShouldRemove(name, args, *this))
  1179. {
  1180. cmFunctionBlocker* b = *pos;
  1181. m_FunctionBlockers.erase(*pos);
  1182. delete b;
  1183. return;
  1184. }
  1185. }
  1186. return;
  1187. }
  1188. void cmMakefile::SetHomeDirectory(const char* dir)
  1189. {
  1190. m_cmHomeDirectory = dir;
  1191. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1192. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  1193. }
  1194. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  1195. {
  1196. m_HomeOutputDirectory = lib;
  1197. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1198. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  1199. }
  1200. /**
  1201. * Register the given cmData instance with its own name.
  1202. */
  1203. void cmMakefile::RegisterData(cmData* data)
  1204. {
  1205. std::string name = data->GetName();
  1206. DataMap::const_iterator d = m_DataMap.find(name);
  1207. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1208. {
  1209. delete d->second;
  1210. }
  1211. m_DataMap[name] = data;
  1212. }
  1213. /**
  1214. * Register the given cmData instance with the given name. This can be used
  1215. * to register a NULL pointer.
  1216. */
  1217. void cmMakefile::RegisterData(const char* name, cmData* data)
  1218. {
  1219. DataMap::const_iterator d = m_DataMap.find(name);
  1220. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  1221. {
  1222. delete d->second;
  1223. }
  1224. m_DataMap[name] = data;
  1225. }
  1226. /**
  1227. * Lookup a cmData instance previously registered with the given name. If
  1228. * the instance cannot be found, return NULL.
  1229. */
  1230. cmData* cmMakefile::LookupData(const char* name) const
  1231. {
  1232. DataMap::const_iterator d = m_DataMap.find(name);
  1233. if(d != m_DataMap.end())
  1234. {
  1235. return d->second;
  1236. }
  1237. else
  1238. {
  1239. return NULL;
  1240. }
  1241. }
  1242. cmSourceFile* cmMakefile::GetSource(const char* sourceName)
  1243. {
  1244. std::string s = sourceName;
  1245. std::string ext;
  1246. std::string::size_type pos = s.rfind('.');
  1247. if(pos != std::string::npos)
  1248. {
  1249. ext = s.substr(pos+1, s.size() - pos-1);
  1250. s = s.substr(0, pos);
  1251. }
  1252. for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
  1253. i != m_SourceFiles.end(); ++i)
  1254. {
  1255. if((*i)->GetSourceName() == s
  1256. && (ext.size() == 0 || (ext == (*i)->GetSourceExtension())))
  1257. {
  1258. return *i;
  1259. }
  1260. }
  1261. return 0;
  1262. }
  1263. cmSourceFile* cmMakefile::AddSource(cmSourceFile const&sf)
  1264. {
  1265. // check to see if it exists
  1266. cmSourceFile* ret = this->GetSource(sf.GetSourceName().c_str());
  1267. if(ret && ret->GetSourceExtension() == sf.GetSourceExtension())
  1268. {
  1269. return ret;
  1270. }
  1271. ret = new cmSourceFile(sf);
  1272. m_SourceFiles.push_back(ret);
  1273. return ret;
  1274. }
  1275. void cmMakefile::EnableLanguage(const char* lang)
  1276. {
  1277. m_MakefileGenerator->EnableLanguage(lang);
  1278. }