cmMakefile.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. // default is not to be building executables
  20. cmMakefile::cmMakefile()
  21. {
  22. m_DefineFlags = " ";
  23. m_MakefileGenerator = 0;
  24. this->AddDefaultCommands();
  25. }
  26. void cmMakefile::AddDefaultCommands()
  27. {
  28. std::list<cmCommand*> commands;
  29. GetPredefinedCommands(commands);
  30. for(std::list<cmCommand*>::iterator i = commands.begin();
  31. i != commands.end(); ++i)
  32. {
  33. this->AddCommand(*i);
  34. }
  35. #ifdef _WIN32
  36. this->AddDefinition("WIN32", "1");
  37. #else
  38. this->AddDefinition("UNIX", "1");
  39. #endif
  40. // Cygwin is more like unix so enable the unix commands
  41. #if defined(__CYGWIN__)
  42. this->AddDefinition("UNIX", "1");
  43. #endif
  44. }
  45. cmMakefile::~cmMakefile()
  46. {
  47. for(int i=0; i < m_UsedCommands.size(); i++)
  48. {
  49. delete m_UsedCommands[i];
  50. }
  51. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  52. j != m_Commands.end(); ++j)
  53. {
  54. delete (*j).second;
  55. }
  56. delete m_MakefileGenerator;
  57. }
  58. void cmMakefile::PrintStringVector(const char* s, std::vector<std::string>& v)
  59. {
  60. std::cout << s << ": ( \n";
  61. for(std::vector<std::string>::iterator i = v.begin();
  62. i != v.end(); ++i)
  63. {
  64. std::cout << (*i).c_str() << " ";
  65. }
  66. std::cout << " )\n";
  67. }
  68. // call print on all the classes in the makefile
  69. void cmMakefile::Print()
  70. {
  71. std::cout << "classes:\n";
  72. for(unsigned int i = 0; i < m_Classes.size(); i++)
  73. m_Classes[i].Print();
  74. std::cout << " m_CurrentOutputDirectory; " <<
  75. m_CurrentOutputDirectory.c_str() << std::endl;
  76. std::cout << " m_StartOutputDirectory; " <<
  77. m_StartOutputDirectory.c_str() << std::endl;
  78. std::cout << " m_HomeOutputDirectory; " <<
  79. m_HomeOutputDirectory.c_str() << std::endl;
  80. std::cout << " m_cmCurrentDirectory; " <<
  81. m_cmCurrentDirectory.c_str() << std::endl;
  82. std::cout << " m_cmStartDirectory; " <<
  83. m_cmStartDirectory.c_str() << std::endl;
  84. std::cout << " m_cmHomeDirectory; " <<
  85. m_cmHomeDirectory.c_str() << std::endl;
  86. std::cout << " m_LibraryName; " << m_LibraryName.c_str() << std::endl;
  87. std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
  88. this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
  89. this->PrintStringVector("m_MakeVerbatim ", m_MakeVerbatim);
  90. this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  91. this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
  92. this->PrintStringVector("m_LinkLibraries", m_LinkLibraries);
  93. this->PrintStringVector("m_LinkLibrariesWin32", m_LinkLibrariesWin32);
  94. this->PrintStringVector("m_LinkLibrariesUnix", m_LinkLibrariesUnix);
  95. }
  96. // Parse the given CMakeLists.txt file into a list of classes.
  97. // Reads in current CMakeLists file and all parent CMakeLists files
  98. // executing all inherited commands in the parents
  99. bool cmMakefile::ReadListFile(const char* filename)
  100. {
  101. // is there a parent CMakeLists file that does not go beyond the
  102. // Home directory? if so recurse and read in that List file
  103. std::string parentList = this->GetParentListFileName(filename);
  104. if (parentList != "")
  105. {
  106. // save the current directory
  107. std::string srcdir = m_cmCurrentDirectory;
  108. std::string bindir = m_CurrentOutputDirectory;
  109. // compute the new current directories
  110. std::string::size_type pos = m_cmCurrentDirectory.rfind('/');
  111. if(pos != std::string::npos)
  112. {
  113. m_cmCurrentDirectory = m_cmCurrentDirectory.substr(0, pos);
  114. }
  115. pos = m_CurrentOutputDirectory.rfind('/');
  116. if(pos != std::string::npos)
  117. {
  118. m_CurrentOutputDirectory = m_CurrentOutputDirectory.substr(0, pos);
  119. }
  120. this->ReadListFile(parentList.c_str());
  121. // restore the current directory
  122. m_cmCurrentDirectory = srcdir;
  123. m_CurrentOutputDirectory = bindir;
  124. }
  125. // are we at the start CMakeLists file or are we processing a parent
  126. // lists file
  127. bool inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
  128. // Now read the input file
  129. std::ifstream fin(filename);
  130. if(!fin)
  131. {
  132. cmSystemTools::Error("error can not open file ", filename);
  133. return false;
  134. }
  135. std::string name;
  136. std::vector<std::string> arguments;
  137. while ( fin )
  138. {
  139. if(cmSystemTools::ParseFunction(fin, name, arguments) )
  140. {
  141. // Special command that needs to be removed when
  142. // ADD_COMMAND is implemented
  143. if(name == "VERBATIM")
  144. {
  145. if (!inheriting)
  146. {
  147. m_MakeVerbatim = arguments;
  148. }
  149. }
  150. else
  151. {
  152. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  153. if(pos != m_Commands.end())
  154. {
  155. cmCommand* rm = (*pos).second;
  156. cmCommand* usedCommand = rm->Clone();
  157. usedCommand->SetMakefile(this);
  158. usedCommand->LoadCache();
  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 function", 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. }