cmMakefile.h 17 KB

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