cmMakefile.h 15 KB

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