cmakemain.cxx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #endif
  149. bool wiz = false;
  150. bool command = false;
  151. bool list_cached = false;
  152. bool list_all_cached = false;
  153. bool list_help = false;
  154. bool view_only = false;
  155. bool script_mode = false;
  156. std::vector<std::string> args;
  157. for(int i =0; i < ac; ++i)
  158. {
  159. if(strcmp(av[i], "-i") == 0)
  160. {
  161. wiz = true;
  162. }
  163. else if (strcmp(av[i], "-E") == 0)
  164. {
  165. command = true;
  166. }
  167. else if (strcmp(av[i], "-N") == 0)
  168. {
  169. view_only = true;
  170. }
  171. else if (strcmp(av[i], "-L") == 0)
  172. {
  173. list_cached = true;
  174. }
  175. else if (strcmp(av[i], "-LA") == 0)
  176. {
  177. list_all_cached = true;
  178. }
  179. else if (strcmp(av[i], "-LH") == 0)
  180. {
  181. list_cached = true;
  182. list_help = true;
  183. }
  184. else if (strcmp(av[i], "-LAH") == 0)
  185. {
  186. list_all_cached = true;
  187. list_help = true;
  188. }
  189. else if (strncmp(av[i], "-P", strlen("-P")) == 0)
  190. {
  191. if ( i == ac -1 )
  192. {
  193. cmSystemTools::Error("No script specified for argument -P");
  194. }
  195. script_mode = true;
  196. args.push_back(av[i]);
  197. i++;
  198. args.push_back(av[i]);
  199. }
  200. else
  201. {
  202. args.push_back(av[i]);
  203. }
  204. }
  205. if(command)
  206. {
  207. int ret = cmake::CMakeCommand(args);
  208. return ret;
  209. }
  210. if (wiz)
  211. {
  212. cmakewizard wizard;
  213. return wizard.RunWizard(args);
  214. }
  215. cmake cm;
  216. cm.SetProgressCallback(updateProgress, 0);
  217. cm.SetScriptMode(script_mode);
  218. int res = cm.Run(args, view_only);
  219. if ( list_cached || list_all_cached )
  220. {
  221. cmCacheManager::CacheIterator it = cm.GetCacheManager()->GetCacheIterator();
  222. std::cout << "-- Cache values" << std::endl;
  223. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  224. {
  225. cmCacheManager::CacheEntryType t = it.GetType();
  226. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  227. t != cmCacheManager::UNINITIALIZED )
  228. {
  229. bool advanced = it.PropertyExists("ADVANCED");
  230. if ( list_all_cached || !advanced)
  231. {
  232. if ( list_help )
  233. {
  234. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  235. }
  236. std::cout << it.GetName() << ":" << cmCacheManager::TypeToString(it.GetType())
  237. << "=" << it.GetValue() << std::endl;
  238. if ( list_help )
  239. {
  240. std::cout << std::endl;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. return res;
  247. }
  248. void updateProgress(const char *msg, float prog, void*)
  249. {
  250. if ( prog < 0 )
  251. {
  252. std::cout << "-- " << msg << std::endl;
  253. }
  254. //else
  255. //{
  256. //std::cout << "-- " << msg << " " << prog << std::endl;
  257. //}
  258. }