cmMakefile.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 3;}
  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. * Get a list of link libraries in the build.
  136. */
  137. cmTarget::LinkLibraries& GetLinkLibraries()
  138. {
  139. return m_LinkLibraries;
  140. }
  141. /**
  142. * Get a list of link libraries in the build.
  143. */
  144. const cmTarget::LinkLibraries& GetLinkLibraries() const
  145. {
  146. return m_LinkLibraries;
  147. }
  148. /**
  149. * Add a link library to the build.
  150. */
  151. void AddLinkLibrary(const char*);
  152. void AddLinkLibrary(const char*, cmTarget::LinkLibraryType type);
  153. void AddLinkLibraryForTarget(const char *tgt, const char*,
  154. cmTarget::LinkLibraryType type);
  155. /**
  156. * Add a link directory to the build.
  157. */
  158. void AddLinkDirectory(const char*);
  159. /**
  160. * Add a subdirectory to the build.
  161. */
  162. void AddSubDirectory(const char*);
  163. /**
  164. * Add an include directory to the build.
  165. */
  166. void AddIncludeDirectory(const char*, bool before = false);
  167. /**
  168. * Add a variable definition to the build. This variable
  169. * can be used in CMake to refer to lists, directories, etc.
  170. */
  171. void AddDefinition(const char* name, const char* value);
  172. ///! Add a definition to this makefile and the global cmake cache.
  173. void AddCacheDefinition(const char* name, const char* value,
  174. const char* doc,
  175. cmCacheManager::CacheEntryType type);
  176. /**
  177. * Add bool variable definition to the build.
  178. */
  179. void AddDefinition(const char* name, bool);
  180. ///! Add a definition to this makefile and the global cmake cache.
  181. void AddCacheDefinition(const char* name, bool, const char* doc);
  182. /**
  183. * Specify the name of the project for this build.
  184. */
  185. void SetProjectName(const char*);
  186. /**
  187. * Get the name of the project for this build.
  188. */
  189. const char* GetProjectName()
  190. {
  191. return m_ProjectName.c_str();
  192. }
  193. /**
  194. * Set the name of the library.
  195. */
  196. void AddLibrary(const char *libname, int shared,
  197. const std::vector<std::string> &srcs);
  198. /**
  199. * Add a class/source file to the build.
  200. */
  201. void AddSource(cmSourceFile& ,const char *srcListName);
  202. /**
  203. * Remove a class/source file from the build.
  204. */
  205. void RemoveSource(cmSourceFile& ,const char *srcListName);
  206. /**
  207. * Add a source group for consideration when adding a new source.
  208. */
  209. void AddSourceGroup(const char* name, const char* regex);
  210. /**
  211. * Add an auxiliary directory to the build.
  212. */
  213. void AddExtraDirectory(const char* dir);
  214. /**
  215. * Add an auxiliary directory to the build.
  216. */
  217. void MakeStartDirectoriesCurrent()
  218. {
  219. m_cmCurrentDirectory = m_cmStartDirectory;
  220. m_CurrentOutputDirectory = m_StartOutputDirectory;
  221. }
  222. //@{
  223. /**
  224. * Set/Get the home directory (or output directory) in the project. The
  225. * home directory is the top directory of the project. It is where
  226. * CMakeSetup or configure was run. Remember that CMake processes
  227. * CMakeLists files by recursing up the tree starting at the StartDirectory
  228. * and going up until it reaches the HomeDirectory.
  229. */
  230. void SetHomeDirectory(const char* dir);
  231. const char* GetHomeDirectory() const
  232. {
  233. return m_cmHomeDirectory.c_str();
  234. }
  235. void SetHomeOutputDirectory(const char* lib);
  236. const char* GetHomeOutputDirectory() const
  237. {
  238. return m_HomeOutputDirectory.c_str();
  239. }
  240. //@}
  241. //@{
  242. /**
  243. * Set/Get the start directory (or output directory). The start directory
  244. * is the directory of the CMakeLists.txt file that started the current
  245. * round of processing. Remember that CMake processes CMakeLists files by
  246. * recursing up the tree starting at the StartDirectory and going up until
  247. * it reaches the HomeDirectory.
  248. */
  249. void SetStartDirectory(const char* dir)
  250. {
  251. m_cmStartDirectory = dir;
  252. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  253. }
  254. const char* GetStartDirectory() const
  255. {
  256. return m_cmStartDirectory.c_str();
  257. }
  258. void SetStartOutputDirectory(const char* lib)
  259. {
  260. m_StartOutputDirectory = lib;
  261. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  262. cmSystemTools::MakeDirectory(m_StartOutputDirectory.c_str());
  263. }
  264. const char* GetStartOutputDirectory() const
  265. {
  266. return m_StartOutputDirectory.c_str();
  267. }
  268. //@}
  269. //@{
  270. /**
  271. * Set/Get the current directory (or output directory) in the project. The
  272. * current directory is the directory of the CMakeLists.txt file that is
  273. * currently being processed. Remember that CMake processes CMakeLists
  274. * files by recursing up the tree starting at the StartDirectory and going
  275. * up until it reaches the HomeDirectory.
  276. */
  277. void SetCurrentDirectory(const char* dir)
  278. {
  279. m_cmCurrentDirectory = dir;
  280. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  281. }
  282. const char* GetCurrentDirectory() const
  283. {
  284. return m_cmCurrentDirectory.c_str();
  285. }
  286. void SetCurrentOutputDirectory(const char* lib)
  287. {
  288. m_CurrentOutputDirectory = lib;
  289. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  290. }
  291. const char* GetCurrentOutputDirectory() const
  292. {
  293. return m_CurrentOutputDirectory.c_str();
  294. }
  295. /* Get the current CMakeLists.txt file that is being processed. This
  296. * is just used in order to be able to 'branch' from one file to a second
  297. * transparently */
  298. const char* GetCurrentListFile() const
  299. {
  300. return m_cmCurrentListFile.c_str();
  301. }
  302. //@}
  303. /**
  304. * Set a regular expression that include files must match
  305. * in order to be considered as part of the depend information.
  306. */
  307. void SetIncludeRegularExpression(const char* regex)
  308. {
  309. m_IncludeFileRegularExpression = regex;
  310. }
  311. /**
  312. * Set a regular expression that include files that are not found
  313. * must match in order to be considered a problem.
  314. */
  315. void SetComplainRegularExpression(const char* regex)
  316. {
  317. m_ComplainFileRegularExpression = regex;
  318. }
  319. /**
  320. * Get the list of targets
  321. */
  322. cmTargets &GetTargets() { return m_Targets; }
  323. const cmTargets &GetTargets() const { return m_Targets; }
  324. /**
  325. * Get a list of the build subdirectories.
  326. */
  327. const std::vector<std::string>& GetSubDirectories()
  328. {
  329. return m_SubDirectories;
  330. }
  331. /**
  332. * Get a list of include directories in the build.
  333. */
  334. std::vector<std::string>& GetIncludeDirectories()
  335. {
  336. return m_IncludeDirectories;
  337. }
  338. const std::vector<std::string>& GetIncludeDirectories() const
  339. {
  340. return m_IncludeDirectories;
  341. }
  342. /**
  343. * Get a list of link directories in the build.
  344. */
  345. std::vector<std::string>& GetLinkDirectories()
  346. {
  347. return m_LinkDirectories;
  348. }
  349. /**
  350. * Return a list of source files in this makefile.
  351. */
  352. typedef std::map<cmStdString,std::vector<cmSourceFile> > SourceMap;
  353. const SourceMap &GetSources() const {return m_Sources;}
  354. SourceMap &GetSources() {return m_Sources;}
  355. cmSourceFile *GetSource(const char *srclist, const char *sourceName);
  356. /**
  357. * Obtain a list of auxiliary source directories.
  358. */
  359. std::vector<std::string>& GetAuxSourceDirectories()
  360. {return m_AuxSourceDirectories;}
  361. //@{
  362. /**
  363. * Return a list of extensions associated with source and header
  364. * files
  365. */
  366. const std::vector<std::string>& GetSourceExtensions() const
  367. {return m_SourceFileExtensions;}
  368. const std::vector<std::string>& GetHeaderExtensions() const
  369. {return m_HeaderFileExtensions;}
  370. //@}
  371. /**
  372. * Given a variable name, return its value (as a string).
  373. * If the variable is not found in this makefile instance, the
  374. * cache is then queried.
  375. */
  376. const char* GetDefinition(const char*) const;
  377. /** Test a boolean cache entry to see if it is true or false,
  378. * returns false if no entry defined.
  379. */
  380. bool IsOn(const char* name);
  381. /**
  382. * Get a list of preprocessor define flags.
  383. */
  384. const char* GetDefineFlags()
  385. {return m_DefineFlags.c_str();}
  386. /**
  387. * Get the vector of used command instances.
  388. */
  389. const std::vector<cmCommand*>& GetUsedCommands() const
  390. {return m_UsedCommands;}
  391. /**
  392. * Get the vector source groups.
  393. */
  394. const std::vector<cmSourceGroup>& GetSourceGroups() const
  395. { return m_SourceGroups; }
  396. /**
  397. * Get the vector of list files on which this makefile depends
  398. */
  399. const std::vector<std::string>& GetListFiles() const
  400. { return m_ListFiles; }
  401. ///! When the file changes cmake will be re-run from the build system.
  402. void AddCMakeDependFile(const char* file)
  403. { m_ListFiles.push_back(file);}
  404. /**
  405. * Dump documentation to a file. If 0 is returned, the
  406. * operation failed.
  407. */
  408. int DumpDocumentationToFile(std::ostream&);
  409. /**
  410. * Expand all defined varibles in the string.
  411. * Defined varibles come from the m_Definitions map.
  412. * They are expanded with ${var} where var is the
  413. * entry in the m_Definitions map. Also @var@ is
  414. * expanded to match autoconf style expansions.
  415. */
  416. const char *ExpandVariablesInString(std::string& source) const;
  417. const char *ExpandVariablesInString(std::string& source, bool escapeQuotes,
  418. bool atOnly = false) const;
  419. /**
  420. * Remove any remaining variables in the string. Anything with ${var} or
  421. * @var@ will be removed.
  422. */
  423. void RemoveVariablesInString(std::string& source, bool atOnly = false) const;
  424. /**
  425. * Expand variables in the makefiles ivars such as link directories etc
  426. */
  427. void ExpandVariables();
  428. /** Recursivly read and create a cmMakefile object for
  429. * all CMakeLists.txt files in the GetSubDirectories list.
  430. * Once the file is found, it ReadListFile is called on
  431. * the cmMakefile created for it.
  432. */
  433. void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  434. /**
  435. * find what source group this source is in
  436. */
  437. cmSourceGroup& FindSourceGroup(const char* source,
  438. std::vector<cmSourceGroup> &groups);
  439. void RegisterData(cmData*);
  440. void RegisterData(const char*, cmData*);
  441. cmData* LookupData(const char*) const;
  442. /**
  443. * execute a single CMake command
  444. */
  445. void ExecuteCommand(std::string &name, std::vector<std::string> const& args);
  446. /** Check if a command exists. */
  447. bool CommandExists(const char* name) const;
  448. protected:
  449. std::string m_Prefix;
  450. std::vector<std::string> m_AuxSourceDirectories; //
  451. std::string m_cmCurrentDirectory;
  452. std::string m_CurrentOutputDirectory;
  453. std::string m_cmStartDirectory;
  454. std::string m_StartOutputDirectory;
  455. std::string m_cmHomeDirectory;
  456. std::string m_HomeOutputDirectory;
  457. std::string m_cmCurrentListFile;
  458. std::string m_ProjectName; // project name
  459. // libraries, classes, and executables
  460. cmTargets m_Targets;
  461. SourceMap m_Sources;
  462. std::vector<std::string> m_SubDirectories; // list of sub directories
  463. struct StringSet : public std::set<cmStdString>
  464. {
  465. };
  466. // The include and link-library paths. These may have order
  467. // dependency, so they must be vectors (not set).
  468. std::vector<std::string> m_IncludeDirectories;
  469. std::vector<std::string> m_LinkDirectories;
  470. std::vector<std::string> m_ListFiles; // list of command files loaded
  471. cmTarget::LinkLibraries m_LinkLibraries;
  472. std::string m_IncludeFileRegularExpression;
  473. std::string m_ComplainFileRegularExpression;
  474. std::vector<std::string> m_SourceFileExtensions;
  475. std::vector<std::string> m_HeaderFileExtensions;
  476. std::string m_DefineFlags;
  477. std::vector<cmSourceGroup> m_SourceGroups;
  478. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  479. typedef std::map<cmStdString, cmStdString> DefinitionMap;
  480. DefinitionMap m_Definitions;
  481. RegisteredCommandsMap m_Commands;
  482. std::vector<cmCommand*> m_UsedCommands;
  483. cmMakefileGenerator* m_MakefileGenerator;
  484. bool IsFunctionBlocked(const char *name, std::vector<std::string> const& args);
  485. private:
  486. /**
  487. * Get the name of the parent directories CMakeLists file
  488. * given a current CMakeLists file name
  489. */
  490. std::string GetParentListFileName(const char *listFileName);
  491. void ReadSources(std::ifstream& fin, bool t);
  492. friend class cmMakeDepend; // make depend needs direct access
  493. // to the m_Sources array
  494. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  495. void AddDefaultCommands();
  496. void AddDefaultDefinitions();
  497. std::set<cmFunctionBlocker *> m_FunctionBlockers;
  498. typedef std::map<cmStdString, cmData*> DataMap;
  499. DataMap m_DataMap;
  500. bool m_Inheriting;
  501. };
  502. #endif