cmMakefile.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. class cmFunctionBlocker;
  41. class cmCommand;
  42. class cmMakefileGenerator;
  43. /** \class cmMakefile
  44. * \brief Process the input CMakeLists.txt file.
  45. *
  46. * Process and store into memory the input CMakeLists.txt file.
  47. * Each CMakeLists.txt file is parsed and the commands found there
  48. * are added into the build process.
  49. */
  50. class cmMakefile
  51. {
  52. public:
  53. /**
  54. * Construct an empty makefile.
  55. */
  56. cmMakefile();
  57. /**
  58. * Destructor.
  59. */
  60. ~cmMakefile();
  61. /**
  62. * Read and parse a CMakeLists.txt file.
  63. */
  64. bool ReadListFile(const char* listfile, const char* external= 0);
  65. /**
  66. * Add a wrapper generator.
  67. */
  68. void AddCommand(cmCommand* );
  69. /**
  70. * Add a function blocker to this makefile
  71. */
  72. void AddFunctionBlocker(cmFunctionBlocker *fb)
  73. { m_FunctionBlockers.insert(fb);}
  74. void RemoveFunctionBlocker(cmFunctionBlocker *fb)
  75. { m_FunctionBlockers.erase(fb);}
  76. void RemoveFunctionBlocker(const char *name, const std::vector<std::string> &args);
  77. /**
  78. * Specify the makefile generator. This is platform/compiler
  79. * dependent, although the interface is through a generic
  80. * superclass.
  81. */
  82. void SetMakefileGenerator(cmMakefileGenerator*);
  83. ///! Get the current makefile generator.
  84. cmMakefileGenerator* GetMakefileGenerator()
  85. { return m_MakefileGenerator;}
  86. /**
  87. * Produce the output makefile.
  88. */
  89. void GenerateMakefile();
  90. /**
  91. * Print the object state to std::cout.
  92. */
  93. void Print() const;
  94. /**
  95. * Add a custom command to the build.
  96. */
  97. void AddCustomCommand(const char* source,
  98. const char* command,
  99. const std::vector<std::string>& depends,
  100. const std::vector<std::string>& outputs,
  101. const char *target);
  102. void AddCustomCommand(const char* source,
  103. const char* command,
  104. const std::vector<std::string>& depends,
  105. const char* output,
  106. const char* target);
  107. /**
  108. * Add a define flag to the build.
  109. */
  110. void AddDefineFlag(const char* definition);
  111. /**
  112. * Add an executable to the build.
  113. */
  114. void AddExecutable(const char *exename,
  115. const std::vector<std::string> &srcs);
  116. void AddExecutable(const char *exename,
  117. const std::vector<std::string> &srcs, bool win32);
  118. /**
  119. * Add a utility to the build. A utiltity target is
  120. * a command that is run every time a target is built.
  121. */
  122. void AddUtilityCommand(const char* utilityName,
  123. const char* command,
  124. bool all);
  125. void AddUtilityCommand(const char* utilityName,
  126. const char* command,
  127. bool all,
  128. const std::vector<std::string> &depends,
  129. const std::vector<std::string> &outputs);
  130. /**
  131. * Add a utility on which this project depends. A utility is an executable
  132. * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
  133. * commands. It is not a full path nor does it have an extension.
  134. */
  135. void AddUtility(const char*);
  136. /**
  137. * Add a directory in which a utility may be built.
  138. */
  139. void AddUtilityDirectory(const char*);
  140. /**
  141. * Get a list of link libraries in the build.
  142. */
  143. cmTarget::LinkLibraries& GetLinkLibraries()
  144. {
  145. return m_LinkLibraries;
  146. }
  147. /**
  148. * Add a link library to the build.
  149. */
  150. void AddLinkLibrary(const char*);
  151. void AddLinkLibrary(const char*, cmTarget::LinkLibraryType type);
  152. void AddLinkLibraryForTarget(const char *tgt, const char*,
  153. cmTarget::LinkLibraryType type);
  154. /**
  155. * Add a link directory to the build.
  156. */
  157. void AddLinkDirectory(const char*);
  158. /**
  159. * Add a subdirectory to the build.
  160. */
  161. void AddSubDirectory(const char*);
  162. /**
  163. * Add an include directory to the build.
  164. */
  165. void AddIncludeDirectory(const char*);
  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. /**
  172. * Add bool variable definition to the build.
  173. */
  174. void AddDefinition(const char* name, bool);
  175. /**
  176. * Specify the name of the project for this build.
  177. */
  178. void SetProjectName(const char*);
  179. /**
  180. * Get the name of the project for this build.
  181. */
  182. const char* GetProjectName()
  183. {
  184. return m_ProjectName.c_str();
  185. }
  186. /**
  187. * Set the name of the library.
  188. */
  189. void AddLibrary(const char *libname, const std::vector<std::string> &srcs);
  190. /**
  191. * Add a class/source file to the build.
  192. */
  193. void AddSource(cmSourceFile& ,const char *srcListName);
  194. /**
  195. * Add a source group for consideration when adding a new source.
  196. */
  197. void AddSourceGroup(const char* name, const char* regex);
  198. /**
  199. * Add an auxiliary directory to the build.
  200. */
  201. void AddExtraDirectory(const char* dir);
  202. /**
  203. * Add an auxiliary directory to the build.
  204. */
  205. void MakeStartDirectoriesCurrent()
  206. {
  207. m_cmCurrentDirectory = m_cmStartDirectory;
  208. m_CurrentOutputDirectory = m_StartOutputDirectory;
  209. }
  210. //@{
  211. /**
  212. * Set/Get the home directory (or output directory) in the project. The
  213. * home directory is the top directory of the project. It is where
  214. * CMakeSetup or configure was run. Remember that CMake processes
  215. * CMakeLists files by recursing up the tree starting at the StartDirectory
  216. * and going up until it reaches the HomeDirectory.
  217. */
  218. void SetHomeDirectory(const char* dir);
  219. const char* GetHomeDirectory() const
  220. {
  221. return m_cmHomeDirectory.c_str();
  222. }
  223. void SetHomeOutputDirectory(const char* lib);
  224. const char* GetHomeOutputDirectory() const
  225. {
  226. return m_HomeOutputDirectory.c_str();
  227. }
  228. //@}
  229. //@{
  230. /**
  231. * Set/Get the start directory (or output directory). The start directory
  232. * is the directory of the CMakeLists.txt file that started the current
  233. * round of processing. Remember that CMake processes CMakeLists files by
  234. * recursing up the tree starting at the StartDirectory and going up until
  235. * it reaches the HomeDirectory.
  236. */
  237. void SetStartDirectory(const char* dir)
  238. {
  239. m_cmStartDirectory = dir;
  240. cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
  241. }
  242. const char* GetStartDirectory() const
  243. {
  244. return m_cmStartDirectory.c_str();
  245. }
  246. void SetStartOutputDirectory(const char* lib)
  247. {
  248. m_StartOutputDirectory = lib;
  249. cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
  250. }
  251. const char* GetStartOutputDirectory() const
  252. {
  253. return m_StartOutputDirectory.c_str();
  254. }
  255. //@}
  256. //@{
  257. /**
  258. * Set/Get the current directory (or output directory) in the project. The
  259. * current directory is the directory of the CMakeLists.txt file that is
  260. * currently being processed. Remember that CMake processes CMakeLists
  261. * files by recursing up the tree starting at the StartDirectory and going
  262. * up until it reaches the HomeDirectory.
  263. */
  264. void SetCurrentDirectory(const char* dir)
  265. {
  266. m_cmCurrentDirectory = dir;
  267. cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
  268. }
  269. const char* GetCurrentDirectory() const
  270. {
  271. return m_cmCurrentDirectory.c_str();
  272. }
  273. void SetCurrentOutputDirectory(const char* lib)
  274. {
  275. m_CurrentOutputDirectory = lib;
  276. cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
  277. }
  278. const char* GetCurrentOutputDirectory() const
  279. {
  280. return m_CurrentOutputDirectory.c_str();
  281. }
  282. /* Get the current CMakeLists.txt file that is being processed. This
  283. * is just used in order to be able to 'branch' from one file to a second
  284. * transparently */
  285. const char* GetCurrentListFile() const
  286. {
  287. return m_cmCurrentListFile.c_str();
  288. }
  289. //@}
  290. /**
  291. * Set a regular expression that include files must match
  292. * in order to be considered as part of the depend information.
  293. */
  294. void SetIncludeRegularExpression(const char* regex)
  295. {
  296. m_IncludeFileRegularExpression = regex;
  297. }
  298. /**
  299. * Get the list of targets
  300. */
  301. cmTargets &GetTargets() { return m_Targets; }
  302. /**
  303. * Get a list of the build subdirectories.
  304. */
  305. const std::vector<std::string>& GetSubDirectories()
  306. {
  307. return m_SubDirectories;
  308. }
  309. /**
  310. * Get a list of include directories in the build.
  311. */
  312. std::vector<std::string>& GetIncludeDirectories()
  313. {
  314. return m_IncludeDirectories;
  315. }
  316. /**
  317. * Get a list of link directories in the build.
  318. */
  319. std::vector<std::string>& GetLinkDirectories()
  320. {
  321. return m_LinkDirectories;
  322. }
  323. /**
  324. * Get a list of utilities on which the project depends.
  325. */
  326. std::vector<std::string>& GetUtilities()
  327. {
  328. return m_Utilities;
  329. }
  330. /**
  331. * Get a list of directories that may contain the Utilities.
  332. */
  333. std::vector<std::string>& GetUtilityDirectories()
  334. {
  335. return m_UtilityDirectories;
  336. }
  337. /**
  338. * Return a list of source files in this makefile.
  339. */
  340. typedef std::map<std::string,std::vector<cmSourceFile> > SourceMap;
  341. const SourceMap &GetSources() const {return m_Sources;}
  342. SourceMap &GetSources() {return m_Sources;}
  343. cmSourceFile *GetSource(const char *srclist, const char *sourceName);
  344. /**
  345. * Obtain a list of auxiliary source directories.
  346. */
  347. std::vector<std::string>& GetAuxSourceDirectories()
  348. {return m_AuxSourceDirectories;}
  349. /**
  350. * Do not use this.
  351. */
  352. std::vector<std::string>& GetMakeVerbatim()
  353. {return m_MakeVerbatim;}
  354. /**
  355. * Given a variable name, return its value (as a string).
  356. */
  357. const char* GetDefinition(const char*);
  358. /**
  359. * Get a list of preprocessor define flags.
  360. */
  361. const char* GetDefineFlags()
  362. {return m_DefineFlags.c_str();}
  363. /**
  364. * Get the vector of used command instances.
  365. */
  366. const std::vector<cmCommand*>& GetUsedCommands() const
  367. {return m_UsedCommands;}
  368. /**
  369. * Get the vector source groups.
  370. */
  371. const std::vector<cmSourceGroup>& GetSourceGroups() const
  372. { return m_SourceGroups; }
  373. /**
  374. * Get the vector of list files on which this makefile depends
  375. */
  376. const std::vector<std::string>& GetListFiles() const
  377. { return m_ListFiles; }
  378. /**
  379. * Dump documentation to a file. If 0 is returned, the
  380. * operation failed.
  381. */
  382. int DumpDocumentationToFile(const char *fileName);
  383. /**
  384. * Expand all defined varibles in the string.
  385. * Defined varibles come from the m_Definitions map.
  386. * They are expanded with ${var} where var is the
  387. * entry in the m_Definitions map. Also @var@ is
  388. * expanded to match autoconf style expansions.
  389. */
  390. void ExpandVariablesInString(std::string& source) const;
  391. /**
  392. * Remove any remaining variables in the string. Anything with ${var} or
  393. * @var@ will be removed.
  394. */
  395. void RemoveVariablesInString(std::string& source) const;
  396. /**
  397. * Expand variables in the makefiles ivars such as link directories etc
  398. */
  399. void ExpandVariables();
  400. /** Recursivly read and create a cmMakefile object for
  401. * all CMakeLists.txt files in the GetSubDirectories list.
  402. * Once the file is found, it ReadListFile is called on
  403. * the cmMakefile created for it.
  404. */
  405. void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  406. /**
  407. * find what source group this source is in
  408. */
  409. cmSourceGroup& FindSourceGroup(const char* source,
  410. std::vector<cmSourceGroup> &groups);
  411. void RegisterData(cmData*);
  412. void RegisterData(const char*, cmData*);
  413. cmData* LookupData(const char*) const;
  414. protected:
  415. std::string m_Prefix;
  416. std::vector<std::string> m_AuxSourceDirectories; //
  417. std::string m_cmCurrentDirectory;
  418. std::string m_CurrentOutputDirectory;
  419. std::string m_cmStartDirectory;
  420. std::string m_StartOutputDirectory;
  421. std::string m_cmHomeDirectory;
  422. std::string m_HomeOutputDirectory;
  423. std::string m_cmCurrentListFile;
  424. std::string m_ProjectName; // project name
  425. // libraries, classes, and executables
  426. cmTargets m_Targets;
  427. SourceMap m_Sources;
  428. std::vector<std::string> m_SubDirectories; // list of sub directories
  429. std::vector<std::string> m_MakeVerbatim; // lines copied from input file
  430. std::vector<std::string> m_IncludeDirectories;
  431. std::vector<std::string> m_LinkDirectories;
  432. std::vector<std::string> m_Utilities;
  433. std::vector<std::string> m_UtilityDirectories;
  434. std::vector<std::string> m_ListFiles; // list of command files loaded
  435. cmTarget::LinkLibraries m_LinkLibraries;
  436. std::string m_IncludeFileRegularExpression;
  437. std::string m_DefineFlags;
  438. std::vector<cmSourceGroup> m_SourceGroups;
  439. typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
  440. typedef std::map<std::string, std::string> DefinitionMap;
  441. DefinitionMap m_Definitions;
  442. RegisteredCommandsMap m_Commands;
  443. std::vector<cmCommand*> m_UsedCommands;
  444. cmMakefileGenerator* m_MakefileGenerator;
  445. bool IsFunctionBlocked(const char *name, std::vector<std::string> &args) const;
  446. private:
  447. /**
  448. * Get the name of the parent directories CMakeLists file
  449. * given a current CMakeLists file name
  450. */
  451. std::string GetParentListFileName(const char *listFileName);
  452. void ReadSources(std::ifstream& fin, bool t);
  453. friend class cmMakeDepend; // make depend needs direct access
  454. // to the m_Sources array
  455. void PrintStringVector(const char* s, const std::vector<std::string>& v) const;
  456. void AddDefaultCommands();
  457. void AddDefaultDefinitions();
  458. std::set<cmFunctionBlocker *> m_FunctionBlockers;
  459. typedef std::map<std::string, cmData*> DataMap;
  460. DataMap m_DataMap;
  461. };
  462. #endif