cmMakefile.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html 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 "cmSystemTools.h"
  18. #include "cmSourceGroup.h"
  19. #include "cmTarget.h"
  20. #include "cmCacheManager.h"
  21. class cmFunctionBlocker;
  22. class cmCommand;
  23. class cmLocalGenerator;
  24. class cmMakeDepend;
  25. class cmSourceFile;
  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. const char *comment = 0);
  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. * Find a library (as in cmSystemTools) but add in compiler specific paths
  162. */
  163. std::string FindLibrary(const char* name,
  164. const std::vector<std::string>& path);
  165. /**
  166. * Add a variable definition to the build. This variable
  167. * can be used in CMake to refer to lists, directories, etc.
  168. */
  169. void AddDefinition(const char* name, const char* value);
  170. ///! Add a definition to this makefile and the global cmake cache.
  171. void AddCacheDefinition(const char* name, const char* value,
  172. const char* doc,
  173. cmCacheManager::CacheEntryType type);
  174. /**
  175. * Add bool variable definition to the build.
  176. */
  177. void AddDefinition(const char* name, bool);
  178. ///! Add a definition to this makefile and the global cmake cache.
  179. void AddCacheDefinition(const char* name, bool, const char* doc);
  180. /**
  181. * Remove a variable definition from the build. This is not valid
  182. * for cache entries, and will only affect the current makefile.
  183. */
  184. void RemoveDefinition(const char* name);
  185. /**
  186. * Specify the name of the project for this build.
  187. */
  188. void SetProjectName(const char*);
  189. /**
  190. * Get the name of the project for this build.
  191. */
  192. const char* GetProjectName()
  193. {
  194. return m_ProjectName.c_str();
  195. }
  196. /**
  197. * Set the name of the library.
  198. */
  199. void AddLibrary(const char *libname, int shared,
  200. const std::vector<std::string> &srcs);
  201. /**
  202. * Add a class/source file to the build.
  203. */
  204. //void AddSource(cmSourceFile& ,const char *srcListName);
  205. /**
  206. * Remove a class/source file from the build.
  207. */
  208. //void RemoveSource(cmSourceFile& ,const char *srcListName);
  209. /**
  210. * Add a source group for consideration when adding a new source.
  211. */
  212. void AddSourceGroup(const char* name, const char* regex=0);
  213. /**
  214. * Add an auxiliary directory to the build.
  215. */
  216. void AddExtraDirectory(const char* dir);
  217. /**
  218. * Add an auxiliary directory to the build.
  219. */
  220. void MakeStartDirectoriesCurrent()
  221. {
  222. m_cmCurrentDirectory = m_cmStartDirectory;
  223. m_CurrentOutputDirectory = m_StartOutputDirectory;
  224. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", m_cmCurrentDirectory.c_str());
  225. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR", m_CurrentOutputDirectory.c_str());
  226. }
  227. //@{
  228. /**
  229. * Set/Get the home directory (or output directory) in the project. The
  230. * home directory is the top directory of the project. It is where
  231. * CMakeSetup or configure was run. Remember that CMake processes
  232. * CMakeLists files by recursing up the tree starting at the StartDirectory
  233. * and going up until it reaches the HomeDirectory.
  234. */
  235. void SetHomeDirectory(const char* dir);
  236. const char* GetHomeDirectory() const
  237. {
  238. return m_cmHomeDirectory.c_str();
  239. }
  240. void SetHomeOutputDirectory(const char* lib);
  241. const char* GetHomeOutputDirectory() const
  242. {
  243. return m_HomeOutputDirectory.c_str();
  244. }
  245. //@}
  246. //@{
  247. /**
  248. * Set/Get the start directory (or output directory). The start directory
  249. * is the directory of the CMakeLists.txt file that started the current
  250. * round of processing. Remember that CMake processes CMakeLists files by
  251. * recursing up the tree starting at the StartDirectory and going up until
  252. * it reaches the HomeDirectory.
  253. */
  254. void SetStartDirectory(const char* dir)
  255. {
  256. m_cmStartDirectory = dir;
  257. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  258. }
  259. const char* GetStartDirectory() const
  260. {
  261. return m_cmStartDirectory.c_str();
  262. }
  263. void SetStartOutputDirectory(const char* lib)
  264. {
  265. m_StartOutputDirectory = lib;
  266. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  267. cmSystemTools::MakeDirectory(m_StartOutputDirectory.c_str());
  268. }
  269. const char* GetStartOutputDirectory() const
  270. {
  271. return m_StartOutputDirectory.c_str();
  272. }
  273. //@}
  274. //@{
  275. /**
  276. * Set/Get the current directory (or output directory) in the project. The
  277. * current directory is the directory of the CMakeLists.txt file that is
  278. * currently being processed. Remember that CMake processes CMakeLists
  279. * files by recursing up the tree starting at the StartDirectory and going
  280. * up until it reaches the HomeDirectory.
  281. */
  282. void SetCurrentDirectory(const char* dir)
  283. {
  284. m_cmCurrentDirectory = dir;
  285. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  286. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", m_cmCurrentDirectory.c_str());
  287. }
  288. const char* GetCurrentDirectory() const
  289. {
  290. return m_cmCurrentDirectory.c_str();
  291. }
  292. void SetCurrentOutputDirectory(const char* lib)
  293. {
  294. m_CurrentOutputDirectory = lib;
  295. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  296. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR", m_CurrentOutputDirectory.c_str());
  297. }
  298. const char* GetCurrentOutputDirectory() const
  299. {
  300. return m_CurrentOutputDirectory.c_str();
  301. }
  302. /* Get the current CMakeLists.txt file that is being processed. This
  303. * is just used in order to be able to 'branch' from one file to a second
  304. * transparently */
  305. const char* GetCurrentListFile() const
  306. {
  307. return m_cmCurrentListFile.c_str();
  308. }
  309. //@}
  310. /**
  311. * Set a regular expression that include files must match
  312. * in order to be considered as part of the depend information.
  313. */
  314. void SetIncludeRegularExpression(const char* regex)
  315. {
  316. m_IncludeFileRegularExpression = regex;
  317. }
  318. /**
  319. * Set a regular expression that include files that are not found
  320. * must match in order to be considered a problem.
  321. */
  322. void SetComplainRegularExpression(const char* regex)
  323. {
  324. m_ComplainFileRegularExpression = regex;
  325. }
  326. /**
  327. * Get the list of targets
  328. */
  329. cmTargets &GetTargets() { return m_Targets; }
  330. const cmTargets &GetTargets() const { return m_Targets; }
  331. /**
  332. * Get a list of the build subdirectories.
  333. */
  334. const std::vector<std::string>& GetSubDirectories()
  335. {
  336. return m_SubDirectories;
  337. }
  338. /**
  339. * Get a list of include directories in the build.
  340. */
  341. std::vector<std::string>& GetIncludeDirectories()
  342. {
  343. return m_IncludeDirectories;
  344. }
  345. const std::vector<std::string>& GetIncludeDirectories() const
  346. {
  347. return m_IncludeDirectories;
  348. }
  349. /** Expand out any arguements in the vector that have ; separated
  350. * strings into multiple arguements. A new vector is created
  351. * containing the expanded versions of all arguments in argsIn.
  352. * This method differes from the one in cmSystemTools in that if
  353. * the CmakeLists file is version 1.2 or earlier it will check for
  354. * source lists being used without ${} around them
  355. */
  356. void ExpandSourceListArguments(std::vector<std::string> const& argsIn,
  357. std::vector<std::string>& argsOut,
  358. unsigned int startArgumentIndex);
  359. /** Get a cmSourceFile pointer for a given source name, if the name is
  360. * not found, then a null pointer is returned.
  361. */
  362. cmSourceFile* GetSource(const char* sourceName) const;
  363. ///! Add a new cmSourceFile to the list of sources for this makefile.
  364. cmSourceFile* AddSource(cmSourceFile const&);
  365. /**
  366. * Obtain a list of auxiliary source directories.
  367. */
  368. std::vector<std::string>& GetAuxSourceDirectories()
  369. {return m_AuxSourceDirectories;}
  370. //@{
  371. /**
  372. * Return a list of extensions associated with source and header
  373. * files
  374. */
  375. const std::vector<std::string>& GetSourceExtensions() const
  376. {return m_SourceFileExtensions;}
  377. const std::vector<std::string>& GetHeaderExtensions() const
  378. {return m_HeaderFileExtensions;}
  379. //@}
  380. /**
  381. * Given a variable name, return its value (as a string).
  382. * If the variable is not found in this makefile instance, the
  383. * cache is then queried.
  384. */
  385. const char* GetDefinition(const char*) const;
  386. /** Test a boolean cache entry to see if it is true or false,
  387. * returns false if no entry defined.
  388. */
  389. bool IsOn(const char* name) const;
  390. /**
  391. * Get a list of preprocessor define flags.
  392. */
  393. const char* GetDefineFlags()
  394. {return m_DefineFlags.c_str();}
  395. /**
  396. * Get the vector of used command instances.
  397. */
  398. const std::vector<cmCommand*>& GetUsedCommands() const
  399. {return m_UsedCommands;}
  400. /**
  401. * Get the vector source groups.
  402. */
  403. const std::vector<cmSourceGroup>& GetSourceGroups() const
  404. { return m_SourceGroups; }
  405. /**
  406. * Get the source group
  407. */
  408. cmSourceGroup* GetSourceGroup(const char* name);
  409. /**
  410. * Get the vector of list files on which this makefile depends
  411. */
  412. const std::vector<std::string>& GetListFiles() const
  413. { return m_ListFiles; }
  414. ///! When the file changes cmake will be re-run from the build system.
  415. void AddCMakeDependFile(const char* file)
  416. { m_ListFiles.push_back(file);}
  417. /**
  418. * Expand all defined varibles in the string.
  419. * Defined varibles come from the m_Definitions map.
  420. * They are expanded with ${var} where var is the
  421. * entry in the m_Definitions map. Also @var@ is
  422. * expanded to match autoconf style expansions.
  423. */
  424. const char *ExpandVariablesInString(std::string& source) const;
  425. const char *ExpandVariablesInString(std::string& source, bool escapeQuotes,
  426. bool atOnly = false) const;
  427. /**
  428. * Remove any remaining variables in the string. Anything with ${var} or
  429. * @var@ will be removed.
  430. */
  431. void RemoveVariablesInString(std::string& source, bool atOnly = false) const;
  432. /**
  433. * Expand variables in the makefiles ivars such as link directories etc
  434. */
  435. void ExpandVariables();
  436. /**
  437. * find what source group this source is in
  438. */
  439. cmSourceGroup& FindSourceGroup(const char* source,
  440. std::vector<cmSourceGroup> &groups);
  441. void RegisterData(cmData*);
  442. void RegisterData(const char*, cmData*);
  443. cmData* LookupData(const char*) const;
  444. /**
  445. * execute a single CMake command
  446. */
  447. void ExecuteCommand(std::string const &name, std::vector<std::string> const& args);
  448. /** Check if a command exists. */
  449. bool CommandExists(const char* name) const;
  450. /**
  451. * Add a command to this cmake instance
  452. */
  453. void AddCommand(cmCommand* );
  454. ///! Enable support for the named language, if null then all languages are enabled.
  455. void EnableLanguage(const char* );
  456. /**
  457. * Set/Get the name of the parent directories CMakeLists file
  458. * given a current CMakeLists file name
  459. */
  460. cmCacheManager *GetCacheManager() const;
  461. //! Determine wether this is a local or global build.
  462. bool GetLocal() const;
  463. ///! Display progress or status message.
  464. void DisplayStatus(const char*, float);
  465. protected:
  466. // add link libraries and directories to the target
  467. void AddGlobalLinkInformation(const char* name, cmTarget& target);
  468. std::string m_Prefix;
  469. std::vector<std::string> m_AuxSourceDirectories; //
  470. std::string m_cmCurrentDirectory;
  471. std::string m_CurrentOutputDirectory;
  472. std::string m_cmStartDirectory;
  473. std::string m_StartOutputDirectory;
  474. std::string m_cmHomeDirectory;
  475. std::string m_HomeOutputDirectory;
  476. std::string m_cmCurrentListFile;
  477. std::string m_ProjectName; // project name
  478. // libraries, classes, and executables
  479. cmTargets m_Targets;
  480. std::vector<cmSourceFile*> m_SourceFiles;
  481. std::vector<std::string> m_SubDirectories; // list of sub directories
  482. struct StringSet : public std::set<cmStdString>
  483. {
  484. };
  485. // The include and link-library paths. These may have order
  486. // dependency, so they must be vectors (not set).
  487. std::vector<std::string> m_IncludeDirectories;
  488. std::vector<std::string> m_LinkDirectories;
  489. std::vector<std::string> m_ListFiles; // list of command files loaded
  490. cmTarget::LinkLibraries m_LinkLibraries;
  491. std::string m_IncludeFileRegularExpression;
  492. std::string m_ComplainFileRegularExpression;
  493. std::vector<std::string> m_SourceFileExtensions;
  494. std::vector<std::string> m_HeaderFileExtensions;
  495. std::string m_DefineFlags;
  496. std::vector<cmSourceGroup> m_SourceGroups;
  497. typedef std::map<cmStdString, cmStdString> DefinitionMap;
  498. DefinitionMap m_Definitions;
  499. std::vector<cmCommand*> m_UsedCommands;
  500. cmLocalGenerator* m_LocalGenerator;
  501. bool IsFunctionBlocked(const char *name, std::vector<std::string> const& args);
  502. private:
  503. /**
  504. * Get the name of the parent directories CMakeLists file
  505. * given a current CMakeLists file name
  506. */
  507. std::string GetParentListFileName(const char *listFileName);
  508. void ReadSources(std::ifstream& fin, bool t);
  509. friend class cmMakeDepend; // make depend needs direct access
  510. // to the m_Sources array
  511. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  512. void AddDefaultDefinitions();
  513. std::list<cmFunctionBlocker *> m_FunctionBlockers;
  514. typedef std::map<cmStdString, cmData*> DataMap;
  515. DataMap m_DataMap;
  516. bool m_Inheriting;
  517. };
  518. #endif