cmMakefile.h 17 KB

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