cmakemain.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmake.h"
  14. #include "cmCacheManager.h"
  15. #include "cmListFileCache.h"
  16. #include "cmakewizard.h"
  17. #ifdef CMAKE_BUILD_WITH_CMAKE
  18. #include "cmDynamicLoader.h"
  19. #include "cmDocumentation.h"
  20. //----------------------------------------------------------------------------
  21. static const cmDocumentationEntry cmDocumentationName[] =
  22. {
  23. {0,
  24. " cmake - Cross-Platform Makefile Generator.", 0},
  25. {0,0,0}
  26. };
  27. //----------------------------------------------------------------------------
  28. static const cmDocumentationEntry cmDocumentationUsage[] =
  29. {
  30. {0,
  31. " cmake [options] <path-to-source>\n"
  32. " cmake [options] <path-to-existing-build>", 0},
  33. {0,0,0}
  34. };
  35. //----------------------------------------------------------------------------
  36. static const cmDocumentationEntry cmDocumentationDescription[] =
  37. {
  38. {0,
  39. "The \"cmake\" executable is the CMake command-line interface. It may "
  40. "be used to configure projects in scripts. Project configuration settings "
  41. "may be specified on the command line with the -D option. The -i option "
  42. "will cause cmake to interactively prompt for such settings.", 0},
  43. CMAKE_STANDARD_INTRODUCTION,
  44. {0,0,0}
  45. };
  46. //----------------------------------------------------------------------------
  47. static const cmDocumentationEntry cmDocumentationOptions[] =
  48. {
  49. CMAKE_STANDARD_OPTIONS_TABLE,
  50. {"-E", "CMake command mode.",
  51. "For true platform independence, CMake provides a list of commands "
  52. "that can be used on all systems. Run with -E help for the usage "
  53. "information."},
  54. {"-i", "Run in wizard mode.",
  55. "Wizard mode runs cmake interactively without a GUI. The user is "
  56. "prompted to answer questions about the project configuration. "
  57. "The answers are used to set cmake cache values."},
  58. {"-L[A][H]", "List non-advanced cached variables.",
  59. "List cache variables will run CMake and list all the variables from the "
  60. "CMake cache that are not marked as INTERNAL or ADVANCED. This will "
  61. "effectively display current CMake settings, which can be then changed "
  62. "with -D option. Changing some of the variable may result in more "
  63. "variables being created. If A is specified, then it will display also "
  64. "advanced variables. If H is specified, it will also display help for "
  65. "each variable."},
  66. {"-N", "View mode only.",
  67. "Only load the cache. Do not actually run configure and generate steps."},
  68. {"-P <file>", "Process script mode.",
  69. "Process the given cmake file as a script written in the CMake language. "
  70. "No configure or generate step is performed and the cache is not"
  71. " modified."},
  72. {"--graphviz=[file]", "Generate graphviz of dependencies.",
  73. "Generate a graphviz input file that will contain all the library and "
  74. "executable dependencies in the project."},
  75. {"--help-command cmd [file]", "Print help for a single command and exit.",
  76. "Full documentation specific to the given command is displayed."},
  77. {"--help-command-list [file]", "List available listfile commands and exit.",
  78. "The list contains all commands for which help may be obtained by using "
  79. "the --help-command argument followed by a command name. If a file is "
  80. "specified, the help is written into it."},
  81. {"--help-module module [file]", "Print help for a single module and exit.",
  82. "Full documentation specific to the given module is displayed."},
  83. {"--help-module-list [file]", "List available modules and exit.",
  84. "The list contains all modules for which help may be obtained by using "
  85. "the --help-module argument followed by a module name. If a file is "
  86. "specified, the help is written into it."},
  87. {0,0,0}
  88. };
  89. //----------------------------------------------------------------------------
  90. static const cmDocumentationEntry cmDocumentationSeeAlso[] =
  91. {
  92. {0, "ccmake", 0},
  93. {0, "ctest", 0},
  94. {0, 0, 0}
  95. };
  96. //----------------------------------------------------------------------------
  97. static const cmDocumentationEntry cmDocumentationNOTE[] =
  98. {
  99. {0,
  100. "CMake no longer configures a project when run with no arguments. "
  101. "In order to configure the project in the current directory, run\n"
  102. " cmake .", 0},
  103. {0,0,0}
  104. };
  105. #endif
  106. int do_cmake(int ac, char** av);
  107. void updateProgress(const char *msg, float prog, void *cd);
  108. int main(int ac, char** av)
  109. {
  110. cmSystemTools::EnableMSVCDebugHook();
  111. int ret = do_cmake(ac, av);
  112. #ifdef CMAKE_BUILD_WITH_CMAKE
  113. cmDynamicLoader::FlushCache();
  114. #endif
  115. return ret;
  116. }
  117. int do_cmake(int ac, char** av)
  118. {
  119. #ifdef CMAKE_BUILD_WITH_CMAKE
  120. cmDocumentation doc;
  121. #endif
  122. int nocwd = 0;
  123. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  124. {
  125. std::cerr << "Current working directory cannot be established."
  126. << std::endl;
  127. nocwd = 1;
  128. }
  129. #ifdef CMAKE_BUILD_WITH_CMAKE
  130. if(doc.CheckOptions(ac, av) || nocwd)
  131. {
  132. // Construct and print requested documentation.
  133. cmake hcm;
  134. hcm.AddCMakePaths(av[0]);
  135. doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
  136. std::vector<cmDocumentationEntry> commands;
  137. std::vector<cmDocumentationEntry> generators;
  138. hcm.GetCommandDocumentation(commands);
  139. hcm.GetGeneratorDocumentation(generators);
  140. doc.SetName("cmake");
  141. doc.SetNameSection(cmDocumentationName);
  142. doc.SetUsageSection(cmDocumentationUsage);
  143. doc.SetDescriptionSection(cmDocumentationDescription);
  144. doc.SetGeneratorsSection(&generators[0]);
  145. doc.SetOptionsSection(cmDocumentationOptions);
  146. doc.SetCommandsSection(&commands[0]);
  147. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  148. int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
  149. // If we were run with no arguments, but a CMakeLists.txt file
  150. // exists, the user may have been trying to use the old behavior
  151. // of cmake to build a project in-source. Print a message
  152. // explaining the change to standard error and return an error
  153. // condition in case the program is running from a script.
  154. if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
  155. {
  156. doc.ClearSections();
  157. doc.AddSection("NOTE", cmDocumentationNOTE);
  158. doc.Print(cmDocumentation::UsageForm, std::cerr);
  159. return 1;
  160. }
  161. return result;
  162. }
  163. #else
  164. if ( nocwd || ac == 1 )
  165. {
  166. std::cout <<
  167. "Bootstrap CMake should not be used outside CMake build process."
  168. << std::endl;
  169. return 0;
  170. }
  171. #endif
  172. bool wiz = false;
  173. bool command = false;
  174. bool list_cached = false;
  175. bool list_all_cached = false;
  176. bool list_help = false;
  177. bool view_only = false;
  178. bool script_mode = false;
  179. std::vector<std::string> args;
  180. for(int i =0; i < ac; ++i)
  181. {
  182. if(strcmp(av[i], "-i") == 0)
  183. {
  184. wiz = true;
  185. }
  186. // if command has already been set, then
  187. // do not eat the -E
  188. else if (!command && strcmp(av[i], "-E") == 0)
  189. {
  190. command = true;
  191. }
  192. else if (strcmp(av[i], "-N") == 0)
  193. {
  194. view_only = true;
  195. }
  196. else if (strcmp(av[i], "-L") == 0)
  197. {
  198. list_cached = true;
  199. }
  200. else if (strcmp(av[i], "-LA") == 0)
  201. {
  202. list_all_cached = true;
  203. }
  204. else if (strcmp(av[i], "-LH") == 0)
  205. {
  206. list_cached = true;
  207. list_help = true;
  208. }
  209. else if (strcmp(av[i], "-LAH") == 0)
  210. {
  211. list_all_cached = true;
  212. list_help = true;
  213. }
  214. else if (strncmp(av[i], "-P", strlen("-P")) == 0)
  215. {
  216. if ( i == ac -1 )
  217. {
  218. cmSystemTools::Error("No script specified for argument -P");
  219. }
  220. script_mode = true;
  221. args.push_back(av[i]);
  222. i++;
  223. args.push_back(av[i]);
  224. }
  225. else
  226. {
  227. args.push_back(av[i]);
  228. }
  229. }
  230. if(command)
  231. {
  232. int ret = cmake::ExecuteCMakeCommand(args);
  233. return ret;
  234. }
  235. if (wiz)
  236. {
  237. cmakewizard wizard;
  238. return wizard.RunWizard(args);
  239. }
  240. cmake cm;
  241. cm.SetProgressCallback(updateProgress, 0);
  242. cm.SetScriptMode(script_mode);
  243. int res = cm.Run(args, view_only);
  244. if ( list_cached || list_all_cached )
  245. {
  246. cmCacheManager::CacheIterator it =
  247. cm.GetCacheManager()->GetCacheIterator();
  248. std::cout << "-- Cache values" << std::endl;
  249. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  250. {
  251. cmCacheManager::CacheEntryType t = it.GetType();
  252. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  253. t != cmCacheManager::UNINITIALIZED )
  254. {
  255. bool advanced = it.PropertyExists("ADVANCED");
  256. if ( list_all_cached || !advanced)
  257. {
  258. if ( list_help )
  259. {
  260. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  261. }
  262. std::cout << it.GetName() << ":" <<
  263. cmCacheManager::TypeToString(it.GetType())
  264. << "=" << it.GetValue() << std::endl;
  265. if ( list_help )
  266. {
  267. std::cout << std::endl;
  268. }
  269. }
  270. }
  271. }
  272. }
  273. return res;
  274. }
  275. void updateProgress(const char *msg, float prog, void*)
  276. {
  277. if ( prog < 0 )
  278. {
  279. std::cout << "-- " << msg << std::endl;
  280. }
  281. //else
  282. //{
  283. //std::cout << "-- " << msg << " " << prog << std::endl;
  284. //}
  285. }