cmMakefile.h 18 KB

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