cmMakefile.h 17 KB

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