cmMakefile.h 19 KB

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