1
0

cmMakefile.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 97;}
  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 std::vector<std::string>& 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 std::vector<std::string>& 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. const char* arguments,
  144. bool all);
  145. void AddUtilityCommand(const char* utilityName,
  146. const char* command,
  147. const char* arguments,
  148. bool all,
  149. const std::vector<std::string> &depends,
  150. const std::vector<std::string> &outputs);
  151. /**
  152. * Get a list of link libraries in the build.
  153. */
  154. cmTarget::LinkLibraries& GetLinkLibraries()
  155. {
  156. return m_LinkLibraries;
  157. }
  158. /**
  159. * Get a list of link libraries in the build.
  160. */
  161. const cmTarget::LinkLibraries& GetLinkLibraries() const
  162. {
  163. return m_LinkLibraries;
  164. }
  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. /**
  173. * Add a link directory to the build.
  174. */
  175. void AddLinkDirectory(const char*);
  176. /**
  177. * Add a subdirectory to the build.
  178. */
  179. void AddSubDirectory(const char*);
  180. /**
  181. * Add a subdirectory dependency.
  182. */
  183. void AddSubdirDependency(const char* subdir, const char* dependency);
  184. /**
  185. * Add an include directory to the build.
  186. */
  187. void AddIncludeDirectory(const char*, bool before = false);
  188. /**
  189. * Add a variable definition to the build. This variable
  190. * can be used in CMake to refer to lists, directories, etc.
  191. */
  192. void AddDefinition(const char* name, const char* value);
  193. ///! Add a definition to this makefile and the global cmake cache.
  194. void AddCacheDefinition(const char* name, const char* value,
  195. const char* doc,
  196. cmCacheManager::CacheEntryType type);
  197. /**
  198. * Add bool variable definition to the build.
  199. */
  200. void AddDefinition(const char* name, bool);
  201. ///! Add a definition to this makefile and the global cmake cache.
  202. void AddCacheDefinition(const char* name, bool, const char* doc);
  203. /**
  204. * Specify the name of the project for this build.
  205. */
  206. void SetProjectName(const char*);
  207. /**
  208. * Get the name of the project for this build.
  209. */
  210. const char* GetProjectName()
  211. {
  212. return m_ProjectName.c_str();
  213. }
  214. /**
  215. * Set the name of the library.
  216. */
  217. void AddLibrary(const char *libname, int shared,
  218. const std::vector<std::string> &srcs);
  219. /**
  220. * Add a class/source file to the build.
  221. */
  222. void AddSource(cmSourceFile& ,const char *srcListName);
  223. /**
  224. * Remove a class/source file from the build.
  225. */
  226. void RemoveSource(cmSourceFile& ,const char *srcListName);
  227. /**
  228. * Add a source group for consideration when adding a new source.
  229. */
  230. void AddSourceGroup(const char* name, const char* regex);
  231. /**
  232. * Add an auxiliary directory to the build.
  233. */
  234. void AddExtraDirectory(const char* dir);
  235. /**
  236. * Add an auxiliary directory to the build.
  237. */
  238. void MakeStartDirectoriesCurrent()
  239. {
  240. m_cmCurrentDirectory = m_cmStartDirectory;
  241. m_CurrentOutputDirectory = m_StartOutputDirectory;
  242. }
  243. //@{
  244. /**
  245. * Set/Get the home directory (or output directory) in the project. The
  246. * home directory is the top directory of the project. It is where
  247. * CMakeSetup or configure was run. Remember that CMake processes
  248. * CMakeLists files by recursing up the tree starting at the StartDirectory
  249. * and going up until it reaches the HomeDirectory.
  250. */
  251. void SetHomeDirectory(const char* dir);
  252. const char* GetHomeDirectory() const
  253. {
  254. return m_cmHomeDirectory.c_str();
  255. }
  256. void SetHomeOutputDirectory(const char* lib);
  257. const char* GetHomeOutputDirectory() const
  258. {
  259. return m_HomeOutputDirectory.c_str();
  260. }
  261. //@}
  262. //@{
  263. /**
  264. * Set/Get the start directory (or output directory). The start directory
  265. * is the directory of the CMakeLists.txt file that started the current
  266. * round of processing. Remember that CMake processes CMakeLists files by
  267. * recursing up the tree starting at the StartDirectory and going up until
  268. * it reaches the HomeDirectory.
  269. */
  270. void SetStartDirectory(const char* dir)
  271. {
  272. m_cmStartDirectory = dir;
  273. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  274. }
  275. const char* GetStartDirectory() const
  276. {
  277. return m_cmStartDirectory.c_str();
  278. }
  279. void SetStartOutputDirectory(const char* lib)
  280. {
  281. m_StartOutputDirectory = lib;
  282. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  283. cmSystemTools::MakeDirectory(m_StartOutputDirectory.c_str());
  284. }
  285. const char* GetStartOutputDirectory() const
  286. {
  287. return m_StartOutputDirectory.c_str();
  288. }
  289. //@}
  290. //@{
  291. /**
  292. * Set/Get the current directory (or output directory) in the project. The
  293. * current directory is the directory of the CMakeLists.txt file that is
  294. * currently being processed. Remember that CMake processes CMakeLists
  295. * files by recursing up the tree starting at the StartDirectory and going
  296. * up until it reaches the HomeDirectory.
  297. */
  298. void SetCurrentDirectory(const char* dir)
  299. {
  300. m_cmCurrentDirectory = dir;
  301. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  302. }
  303. const char* GetCurrentDirectory() const
  304. {
  305. return m_cmCurrentDirectory.c_str();
  306. }
  307. void SetCurrentOutputDirectory(const char* lib)
  308. {
  309. m_CurrentOutputDirectory = lib;
  310. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  311. }
  312. const char* GetCurrentOutputDirectory() const
  313. {
  314. return m_CurrentOutputDirectory.c_str();
  315. }
  316. /* Get the current CMakeLists.txt file that is being processed. This
  317. * is just used in order to be able to 'branch' from one file to a second
  318. * transparently */
  319. const char* GetCurrentListFile() const
  320. {
  321. return m_cmCurrentListFile.c_str();
  322. }
  323. //@}
  324. /**
  325. * Set a regular expression that include files must match
  326. * in order to be considered as part of the depend information.
  327. */
  328. void SetIncludeRegularExpression(const char* regex)
  329. {
  330. m_IncludeFileRegularExpression = regex;
  331. }
  332. /**
  333. * Set a regular expression that include files that are not found
  334. * must match in order to be considered a problem.
  335. */
  336. void SetComplainRegularExpression(const char* regex)
  337. {
  338. m_ComplainFileRegularExpression = regex;
  339. }
  340. /**
  341. * Get the list of targets
  342. */
  343. cmTargets &GetTargets() { return m_Targets; }
  344. const cmTargets &GetTargets() const { return m_Targets; }
  345. /**
  346. * Get a list of the build subdirectories.
  347. */
  348. const std::vector<std::string>& GetSubDirectories()
  349. {
  350. return m_SubDirectories;
  351. }
  352. /**
  353. * Get the subdirectory dependencies for the given subdirectory.
  354. */
  355. const std::set<cmStdString>& GetSubdirDepends(const char* subdir)
  356. {
  357. return m_SubdirDepends[subdir];
  358. }
  359. /**
  360. * Get a list of include directories in the build.
  361. */
  362. std::vector<std::string>& GetIncludeDirectories()
  363. {
  364. return m_IncludeDirectories;
  365. }
  366. const std::vector<std::string>& GetIncludeDirectories() const
  367. {
  368. return m_IncludeDirectories;
  369. }
  370. /**
  371. * Get a list of link directories in the build.
  372. */
  373. std::vector<std::string>& GetLinkDirectories()
  374. {
  375. return m_LinkDirectories;
  376. }
  377. /**
  378. * Return a list of source files in this makefile.
  379. */
  380. typedef std::map<cmStdString,std::vector<cmSourceFile> > SourceMap;
  381. const SourceMap &GetSources() const {return m_Sources;}
  382. SourceMap &GetSources() {return m_Sources;}
  383. cmSourceFile *GetSource(const char *srclist, const char *sourceName);
  384. /**
  385. * Obtain a list of auxiliary source directories.
  386. */
  387. std::vector<std::string>& GetAuxSourceDirectories()
  388. {return m_AuxSourceDirectories;}
  389. //@{
  390. /**
  391. * Return a list of extensions associated with source and header
  392. * files
  393. */
  394. const std::vector<std::string>& GetSourceExtensions() const
  395. {return m_SourceFileExtensions;}
  396. const std::vector<std::string>& GetHeaderExtensions() const
  397. {return m_HeaderFileExtensions;}
  398. //@}
  399. /**
  400. * Given a variable name, return its value (as a string).
  401. * If the variable is not found in this makefile instance, the
  402. * cache is then queried.
  403. */
  404. const char* GetDefinition(const char*) const;
  405. /** Test a boolean cache entry to see if it is true or false,
  406. * returns false if no entry defined.
  407. */
  408. bool IsOn(const char* name);
  409. /**
  410. * Get a list of preprocessor define flags.
  411. */
  412. const char* GetDefineFlags()
  413. {return m_DefineFlags.c_str();}
  414. /**
  415. * Get the vector of used command instances.
  416. */
  417. const std::vector<cmCommand*>& GetUsedCommands() const
  418. {return m_UsedCommands;}
  419. /**
  420. * Get the vector source groups.
  421. */
  422. const std::vector<cmSourceGroup>& GetSourceGroups() const
  423. { return m_SourceGroups; }
  424. /**
  425. * Get the vector of list files on which this makefile depends
  426. */
  427. const std::vector<std::string>& GetListFiles() const
  428. { return m_ListFiles; }
  429. ///! When the file changes cmake will be re-run from the build system.
  430. void AddCMakeDependFile(const char* file)
  431. { m_ListFiles.push_back(file);}
  432. /**
  433. * Dump documentation to a file. If 0 is returned, the
  434. * operation failed.
  435. */
  436. int DumpDocumentationToFile(std::ostream&);
  437. /**
  438. * Expand all defined varibles in the string.
  439. * Defined varibles come from the m_Definitions map.
  440. * They are expanded with ${var} where var is the
  441. * entry in the m_Definitions map. Also @var@ is
  442. * expanded to match autoconf style expansions.
  443. */
  444. const char *ExpandVariablesInString(std::string& source) const;
  445. const char *ExpandVariablesInString(std::string& source, bool escapeQuotes,
  446. bool atOnly = false) const;
  447. /**
  448. * Remove any remaining variables in the string. Anything with ${var} or
  449. * @var@ will be removed.
  450. */
  451. void RemoveVariablesInString(std::string& source, bool atOnly = false) const;
  452. /**
  453. * Expand variables in the makefiles ivars such as link directories etc
  454. */
  455. void ExpandVariables();
  456. /** Recursivly read and create a cmMakefile object for
  457. * all CMakeLists.txt files in the GetSubDirectories list.
  458. * Once the file is found, it ReadListFile is called on
  459. * the cmMakefile created for it.
  460. */
  461. void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  462. /**
  463. * find what source group this source is in
  464. */
  465. cmSourceGroup& FindSourceGroup(const char* source,
  466. std::vector<cmSourceGroup> &groups);
  467. void RegisterData(cmData*);
  468. void RegisterData(const char*, cmData*);
  469. cmData* LookupData(const char*) const;
  470. /**
  471. * execute a single CMake command
  472. */
  473. void ExecuteCommand(std::string &name, std::vector<std::string> const& args);
  474. protected:
  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. SourceMap m_Sources;
  488. std::vector<std::string> m_SubDirectories; // list of sub directories
  489. struct StringSet : public std::set<cmStdString>
  490. {
  491. };
  492. std::map<cmStdString, StringSet > m_SubdirDepends;
  493. // The include and link-library paths. These may have order
  494. // dependency, so they must be vectors (not set).
  495. std::vector<std::string> m_IncludeDirectories;
  496. std::vector<std::string> m_LinkDirectories;
  497. std::vector<std::string> m_ListFiles; // list of command files loaded
  498. cmTarget::LinkLibraries m_LinkLibraries;
  499. std::string m_IncludeFileRegularExpression;
  500. std::string m_ComplainFileRegularExpression;
  501. std::vector<std::string> m_SourceFileExtensions;
  502. std::vector<std::string> m_HeaderFileExtensions;
  503. std::string m_DefineFlags;
  504. std::vector<cmSourceGroup> m_SourceGroups;
  505. typedef std::map<cmStdString, cmCommand*> RegisteredCommandsMap;
  506. typedef std::map<cmStdString, cmStdString> DefinitionMap;
  507. DefinitionMap m_Definitions;
  508. RegisteredCommandsMap m_Commands;
  509. std::vector<cmCommand*> m_UsedCommands;
  510. cmMakefileGenerator* m_MakefileGenerator;
  511. bool IsFunctionBlocked(const char *name, std::vector<std::string> const& args);
  512. private:
  513. /**
  514. * Get the name of the parent directories CMakeLists file
  515. * given a current CMakeLists file name
  516. */
  517. std::string GetParentListFileName(const char *listFileName);
  518. void ReadSources(std::ifstream& fin, bool t);
  519. friend class cmMakeDepend; // make depend needs direct access
  520. // to the m_Sources array
  521. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  522. void AddDefaultCommands();
  523. void AddDefaultDefinitions();
  524. std::set<cmFunctionBlocker *> m_FunctionBlockers;
  525. typedef std::map<cmStdString, cmData*> DataMap;
  526. DataMap m_DataMap;
  527. bool m_Inheriting;
  528. };
  529. #endif