cmakemain.cxx 8.3 KB

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