cmMakefile.h 16 KB

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