cmMakefile.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 AddCustomCommandToOutput(const char* output,
  106. const char* command,
  107. const std::vector<std::string>& commandArgs,
  108. const char *main_dependency,
  109. const std::vector<std::string>& depends,
  110. const char *comment = 0,
  111. bool replace = false);
  112. void AddCustomCommandToTarget(const char* target,
  113. const char* command,
  114. const std::vector<std::string>& commandArgs,
  115. cmTarget::CustomCommandType type,
  116. const char *comment = 0);
  117. void AddCustomCommandToTarget(const char* target,
  118. const char* command,
  119. const std::vector<std::string>& commandArgs,
  120. cmTarget::CustomCommandType type,
  121. const char *comment,
  122. const std::vector<std::string>& depends);
  123. /**
  124. * Add a custom command to the build.
  125. */
  126. void AddCustomCommand(const char* source,
  127. const char* command,
  128. const std::vector<std::string>& commandArgs,
  129. const std::vector<std::string>& depends,
  130. const std::vector<std::string>& outputs,
  131. const char *target,
  132. const char *comment = 0);
  133. void AddCustomCommand(const char* source,
  134. const char* command,
  135. const std::vector<std::string>& commandArgs,
  136. const std::vector<std::string>& depends,
  137. const char* output,
  138. const char* target);
  139. /**
  140. * Add a define flag to the build.
  141. */
  142. void AddDefineFlag(const char* definition);
  143. /**
  144. * Add an executable to the build.
  145. */
  146. void AddExecutable(const char *exename,
  147. const std::vector<std::string> &srcs);
  148. void AddExecutable(const char *exename,
  149. const std::vector<std::string> &srcs, bool win32);
  150. /**
  151. * Add a utility to the build. A utiltity target is
  152. * a command that is run every time a target is built.
  153. */
  154. void AddUtilityCommand(const char* utilityName,
  155. const char* command,
  156. const char* arguments,
  157. bool all,
  158. const std::vector<std::string> &depends);
  159. void AddUtilityCommand(const char* utilityName,
  160. const char* command,
  161. const char* arguments,
  162. bool all,
  163. const std::vector<std::string> &depends,
  164. const std::vector<std::string> &outputs);
  165. /**
  166. * Add a link library to the build.
  167. */
  168. void AddLinkLibrary(const char*);
  169. void AddLinkLibrary(const char*, cmTarget::LinkLibraryType type);
  170. void AddLinkLibraryForTarget(const char *tgt, const char*,
  171. cmTarget::LinkLibraryType type);
  172. void AddLinkDirectoryForTarget(const char *tgt, const char* d);
  173. /**
  174. * Add a link directory to the build.
  175. */
  176. void AddLinkDirectory(const char*);
  177. /**
  178. * Add a subdirectory to the build.
  179. */
  180. void AddSubDirectory(const char*);
  181. /**
  182. * Add an include directory to the build.
  183. */
  184. void AddIncludeDirectory(const char*, bool before = false);
  185. /**
  186. * Find a library (as in cmSystemTools) but add in compiler specific paths
  187. */
  188. std::string FindLibrary(const char* name,
  189. const std::vector<std::string>& path);
  190. /**
  191. * Add a variable definition to the build. This variable
  192. * can be used in CMake to refer to lists, directories, etc.
  193. */
  194. void AddDefinition(const char* name, const char* value);
  195. ///! Add a definition to this makefile and the global cmake cache.
  196. void AddCacheDefinition(const char* name, const char* value,
  197. const char* doc,
  198. cmCacheManager::CacheEntryType type);
  199. /**
  200. * Add bool variable definition to the build.
  201. */
  202. void AddDefinition(const char* name, bool);
  203. ///! Add a definition to this makefile and the global cmake cache.
  204. void AddCacheDefinition(const char* name, bool, const char* doc);
  205. /**
  206. * Remove a variable definition from the build. This is not valid
  207. * for cache entries, and will only affect the current makefile.
  208. */
  209. void RemoveDefinition(const char* name);
  210. /**
  211. * Specify the name of the project for this build.
  212. */
  213. void SetProjectName(const char*);
  214. /**
  215. * Get the name of the project for this build.
  216. */
  217. const char* GetProjectName()
  218. {
  219. return m_ProjectName.c_str();
  220. }
  221. /**
  222. * Set the name of the library.
  223. */
  224. void AddLibrary(const char *libname, int shared,
  225. const std::vector<std::string> &srcs);
  226. /**
  227. * Add a class/source file to the build.
  228. */
  229. //void AddSource(cmSourceFile& ,const char *srcListName);
  230. /**
  231. * Remove a class/source file from the build.
  232. */
  233. //void RemoveSource(cmSourceFile& ,const char *srcListName);
  234. /**
  235. * Add a source group for consideration when adding a new source.
  236. */
  237. void AddSourceGroup(const char* name, const char* regex=0);
  238. /**
  239. * Add an auxiliary directory to the build.
  240. */
  241. void AddExtraDirectory(const char* dir);
  242. /**
  243. * Add an auxiliary directory to the build.
  244. */
  245. void MakeStartDirectoriesCurrent()
  246. {
  247. m_cmCurrentDirectory = m_cmStartDirectory;
  248. m_CurrentOutputDirectory = m_StartOutputDirectory;
  249. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", m_cmCurrentDirectory.c_str());
  250. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR", m_CurrentOutputDirectory.c_str());
  251. }
  252. //@{
  253. /**
  254. * Set/Get the home directory (or output directory) in the project. The
  255. * home directory is the top directory of the project. It is where
  256. * CMakeSetup or configure was run. Remember that CMake processes
  257. * CMakeLists files by recursing up the tree starting at the StartDirectory
  258. * and going up until it reaches the HomeDirectory.
  259. */
  260. void SetHomeDirectory(const char* dir);
  261. const char* GetHomeDirectory() const
  262. {
  263. return m_cmHomeDirectory.c_str();
  264. }
  265. void SetHomeOutputDirectory(const char* lib);
  266. const char* GetHomeOutputDirectory() const
  267. {
  268. return m_HomeOutputDirectory.c_str();
  269. }
  270. //@}
  271. //@{
  272. /**
  273. * Set/Get the start directory (or output directory). The start directory
  274. * is the directory of the CMakeLists.txt file that started the current
  275. * round of processing. Remember that CMake processes CMakeLists files by
  276. * recursing up the tree starting at the StartDirectory and going up until
  277. * it reaches the HomeDirectory.
  278. */
  279. void SetStartDirectory(const char* dir)
  280. {
  281. m_cmStartDirectory = dir;
  282. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  283. }
  284. const char* GetStartDirectory() const
  285. {
  286. return m_cmStartDirectory.c_str();
  287. }
  288. void SetStartOutputDirectory(const char* lib)
  289. {
  290. m_StartOutputDirectory = lib;
  291. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  292. cmSystemTools::MakeDirectory(m_StartOutputDirectory.c_str());
  293. }
  294. const char* GetStartOutputDirectory() const
  295. {
  296. return m_StartOutputDirectory.c_str();
  297. }
  298. //@}
  299. //@{
  300. /**
  301. * Set/Get the current directory (or output directory) in the project. The
  302. * current directory is the directory of the CMakeLists.txt file that is
  303. * currently being processed. Remember that CMake processes CMakeLists
  304. * files by recursing up the tree starting at the StartDirectory and going
  305. * up until it reaches the HomeDirectory.
  306. */
  307. void SetCurrentDirectory(const char* dir)
  308. {
  309. m_cmCurrentDirectory = dir;
  310. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  311. this->AddDefinition("CMAKE_CURRENT_SOURCE_DIR", m_cmCurrentDirectory.c_str());
  312. }
  313. const char* GetCurrentDirectory() const
  314. {
  315. return m_cmCurrentDirectory.c_str();
  316. }
  317. void SetCurrentOutputDirectory(const char* lib)
  318. {
  319. m_CurrentOutputDirectory = lib;
  320. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  321. this->AddDefinition("CMAKE_CURRENT_BINARY_DIR", m_CurrentOutputDirectory.c_str());
  322. }
  323. const char* GetCurrentOutputDirectory() const
  324. {
  325. return m_CurrentOutputDirectory.c_str();
  326. }
  327. /* Get the current CMakeLists.txt file that is being processed. This
  328. * is just used in order to be able to 'branch' from one file to a second
  329. * transparently */
  330. const char* GetCurrentListFile() const
  331. {
  332. return m_cmCurrentListFile.c_str();
  333. }
  334. //@}
  335. /**
  336. * Set a regular expression that include files must match
  337. * in order to be considered as part of the depend information.
  338. */
  339. void SetIncludeRegularExpression(const char* regex)
  340. {
  341. m_IncludeFileRegularExpression = regex;
  342. }
  343. /**
  344. * Set a regular expression that include files that are not found
  345. * must match in order to be considered a problem.
  346. */
  347. void SetComplainRegularExpression(const char* regex)
  348. {
  349. m_ComplainFileRegularExpression = regex;
  350. }
  351. /**
  352. * Get the list of targets
  353. */
  354. cmTargets &GetTargets() { return m_Targets; }
  355. const cmTargets &GetTargets() const { return m_Targets; }
  356. /**
  357. * Get a list of the build subdirectories.
  358. */
  359. const std::vector<std::string>& GetSubDirectories()
  360. {
  361. return m_SubDirectories;
  362. }
  363. /**
  364. * Get a list of include directories in the build.
  365. */
  366. std::vector<std::string>& GetIncludeDirectories()
  367. {
  368. return m_IncludeDirectories;
  369. }
  370. const std::vector<std::string>& GetIncludeDirectories() const
  371. {
  372. return m_IncludeDirectories;
  373. }
  374. /** Expand out any arguements in the vector that have ; separated
  375. * strings into multiple arguements. A new vector is created
  376. * containing the expanded versions of all arguments in argsIn.
  377. * This method differes from the one in cmSystemTools in that if
  378. * the CmakeLists file is version 1.2 or earlier it will check for
  379. * source lists being used without ${} around them
  380. */
  381. void ExpandSourceListArguments(std::vector<std::string> const& argsIn,
  382. std::vector<std::string>& argsOut,
  383. unsigned int startArgumentIndex);
  384. /** Get a cmSourceFile pointer for a given source name, if the name is
  385. * not found, then a null pointer is returned.
  386. */
  387. cmSourceFile* GetSource(const char* sourceName) const;
  388. ///! Add a new cmSourceFile to the list of sources for this makefile.
  389. cmSourceFile* AddSource(cmSourceFile const&);
  390. /** Get a cmSourceFile pointer for a given source name, if the name is
  391. * not found, then create the source file and return it. generated
  392. * indicates if it is a generated file, this is used in determining
  393. * how to create the source file instance e.g. name
  394. */
  395. cmSourceFile* GetOrCreateSource(const char* sourceName,
  396. bool generated = false);
  397. /**
  398. * Obtain a list of auxiliary source directories.
  399. */
  400. std::vector<std::string>& GetAuxSourceDirectories()
  401. {return m_AuxSourceDirectories;}
  402. //@{
  403. /**
  404. * Return a list of extensions associated with source and header
  405. * files
  406. */
  407. const std::vector<std::string>& GetSourceExtensions() const
  408. {return m_SourceFileExtensions;}
  409. const std::vector<std::string>& GetHeaderExtensions() const
  410. {return m_HeaderFileExtensions;}
  411. //@}
  412. /**
  413. * Given a variable name, return its value (as a string).
  414. * If the variable is not found in this makefile instance, the
  415. * cache is then queried.
  416. */
  417. const char* GetDefinition(const char*) const;
  418. /**
  419. * Get the list of all variables in the current space. If argument
  420. * cacheonly is specified and is greater than 0, then only cache
  421. * variables will be listed.
  422. */
  423. std::vector<std::string> GetDefinitions(int cacheonly=0) const;
  424. /** Test a boolean cache entry to see if it is true or false,
  425. * returns false if no entry defined.
  426. */
  427. bool IsOn(const char* name) const;
  428. /**
  429. * Get a list of preprocessor define flags.
  430. */
  431. const char* GetDefineFlags()
  432. {return m_DefineFlags.c_str();}
  433. /**
  434. * Get the vector of used command instances.
  435. */
  436. const std::vector<cmCommand*>& GetUsedCommands() const
  437. {return m_UsedCommands;}
  438. /**
  439. * Get the vector source groups.
  440. */
  441. const std::vector<cmSourceGroup>& GetSourceGroups() const
  442. { return m_SourceGroups; }
  443. /**
  444. * Get the source group
  445. */
  446. cmSourceGroup* GetSourceGroup(const char* name);
  447. /**
  448. * Get the vector of list files on which this makefile depends
  449. */
  450. const std::vector<std::string>& GetListFiles() const
  451. { return m_ListFiles; }
  452. ///! When the file changes cmake will be re-run from the build system.
  453. void AddCMakeDependFile(const char* file)
  454. { m_ListFiles.push_back(file);}
  455. /**
  456. * Expand all defined variables in the string.
  457. * Defined variables come from the m_Definitions map.
  458. * They are expanded with ${var} where var is the
  459. * entry in the m_Definitions map. Also @var@ is
  460. * expanded to match autoconf style expansions.
  461. */
  462. const char *ExpandVariablesInString(std::string& source) const;
  463. const char *ExpandVariablesInString(std::string& source, bool escapeQuotes,
  464. bool atOnly = false) const;
  465. /**
  466. * Remove any remaining variables in the string. Anything with ${var} or
  467. * @var@ will be removed.
  468. */
  469. void RemoveVariablesInString(std::string& source, bool atOnly = false) const;
  470. /**
  471. * Expand variables in the makefiles ivars such as link directories etc
  472. */
  473. void ExpandVariables();
  474. void ExpandVariablesInCustomCommands();
  475. /**
  476. * find what source group this source is in
  477. */
  478. cmSourceGroup& FindSourceGroup(const char* source,
  479. std::vector<cmSourceGroup> &groups);
  480. void RegisterData(cmData*);
  481. void RegisterData(const char*, cmData*);
  482. cmData* LookupData(const char*) const;
  483. /**
  484. * Execute a single CMake command. Returns true if the command
  485. * succeeded or false if it failed.
  486. */
  487. bool ExecuteCommand(const cmListFileFunction& lff);
  488. /** Check if a command exists. */
  489. bool CommandExists(const char* name) const;
  490. /**
  491. * Add a command to this cmake instance
  492. */
  493. void AddCommand(cmCommand* );
  494. ///! Enable support for the named language, if null then all languages are enabled.
  495. void EnableLanguage(const char* );
  496. /**
  497. * Set/Get the name of the parent directories CMakeLists file
  498. * given a current CMakeLists file name
  499. */
  500. cmCacheManager *GetCacheManager() const;
  501. cmVariableWatch* GetVariableWatch() const;
  502. //! Determine wether this is a local or global build.
  503. bool GetLocal() const;
  504. ///! Display progress or status message.
  505. void DisplayStatus(const char*, float);
  506. /**
  507. * Expand the given list file arguments into the full set after
  508. * variable replacement and list expansion.
  509. */
  510. void ExpandArguments(std::vector<cmListFileArgument> const& inArgs,
  511. std::vector<std::string>& outArgs);
  512. /**
  513. * Get the instance
  514. */
  515. cmake *GetCMakeInstance() const;
  516. /**
  517. * Get all the source files this makefile knows about
  518. */
  519. const std::vector<cmSourceFile*> &GetSourceFiles() const
  520. {return m_SourceFiles;}
  521. std::vector<cmSourceFile*> &GetSourceFiles() {return m_SourceFiles;}
  522. /**
  523. * Is there a source file that has the provided source file as an output?
  524. * if so then return it
  525. */
  526. cmSourceFile *GetSourceFileWithOutput(const char *outName);
  527. protected:
  528. // add link libraries and directories to the target
  529. void AddGlobalLinkInformation(const char* name, cmTarget& target);
  530. std::string m_Prefix;
  531. std::vector<std::string> m_AuxSourceDirectories; //
  532. std::string m_cmCurrentDirectory;
  533. std::string m_CurrentOutputDirectory;
  534. std::string m_cmStartDirectory;
  535. std::string m_StartOutputDirectory;
  536. std::string m_cmHomeDirectory;
  537. std::string m_HomeOutputDirectory;
  538. std::string m_cmCurrentListFile;
  539. std::string m_ProjectName; // project name
  540. // libraries, classes, and executables
  541. cmTargets m_Targets;
  542. std::vector<cmSourceFile*> m_SourceFiles;
  543. std::vector<std::string> m_SubDirectories; // list of sub directories
  544. struct StringSet : public std::set<cmStdString>
  545. {
  546. };
  547. // The include and link-library paths. These may have order
  548. // dependency, so they must be vectors (not set).
  549. std::vector<std::string> m_IncludeDirectories;
  550. std::vector<std::string> m_LinkDirectories;
  551. std::vector<std::string> m_ListFiles; // list of command files loaded
  552. cmTarget::LinkLibraries m_LinkLibraries;
  553. std::string m_IncludeFileRegularExpression;
  554. std::string m_ComplainFileRegularExpression;
  555. std::vector<std::string> m_SourceFileExtensions;
  556. std::vector<std::string> m_HeaderFileExtensions;
  557. std::string m_DefineFlags;
  558. std::vector<cmSourceGroup> m_SourceGroups;
  559. typedef std::map<cmStdString, cmStdString> DefinitionMap;
  560. DefinitionMap m_Definitions;
  561. std::vector<cmCommand*> m_UsedCommands;
  562. cmLocalGenerator* m_LocalGenerator;
  563. bool IsFunctionBlocked(const cmListFileFunction& lff);
  564. private:
  565. /**
  566. * Get the name of the parent directories CMakeLists file
  567. * given a current CMakeLists file name
  568. */
  569. std::string GetParentListFileName(const char *listFileName);
  570. void ReadSources(std::ifstream& fin, bool t);
  571. friend class cmMakeDepend; // make depend needs direct access
  572. // to the m_Sources array
  573. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  574. void AddDefaultDefinitions();
  575. std::list<cmFunctionBlocker *> m_FunctionBlockers;
  576. typedef std::map<cmStdString, cmData*> DataMap;
  577. DataMap m_DataMap;
  578. bool m_Inheriting;
  579. // used in AddDefinition for performance improvement
  580. DefinitionMap::key_type m_TemporaryDefinitionKey;
  581. };
  582. #endif