cmMakefile.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #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. * Add a link library to the build.
  102. */
  103. void AddLinkLibrary(const char*);
  104. /**
  105. * Add a link directory to the build.
  106. */
  107. void AddLinkDirectory(const char*);
  108. /**
  109. * Add a subdirectory to the build.
  110. */
  111. void AddSubDirectory(const char*);
  112. /**
  113. * Add an include directory to the build.
  114. */
  115. void AddIncludeDirectory(const char*);
  116. /**
  117. * Add a variable definition to the build. This variable
  118. * can be used in CMake to refer to lists, directories, etc.
  119. */
  120. void AddDefinition(const char* name, const char* value);
  121. /**
  122. * Specify the name of the project for this build.
  123. */
  124. void SetProjectName(const char*);
  125. /**
  126. * Get the name of the project for this build.
  127. */
  128. const char* GetProjectName()
  129. {
  130. return m_ProjectName.c_str();
  131. }
  132. /**
  133. * Set the name of the library.
  134. */
  135. void AddLibrary(const char *libname, const std::vector<std::string> &srcs);
  136. /**
  137. * Add a class/source file to the build.
  138. */
  139. void AddClass(cmClassFile& ,const char *srcListName);
  140. /**
  141. * Add a source group for consideration when adding a new source.
  142. */
  143. void AddSourceGroup(const char* name, const char* regex);
  144. /**
  145. * Add an auxiliary directory to the build.
  146. */
  147. void AddExtraDirectory(const char* dir);
  148. /**
  149. * Add an auxiliary directory to the build.
  150. */
  151. void MakeStartDirectoriesCurrent()
  152. {
  153. m_cmCurrentDirectory = m_cmStartDirectory;
  154. m_CurrentOutputDirectory = m_StartOutputDirectory;
  155. }
  156. //@{
  157. /**
  158. * Set/Get the home directory (or output directory) in the project. The
  159. * home directory is the top directory of the project. It is where
  160. * CMakeSetup or configure was run. Remember that CMake processes
  161. * CMakeLists files by recursing up the tree starting at the StartDirectory
  162. * and going up until it reaches the HomeDirectory.
  163. */
  164. void SetHomeDirectory(const char* dir)
  165. {
  166. m_cmHomeDirectory = dir;
  167. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  168. this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
  169. }
  170. const char* GetHomeDirectory()
  171. {
  172. return m_cmHomeDirectory.c_str();
  173. }
  174. void SetHomeOutputDirectory(const char* lib)
  175. {
  176. m_HomeOutputDirectory = lib;
  177. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  178. this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
  179. }
  180. const char* GetHomeOutputDirectory()
  181. {
  182. return m_HomeOutputDirectory.c_str();
  183. }
  184. //@}
  185. //@{
  186. /**
  187. * Set/Get the start directory (or output directory). The start directory
  188. * is the directory of the CMakeLists.txt file that started the current
  189. * round of processing. Remember that CMake processes CMakeLists files by
  190. * recursing up the tree starting at the StartDirectory and going up until
  191. * it reaches the HomeDirectory.
  192. */
  193. void SetStartDirectory(const char* dir)
  194. {
  195. m_cmStartDirectory = dir;
  196. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  197. }
  198. const char* GetStartDirectory()
  199. {
  200. return m_cmStartDirectory.c_str();
  201. }
  202. void SetStartOutputDirectory(const char* lib)
  203. {
  204. m_StartOutputDirectory = lib;
  205. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  206. }
  207. const char* GetStartOutputDirectory()
  208. {
  209. return m_StartOutputDirectory.c_str();
  210. }
  211. //@}
  212. //@{
  213. /**
  214. * Set/Get the current directory (or output directory) in the project. The
  215. * current directory is the directory of the CMakeLists.txt file that is
  216. * currently being processed. Remember that CMake processes CMakeLists
  217. * files by recursing up the tree starting at the StartDirectory and going
  218. * up until it reaches the HomeDirectory.
  219. */
  220. void SetCurrentDirectory(const char* dir)
  221. {
  222. m_cmCurrentDirectory = dir;
  223. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  224. }
  225. const char* GetCurrentDirectory()
  226. {
  227. return m_cmCurrentDirectory.c_str();
  228. }
  229. void SetCurrentOutputDirectory(const char* lib)
  230. {
  231. m_CurrentOutputDirectory = lib;
  232. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  233. }
  234. const char* GetCurrentOutputDirectory()
  235. {
  236. return m_CurrentOutputDirectory.c_str();
  237. }
  238. //@}
  239. /**
  240. * Set a regular expression that include files must match
  241. * in order to be considered as part of the depend information.
  242. */
  243. void SetIncludeRegularExpression(const char* regex)
  244. {
  245. m_IncludeFileRegularExpression = regex;
  246. }
  247. /**
  248. * Get the list of targets
  249. */
  250. cmTargets &GetTargets() { return m_Targets; }
  251. /**
  252. * Get a list of the build subdirectories.
  253. */
  254. const std::vector<std::string>& GetSubDirectories()
  255. {
  256. return m_SubDirectories;
  257. }
  258. /**
  259. * Get a list of include directories in the build.
  260. */
  261. std::vector<std::string>& GetIncludeDirectories()
  262. {
  263. return m_IncludeDirectories;
  264. }
  265. /**
  266. * Get a list of link directories in the build.
  267. */
  268. std::vector<std::string>& GetLinkDirectories()
  269. {
  270. return m_LinkDirectories;
  271. }
  272. /**
  273. * Get a list of utilities on which the project depends.
  274. */
  275. std::vector<std::string>& GetUtilities()
  276. {
  277. return m_Utilities;
  278. }
  279. /**
  280. * Get a list of directories that may contain the Utilities.
  281. */
  282. std::vector<std::string>& GetUtilityDirectories()
  283. {
  284. return m_UtilityDirectories;
  285. }
  286. /**
  287. * Get a list of link libraries in the build.
  288. */
  289. std::vector<std::string>& GetLinkLibraries()
  290. {
  291. return m_LinkLibraries;
  292. }
  293. /**
  294. * Get a list of Win32 link libraries in the build.
  295. */
  296. std::vector<std::string>& GetLinkLibrariesWin32()
  297. {
  298. return m_LinkLibrariesWin32;
  299. }
  300. /**
  301. * Get a list of Unix link libraries in the build.
  302. */
  303. std::vector<std::string>& GetLinkLibrariesUnix()
  304. {
  305. return m_LinkLibrariesUnix;
  306. }
  307. /**
  308. * Return a list of source files in this makefile.
  309. */
  310. typedef std::map<std::string,std::vector<cmClassFile> > ClassMap;
  311. ClassMap &GetClasses() {return m_Classes;}
  312. cmClassFile *GetClass(const char *srclist, const char *className);
  313. /**
  314. * Return a list of classes in the passed source lists
  315. */
  316. std::vector<cmClassFile> GetClassesFromSourceLists(
  317. const std::vector<std::string> &srcLists);
  318. /**
  319. * Obtain a list of auxiliary source directories.
  320. */
  321. std::vector<std::string>& GetAuxSourceDirectories()
  322. {return m_AuxSourceDirectories;}
  323. /**
  324. * Do not use this.
  325. */
  326. std::vector<std::string>& GetMakeVerbatim()
  327. {return m_MakeVerbatim;}
  328. /**
  329. * Given a variable name, return its value (as a string).
  330. */
  331. const char* GetDefinition(const char*);
  332. /**
  333. * Get a list of preprocessor define flags.
  334. */
  335. const char* GetDefineFlags()
  336. {return m_DefineFlags.c_str();}
  337. /**
  338. * Get the vector of used command instances.
  339. */
  340. const std::vector<cmCommand*>& GetUsedCommands() const
  341. {return m_UsedCommands;}
  342. /**
  343. * Get the vector source groups.
  344. */
  345. const std::vector<cmSourceGroup>& GetSourceGroups() const
  346. { return m_SourceGroups; }
  347. /**
  348. * Dump documentation to a file. If 0 is returned, the
  349. * operation failed.
  350. */
  351. int DumpDocumentationToFile(const char *fileName);
  352. /**
  353. * Expand all defined varibles in the string.
  354. * Defined varibles come from the m_Definitions map.
  355. * They are expanded with ${var} where var is the
  356. * entry in the m_Definitions map. Also @var@ is
  357. * expanded to match autoconf style expansions.
  358. */
  359. void ExpandVariablesInString(std::string& source) const;
  360. /**
  361. * Expand variables in the makefiles ivars such as link directories etc
  362. */
  363. void ExpandVariables();
  364. /** Recursivly read and create a cmMakefile object for
  365. * all CMakeLists.txt files in the GetSubDirectories list.
  366. * Once the file is found, it ReadListFile is called on
  367. * the cmMakefile created for it.
  368. */
  369. void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  370. /** Generate the cache file only. This is done
  371. * by calling FindSubDirectoryCMakeListsFiles which
  372. * will cause all the rules to fire, and the cache to
  373. * be filled.
  374. */
  375. void GenerateCacheOnly();
  376. /**
  377. * find what source group this source is in
  378. */
  379. cmSourceGroup& FindSourceGroup(const char* source,
  380. std::vector<cmSourceGroup> &groups);
  381. protected:
  382. std::string m_Prefix;
  383. std::vector<std::string> m_AuxSourceDirectories; //
  384. std::string m_cmCurrentDirectory;
  385. std::string m_CurrentOutputDirectory;
  386. std::string m_cmStartDirectory;
  387. std::string m_StartOutputDirectory;
  388. std::string m_cmHomeDirectory;
  389. std::string m_HomeOutputDirectory;
  390. std::string m_ProjectName; // project name
  391. // libraries, classes, and executables
  392. cmTargets m_Targets;
  393. ClassMap m_Classes;
  394. std::vector<std::string> m_SubDirectories; // list of sub directories
  395. std::vector<std::string> m_MakeVerbatim; // lines copied from input file
  396. std::vector<std::string> m_IncludeDirectories;
  397. std::vector<std::string> m_LinkDirectories;
  398. std::vector<std::string> m_Utilities;
  399. std::vector<std::string> m_UtilityDirectories;
  400. std::vector<std::string> m_LinkLibraries;
  401. std::vector<std::string> m_LinkLibrariesWin32;
  402. std::vector<std::string> m_LinkLibrariesUnix;
  403. std::string m_IncludeFileRegularExpression;
  404. std::string m_DefineFlags;
  405. std::vector<cmSourceGroup> m_SourceGroups;
  406. typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
  407. typedef std::map<std::string, std::string> DefinitionMap;
  408. DefinitionMap m_Definitions;
  409. RegisteredCommandsMap m_Commands;
  410. std::vector<cmCommand*> m_UsedCommands;
  411. cmMakefileGenerator* m_MakefileGenerator;
  412. bool IsFunctionBlocked(const char *name, std::vector<std::string> &args) const;
  413. private:
  414. /**
  415. * Get the name of the parent directories CMakeLists file
  416. * given a current CMakeLists file name
  417. */
  418. std::string GetParentListFileName(const char *listFileName);
  419. void ReadClasses(std::ifstream& fin, bool t);
  420. friend class cmMakeDepend; // make depend needs direct access
  421. // to the m_Classes array
  422. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  423. void AddDefaultCommands();
  424. void AddDefaultDefinitions();
  425. std::set<cmFunctionBlocker *> m_FunctionBlockers;
  426. };
  427. #endif