cmMakefile.cxx 25 KB

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