cmMakefile.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #ifndef cmMakefile_h
  33. #define cmMakefile_h
  34. #include "cmStandardIncludes.h"
  35. #include "cmData.h"
  36. #include "cmSourceFile.h"
  37. #include "cmSystemTools.h"
  38. #include "cmSourceGroup.h"
  39. #include "cmTarget.h"
  40. #include "cmCacheManager.h"
  41. class cmFunctionBlocker;
  42. class cmCommand;
  43. class cmMakefileGenerator;
  44. /** \class cmMakefile
  45. * \brief Process the input CMakeLists.txt file.
  46. *
  47. * Process and store into memory the input CMakeLists.txt file.
  48. * Each CMakeLists.txt file is parsed and the commands found there
  49. * are added into the build process.
  50. */
  51. class cmMakefile
  52. {
  53. public:
  54. /**
  55. * Return major and minor version numbers for cmake.
  56. */
  57. static unsigned int GetMajorVersion() { return 0;}
  58. static unsigned int GetMinorVersion() { return 7;}
  59. /**
  60. * Return the major and minor version of the cmake that
  61. * was used to write the currently loaded cache, note
  62. * this method will not work before the cache is loaded.
  63. */
  64. static unsigned int GetCacheMajorVersion();
  65. static unsigned int GetCacheMinorVersion();
  66. /**
  67. * Construct an empty makefile.
  68. */
  69. cmMakefile();
  70. /**
  71. * Destructor.
  72. */
  73. ~cmMakefile();
  74. /**
  75. * Read and parse a CMakeLists.txt file.
  76. */
  77. bool ReadListFile(const char* listfile, const char* external= 0);
  78. /**
  79. * Add a wrapper generator.
  80. */
  81. void AddCommand(cmCommand* );
  82. /**
  83. * Add a function blocker to this makefile
  84. */
  85. void AddFunctionBlocker(cmFunctionBlocker *fb)
  86. { m_FunctionBlockers.insert(fb);}
  87. void RemoveFunctionBlocker(cmFunctionBlocker *fb)
  88. { m_FunctionBlockers.erase(fb);}
  89. void RemoveFunctionBlocker(const char *name, const std::vector<std::string> &args);
  90. /**
  91. * Specify the makefile generator. This is platform/compiler
  92. * dependent, although the interface is through a generic
  93. * superclass.
  94. */
  95. void SetMakefileGenerator(cmMakefileGenerator*);
  96. ///! Get the current makefile generator.
  97. cmMakefileGenerator* GetMakefileGenerator()
  98. { return m_MakefileGenerator;}
  99. /**
  100. * Produce the output makefile.
  101. */
  102. void GenerateMakefile();
  103. /**
  104. * run the final pass on all commands.
  105. */
  106. void FinalPass();
  107. /**
  108. * Print the object state to std::cout.
  109. */
  110. void Print() const;
  111. /**
  112. * Add a custom command to the build.
  113. */
  114. void AddCustomCommand(const char* source,
  115. const char* command,
  116. const char* commandArgs,
  117. const std::vector<std::string>& depends,
  118. const std::vector<std::string>& outputs,
  119. const char *target);
  120. void AddCustomCommand(const char* source,
  121. const char* command,
  122. const char* commandArgs,
  123. const std::vector<std::string>& depends,
  124. const char* output,
  125. const char* target);
  126. /**
  127. * Add a define flag to the build.
  128. */
  129. void AddDefineFlag(const char* definition);
  130. /**
  131. * Add an executable to the build.
  132. */
  133. void AddExecutable(const char *exename,
  134. const std::vector<std::string> &srcs);
  135. void AddExecutable(const char *exename,
  136. const std::vector<std::string> &srcs, bool win32);
  137. /**
  138. * Add a utility to the build. A utiltity target is
  139. * a command that is run every time a target is built.
  140. */
  141. void AddUtilityCommand(const char* utilityName,
  142. const char* command,
  143. bool all);
  144. void AddUtilityCommand(const char* utilityName,
  145. const char* command,
  146. bool all,
  147. const std::vector<std::string> &depends,
  148. const std::vector<std::string> &outputs);
  149. /**
  150. * Get a list of link libraries in the build.
  151. */
  152. cmTarget::LinkLibraries& GetLinkLibraries()
  153. {
  154. return m_LinkLibraries;
  155. }
  156. /**
  157. * Add a link library to the build.
  158. */
  159. void AddLinkLibrary(const char*);
  160. void AddLinkLibrary(const char*, cmTarget::LinkLibraryType type);
  161. void AddLinkLibraryForTarget(const char *tgt, const char*,
  162. cmTarget::LinkLibraryType type);
  163. /**
  164. * Add a link directory to the build.
  165. */
  166. void AddLinkDirectory(const char*);
  167. /**
  168. * Add a subdirectory to the build.
  169. */
  170. void AddSubDirectory(const char*);
  171. /**
  172. * Add a subdirectory dependency.
  173. */
  174. void AddSubdirDependency(const char* subdir, const char* dependency);
  175. /**
  176. * Add an include directory to the build.
  177. */
  178. void AddIncludeDirectory(const char*);
  179. /**
  180. * Add a variable definition to the build. This variable
  181. * can be used in CMake to refer to lists, directories, etc.
  182. */
  183. void AddDefinition(const char* name, const char* value);
  184. ///! Add a definition to this makefile and the global cmake cache.
  185. void AddCacheDefinition(const char* name, const char* value,
  186. const char* doc,
  187. cmCacheManager::CacheEntryType type);
  188. /**
  189. * Add bool variable definition to the build.
  190. */
  191. void AddDefinition(const char* name, bool);
  192. ///! Add a definition to this makefile and the global cmake cache.
  193. void AddCacheDefinition(const char* name, bool, const char* doc);
  194. /**
  195. * Specify the name of the project for this build.
  196. */
  197. void SetProjectName(const char*);
  198. /**
  199. * Get the name of the project for this build.
  200. */
  201. const char* GetProjectName()
  202. {
  203. return m_ProjectName.c_str();
  204. }
  205. /**
  206. * Set the name of the library.
  207. */
  208. void AddLibrary(const char *libname, bool shared,
  209. const std::vector<std::string> &srcs);
  210. /**
  211. * Add a class/source file to the build.
  212. */
  213. void AddSource(cmSourceFile& ,const char *srcListName);
  214. /**
  215. * Remove a class/source file from the build.
  216. */
  217. void RemoveSource(cmSourceFile& ,const char *srcListName);
  218. /**
  219. * Add a source group for consideration when adding a new source.
  220. */
  221. void AddSourceGroup(const char* name, const char* regex);
  222. /**
  223. * Add an auxiliary directory to the build.
  224. */
  225. void AddExtraDirectory(const char* dir);
  226. /**
  227. * Add an auxiliary directory to the build.
  228. */
  229. void MakeStartDirectoriesCurrent()
  230. {
  231. m_cmCurrentDirectory = m_cmStartDirectory;
  232. m_CurrentOutputDirectory = m_StartOutputDirectory;
  233. }
  234. //@{
  235. /**
  236. * Set/Get the home directory (or output directory) in the project. The
  237. * home directory is the top directory of the project. It is where
  238. * CMakeSetup or configure was run. Remember that CMake processes
  239. * CMakeLists files by recursing up the tree starting at the StartDirectory
  240. * and going up until it reaches the HomeDirectory.
  241. */
  242. void SetHomeDirectory(const char* dir);
  243. const char* GetHomeDirectory() const
  244. {
  245. return m_cmHomeDirectory.c_str();
  246. }
  247. void SetHomeOutputDirectory(const char* lib);
  248. const char* GetHomeOutputDirectory() const
  249. {
  250. return m_HomeOutputDirectory.c_str();
  251. }
  252. //@}
  253. //@{
  254. /**
  255. * Set/Get the start directory (or output directory). The start directory
  256. * is the directory of the CMakeLists.txt file that started the current
  257. * round of processing. Remember that CMake processes CMakeLists files by
  258. * recursing up the tree starting at the StartDirectory and going up until
  259. * it reaches the HomeDirectory.
  260. */
  261. void SetStartDirectory(const char* dir)
  262. {
  263. m_cmStartDirectory = dir;
  264. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  265. }
  266. const char* GetStartDirectory() const
  267. {
  268. return m_cmStartDirectory.c_str();
  269. }
  270. void SetStartOutputDirectory(const char* lib)
  271. {
  272. m_StartOutputDirectory = lib;
  273. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  274. }
  275. const char* GetStartOutputDirectory() const
  276. {
  277. return m_StartOutputDirectory.c_str();
  278. }
  279. //@}
  280. //@{
  281. /**
  282. * Set/Get the current directory (or output directory) in the project. The
  283. * current directory is the directory of the CMakeLists.txt file that is
  284. * currently being processed. Remember that CMake processes CMakeLists
  285. * files by recursing up the tree starting at the StartDirectory and going
  286. * up until it reaches the HomeDirectory.
  287. */
  288. void SetCurrentDirectory(const char* dir)
  289. {
  290. m_cmCurrentDirectory = dir;
  291. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  292. }
  293. const char* GetCurrentDirectory() const
  294. {
  295. return m_cmCurrentDirectory.c_str();
  296. }
  297. void SetCurrentOutputDirectory(const char* lib)
  298. {
  299. m_CurrentOutputDirectory = lib;
  300. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  301. }
  302. const char* GetCurrentOutputDirectory() const
  303. {
  304. return m_CurrentOutputDirectory.c_str();
  305. }
  306. /* Get the current CMakeLists.txt file that is being processed. This
  307. * is just used in order to be able to 'branch' from one file to a second
  308. * transparently */
  309. const char* GetCurrentListFile() const
  310. {
  311. return m_cmCurrentListFile.c_str();
  312. }
  313. //@}
  314. /**
  315. * Set a regular expression that include files must match
  316. * in order to be considered as part of the depend information.
  317. */
  318. void SetIncludeRegularExpression(const char* regex)
  319. {
  320. m_IncludeFileRegularExpression = regex;
  321. }
  322. /**
  323. * Set a regular expression that include files that are not found
  324. * must match in order to be considered a problem.
  325. */
  326. void SetComplainRegularExpression(const char* regex)
  327. {
  328. m_ComplainFileRegularExpression = regex;
  329. }
  330. /**
  331. * Get the list of targets
  332. */
  333. cmTargets &GetTargets() { return m_Targets; }
  334. const cmTargets &GetTargets() const { return m_Targets; }
  335. /**
  336. * Get a list of the build subdirectories.
  337. */
  338. const std::vector<std::string>& GetSubDirectories()
  339. {
  340. return m_SubDirectories;
  341. }
  342. /**
  343. * Get the subdirectory dependencies for the given subdirectory.
  344. */
  345. const std::set<std::string>& GetSubdirDepends(const char* subdir)
  346. {
  347. return m_SubdirDepends[subdir];
  348. }
  349. /**
  350. * Get a list of include directories in the build.
  351. */
  352. std::vector<std::string>& GetIncludeDirectories()
  353. {
  354. return m_IncludeDirectories;
  355. }
  356. const std::vector<std::string>& GetIncludeDirectories() const
  357. {
  358. return m_IncludeDirectories;
  359. }
  360. /**
  361. * Get a list of link directories in the build.
  362. */
  363. std::vector<std::string>& GetLinkDirectories()
  364. {
  365. return m_LinkDirectories;
  366. }
  367. /**
  368. * Return a list of source files in this makefile.
  369. */
  370. typedef std::map<std::string,std::vector<cmSourceFile> > SourceMap;
  371. const SourceMap &GetSources() const {return m_Sources;}
  372. SourceMap &GetSources() {return m_Sources;}
  373. cmSourceFile *GetSource(const char *srclist, const char *sourceName);
  374. /**
  375. * Obtain a list of auxiliary source directories.
  376. */
  377. std::vector<std::string>& GetAuxSourceDirectories()
  378. {return m_AuxSourceDirectories;}
  379. //@{
  380. /**
  381. * Return a list of extensions associated with source and header
  382. * files
  383. */
  384. const std::vector<std::string>& GetSourceExtensions() const
  385. {return m_SourceFileExtensions;}
  386. const std::vector<std::string>& GetHeaderExtensions() const
  387. {return m_HeaderFileExtensions;}
  388. //@}
  389. /**
  390. * Given a variable name, return its value (as a string).
  391. * If the variable is not found in this makefile instance, the
  392. * cache is then queried.
  393. */
  394. const char* GetDefinition(const char*);
  395. /** Test a boolean cache entry to see if it is true or false,
  396. * returns false if no entry defined.
  397. */
  398. bool IsOn(const char* name);
  399. /**
  400. * Get a list of preprocessor define flags.
  401. */
  402. const char* GetDefineFlags()
  403. {return m_DefineFlags.c_str();}
  404. /**
  405. * Get the vector of used command instances.
  406. */
  407. const std::vector<cmCommand*>& GetUsedCommands() const
  408. {return m_UsedCommands;}
  409. /**
  410. * Get the vector source groups.
  411. */
  412. const std::vector<cmSourceGroup>& GetSourceGroups() const
  413. { return m_SourceGroups; }
  414. /**
  415. * Get the vector of list files on which this makefile depends
  416. */
  417. const std::vector<std::string>& GetListFiles() const
  418. { return m_ListFiles; }
  419. /**
  420. * Dump documentation to a file. If 0 is returned, the
  421. * operation failed.
  422. */
  423. int DumpDocumentationToFile(const char *fileName);
  424. /**
  425. * Expand all defined varibles in the string.
  426. * Defined varibles come from the m_Definitions map.
  427. * They are expanded with ${var} where var is the
  428. * entry in the m_Definitions map. Also @var@ is
  429. * expanded to match autoconf style expansions.
  430. */
  431. void ExpandVariablesInString(std::string& source) const;
  432. void ExpandVariablesInString(std::string& source, bool escapeQuotes) const;
  433. /**
  434. * Remove any remaining variables in the string. Anything with ${var} or
  435. * @var@ will be removed.
  436. */
  437. void RemoveVariablesInString(std::string& source) const;
  438. /**
  439. * Expand variables in the makefiles ivars such as link directories etc
  440. */
  441. void ExpandVariables();
  442. /** Recursivly read and create a cmMakefile object for
  443. * all CMakeLists.txt files in the GetSubDirectories list.
  444. * Once the file is found, it ReadListFile is called on
  445. * the cmMakefile created for it.
  446. */
  447. void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  448. /**
  449. * find what source group this source is in
  450. */
  451. cmSourceGroup& FindSourceGroup(const char* source,
  452. std::vector<cmSourceGroup> &groups);
  453. void RegisterData(cmData*);
  454. void RegisterData(const char*, cmData*);
  455. cmData* LookupData(const char*) const;
  456. /**
  457. * execute a single CMake command
  458. */
  459. void ExecuteCommand(std::string &name, std::vector<std::string> &args);
  460. protected:
  461. std::string m_Prefix;
  462. std::vector<std::string> m_AuxSourceDirectories; //
  463. std::string m_cmCurrentDirectory;
  464. std::string m_CurrentOutputDirectory;
  465. std::string m_cmStartDirectory;
  466. std::string m_StartOutputDirectory;
  467. std::string m_cmHomeDirectory;
  468. std::string m_HomeOutputDirectory;
  469. std::string m_cmCurrentListFile;
  470. std::string m_ProjectName; // project name
  471. // libraries, classes, and executables
  472. cmTargets m_Targets;
  473. SourceMap m_Sources;
  474. std::vector<std::string> m_SubDirectories; // list of sub directories
  475. std::map<std::string, std::set<std::string> > m_SubdirDepends;
  476. // The include and link-library paths. These may have order
  477. // dependency, so they must be vectors (not set).
  478. std::vector<std::string> m_IncludeDirectories;
  479. std::vector<std::string> m_LinkDirectories;
  480. std::vector<std::string> m_ListFiles; // list of command files loaded
  481. cmTarget::LinkLibraries m_LinkLibraries;
  482. std::string m_IncludeFileRegularExpression;
  483. std::string m_ComplainFileRegularExpression;
  484. std::vector<std::string> m_SourceFileExtensions;
  485. std::vector<std::string> m_HeaderFileExtensions;
  486. std::string m_DefineFlags;
  487. std::vector<cmSourceGroup> m_SourceGroups;
  488. typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
  489. typedef std::map<std::string, std::string> DefinitionMap;
  490. DefinitionMap m_Definitions;
  491. RegisteredCommandsMap m_Commands;
  492. std::vector<cmCommand*> m_UsedCommands;
  493. cmMakefileGenerator* m_MakefileGenerator;
  494. bool IsFunctionBlocked(const char *name, std::vector<std::string> &args);
  495. private:
  496. /**
  497. * Get the name of the parent directories CMakeLists file
  498. * given a current CMakeLists file name
  499. */
  500. std::string GetParentListFileName(const char *listFileName);
  501. void ReadSources(std::ifstream& fin, bool t);
  502. friend class cmMakeDepend; // make depend needs direct access
  503. // to the m_Sources array
  504. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  505. void AddDefaultCommands();
  506. void AddDefaultDefinitions();
  507. std::set<cmFunctionBlocker *> m_FunctionBlockers;
  508. typedef std::map<std::string, cmData*> DataMap;
  509. DataMap m_DataMap;
  510. bool m_Inheriting;
  511. };
  512. #endif