cmMakefile.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmMakefile_h
  14. #define cmMakefile_h
  15. #include "cmStandardIncludes.h"
  16. #include "cmData.h"
  17. #include "cmSourceFile.h"
  18. #include "cmSystemTools.h"
  19. #include "cmSourceGroup.h"
  20. #include "cmTarget.h"
  21. #include "cmCacheManager.h"
  22. class cmFunctionBlocker;
  23. class cmCommand;
  24. class cmMakefileGenerator;
  25. class cmMakeDepend;
  26. /** \class cmMakefile
  27. * \brief Process the input CMakeLists.txt file.
  28. *
  29. * Process and store into memory the input CMakeLists.txt file.
  30. * Each CMakeLists.txt file is parsed and the commands found there
  31. * are added into the build process.
  32. */
  33. class cmMakefile
  34. {
  35. public:
  36. /**
  37. * Return major and minor version numbers for cmake.
  38. */
  39. static unsigned int GetMajorVersion() { return 1;}
  40. static unsigned int GetMinorVersion() { return 5;}
  41. static const char *GetReleaseVersion() { return "development";}
  42. /**
  43. * Return the major and minor version of the cmake that
  44. * was used to write the currently loaded cache, note
  45. * this method will not work before the cache is loaded.
  46. */
  47. static unsigned int GetCacheMajorVersion();
  48. static unsigned int GetCacheMinorVersion();
  49. /**
  50. * Construct an empty makefile.
  51. */
  52. cmMakefile();
  53. /**
  54. * Destructor.
  55. */
  56. ~cmMakefile();
  57. /**
  58. * Read and parse a CMakeLists.txt file.
  59. */
  60. bool ReadListFile(const char* listfile, const char* external= 0);
  61. /**
  62. * Add a wrapper generator.
  63. */
  64. void AddCommand(cmCommand* );
  65. /**
  66. * Add a function blocker to this makefile
  67. */
  68. void AddFunctionBlocker(cmFunctionBlocker *fb)
  69. { m_FunctionBlockers.insert(fb);}
  70. void RemoveFunctionBlocker(cmFunctionBlocker *fb)
  71. { m_FunctionBlockers.erase(fb);}
  72. void RemoveFunctionBlocker(const char *name, const std::vector<std::string> &args);
  73. /**
  74. * Specify the makefile generator. This is platform/compiler
  75. * dependent, although the interface is through a generic
  76. * superclass.
  77. */
  78. void SetMakefileGenerator(cmMakefileGenerator*);
  79. ///! Get the current makefile generator.
  80. cmMakefileGenerator* GetMakefileGenerator()
  81. { return m_MakefileGenerator;}
  82. /**
  83. * Produce the output makefile.
  84. */
  85. void GenerateMakefile();
  86. /**
  87. * run the final pass on all commands.
  88. */
  89. void FinalPass();
  90. /**
  91. * Print the object state to std::cout.
  92. */
  93. void Print() const;
  94. /**
  95. * Add a custom command to the build.
  96. */
  97. void AddCustomCommand(const char* source,
  98. const char* command,
  99. const std::vector<std::string>& commandArgs,
  100. const std::vector<std::string>& depends,
  101. const std::vector<std::string>& outputs,
  102. const char *target);
  103. void AddCustomCommand(const char* source,
  104. const char* command,
  105. const std::vector<std::string>& commandArgs,
  106. const std::vector<std::string>& depends,
  107. const char* output,
  108. const char* target);
  109. /**
  110. * Add a define flag to the build.
  111. */
  112. void AddDefineFlag(const char* definition);
  113. /**
  114. * Add an executable to the build.
  115. */
  116. void AddExecutable(const char *exename,
  117. const std::vector<std::string> &srcs);
  118. void AddExecutable(const char *exename,
  119. const std::vector<std::string> &srcs, bool win32);
  120. /**
  121. * Add a utility to the build. A utiltity target is
  122. * a command that is run every time a target is built.
  123. */
  124. void AddUtilityCommand(const char* utilityName,
  125. const char* command,
  126. const char* arguments,
  127. bool all);
  128. void AddUtilityCommand(const char* utilityName,
  129. const char* command,
  130. const char* arguments,
  131. bool all,
  132. const std::vector<std::string> &depends,
  133. const std::vector<std::string> &outputs);
  134. /**
  135. * Add a link library to the build.
  136. */
  137. void AddLinkLibrary(const char*);
  138. void AddLinkLibrary(const char*, cmTarget::LinkLibraryType type);
  139. void AddLinkLibraryForTarget(const char *tgt, const char*,
  140. cmTarget::LinkLibraryType type);
  141. void AddLinkDirectoryForTarget(const char *tgt, const char* d);
  142. /**
  143. * Add a link directory to the build.
  144. */
  145. void AddLinkDirectory(const char*);
  146. /**
  147. * Add a subdirectory to the build.
  148. */
  149. void AddSubDirectory(const char*);
  150. /**
  151. * Add an include directory to the build.
  152. */
  153. void AddIncludeDirectory(const char*, bool before = false);
  154. /**
  155. * Add a variable definition to the build. This variable
  156. * can be used in CMake to refer to lists, directories, etc.
  157. */
  158. void AddDefinition(const char* name, const char* value);
  159. ///! Add a definition to this makefile and the global cmake cache.
  160. void AddCacheDefinition(const char* name, const char* value,
  161. const char* doc,
  162. cmCacheManager::CacheEntryType type);
  163. /**
  164. * Add bool variable definition to the build.
  165. */
  166. void AddDefinition(const char* name, bool);
  167. ///! Add a definition to this makefile and the global cmake cache.
  168. void AddCacheDefinition(const char* name, bool, const char* doc);
  169. /**
  170. * Specify the name of the project for this build.
  171. */
  172. void SetProjectName(const char*);
  173. /**
  174. * Get the name of the project for this build.
  175. */
  176. const char* GetProjectName()
  177. {
  178. return m_ProjectName.c_str();
  179. }
  180. /**
  181. * Set the name of the library.
  182. */
  183. void AddLibrary(const char *libname, int shared,
  184. const std::vector<std::string> &srcs);
  185. /**
  186. * Add a class/source file to the build.
  187. */
  188. void AddSource(cmSourceFile& ,const char *srcListName);
  189. /**
  190. * Remove a class/source file from the build.
  191. */
  192. void RemoveSource(cmSourceFile& ,const char *srcListName);
  193. /**
  194. * Add a source group for consideration when adding a new source.
  195. */
  196. void AddSourceGroup(const char* name, const char* regex);
  197. /**
  198. * Add an auxiliary directory to the build.
  199. */
  200. void AddExtraDirectory(const char* dir);
  201. /**
  202. * Add an auxiliary directory to the build.
  203. */
  204. void MakeStartDirectoriesCurrent()
  205. {
  206. m_cmCurrentDirectory = m_cmStartDirectory;
  207. m_CurrentOutputDirectory = m_StartOutputDirectory;
  208. }
  209. //@{
  210. /**
  211. * Set/Get the home directory (or output directory) in the project. The
  212. * home directory is the top directory of the project. It is where
  213. * CMakeSetup or configure was run. Remember that CMake processes
  214. * CMakeLists files by recursing up the tree starting at the StartDirectory
  215. * and going up until it reaches the HomeDirectory.
  216. */
  217. void SetHomeDirectory(const char* dir);
  218. const char* GetHomeDirectory() const
  219. {
  220. return m_cmHomeDirectory.c_str();
  221. }
  222. void SetHomeOutputDirectory(const char* lib);
  223. const char* GetHomeOutputDirectory() const
  224. {
  225. return m_HomeOutputDirectory.c_str();
  226. }
  227. //@}
  228. //@{
  229. /**
  230. * Set/Get the start directory (or output directory). The start directory
  231. * is the directory of the CMakeLists.txt file that started the current
  232. * round of processing. Remember that CMake processes CMakeLists files by
  233. * recursing up the tree starting at the StartDirectory and going up until
  234. * it reaches the HomeDirectory.
  235. */
  236. void SetStartDirectory(const char* dir)
  237. {
  238. m_cmStartDirectory = dir;
  239. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  240. }
  241. const char* GetStartDirectory() const
  242. {
  243. return m_cmStartDirectory.c_str();
  244. }
  245. void SetStartOutputDirectory(const char* lib)
  246. {
  247. m_StartOutputDirectory = lib;
  248. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  249. cmSystemTools::MakeDirectory(m_StartOutputDirectory.c_str());
  250. }
  251. const char* GetStartOutputDirectory() const
  252. {
  253. return m_StartOutputDirectory.c_str();
  254. }
  255. //@}
  256. //@{
  257. /**
  258. * Set/Get the current directory (or output directory) in the project. The
  259. * current directory is the directory of the CMakeLists.txt file that is
  260. * currently being processed. Remember that CMake processes CMakeLists
  261. * files by recursing up the tree starting at the StartDirectory and going
  262. * up until it reaches the HomeDirectory.
  263. */
  264. void SetCurrentDirectory(const char* dir)
  265. {
  266. m_cmCurrentDirectory = dir;
  267. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  268. }
  269. const char* GetCurrentDirectory() const
  270. {
  271. return m_cmCurrentDirectory.c_str();
  272. }
  273. void SetCurrentOutputDirectory(const char* lib)
  274. {
  275. m_CurrentOutputDirectory = lib;
  276. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  277. }
  278. const char* GetCurrentOutputDirectory() const
  279. {
  280. return m_CurrentOutputDirectory.c_str();
  281. }
  282. /* Get the current CMakeLists.txt file that is being processed. This
  283. * is just used in order to be able to 'branch' from one file to a second
  284. * transparently */
  285. const char* GetCurrentListFile() const
  286. {
  287. return m_cmCurrentListFile.c_str();
  288. }
  289. //@}
  290. /**
  291. * Set a regular expression that include files must match
  292. * in order to be considered as part of the depend information.
  293. */
  294. void SetIncludeRegularExpression(const char* regex)
  295. {
  296. m_IncludeFileRegularExpression = regex;
  297. }
  298. /**
  299. * Set a regular expression that include files that are not found
  300. * must match in order to be considered a problem.
  301. */
  302. void SetComplainRegularExpression(const char* regex)
  303. {
  304. m_ComplainFileRegularExpression = regex;
  305. }
  306. /**
  307. * Get the list of targets
  308. */
  309. cmTargets &GetTargets() { return m_Targets; }
  310. const cmTargets &GetTargets() const { return m_Targets; }
  311. /**
  312. * Get a list of the build subdirectories.
  313. */
  314. const std::vector<std::string>& GetSubDirectories()
  315. {
  316. return m_SubDirectories;
  317. }
  318. /**
  319. * Get a list of include directories in the build.
  320. */
  321. std::vector<std::string>& GetIncludeDirectories()
  322. {
  323. return m_IncludeDirectories;
  324. }
  325. const std::vector<std::string>& GetIncludeDirectories() const
  326. {
  327. return m_IncludeDirectories;
  328. }
  329. /**
  330. * Return a list of source files in this makefile.
  331. */
  332. typedef std::map<cmStdString,std::vector<cmSourceFile*> > SourceMap;
  333. const SourceMap &GetSources() const {return m_Sources;}
  334. SourceMap &GetSources() {return m_Sources;}
  335. cmSourceFile* GetSource(const char *srclist, const char *sourceName);
  336. /** Get a cmSourceFile pointer for a given source name, if the name is
  337. * not found, then a null pointer is returned.
  338. */
  339. cmSourceFile* GetSource(const char* sourceName);
  340. ///! Add a new cmSourceFile to the list of sources for this makefile.
  341. cmSourceFile* AddSource(cmSourceFile const&);
  342. /**
  343. * Obtain a list of auxiliary source directories.
  344. */
  345. std::vector<std::string>& GetAuxSourceDirectories()
  346. {return m_AuxSourceDirectories;}
  347. //@{
  348. /**
  349. * Return a list of extensions associated with source and header
  350. * files
  351. */
  352. const std::vector<std::string>& GetSourceExtensions() const
  353. {return m_SourceFileExtensions;}
  354. const std::vector<std::string>& GetHeaderExtensions() const
  355. {return m_HeaderFileExtensions;}
  356. //@}
  357. /**
  358. * Given a variable name, return its value (as a string).
  359. * If the variable is not found in this makefile instance, the
  360. * cache is then queried.
  361. */
  362. const char* GetDefinition(const char*) const;
  363. /** Test a boolean cache entry to see if it is true or false,
  364. * returns false if no entry defined.
  365. */
  366. bool IsOn(const char* name) const;
  367. /**
  368. * Get a list of preprocessor define flags.
  369. */
  370. const char* GetDefineFlags()
  371. {return m_DefineFlags.c_str();}
  372. /**
  373. * Get the vector of used command instances.
  374. */
  375. const std::vector<cmCommand*>& GetUsedCommands() const
  376. {return m_UsedCommands;}
  377. /**
  378. * Get the vector source groups.
  379. */
  380. const std::vector<cmSourceGroup>& GetSourceGroups() const
  381. { return m_SourceGroups; }
  382. /**
  383. * Get the vector of list files on which this makefile depends
  384. */
  385. const std::vector<std::string>& GetListFiles() const
  386. { return m_ListFiles; }
  387. ///! When the file changes cmake will be re-run from the build system.
  388. void AddCMakeDependFile(const char* file)
  389. { m_ListFiles.push_back(file);}
  390. /**
  391. * Dump documentation to a file. If 0 is returned, the
  392. * operation failed.
  393. */
  394. int DumpDocumentationToFile(std::ostream&);
  395. /**
  396. * Expand all defined varibles in the string.
  397. * Defined varibles come from the m_Definitions map.
  398. * They are expanded with ${var} where var is the
  399. * entry in the m_Definitions map. Also @var@ is
  400. * expanded to match autoconf style expansions.
  401. */
  402. const char *ExpandVariablesInString(std::string& source) const;
  403. const char *ExpandVariablesInString(std::string& source, bool escapeQuotes,
  404. bool atOnly = false) const;
  405. /**
  406. * Remove any remaining variables in the string. Anything with ${var} or
  407. * @var@ will be removed.
  408. */
  409. void RemoveVariablesInString(std::string& source, bool atOnly = false) const;
  410. /**
  411. * Expand variables in the makefiles ivars such as link directories etc
  412. */
  413. void ExpandVariables();
  414. /** Recursivly read and create a cmMakefile object for
  415. * all CMakeLists.txt files in the GetSubDirectories list.
  416. * Once the file is found, it ReadListFile is called on
  417. * the cmMakefile created for it. CreateObject is called on
  418. * the prototype to create a cmMakefileGenerator for each cmMakefile that
  419. * is created.
  420. */
  421. void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  422. /**
  423. * find what source group this source is in
  424. */
  425. cmSourceGroup& FindSourceGroup(const char* source,
  426. std::vector<cmSourceGroup> &groups);
  427. void RegisterData(cmData*);
  428. void RegisterData(const char*, cmData*);
  429. cmData* LookupData(const char*) const;
  430. /**
  431. * execute a single CMake command
  432. */
  433. void ExecuteCommand(std::string &name, std::vector<std::string> const& args);
  434. /** Check if a command exists. */
  435. bool CommandExists(const char* name) const;
  436. ///! Enable support for the named language, if null then all languages are enabled.
  437. void EnableLanguage(const char* );
  438. protected:
  439. // add link libraries and directories to the target
  440. void AddGlobalLinkInformation(const char* name, cmTarget& target);
  441. std::string m_Prefix;
  442. std::vector<std::string> m_AuxSourceDirectories; //
  443. std::string m_cmCurrentDirectory;
  444. std::string m_CurrentOutputDirectory;
  445. std::string m_cmStartDirectory;
  446. std::string m_StartOutputDirectory;
  447. std::string m_cmHomeDirectory;
  448. std::string m_HomeOutputDirectory;
  449. std::string m_cmCurrentListFile;
  450. std::string m_ProjectName; // project name
  451. // libraries, classes, and executables
  452. cmTargets m_Targets;
  453. SourceMap m_Sources;
  454. std::vector<cmSourceFile*> m_SourceFiles;
  455. std::vector<std::string> m_SubDirectories; // list of sub directories
  456. struct StringSet : public std::set<cmStdString>
  457. {
  458. };
  459. // The include and link-library paths. These may have order
  460. // dependency, so they must be vectors (not set).
  461. std::vector<std::string> m_IncludeDirectories;
  462. std::vector<std::string> m_LinkDirectories;
  463. std::vector<std::string> m_ListFiles; // list of command files loaded
  464. cmTarget::LinkLibraries m_LinkLibraries;
  465. std::string m_IncludeFileRegularExpression;
  466. std::string m_ComplainFileRegularExpression;
  467. std::vector<std::string> m_SourceFileExtensions;
  468. std::vector<std::string> m_HeaderFileExtensions;
  469. std::string m_DefineFlags;
  470. std::vector<cmSourceGroup> m_SourceGroups;
  471. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  472. typedef std::map<cmStdString, cmStdString> DefinitionMap;
  473. DefinitionMap m_Definitions;
  474. RegisteredCommandsMap m_Commands;
  475. std::vector<cmCommand*> m_UsedCommands;
  476. cmMakefileGenerator* m_MakefileGenerator;
  477. bool IsFunctionBlocked(const char *name, std::vector<std::string> const& args);
  478. private:
  479. /**
  480. * Get the name of the parent directories CMakeLists file
  481. * given a current CMakeLists file name
  482. */
  483. std::string GetParentListFileName(const char *listFileName);
  484. void ReadSources(std::ifstream& fin, bool t);
  485. friend class cmMakeDepend; // make depend needs direct access
  486. // to the m_Sources array
  487. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  488. void AddDefaultCommands();
  489. void AddDefaultDefinitions();
  490. std::set<cmFunctionBlocker *> m_FunctionBlockers;
  491. typedef std::map<cmStdString, cmData*> DataMap;
  492. DataMap m_DataMap;
  493. bool m_Inheriting;
  494. };
  495. #endif