cmMakefile.h 13 KB

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