cmMakefile.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. #ifndef cmMakefile_h
  12. #define cmMakefile_h
  13. #include "cmStandardIncludes.h"
  14. #include "cmClassFile.h"
  15. #include "cmSystemTools.h"
  16. class cmCommand;
  17. class cmMakefileGenerator;
  18. /** \class cmMakefile
  19. * \brief Process the input CMakeLists.txt file.
  20. *
  21. * Process and store into memory the input CMakeLists.txt file.
  22. * Each CMakeLists.txt file is parsed and the commands found there
  23. * are added into the build process.
  24. */
  25. class cmMakefile
  26. {
  27. public:
  28. /**
  29. * Construct an empty makefile.
  30. */
  31. cmMakefile();
  32. /**
  33. * Destructor.
  34. */
  35. ~cmMakefile();
  36. /**
  37. * Read and parse a CMakeLists.txt file.
  38. */
  39. bool ReadListFile(const char* listfile);
  40. /**
  41. * Add a wrapper generator.
  42. */
  43. void AddCommand(cmCommand* );
  44. /**
  45. * Specify the makefile generator. This is platform/compiler
  46. * dependent, although the interface is through a generic
  47. * superclass.
  48. */
  49. void SetMakefileGenerator(cmMakefileGenerator*);
  50. /**
  51. * Produce the output makefile.
  52. */
  53. void GenerateMakefile();
  54. /**
  55. * Print the object state to std::cout.
  56. */
  57. void Print();
  58. /**
  59. * Add a custom command to the build.
  60. */
  61. void AddCustomCommand(const char* source,
  62. const char* result,
  63. const char* command,
  64. std::vector<std::string>& depends);
  65. /**
  66. * Add a define flag to the build.
  67. */
  68. void AddDefineFlag(const char* definition);
  69. /**
  70. * Add an executable to the build.
  71. */
  72. void AddExecutable(cmClassFile&);
  73. /**
  74. * Add a utility on which this project depends.
  75. */
  76. void AddUtility(const char*);
  77. /**
  78. * Add a directory in which a utility may be built.
  79. */
  80. void AddUtilityDirectory(const char*);
  81. /**
  82. * Add a link library to the build.
  83. */
  84. void AddLinkLibrary(const char*);
  85. /**
  86. * Add a link directory to the build.
  87. */
  88. void AddLinkDirectory(const char*);
  89. /**
  90. * Add a subdirectory to the build.
  91. */
  92. void AddSubDirectory(const char*);
  93. /**
  94. * Add an include directory to the build.
  95. */
  96. void AddIncludeDirectory(const char*);
  97. /**
  98. * Add a variable definition to the build. This variable
  99. * can be used in CMake to refer to lists, directories, etc.
  100. */
  101. void AddDefinition(const char* name, const char* value);
  102. /**
  103. * Specify the name of the project for this build.
  104. */
  105. void SetProjectName(const char*);
  106. /**
  107. * Get the name of the project for this build.
  108. */
  109. const char* GetProjectName()
  110. {
  111. return m_ProjectName.c_str();
  112. }
  113. /**
  114. * Set the name of the library.
  115. */
  116. void SetLibraryName(const char*);
  117. /**
  118. * Add a class/source file to the build.
  119. */
  120. void AddClass(cmClassFile& );
  121. /**
  122. * Add an auxiliary directory to the build.
  123. */
  124. void AddExtraDirectory(const char* dir);
  125. /**
  126. * Add an auxiliary directory to the build.
  127. */
  128. void MakeStartDirectoriesCurrent()
  129. {
  130. m_cmCurrentDirectory = m_cmStartDirectory;
  131. m_CurrentOutputDirectory = m_StartOutputDirectory;
  132. }
  133. //@{
  134. /**
  135. * Set/Get the home directory (or output directory) in the project. The
  136. * home directory is the top directory of the project. It is where
  137. * CMakeSetup or configure was run. Remember that CMake processes
  138. * CMakeLists files by recursing up the tree starting at the StartDirectory
  139. * and going up until it reaches the HomeDirectory.
  140. */
  141. void SetHomeDirectory(const char* dir)
  142. {
  143. m_cmHomeDirectory = dir;
  144. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  145. }
  146. const char* GetHomeDirectory()
  147. {
  148. return m_cmHomeDirectory.c_str();
  149. }
  150. void SetHomeOutputDirectory(const char* lib)
  151. {
  152. m_HomeOutputDirectory = lib;
  153. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  154. }
  155. const char* GetHomeOutputDirectory()
  156. {
  157. return m_HomeOutputDirectory.c_str();
  158. }
  159. //@}
  160. //@{
  161. /**
  162. * Set/Get the start directory (or output directory). The start directory
  163. * is the directory of the CMakeLists.txt file that started the current
  164. * round of processing. Remember that CMake processes CMakeLists files by
  165. * recursing up the tree starting at the StartDirectory and going up until
  166. * it reaches the HomeDirectory.
  167. */
  168. void SetStartDirectory(const char* dir)
  169. {
  170. m_cmStartDirectory = dir;
  171. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  172. }
  173. const char* GetStartDirectory()
  174. {
  175. return m_cmStartDirectory.c_str();
  176. }
  177. void SetStartOutputDirectory(const char* lib)
  178. {
  179. m_StartOutputDirectory = lib;
  180. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  181. }
  182. const char* GetStartOutputDirectory()
  183. {
  184. return m_StartOutputDirectory.c_str();
  185. }
  186. //@}
  187. //@{
  188. /**
  189. * Set/Get the current directory (or output directory) in the project. The
  190. * current directory is the directory of the CMakeLists.txt file that is
  191. * currently being processed. Remember that CMake processes CMakeLists
  192. * files by recursing up the tree starting at the StartDirectory and going
  193. * up until it reaches the HomeDirectory.
  194. */
  195. void SetCurrentDirectory(const char* dir)
  196. {
  197. m_cmCurrentDirectory = dir;
  198. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  199. }
  200. const char* GetCurrentDirectory()
  201. {
  202. return m_cmCurrentDirectory.c_str();
  203. }
  204. void SetCurrentOutputDirectory(const char* lib)
  205. {
  206. m_CurrentOutputDirectory = lib;
  207. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  208. }
  209. const char* GetCurrentOutputDirectory()
  210. {
  211. return m_CurrentOutputDirectory.c_str();
  212. }
  213. //@}
  214. /**
  215. * Specify the name of the library that is built by this makefile.
  216. */
  217. const char* GetLibraryName()
  218. {
  219. return m_LibraryName.c_str();
  220. }
  221. /**
  222. * Get a list of the build subdirectories.
  223. */
  224. const std::vector<std::string>& GetSubDirectories()
  225. {
  226. return m_SubDirectories;
  227. }
  228. /**
  229. * Return a boolean flag indicating whether the build generates
  230. * any executables.
  231. */
  232. bool HasExecutables();
  233. /**
  234. * Get a list of include directories in the build.
  235. */
  236. std::vector<std::string>& GetIncludeDirectories()
  237. {
  238. return m_IncludeDirectories;
  239. }
  240. /**
  241. * Get a list of link directories in the build.
  242. */
  243. std::vector<std::string>& GetLinkDirectories()
  244. {
  245. return m_LinkDirectories;
  246. }
  247. /**
  248. * Get a list of utilities on which the project depends.
  249. */
  250. std::vector<std::string>& GetUtilities()
  251. {
  252. return m_Utilities;
  253. }
  254. /**
  255. * Get a list of directories that may contain the Utilities.
  256. */
  257. std::vector<std::string>& GetUtilityDirectories()
  258. {
  259. return m_UtilityDirectories;
  260. }
  261. /**
  262. * Get a list of link libraries in the build.
  263. */
  264. std::vector<std::string>& GetLinkLibraries()
  265. {
  266. return m_LinkLibraries;
  267. }
  268. /**
  269. * Get a list of Win32 link libraries in the build.
  270. */
  271. std::vector<std::string>& GetLinkLibrariesWin32()
  272. {
  273. return m_LinkLibrariesWin32;
  274. }
  275. /**
  276. * Get a list of Unix link libraries in the build.
  277. */
  278. std::vector<std::string>& GetLinkLibrariesUnix()
  279. {
  280. return m_LinkLibrariesUnix;
  281. }
  282. /**
  283. * Return a list of source files in this makefile.
  284. */
  285. std::vector<cmClassFile>& GetClasses()
  286. {return m_Classes;}
  287. /**
  288. * Obtain a list of auxiliary source directories.
  289. */
  290. std::vector<std::string>& GetAuxSourceDirectories()
  291. {return m_AuxSourceDirectories;}
  292. /**
  293. * Do not use this.
  294. */
  295. std::vector<std::string>& GetMakeVerbatim()
  296. {return m_MakeVerbatim;}
  297. /**
  298. * Given a variable name, return its value (as a string).
  299. */
  300. const char* GetDefinition(const char*);
  301. /**
  302. * Get a list of preprocessor define flags.
  303. */
  304. const char* GetDefineFlags()
  305. {return m_DefineFlags.c_str();}
  306. /**
  307. * Get the vector of used command instances.
  308. */
  309. const std::vector<cmCommand*>& GetUsedCommands() const
  310. {return m_UsedCommands;}
  311. /**
  312. * Dump documentation to a file. If 0 is returned, the
  313. * operation failed.
  314. */
  315. int DumpDocumentationToFile(const char *fileName);
  316. /**
  317. * Expand all defined varibles in the string.
  318. * Defined varibles come from the m_Definitions map.
  319. * They are expanded with ${var} where var is the
  320. * entry in the m_Definitions map. Also @var@ is
  321. * expanded to match autoconf style expansions.
  322. */
  323. void ExpandVariablesInString(std::string& source);
  324. /**
  325. * Expand variables in the makefiles ivars such as link directories etc
  326. */
  327. void ExpandVariables();
  328. struct customCommand
  329. {
  330. std::string m_Source;
  331. std::string m_Result;
  332. std::string m_Command;
  333. std::vector<std::string> m_Depends;
  334. };
  335. std::vector<customCommand>& GetCustomCommands() {
  336. return m_CustomCommands; };
  337. /** Recursivly read and create a cmMakefile object for
  338. * all CMakeLists.txt files in the GetSubDirectories list.
  339. * Once the file is found, it ReadListFile is called on
  340. * the cmMakefile created for it.
  341. */
  342. void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  343. /** Generate the cache file only. This is done
  344. * by calling FindSubDirectoryCMakeListsFiles which
  345. * will cause all the rules to fire, and the cache to
  346. * be filled.
  347. */
  348. void GenerateCacheOnly();
  349. protected:
  350. std::string m_Prefix;
  351. std::vector<std::string> m_AuxSourceDirectories; //
  352. std::string m_cmCurrentDirectory;
  353. std::string m_CurrentOutputDirectory;
  354. std::string m_cmStartDirectory;
  355. std::string m_StartOutputDirectory;
  356. std::string m_cmHomeDirectory;
  357. std::string m_HomeOutputDirectory;
  358. std::string m_LibraryName; // library name
  359. std::string m_ProjectName; // project name
  360. std::vector<cmClassFile> m_Classes; // list of classes in makefile
  361. std::vector<std::string> m_SubDirectories; // list of sub directories
  362. std::vector<std::string> m_MakeVerbatim; // lines copied from input file
  363. std::vector<std::string> m_IncludeDirectories;
  364. std::vector<std::string> m_LinkDirectories;
  365. std::vector<std::string> m_Utilities;
  366. std::vector<std::string> m_UtilityDirectories;
  367. std::vector<std::string> m_LinkLibraries;
  368. std::vector<std::string> m_LinkLibrariesWin32;
  369. std::vector<std::string> m_LinkLibrariesUnix;
  370. std::string m_DefineFlags;
  371. std::vector<customCommand> m_CustomCommands;
  372. typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
  373. typedef std::map<std::string, std::string> DefinitionMap;
  374. DefinitionMap m_Definitions;
  375. RegisteredCommandsMap m_Commands;
  376. std::vector<cmCommand*> m_UsedCommands;
  377. cmMakefileGenerator* m_MakefileGenerator;
  378. private:
  379. /**
  380. * Get the name of the parent directories CMakeLists file
  381. * given a current CMakeLists file name
  382. */
  383. std::string GetParentListFileName(const char *listFileName);
  384. void ReadClasses(std::ifstream& fin, bool t);
  385. friend class cmMakeDepend; // make depend needs direct access
  386. // to the m_Classes array
  387. void PrintStringVector(const char* s, std::vector<std::string>& v);
  388. void AddDefaultCommands();
  389. void AddDefaultDefinitions();
  390. };
  391. #endif