cmakemain.cxx 8.7 KB

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