cmakemain.cxx 11 KB

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