cmMakefile.cxx 27 KB

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