cmMakefile.cxx 14 KB

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