cmakemain.cxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "cmakewizard.h"
  14. #include "cmake.h"
  15. #include "cmCacheManager.h"
  16. #include "cmListFileCache.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. {"--help-command cmd [file]", "Print help for a single command and exit.",
  73. "Full documentation specific to the given command is displayed."},
  74. {"--help-command-list [file]", "List available listfile commands and exit.",
  75. "The list contains all commands for which help may be obtained by using "
  76. "the --help-command argument followed by a command name. If a file is "
  77. "specified, the help is written into it."},
  78. {"--help-module module [file]", "Print help for a single module and exit.",
  79. "Full documentation specific to the given module is displayed."},
  80. {"--help-module-list [file]", "List available modules and exit.",
  81. "The list contains all modules for which help may be obtained by using "
  82. "the --help-module argument followed by a module name. If a file is "
  83. "specified, the help is written into it."},
  84. {0,0,0}
  85. };
  86. //----------------------------------------------------------------------------
  87. static const cmDocumentationEntry cmDocumentationSeeAlso[] =
  88. {
  89. {0, "ccmake", 0},
  90. {0, "ctest", 0},
  91. {0, 0, 0}
  92. };
  93. //----------------------------------------------------------------------------
  94. static const cmDocumentationEntry cmDocumentationNOTE[] =
  95. {
  96. {0,
  97. "CMake no longer configures a project when run with no arguments. "
  98. "In order to configure the project in the current directory, run\n"
  99. " cmake .", 0},
  100. {0,0,0}
  101. };
  102. #endif
  103. int do_cmake(int ac, char** av);
  104. void updateProgress(const char *msg, float prog, void *cd);
  105. int main(int ac, char** av)
  106. {
  107. cmSystemTools::EnableMSVCDebugHook();
  108. int ret = do_cmake(ac, av);
  109. #ifdef CMAKE_BUILD_WITH_CMAKE
  110. cmDynamicLoader::FlushCache();
  111. #endif
  112. return ret;
  113. }
  114. int do_cmake(int ac, char** av)
  115. {
  116. #ifdef CMAKE_BUILD_WITH_CMAKE
  117. cmDocumentation doc;
  118. #endif
  119. int nocwd = 0;
  120. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  121. {
  122. std::cerr << "Current working directory cannot be established."
  123. << std::endl;
  124. nocwd = 1;
  125. }
  126. #ifdef CMAKE_BUILD_WITH_CMAKE
  127. if(doc.CheckOptions(ac, av) || nocwd)
  128. {
  129. // Construct and print requested documentation.
  130. cmake hcm;
  131. std::vector<cmDocumentationEntry> commands;
  132. std::vector<cmDocumentationEntry> generators;
  133. hcm.GetCommandDocumentation(commands);
  134. hcm.GetGeneratorDocumentation(generators);
  135. doc.SetName("cmake");
  136. doc.SetNameSection(cmDocumentationName);
  137. doc.SetUsageSection(cmDocumentationUsage);
  138. doc.SetDescriptionSection(cmDocumentationDescription);
  139. doc.SetGeneratorsSection(&generators[0]);
  140. doc.SetOptionsSection(cmDocumentationOptions);
  141. doc.SetCommandsSection(&commands[0]);
  142. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  143. int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
  144. // If we were run with no arguments, but a CMakeLists.txt file
  145. // exists, the user may have been trying to use the old behavior
  146. // of cmake to build a project in-source. Print a message
  147. // explaining the change to standard error and return an error
  148. // condition in case the program is running from a script.
  149. if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
  150. {
  151. doc.ClearSections();
  152. doc.AddSection("NOTE", cmDocumentationNOTE);
  153. doc.Print(cmDocumentation::UsageForm, std::cerr);
  154. return 1;
  155. }
  156. return result;
  157. }
  158. #else
  159. if ( nocwd || ac == 1 )
  160. {
  161. std::cout <<
  162. "Bootstrap CMake should not be used outside CMake build process."
  163. << std::endl;
  164. return 0;
  165. }
  166. #endif
  167. bool wiz = false;
  168. bool command = false;
  169. bool list_cached = false;
  170. bool list_all_cached = false;
  171. bool list_help = false;
  172. bool view_only = false;
  173. bool script_mode = false;
  174. std::vector<std::string> args;
  175. for(int i =0; i < ac; ++i)
  176. {
  177. if(strcmp(av[i], "-i") == 0)
  178. {
  179. wiz = true;
  180. }
  181. // if command has already been set, then
  182. // do not eat the -E
  183. else if (!command && strcmp(av[i], "-E") == 0)
  184. {
  185. command = true;
  186. }
  187. else if (strcmp(av[i], "-N") == 0)
  188. {
  189. view_only = true;
  190. }
  191. else if (strcmp(av[i], "-L") == 0)
  192. {
  193. list_cached = true;
  194. }
  195. else if (strcmp(av[i], "-LA") == 0)
  196. {
  197. list_all_cached = true;
  198. }
  199. else if (strcmp(av[i], "-LH") == 0)
  200. {
  201. list_cached = true;
  202. list_help = true;
  203. }
  204. else if (strcmp(av[i], "-LAH") == 0)
  205. {
  206. list_all_cached = true;
  207. list_help = true;
  208. }
  209. else if (strncmp(av[i], "-P", strlen("-P")) == 0)
  210. {
  211. if ( i == ac -1 )
  212. {
  213. cmSystemTools::Error("No script specified for argument -P");
  214. }
  215. script_mode = true;
  216. args.push_back(av[i]);
  217. i++;
  218. args.push_back(av[i]);
  219. }
  220. else
  221. {
  222. args.push_back(av[i]);
  223. }
  224. }
  225. if(command)
  226. {
  227. int ret = cmake::ExecuteCMakeCommand(args);
  228. return ret;
  229. }
  230. if (wiz)
  231. {
  232. cmakewizard wizard;
  233. return wizard.RunWizard(args);
  234. }
  235. cmake cm;
  236. cm.SetProgressCallback(updateProgress, 0);
  237. cm.SetScriptMode(script_mode);
  238. int res = cm.Run(args, view_only);
  239. if ( list_cached || list_all_cached )
  240. {
  241. cmCacheManager::CacheIterator it =
  242. cm.GetCacheManager()->GetCacheIterator();
  243. std::cout << "-- Cache values" << std::endl;
  244. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  245. {
  246. cmCacheManager::CacheEntryType t = it.GetType();
  247. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  248. t != cmCacheManager::UNINITIALIZED )
  249. {
  250. bool advanced = it.PropertyExists("ADVANCED");
  251. if ( list_all_cached || !advanced)
  252. {
  253. if ( list_help )
  254. {
  255. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  256. }
  257. std::cout << it.GetName() << ":" <<
  258. cmCacheManager::TypeToString(it.GetType())
  259. << "=" << it.GetValue() << std::endl;
  260. if ( list_help )
  261. {
  262. std::cout << std::endl;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. return res;
  269. }
  270. void updateProgress(const char *msg, float prog, void*)
  271. {
  272. if ( prog < 0 )
  273. {
  274. std::cout << "-- " << msg << std::endl;
  275. }
  276. //else
  277. //{
  278. //std::cout << "-- " << msg << " " << prog << std::endl;
  279. //}
  280. }