cmMakefile.cxx 27 KB

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