cmMakefile.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2000 National Library of Medicine
  8. All rights reserved.
  9. See COPYRIGHT.txt for copyright details.
  10. =========================================================================*/
  11. #include "cmMakefile.h"
  12. #include "cmCommand.h"
  13. #include "cmStandardIncludes.h"
  14. #include "cmClassFile.h"
  15. #include "cmDirectory.h"
  16. #include "cmSystemTools.h"
  17. #include "cmMakefileGenerator.h"
  18. #include "cmCommands.h"
  19. #include "cmCacheManager.h"
  20. // default is not to be building executables
  21. cmMakefile::cmMakefile()
  22. {
  23. m_DefineFlags = " ";
  24. m_MakefileGenerator = 0;
  25. this->AddDefaultCommands();
  26. this->AddDefaultDefinitions();
  27. }
  28. void cmMakefile::AddDefaultCommands()
  29. {
  30. std::list<cmCommand*> commands;
  31. GetPredefinedCommands(commands);
  32. for(std::list<cmCommand*>::iterator i = commands.begin();
  33. i != commands.end(); ++i)
  34. {
  35. this->AddCommand(*i);
  36. }
  37. #ifdef _WIN32
  38. this->AddDefinition("WIN32", "1");
  39. #else
  40. this->AddDefinition("UNIX", "1");
  41. #endif
  42. // Cygwin is more like unix so enable the unix commands
  43. #if defined(__CYGWIN__)
  44. this->AddDefinition("UNIX", "1");
  45. #endif
  46. }
  47. cmMakefile::~cmMakefile()
  48. {
  49. for(unsigned int i=0; i < m_UsedCommands.size(); i++)
  50. {
  51. delete m_UsedCommands[i];
  52. }
  53. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  54. j != m_Commands.end(); ++j)
  55. {
  56. delete (*j).second;
  57. }
  58. delete m_MakefileGenerator;
  59. }
  60. void cmMakefile::PrintStringVector(const char* s, std::vector<std::string>& v)
  61. {
  62. std::cout << s << ": ( \n";
  63. for(std::vector<std::string>::iterator i = v.begin();
  64. i != v.end(); ++i)
  65. {
  66. std::cout << (*i).c_str() << " ";
  67. }
  68. std::cout << " )\n";
  69. }
  70. // call print on all the classes in the makefile
  71. void cmMakefile::Print()
  72. {
  73. std::cout << "classes:\n";
  74. for(unsigned int i = 0; i < m_Classes.size(); i++)
  75. m_Classes[i].Print();
  76. std::cout << " m_CurrentOutputDirectory; " <<
  77. m_CurrentOutputDirectory.c_str() << std::endl;
  78. std::cout << " m_StartOutputDirectory; " <<
  79. m_StartOutputDirectory.c_str() << std::endl;
  80. std::cout << " m_HomeOutputDirectory; " <<
  81. m_HomeOutputDirectory.c_str() << std::endl;
  82. std::cout << " m_cmCurrentDirectory; " <<
  83. m_cmCurrentDirectory.c_str() << std::endl;
  84. std::cout << " m_cmStartDirectory; " <<
  85. m_cmStartDirectory.c_str() << std::endl;
  86. std::cout << " m_cmHomeDirectory; " <<
  87. m_cmHomeDirectory.c_str() << std::endl;
  88. std::cout << " m_LibraryName; " << m_LibraryName.c_str() << std::endl;
  89. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  90. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  91. this->PrintStringVector("m_MakeVerbatim ", m_MakeVerbatim);
  92. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  93. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  94. this->PrintStringVector("m_LinkLibraries", m_LinkLibraries);
  95. this->PrintStringVector("m_LinkLibrariesWin32", m_LinkLibrariesWin32);
  96. this->PrintStringVector("m_LinkLibrariesUnix", m_LinkLibrariesUnix);
  97. this->PrintStringVector("m_Utilities", m_Utilities);
  98. this->PrintStringVector("m_UtilityDirectories", m_UtilityDirectories);
  99. }
  100. // Parse the given CMakeLists.txt file into a list of classes.
  101. // Reads in current CMakeLists file and all parent CMakeLists files
  102. // executing all inherited commands in the parents
  103. bool cmMakefile::ReadListFile(const char* filename)
  104. {
  105. // is there a parent CMakeLists file that does not go beyond the
  106. // Home directory? if so recurse and read in that List file
  107. std::string parentList = this->GetParentListFileName(filename);
  108. if (parentList != "")
  109. {
  110. // save the current directory
  111. std::string srcdir = m_cmCurrentDirectory;
  112. std::string bindir = m_CurrentOutputDirectory;
  113. // compute the new current directories
  114. std::string::size_type pos = m_cmCurrentDirectory.rfind('/');
  115. if(pos != std::string::npos)
  116. {
  117. m_cmCurrentDirectory = m_cmCurrentDirectory.substr(0, pos);
  118. }
  119. pos = m_CurrentOutputDirectory.rfind('/');
  120. if(pos != std::string::npos)
  121. {
  122. m_CurrentOutputDirectory = m_CurrentOutputDirectory.substr(0, pos);
  123. }
  124. this->ReadListFile(parentList.c_str());
  125. // restore the current directory
  126. m_cmCurrentDirectory = srcdir;
  127. m_CurrentOutputDirectory = bindir;
  128. }
  129. // are we at the start CMakeLists file or are we processing a parent
  130. // lists file
  131. bool inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  132. // Now read the input file
  133. std::ifstream fin(filename);
  134. if(!fin)
  135. {
  136. cmSystemTools::Error("error can not open file ", filename);
  137. return false;
  138. }
  139. std::string name;
  140. std::vector<std::string> arguments;
  141. while ( fin )
  142. {
  143. if(cmSystemTools::ParseFunction(fin, name, arguments) )
  144. {
  145. // Special command that needs to be removed when
  146. // ADD_COMMAND is implemented
  147. if(name == "VERBATIM")
  148. {
  149. if (!inheriting)
  150. {
  151. m_MakeVerbatim = arguments;
  152. }
  153. }
  154. else
  155. {
  156. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  157. if(pos != m_Commands.end())
  158. {
  159. cmCommand* rm = (*pos).second;
  160. cmCommand* usedCommand = rm->Clone();
  161. usedCommand->SetMakefile(this);
  162. bool keepCommand = false;
  163. if(usedCommand->GetEnabled())
  164. {
  165. // if not running in inherit mode or
  166. // if the command is inherited then Invoke it.
  167. if(!inheriting || usedCommand->IsInherited())
  168. {
  169. if(!usedCommand->Invoke(arguments))
  170. {
  171. cmSystemTools::Error(usedCommand->GetError());
  172. }
  173. else
  174. {
  175. // use the command
  176. keepCommand = true;
  177. m_UsedCommands.push_back(usedCommand);
  178. }
  179. }
  180. }
  181. // if the Cloned command was not used
  182. // then delete it
  183. if(!keepCommand)
  184. {
  185. delete usedCommand;
  186. }
  187. }
  188. else
  189. {
  190. cmSystemTools::Error("unknown CMake command ", name.c_str());
  191. }
  192. }
  193. }
  194. }
  195. return true;
  196. }
  197. void cmMakefile::AddCommand(cmCommand* wg)
  198. {
  199. std::string name = wg->GetName();
  200. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  201. }
  202. // Set the make file
  203. void cmMakefile::SetMakefileGenerator(cmMakefileGenerator* mf)
  204. {
  205. delete m_MakefileGenerator;
  206. m_MakefileGenerator = mf;
  207. }
  208. // Generate the output file
  209. void cmMakefile::GenerateMakefile()
  210. {
  211. // do all the variable expansions here
  212. this->ExpandVariables();
  213. // set the makefile on the generator
  214. m_MakefileGenerator->SetMakefile(this);
  215. // give all the commands a chance to do something
  216. // after the file has been parsed before generation
  217. for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
  218. i != m_UsedCommands.end(); ++i)
  219. {
  220. (*i)->FinalPass();
  221. }
  222. // now do the generation
  223. m_MakefileGenerator->GenerateMakefile();
  224. }
  225. void cmMakefile::AddClass(cmClassFile& cmfile)
  226. {
  227. m_Classes.push_back(cmfile);
  228. }
  229. void cmMakefile::AddCustomCommand(const char* source,
  230. const char* result,
  231. const char* command,
  232. std::vector<std::string>& depends)
  233. {
  234. cmMakefile::customCommand customCommand;
  235. customCommand.m_Source = source;
  236. customCommand.m_Result = result;
  237. customCommand.m_Command = command;
  238. customCommand.m_Depends = depends;
  239. m_CustomCommands.push_back(customCommand);
  240. }
  241. void cmMakefile::AddDefineFlag(const char* flag)
  242. {
  243. m_DefineFlags += " ";
  244. m_DefineFlags += flag;
  245. }
  246. void cmMakefile::AddExecutable(cmClassFile& cf)
  247. {
  248. cf.m_IsExecutable = true;
  249. m_Classes.push_back(cf);
  250. }
  251. bool cmMakefile::HasExecutables()
  252. {
  253. for(unsigned int i = 0; i < m_Classes.size(); i++)
  254. {
  255. if (m_Classes[i].m_IsExecutable)
  256. {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. void cmMakefile::AddUtility(const char* util)
  263. {
  264. m_Utilities.push_back(util);
  265. }
  266. void cmMakefile::AddUtilityDirectory(const char* dir)
  267. {
  268. m_UtilityDirectories.push_back(dir);
  269. }
  270. void cmMakefile::AddLinkLibrary(const char* lib)
  271. {
  272. m_LinkLibraries.push_back(lib);
  273. }
  274. void cmMakefile::AddLinkDirectory(const char* dir)
  275. {
  276. m_LinkDirectories.push_back(dir);
  277. }
  278. void cmMakefile::AddSubDirectory(const char* sub)
  279. {
  280. m_SubDirectories.push_back(sub);
  281. }
  282. void cmMakefile::AddIncludeDirectory(const char* inc)
  283. {
  284. m_IncludeDirectories.push_back(inc);
  285. }
  286. void cmMakefile::AddDefinition(const char* name, const char* value)
  287. {
  288. m_Definitions.insert(DefinitionMap::value_type(name, value));
  289. }
  290. void cmMakefile::SetProjectName(const char* p)
  291. {
  292. m_ProjectName = p;
  293. }
  294. void cmMakefile::SetLibraryName(const char* l)
  295. {
  296. m_LibraryName = l;
  297. }
  298. void cmMakefile::AddExtraDirectory(const char* dir)
  299. {
  300. m_AuxSourceDirectories.push_back(dir);
  301. }
  302. // return the file name for the parent CMakeLists file to the
  303. // one passed in. Zero is returned if the CMakeLists file is the
  304. // one in the home directory or if for some reason a parent cmake lists
  305. // file cannot be found.
  306. std::string cmMakefile::GetParentListFileName(const char *currentFileName)
  307. {
  308. // extract the directory name
  309. std::string parentFile;
  310. std::string listsDir = currentFileName;
  311. std::string::size_type pos = listsDir.rfind('/');
  312. // if we could not find the directory return 0
  313. if(pos == std::string::npos)
  314. {
  315. return parentFile;
  316. }
  317. listsDir = listsDir.substr(0, pos);
  318. // if we are in the home directory then stop, return 0
  319. if(m_cmHomeDirectory == listsDir)
  320. {
  321. return parentFile;
  322. }
  323. // is there a parent directory we can check
  324. pos = listsDir.rfind('/');
  325. // if we could not find the directory return 0
  326. if(pos == std::string::npos)
  327. {
  328. return parentFile;
  329. }
  330. listsDir = listsDir.substr(0, pos);
  331. // is there a CMakeLists.txt file in the parent directory ?
  332. parentFile = listsDir;
  333. parentFile += "/CMakeLists.txt";
  334. if(!cmSystemTools::FileExists(parentFile.c_str()))
  335. {
  336. parentFile = "";
  337. return parentFile;
  338. }
  339. return parentFile;
  340. }
  341. // expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
  342. // include and library directories.
  343. void cmMakefile::ExpandVariables()
  344. {
  345. // make sure binary and source dir are defined
  346. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  347. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  348. // Now expand varibles in the include and link strings
  349. std::vector<std::string>::iterator j, begin, end;
  350. begin = m_IncludeDirectories.begin();
  351. end = m_IncludeDirectories.end();
  352. for(j = begin; j != end; ++j)
  353. {
  354. this->ExpandVariablesInString(*j);
  355. }
  356. begin = m_LinkDirectories.begin();
  357. end = m_LinkDirectories.end();
  358. for(j = begin; j != end; ++j)
  359. {
  360. this->ExpandVariablesInString(*j);
  361. }
  362. begin = m_LinkLibraries.begin();
  363. end = m_LinkLibraries.end();
  364. for(j = begin; j != end; ++j)
  365. {
  366. this->ExpandVariablesInString(*j);
  367. }
  368. }
  369. const char* cmMakefile::GetDefinition(const char* name)
  370. {
  371. DefinitionMap::iterator pos = m_Definitions.find(name);
  372. if(pos != m_Definitions.end())
  373. {
  374. return (*pos).second.c_str();
  375. }
  376. return 0;
  377. }
  378. int cmMakefile::DumpDocumentationToFile(const char *fileName)
  379. {
  380. // Open the supplied filename
  381. std::ofstream f;
  382. f.open(fileName, std::ios::out);
  383. if ( f.fail() )
  384. {
  385. return 0;
  386. }
  387. // Loop over all registered commands and print out documentation
  388. const char *name;
  389. const char *terse;
  390. const char *full;
  391. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  392. j != m_Commands.end(); ++j)
  393. {
  394. name = (*j).second->GetName();
  395. terse = (*j).second->GetTerseDocumentation();
  396. full = (*j).second->GetFullDocumentation();
  397. f << name << " - " << terse << std::endl
  398. << "Usage: " << full << std::endl << std::endl;
  399. }
  400. return 1;
  401. }
  402. void cmMakefile::ExpandVariablesInString(std::string& source)
  403. {
  404. for(DefinitionMap::iterator i = m_Definitions.begin();
  405. i != m_Definitions.end(); ++i)
  406. {
  407. std::string variable = "${";
  408. variable += (*i).first;
  409. variable += "}";
  410. cmSystemTools::ReplaceString(source, variable.c_str(),
  411. (*i).second.c_str());
  412. variable = "@";
  413. variable += (*i).first;
  414. variable += "@";
  415. cmSystemTools::ReplaceString(source, variable.c_str(),
  416. (*i).second.c_str());
  417. }
  418. }
  419. // recursive function to create a vector of cmMakefile objects
  420. // This is done by reading the sub directory CMakeLists.txt files,
  421. // then calling this function with the new cmMakefile object
  422. void
  423. cmMakefile::FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>&
  424. makefiles)
  425. {
  426. // loop over all the sub directories of this makefile
  427. const std::vector<std::string>& subdirs = this->GetSubDirectories();
  428. for(std::vector<std::string>::const_iterator i = subdirs.begin();
  429. i != subdirs.end(); ++i)
  430. {
  431. std::string subdir = *i;
  432. // Create a path to the list file in the sub directory
  433. std::string listFile = this->GetCurrentDirectory();
  434. listFile += "/";
  435. listFile += subdir;
  436. listFile += "/CMakeLists.txt";
  437. // if there is a CMakeLists.txt file read it
  438. if(!cmSystemTools::FileExists(listFile.c_str()))
  439. {
  440. cmSystemTools::Error("CMakeLists.txt file missing from sub directory:",
  441. listFile.c_str());
  442. }
  443. else
  444. {
  445. cmMakefile* mf = new cmMakefile;
  446. makefiles.push_back(mf);
  447. // initialize new makefile
  448. mf->SetHomeOutputDirectory(this->GetHomeOutputDirectory());
  449. mf->SetHomeDirectory(this->GetHomeDirectory());
  450. // add the subdir to the start output directory
  451. std::string outdir = this->GetStartOutputDirectory();
  452. outdir += "/";
  453. outdir += subdir;
  454. mf->SetStartOutputDirectory(outdir.c_str());
  455. // add the subdir to the start source directory
  456. std::string currentDir = this->GetStartDirectory();
  457. currentDir += "/";
  458. currentDir += subdir;
  459. mf->SetStartDirectory(currentDir.c_str());
  460. // Parse the CMakeLists.txt file
  461. currentDir += "/CMakeLists.txt";
  462. mf->MakeStartDirectoriesCurrent();
  463. mf->ReadListFile(currentDir.c_str());
  464. // recurse into nextDir
  465. mf->FindSubDirectoryCMakeListsFiles(makefiles);
  466. }
  467. }
  468. }
  469. void cmMakefile::GenerateCacheOnly()
  470. {
  471. std::vector<cmMakefile*> makefiles;
  472. this->FindSubDirectoryCMakeListsFiles(makefiles);
  473. for(unsigned int i =0; i < makefiles.size(); ++i)
  474. {
  475. delete makefiles[i];
  476. }
  477. }
  478. /**
  479. * Add the default definitions to the makefile. These values must not
  480. * be dependent on anything that isn't known when this cmMakefile instance
  481. * is constructed.
  482. */
  483. void cmMakefile::AddDefaultDefinitions()
  484. {
  485. #if defined(_WIN32) && !defined(__CYGWIN__)
  486. this->AddDefinition("CMAKE_CFG_OUTDIR","$(OUTDIR)");
  487. #else
  488. this->AddDefinition("CMAKE_CFG_OUTDIR",".");
  489. #endif
  490. }