cmMakefile.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmMakefile.h"
  33. #include "cmCommand.h"
  34. #include "cmStandardIncludes.h"
  35. #include "cmSourceFile.h"
  36. #include "cmDirectory.h"
  37. #include "cmSystemTools.h"
  38. #include "cmMakefileGenerator.h"
  39. #include "cmCommands.h"
  40. #include "cmCacheManager.h"
  41. #include "cmFunctionBlocker.h"
  42. #include "stdio.h"
  43. // default is not to be building executables
  44. cmMakefile::cmMakefile()
  45. {
  46. // Setup the default include file regular expression (match everything).
  47. m_IncludeFileRegularExpression = "^.*$";
  48. // Setup the default include complaint regular expression (match nothing).
  49. m_ComplainFileRegularExpression = "^$";
  50. m_DefineFlags = " ";
  51. m_MakefileGenerator = 0;
  52. this->AddSourceGroup("", "^.*$");
  53. this->AddSourceGroup("Source Files", "\\.(cpp|C|c|cxx|rc|def|r|odl|idl|hpj|bat)$");
  54. this->AddSourceGroup("Header Files", "\\.(h|hh|hpp|hxx|hm|inl)$");
  55. this->AddDefaultCommands();
  56. this->AddDefaultDefinitions();
  57. cmCacheManager::GetInstance()->DefineCache(this);
  58. }
  59. unsigned int cmMakefile::GetCacheMajorVersion()
  60. {
  61. if(!cmCacheManager::GetInstance()->
  62. GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
  63. {
  64. return 0;
  65. }
  66. return atoi(cmCacheManager::GetInstance()->
  67. GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
  68. }
  69. unsigned int cmMakefile::GetCacheMinorVersion()
  70. {
  71. if(!cmCacheManager::GetInstance()->
  72. GetCacheValue("Cmake_Cache_MINOR_VERSION"))
  73. {
  74. return 0;
  75. }
  76. return atoi(cmCacheManager::GetInstance()->
  77. GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
  78. }
  79. void cmMakefile::AddDefaultCommands()
  80. {
  81. std::list<cmCommand*> commands;
  82. GetPredefinedCommands(commands);
  83. for(std::list<cmCommand*>::iterator i = commands.begin();
  84. i != commands.end(); ++i)
  85. {
  86. this->AddCommand(*i);
  87. }
  88. #if defined(_WIN32) || defined(__CYGWIN__)
  89. this->AddDefinition("WIN32", "1");
  90. #else
  91. this->AddDefinition("UNIX", "1");
  92. #endif
  93. // Cygwin is more like unix so enable the unix commands
  94. #if defined(__CYGWIN__)
  95. this->AddDefinition("UNIX", "1");
  96. #endif
  97. // always creat an empty install target
  98. cmTarget target;
  99. target.SetType(cmTarget::INSTALL);
  100. target.SetInAll(false);
  101. m_Targets.insert(cmTargets::value_type("INSTALL",target));
  102. }
  103. cmMakefile::~cmMakefile()
  104. {
  105. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  106. {
  107. delete m_UsedCommands[i];
  108. }
  109. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  110. j != m_Commands.end(); ++j)
  111. {
  112. delete (*j).second;
  113. }
  114. for(DataMap::const_iterator d = m_DataMap.begin();
  115. d != m_DataMap.end(); ++d)
  116. {
  117. if(d->second)
  118. {
  119. delete d->second;
  120. }
  121. }
  122. std::set<cmFunctionBlocker *>::const_iterator pos;
  123. for (pos = m_FunctionBlockers.begin();
  124. pos != m_FunctionBlockers.end(); ++pos)
  125. {
  126. cmFunctionBlocker* b = *pos;
  127. m_FunctionBlockers.erase(*pos);
  128. delete b;
  129. }
  130. delete m_MakefileGenerator;
  131. }
  132. void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
  133. {
  134. std::cout << s << ": ( \n";
  135. for(std::vector<std::string>::const_iterator i = v.begin();
  136. i != v.end(); ++i)
  137. {
  138. std::cout << (*i).c_str() << " ";
  139. }
  140. std::cout << " )\n";
  141. }
  142. // call print on all the classes in the makefile
  143. void cmMakefile::Print() const
  144. {
  145. // print the class lists
  146. std::cout << "classes:\n";
  147. for(SourceMap::const_iterator l = m_Sources.begin();
  148. l != m_Sources.end(); l++)
  149. {
  150. std::cout << " Class list named: " << l->first << std::endl;
  151. for(std::vector<cmSourceFile>::const_iterator i = l->second.begin();
  152. i != l->second.end(); i++)
  153. {
  154. i->Print();
  155. }
  156. }
  157. std::cout << " m_Targets: ";
  158. for (cmTargets::const_iterator l = m_Targets.begin();
  159. l != m_Targets.end(); l++)
  160. {
  161. std::cout << l->first << std::endl;
  162. }
  163. std::cout << " m_CurrentOutputDirectory; " <<
  164. m_CurrentOutputDirectory.c_str() << std::endl;
  165. std::cout << " m_StartOutputDirectory; " <<
  166. m_StartOutputDirectory.c_str() << std::endl;
  167. std::cout << " m_HomeOutputDirectory; " <<
  168. m_HomeOutputDirectory.c_str() << std::endl;
  169. std::cout << " m_cmCurrentDirectory; " <<
  170. m_cmCurrentDirectory.c_str() << std::endl;
  171. std::cout << " m_cmStartDirectory; " <<
  172. m_cmStartDirectory.c_str() << std::endl;
  173. std::cout << " m_cmHomeDirectory; " <<
  174. m_cmHomeDirectory.c_str() << std::endl;
  175. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  176. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  177. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  178. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  179. for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
  180. i != m_SourceGroups.end(); ++i)
  181. {
  182. i->Print();
  183. }
  184. }
  185. // Parse the given CMakeLists.txt file into a list of classes.
  186. // Reads in current CMakeLists file and all parent CMakeLists files
  187. // executing all inherited commands in the parents
  188. //
  189. // if external is non-zero, this means that we have branched to grab some
  190. // commands from a remote list-file (that is, the equivalent of a
  191. // #include has been called). We DO NOT look at the parents of this
  192. // list-file, and for all other purposes, the name of this list-file
  193. // is "filename" and not "external".
  194. bool cmMakefile::ReadListFile(const char* filename, const char* external)
  195. {
  196. // keep track of the current file being read
  197. if (filename)
  198. {
  199. if(m_cmCurrentListFile != filename)
  200. {
  201. m_cmCurrentListFile = filename;
  202. }
  203. }
  204. // if this is not a remote makefile
  205. // (if it were, this would be called from the "filename" call,
  206. // rather than the "external" call)
  207. if (!external)
  208. {
  209. // is there a parent CMakeLists file that does not go beyond the
  210. // Home directory? if so recurse and read in that List file
  211. std::string parentList = this->GetParentListFileName(filename);
  212. if (parentList != "")
  213. {
  214. // save the current directory
  215. std::string srcdir = m_cmCurrentDirectory;
  216. std::string bindir = m_CurrentOutputDirectory;
  217. // compute the new current directories
  218. std::string::size_type pos = m_cmCurrentDirectory.rfind('/');
  219. if(pos != std::string::npos)
  220. {
  221. m_cmCurrentDirectory = m_cmCurrentDirectory.substr(0, pos);
  222. }
  223. pos = m_CurrentOutputDirectory.rfind('/');
  224. if(pos != std::string::npos)
  225. {
  226. m_CurrentOutputDirectory = m_CurrentOutputDirectory.substr(0, pos);
  227. }
  228. this->ReadListFile(parentList.c_str());
  229. // restore the current directory
  230. m_cmCurrentDirectory = srcdir;
  231. m_CurrentOutputDirectory = bindir;
  232. }
  233. }
  234. // are we at the start CMakeLists file or are we processing a parent
  235. // lists file
  236. //
  237. // this might, or might not be true, irrespective if we are
  238. // off looking at an external makefile.
  239. bool inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  240. // Now read the input file
  241. const char *filenametoread= filename;
  242. if( external)
  243. {
  244. filenametoread= external;
  245. }
  246. std::ifstream fin(filenametoread);
  247. if(!fin)
  248. {
  249. cmSystemTools::Error("error can not open file ", filenametoread);
  250. return false;
  251. }
  252. std::string name;
  253. std::vector<std::string> arguments;
  254. while ( fin )
  255. {
  256. // add this list file to the list of dependencies
  257. m_ListFiles.push_back( filenametoread);
  258. if(cmSystemTools::ParseFunction(fin, name, arguments) &&
  259. !this->IsFunctionBlocked(name.c_str(),arguments))
  260. {
  261. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  262. if(pos != m_Commands.end())
  263. {
  264. cmCommand* rm = (*pos).second;
  265. cmCommand* usedCommand = rm->Clone();
  266. usedCommand->SetMakefile(this);
  267. bool keepCommand = false;
  268. if(usedCommand->GetEnabled())
  269. {
  270. // if not running in inherit mode or
  271. // if the command is inherited then InitialPass it.
  272. if(!inheriting || usedCommand->IsInherited())
  273. {
  274. if(!usedCommand->InitialPass(arguments))
  275. {
  276. cmSystemTools::Error(usedCommand->GetName(),
  277. ": Error : \n",
  278. usedCommand->GetError(),
  279. m_cmCurrentDirectory.c_str());
  280. }
  281. else
  282. {
  283. // use the command
  284. keepCommand = true;
  285. m_UsedCommands.push_back(usedCommand);
  286. }
  287. }
  288. }
  289. // if the Cloned command was not used
  290. // then delete it
  291. if(!keepCommand)
  292. {
  293. delete usedCommand;
  294. }
  295. }
  296. else
  297. {
  298. cmSystemTools::Error("unknown CMake command ", name.c_str(), filename);
  299. }
  300. }
  301. }
  302. // send scope ended to and funciton blockers
  303. if (filename)
  304. {
  305. // loop over all function blockers to see if any block this command
  306. std::set<cmFunctionBlocker *>::const_iterator pos;
  307. for (pos = m_FunctionBlockers.begin();
  308. pos != m_FunctionBlockers.end(); ++pos)
  309. {
  310. (*pos)->ScopeEnded(*this);
  311. }
  312. }
  313. return true;
  314. }
  315. cmSourceFile *cmMakefile::GetSource(const char *srclist, const char *cname)
  316. {
  317. SourceMap::iterator sl = m_Sources.find(srclist);
  318. // find the src list
  319. if (sl == m_Sources.end())
  320. {
  321. return 0;
  322. }
  323. // find the class
  324. for (std::vector<cmSourceFile>::iterator i = sl->second.begin();
  325. i != sl->second.end(); ++i)
  326. {
  327. if (i->GetSourceName() == cname)
  328. {
  329. return &(*i);
  330. }
  331. }
  332. return 0;
  333. }
  334. void cmMakefile::AddCommand(cmCommand* wg)
  335. {
  336. std::string name = wg->GetName();
  337. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  338. }
  339. // Set the make file
  340. void cmMakefile::SetMakefileGenerator(cmMakefileGenerator* mf)
  341. {
  342. if(mf == m_MakefileGenerator)
  343. {
  344. return;
  345. }
  346. delete m_MakefileGenerator;
  347. m_MakefileGenerator = mf;
  348. mf->SetMakefile(this);
  349. }
  350. // Generate the output file
  351. void cmMakefile::GenerateMakefile()
  352. {
  353. // do all the variable expansions here
  354. this->ExpandVariables();
  355. // give all the commands a chance to do something
  356. // after the file has been parsed before generation
  357. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  358. i != m_UsedCommands.end(); ++i)
  359. {
  360. (*i)->FinalPass();
  361. }
  362. // merge libraries
  363. for (cmTargets::iterator l = m_Targets.begin();
  364. l != m_Targets.end(); l++)
  365. {
  366. l->second.GenerateSourceFilesFromSourceLists(*this);
  367. l->second.MergeLibraries(m_LinkLibraries);
  368. }
  369. // now do the generation
  370. m_MakefileGenerator->GenerateMakefile();
  371. }
  372. void cmMakefile::AddSource(cmSourceFile& cmfile, const char *srclist)
  373. {
  374. m_Sources[srclist].push_back(cmfile);
  375. }
  376. struct FindSrcByName : std::binary_function<cmSourceFile, cmSourceFile, bool>
  377. {
  378. public:
  379. bool operator () (const cmSourceFile &f, const cmSourceFile &test) const
  380. {
  381. return !strcmp(f.GetSourceName().c_str(),test.GetSourceName().c_str());
  382. }
  383. };
  384. void cmMakefile::RemoveSource(cmSourceFile& cmfile,const char *srclist)
  385. {
  386. std::vector<cmSourceFile> &maplist = m_Sources[srclist];
  387. std::vector<cmSourceFile>::iterator f =
  388. std::find_if(maplist.begin(), maplist.end(), std::bind2nd(FindSrcByName(),cmfile));
  389. // std::vector<cmSourceFile>::iterator f = find_if(maplist.begin(), maplist.end(), matches(srclist);
  390. if (f!=maplist.end())
  391. {
  392. maplist.erase(f);
  393. }
  394. }
  395. void cmMakefile::AddCustomCommand(const char* source,
  396. const char* command,
  397. const char* commandArgs,
  398. const std::vector<std::string>& depends,
  399. const std::vector<std::string>& outputs,
  400. const char *target)
  401. {
  402. // find the target,
  403. if (m_Targets.find(target) != m_Targets.end())
  404. {
  405. std::string c = cmSystemTools::EscapeSpaces(command);
  406. c += " ";
  407. c += commandArgs;
  408. cmCustomCommand cc(source,c.c_str(),depends,outputs);
  409. m_Targets[target].GetCustomCommands().push_back(cc);
  410. std::string cacheCommand = command;
  411. this->ExpandVariablesInString(cacheCommand);
  412. if(cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()))
  413. {
  414. m_Targets[target].AddUtility(
  415. cmCacheManager::GetInstance()->GetCacheValue(cacheCommand.c_str()));
  416. }
  417. }
  418. }
  419. void cmMakefile::AddCustomCommand(const char* source,
  420. const char* command,
  421. const char* commandArgs,
  422. const std::vector<std::string>& depends,
  423. const char* output,
  424. const char *target)
  425. {
  426. std::vector<std::string> outputs;
  427. outputs.push_back(output);
  428. this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
  429. }
  430. void cmMakefile::AddDefineFlag(const char* flag)
  431. {
  432. m_DefineFlags += " ";
  433. m_DefineFlags += flag;
  434. }
  435. void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
  436. {
  437. m_LinkLibraries.push_back(
  438. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  439. }
  440. void cmMakefile::AddLinkLibraryForTarget(const char *target,
  441. const char* lib,
  442. cmTarget::LinkLibraryType llt)
  443. {
  444. if (m_Targets.find(target) != m_Targets.end())
  445. {
  446. m_Targets[target].GetLinkLibraries().
  447. push_back(
  448. std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
  449. }
  450. }
  451. void cmMakefile::AddLinkLibrary(const char* lib)
  452. {
  453. this->AddLinkLibrary(lib,cmTarget::GENERAL);
  454. }
  455. void cmMakefile::AddLinkDirectory(const char* dir)
  456. {
  457. m_LinkDirectories.push_back(dir);
  458. }
  459. void cmMakefile::AddSubDirectory(const char* sub)
  460. {
  461. m_SubDirectories.push_back(sub);
  462. }
  463. void cmMakefile::AddIncludeDirectory(const char* inc)
  464. {
  465. m_IncludeDirectories.push_back(inc);
  466. }
  467. void cmMakefile::AddDefinition(const char* name, const char* value)
  468. {
  469. m_Definitions.erase( DefinitionMap::key_type(name));
  470. m_Definitions.insert(DefinitionMap::value_type(name, value));
  471. }
  472. void cmMakefile::AddDefinition(const char* name, bool value)
  473. {
  474. if(value)
  475. {
  476. m_Definitions.erase( DefinitionMap::key_type(name));
  477. m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
  478. }
  479. else
  480. {
  481. m_Definitions.erase( DefinitionMap::key_type(name));
  482. m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
  483. }
  484. }
  485. void cmMakefile::SetProjectName(const char* p)
  486. {
  487. m_ProjectName = p;
  488. }
  489. void cmMakefile::AddLibrary(const char* lname, const std::vector<std::string> &srcs)
  490. {
  491. cmTarget target;
  492. target.SetType(cmTarget::LIBRARY);
  493. target.SetInAll(true);
  494. target.GetSourceLists() = srcs;
  495. m_Targets.insert(cmTargets::value_type(lname,target));
  496. // Add an entry into the cache
  497. cmCacheManager::GetInstance()->
  498. AddCacheEntry(lname,
  499. this->GetCurrentOutputDirectory(),
  500. "Path to a library", cmCacheManager::INTERNAL);
  501. }
  502. void cmMakefile::AddExecutable(const char *exeName,
  503. const std::vector<std::string> &srcs)
  504. {
  505. this->AddExecutable(exeName,srcs,false);
  506. }
  507. void cmMakefile::AddExecutable(const char *exeName,
  508. const std::vector<std::string> &srcs,
  509. bool win32)
  510. {
  511. cmTarget target;
  512. if (win32)
  513. {
  514. target.SetType(cmTarget::WIN32_EXECUTABLE);
  515. }
  516. else
  517. {
  518. target.SetType(cmTarget::EXECUTABLE);
  519. }
  520. target.SetInAll(true);
  521. target.GetSourceLists() = srcs;
  522. m_Targets.insert(cmTargets::value_type(exeName,target));
  523. // Add an entry into the cache
  524. cmCacheManager::GetInstance()->
  525. AddCacheEntry(exeName,
  526. this->GetCurrentOutputDirectory(),
  527. "Path to an executable", cmCacheManager::INTERNAL);
  528. }
  529. void cmMakefile::AddUtilityCommand(const char* utilityName,
  530. const char* command,
  531. bool all)
  532. {
  533. std::vector<std::string> empty;
  534. this->AddUtilityCommand(utilityName,command,all,
  535. empty,empty);
  536. }
  537. void cmMakefile::AddUtilityCommand(const char* utilityName,
  538. const char* command,
  539. bool all,
  540. const std::vector<std::string> &dep,
  541. const std::vector<std::string> &out)
  542. {
  543. cmTarget target;
  544. target.SetType(cmTarget::UTILITY);
  545. target.SetInAll(all);
  546. cmCustomCommand cc(utilityName, command, dep, out);
  547. target.GetCustomCommands().push_back(cc);
  548. m_Targets.insert(cmTargets::value_type(utilityName,target));
  549. }
  550. void cmMakefile::AddSourceGroup(const char* name, const char* regex)
  551. {
  552. // First see if the group exists. If so, replace its regular expression.
  553. for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
  554. sg != m_SourceGroups.end(); ++sg)
  555. {
  556. std::string sgName = sg->GetName();
  557. if(sgName == name)
  558. {
  559. // We only want to set the regular expression. If there are already
  560. // source files in the group, we don't want to remove them.
  561. sg->SetGroupRegex(regex);
  562. return;
  563. }
  564. }
  565. // The group doesn't exist. Add it.
  566. m_SourceGroups.push_back(cmSourceGroup(name, regex));
  567. }
  568. void cmMakefile::AddExtraDirectory(const char* dir)
  569. {
  570. m_AuxSourceDirectories.push_back(dir);
  571. }
  572. // return the file name for the parent CMakeLists file to the
  573. // one passed in. Zero is returned if the CMakeLists file is the
  574. // one in the home directory or if for some reason a parent cmake lists
  575. // file cannot be found.
  576. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  577. {
  578. // extract the directory name
  579. std::string parentFile;
  580. std::string listsDir = currentFileName;
  581. std::string::size_type pos = listsDir.rfind('/');
  582. // if we could not find the directory return 0
  583. if(pos == std::string::npos)
  584. {
  585. return parentFile;
  586. }
  587. listsDir = listsDir.substr(0, pos);
  588. // if we are in the home directory then stop, return 0
  589. if(m_cmHomeDirectory == listsDir)
  590. {
  591. return parentFile;
  592. }
  593. // is there a parent directory we can check
  594. pos = listsDir.rfind('/');
  595. // if we could not find the directory return 0
  596. if(pos == std::string::npos)
  597. {
  598. return parentFile;
  599. }
  600. listsDir = listsDir.substr(0, pos);
  601. // is there a CMakeLists.txt file in the parent directory ?
  602. parentFile = listsDir;
  603. parentFile += "/CMakeLists.txt";
  604. if(!cmSystemTools::FileExists(parentFile.c_str()))
  605. {
  606. parentFile = "";
  607. return parentFile;
  608. }
  609. return parentFile;
  610. }
  611. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  612. // include and library directories.
  613. void cmMakefile::ExpandVariables()
  614. {
  615. // Now expand varibles in the include and link strings
  616. std::vector<std::string>::iterator j, begin, end;
  617. begin = m_IncludeDirectories.begin();
  618. end = m_IncludeDirectories.end();
  619. for(j = begin; j != end; ++j)
  620. {
  621. this->ExpandVariablesInString(*j);
  622. }
  623. begin = m_LinkDirectories.begin();
  624. end = m_LinkDirectories.end();
  625. for(j = begin; j != end; ++j)
  626. {
  627. this->ExpandVariablesInString(*j);
  628. }
  629. cmTarget::LinkLibraries::iterator j2, end2;
  630. j2 = m_LinkLibraries.begin();
  631. end2 = m_LinkLibraries.end();
  632. for(; j2 != end2; ++j2)
  633. {
  634. this->ExpandVariablesInString(j2->first);
  635. }
  636. }
  637. const char* cmMakefile::GetDefinition(const char* name)
  638. {
  639. DefinitionMap::iterator pos = m_Definitions.find(name);
  640. if(pos != m_Definitions.end())
  641. {
  642. return (*pos).second.c_str();
  643. }
  644. return 0;
  645. }
  646. int cmMakefile::DumpDocumentationToFile(const char *fileName)
  647. {
  648. // Open the supplied filename
  649. std::ofstream f;
  650. f.open(fileName, std::ios::out);
  651. if ( f.fail() )
  652. {
  653. return 0;
  654. }
  655. // Loop over all registered commands and print out documentation
  656. const char *name;
  657. const char *terse;
  658. const char *full;
  659. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  660. j != m_Commands.end(); ++j)
  661. {
  662. name = (*j).second->GetName();
  663. terse = (*j).second->GetTerseDocumentation();
  664. full = (*j).second->GetFullDocumentation();
  665. f << name << " - " << terse << std::endl
  666. << "Usage: " << full << std::endl << std::endl;
  667. }
  668. return 1;
  669. }
  670. void cmMakefile::ExpandVariablesInString(std::string& source) const
  671. {
  672. this->ExpandVariablesInString(source, false);
  673. }
  674. void cmMakefile::ExpandVariablesInString(std::string& source,
  675. bool escapeQuotes) const
  676. {
  677. for(DefinitionMap::const_iterator i = m_Definitions.begin();
  678. i != m_Definitions.end(); ++i)
  679. {
  680. std::string replace = (*i).second;
  681. if (escapeQuotes)
  682. {
  683. replace = cmSystemTools::EscapeQuotes(replace.c_str());
  684. }
  685. std::string variable = "${";
  686. variable += (*i).first;
  687. variable += "}";
  688. cmSystemTools::ReplaceString(source, variable.c_str(),
  689. replace.c_str());
  690. variable = "@";
  691. variable += (*i).first;
  692. variable += "@";
  693. cmSystemTools::ReplaceString(source, variable.c_str(),
  694. replace.c_str());
  695. }
  696. }
  697. void cmMakefile::RemoveVariablesInString(std::string& source) const
  698. {
  699. cmRegularExpression var("(\\${[A-Za-z_0-9]*})");
  700. cmRegularExpression var2("(@[A-Za-z_0-9]*@)");
  701. while (var.find(source))
  702. {
  703. source.erase(var.start(),var.end() - var.start());
  704. }
  705. while (var2.find(source))
  706. {
  707. source.erase(var2.start(),var2.end() - var2.start());
  708. }
  709. }
  710. // recursive function to create a vector of cmMakefile objects
  711. // This is done by reading the sub directory CMakeLists.txt files,
  712. // then calling this function with the new cmMakefile object
  713. void
  714. cmMakefile::FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>&
  715. makefiles)
  716. {
  717. // loop over all the sub directories of this makefile
  718. const std::vector<std::string>& subdirs = this->GetSubDirectories();
  719. for(std::vector<std::string>::const_iterator i = subdirs.begin();
  720. i != subdirs.end(); ++i)
  721. {
  722. std::string subdir = *i;
  723. // Create a path to the list file in the sub directory
  724. std::string listFile = this->GetCurrentDirectory();
  725. listFile += "/";
  726. listFile += subdir;
  727. listFile += "/CMakeLists.txt";
  728. // if there is a CMakeLists.txt file read it
  729. if(!cmSystemTools::FileExists(listFile.c_str()))
  730. {
  731. cmSystemTools::Error("CMakeLists.txt file missing from sub directory:",
  732. listFile.c_str());
  733. }
  734. else
  735. {
  736. cmMakefile* mf = new cmMakefile;
  737. makefiles.push_back(mf);
  738. // initialize new makefile
  739. mf->SetHomeOutputDirectory(this->GetHomeOutputDirectory());
  740. mf->SetHomeDirectory(this->GetHomeDirectory());
  741. // add the subdir to the start output directory
  742. std::string outdir = this->GetStartOutputDirectory();
  743. outdir += "/";
  744. outdir += subdir;
  745. mf->SetStartOutputDirectory(outdir.c_str());
  746. // add the subdir to the start source directory
  747. std::string currentDir = this->GetStartDirectory();
  748. currentDir += "/";
  749. currentDir += subdir;
  750. mf->SetStartDirectory(currentDir.c_str());
  751. // Parse the CMakeLists.txt file
  752. currentDir += "/CMakeLists.txt";
  753. mf->MakeStartDirectoriesCurrent();
  754. mf->ReadListFile(currentDir.c_str());
  755. // recurse into nextDir
  756. mf->FindSubDirectoryCMakeListsFiles(makefiles);
  757. }
  758. }
  759. }
  760. /**
  761. * Add the default definitions to the makefile. These values must not
  762. * be dependent on anything that isn't known when this cmMakefile instance
  763. * is constructed.
  764. */
  765. void cmMakefile::AddDefaultDefinitions()
  766. {
  767. #if defined(_WIN32) && !defined(__CYGWIN__)
  768. this->AddDefinition("CMAKE_CFG_INTDIR","$(IntDir)");
  769. #else
  770. this->AddDefinition("CMAKE_CFG_INTDIR",".");
  771. #endif
  772. char temp[1024];
  773. sprintf(temp, "%d", cmMakefile::GetMinorVersion());
  774. this->AddDefinition("CMAKE_MINOR_VERSION", temp);
  775. sprintf(temp, "%d", cmMakefile::GetMajorVersion());
  776. this->AddDefinition("CMAKE_MAJOR_VERSION", temp);
  777. }
  778. /**
  779. * Find a source group whose regular expression matches the filename
  780. * part of the given source name. Search backward through the list of
  781. * source groups, and take the first matching group found. This way
  782. * non-inherited SOURCE_GROUP commands will have precedence over
  783. * inherited ones.
  784. */
  785. cmSourceGroup&
  786. cmMakefile::FindSourceGroup(const char* source,
  787. std::vector<cmSourceGroup> &groups)
  788. {
  789. std::string file = source;
  790. std::string::size_type pos = file.rfind('/');
  791. if(pos != std::string::npos)
  792. {
  793. file = file.substr(pos, file.length()-pos);
  794. }
  795. for(std::vector<cmSourceGroup>::reverse_iterator sg = groups.rbegin();
  796. sg != groups.rend(); ++sg)
  797. {
  798. if(sg->Matches(file.c_str()))
  799. {
  800. return *sg;
  801. }
  802. }
  803. // Shouldn't get here, but just in case, return the default group.
  804. return groups.front();
  805. }
  806. bool cmMakefile::IsFunctionBlocked(const char *name,
  807. std::vector<std::string> &args) const
  808. {
  809. // loop over all function blockers to see if any block this command
  810. std::set<cmFunctionBlocker *>::const_iterator pos;
  811. for (pos = m_FunctionBlockers.begin();
  812. pos != m_FunctionBlockers.end(); ++pos)
  813. {
  814. if ((*pos)->IsFunctionBlocked(name, args, *this))
  815. {
  816. return true;
  817. }
  818. }
  819. return false;
  820. }
  821. void cmMakefile::RemoveFunctionBlocker(const char *name,
  822. const std::vector<std::string> &args)
  823. {
  824. // loop over all function blockers to see if any block this command
  825. std::set<cmFunctionBlocker *>::const_iterator pos;
  826. for (pos = m_FunctionBlockers.begin();
  827. pos != m_FunctionBlockers.end(); ++pos)
  828. {
  829. if ((*pos)->ShouldRemove(name, args, *this))
  830. {
  831. cmFunctionBlocker* b = *pos;
  832. m_FunctionBlockers.erase(*pos);
  833. delete b;
  834. return;
  835. }
  836. }
  837. return;
  838. }
  839. void cmMakefile::SetHomeDirectory(const char* dir)
  840. {
  841. m_cmHomeDirectory = dir;
  842. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  843. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  844. }
  845. void cmMakefile::SetHomeOutputDirectory(const char* lib)
  846. {
  847. m_HomeOutputDirectory = lib;
  848. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  849. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  850. }
  851. /**
  852. * Register the given cmData instance with its own name.
  853. */
  854. void cmMakefile::RegisterData(cmData* data)
  855. {
  856. std::string name = data->GetName();
  857. DataMap::const_iterator d = m_DataMap.find(name);
  858. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  859. {
  860. delete d->second;
  861. }
  862. m_DataMap[name] = data;
  863. }
  864. /**
  865. * Register the given cmData instance with the given name. This can be used
  866. * to register a NULL pointer.
  867. */
  868. void cmMakefile::RegisterData(const char* name, cmData* data)
  869. {
  870. DataMap::const_iterator d = m_DataMap.find(name);
  871. if((d != m_DataMap.end()) && (d->second != NULL) && (d->second != data))
  872. {
  873. delete d->second;
  874. }
  875. m_DataMap[name] = data;
  876. }
  877. /**
  878. * Lookup a cmData instance previously registered with the given name. If
  879. * the instance cannot be found, return NULL.
  880. */
  881. cmData* cmMakefile::LookupData(const char* name) const
  882. {
  883. DataMap::const_iterator d = m_DataMap.find(name);
  884. if(d != m_DataMap.end())
  885. {
  886. return d->second;
  887. }
  888. else
  889. {
  890. return NULL;
  891. }
  892. }