cmMakefile.cxx 22 KB

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