cmakemain.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 these first, otherwise there will be problems on Windows
  14. // with GetCurrentDirectory() being redefined
  15. #ifdef CMAKE_BUILD_WITH_CMAKE
  16. #include "cmDynamicLoader.h"
  17. #include "cmDocumentation.h"
  18. #endif
  19. #include "cmake.h"
  20. #include "cmCacheManager.h"
  21. #include "cmListFileCache.h"
  22. #include "cmakewizard.h"
  23. #include "cmSourceFile.h"
  24. #include "cmGlobalGenerator.h"
  25. #include "cmLocalGenerator.h"
  26. #include "cmMakefile.h"
  27. #ifdef CMAKE_BUILD_WITH_CMAKE
  28. //----------------------------------------------------------------------------
  29. static const char * cmDocumentationName[][3] =
  30. {
  31. {0,
  32. " cmake - Cross-Platform Makefile Generator.", 0},
  33. {0,0,0}
  34. };
  35. //----------------------------------------------------------------------------
  36. static const char * cmDocumentationUsage[][3] =
  37. {
  38. {0,
  39. " cmake [options] <path-to-source>\n"
  40. " cmake [options] <path-to-existing-build>", 0},
  41. {0,0,0}
  42. };
  43. //----------------------------------------------------------------------------
  44. static const char * cmDocumentationDescription[][3] =
  45. {
  46. {0,
  47. "The \"cmake\" executable is the CMake command-line interface. It may "
  48. "be used to configure projects in scripts. Project configuration "
  49. "settings "
  50. "may be specified on the command line with the -D option. The -i option "
  51. "will cause cmake to interactively prompt for such settings.", 0},
  52. CMAKE_STANDARD_INTRODUCTION,
  53. {0,0,0}
  54. };
  55. //----------------------------------------------------------------------------
  56. static const char * cmDocumentationOptions[][3] =
  57. {
  58. CMAKE_STANDARD_OPTIONS_TABLE,
  59. {"-E", "CMake command mode.",
  60. "For true platform independence, CMake provides a list of commands "
  61. "that can be used on all systems. Run with -E help for the usage "
  62. "information."},
  63. {"-i", "Run in wizard mode.",
  64. "Wizard mode runs cmake interactively without a GUI. The user is "
  65. "prompted to answer questions about the project configuration. "
  66. "The answers are used to set cmake cache values."},
  67. {"-L[A][H]", "List non-advanced cached variables.",
  68. "List cache variables will run CMake and list all the variables from the "
  69. "CMake cache that are not marked as INTERNAL or ADVANCED. This will "
  70. "effectively display current CMake settings, which can be then changed "
  71. "with -D option. Changing some of the variable may result in more "
  72. "variables being created. If A is specified, then it will display also "
  73. "advanced variables. If H is specified, it will also display help for "
  74. "each variable."},
  75. {"-N", "View mode only.",
  76. "Only load the cache. Do not actually run configure and generate steps."},
  77. {"-P <file>", "Process script mode.",
  78. "Process the given cmake file as a script written in the CMake language. "
  79. "No configure or generate step is performed and the cache is not"
  80. " modified. If variables are defined using -D, this must be done "
  81. "before the -P argument."},
  82. {"--graphviz=[file]", "Generate graphviz of dependencies.",
  83. "Generate a graphviz input file that will contain all the library and "
  84. "executable dependencies in the project."},
  85. {"--system-information [file]", "Dump information about this system.",
  86. "Dump a wide range of information about the current system. If run "
  87. "from the top of a binary tree for a CMake project it will dump "
  88. "additional information such as the cache, log files etc."},
  89. {"--debug-trycompile", "Do not delete the try compile directories..",
  90. "Do not delete the files and directories created for try_compile calls. "
  91. "This is useful in debugging failed try_compiles."},
  92. {"--debug-output", "Put cmake in a debug mode.",
  93. "Print extra stuff during the cmake run like stack traces with "
  94. "message(send_error ) calls."},
  95. {"--help-command cmd [file]", "Print help for a single command and exit.",
  96. "Full documentation specific to the given command is displayed. "
  97. "If a file is specified, the documentation is written into and the output "
  98. "format is determined depending on the filename suffix. Supported are man "
  99. "page, HTML and plain text."},
  100. {"--help-command-list [file]", "List available listfile commands and exit.",
  101. "The list contains all commands for which help may be obtained by using "
  102. "the --help-command argument followed by a command name. "
  103. "If a file is specified, the documentation is written into and the output "
  104. "format is determined depending on the filename suffix. Supported are man "
  105. "page, HTML and plain text."},
  106. {"--help-commands [file]", "Print help for all commands and exit.",
  107. "Full documentation specific for all current command is displayed."
  108. "If a file is specified, the documentation is written into and the output "
  109. "format is determined depending on the filename suffix. Supported are man "
  110. "page, HTML and plain text."},
  111. {"--help-compatcommands [file]", "Print help for compatibility commands. ",
  112. "Full documentation specific for all compatibility commands is displayed."
  113. "If a file is specified, the documentation is written into and the output "
  114. "format is determined depending on the filename suffix. Supported are man "
  115. "page, HTML and plain text."},
  116. {"--help-module module [file]", "Print help for a single module and exit.",
  117. "Full documentation specific to the given module is displayed."
  118. "If a file is specified, the documentation is written into and the output "
  119. "format is determined depending on the filename suffix. Supported are man "
  120. "page, HTML and plain text."},
  121. {"--help-module-list [file]", "List available modules and exit.",
  122. "The list contains all modules for which help may be obtained by using "
  123. "the --help-module argument followed by a module name. "
  124. "If a file is specified, the documentation is written into and the output "
  125. "format is determined depending on the filename suffix. Supported are man "
  126. "page, HTML and plain text."},
  127. {"--help-modules [file]", "Print help for all modules and exit.",
  128. "Full documentation for all modules is displayed. "
  129. "If a file is specified, the documentation is written into and the output "
  130. "format is determined depending on the filename suffix. Supported are man "
  131. "page, HTML and plain text."},
  132. {"--help-custom-modules [file]" , "Print help for all custom modules and "
  133. "exit.",
  134. "Full documentation for all custom modules is displayed. "
  135. "If a file is specified, the documentation is written into and the output "
  136. "format is determined depending on the filename suffix. Supported are man "
  137. "page, HTML and plain text."},
  138. {"--help-property prop [file]",
  139. "Print help for a single property and exit.",
  140. "Full documentation specific to the given module is displayed."
  141. "If a file is specified, the documentation is written into and the output "
  142. "format is determined depending on the filename suffix. Supported are man "
  143. "page, HTML and plain text."},
  144. {"--help-property-list [file]", "List available properties and exit.",
  145. "The list contains all properties for which help may be obtained by using "
  146. "the --help-property argument followed by a property name. If a file is "
  147. "specified, the help is written into it."
  148. "If a file is specified, the documentation is written into and the output "
  149. "format is determined depending on the filename suffix. Supported are man "
  150. "page, HTML and plain text."},
  151. {"--help-properties [file]", "Print help for all properties and exit.",
  152. "Full documentation for all properties is displayed."
  153. "If a file is specified, the documentation is written into and the output "
  154. "format is determined depending on the filename suffix. Supported are man "
  155. "page, HTML and plain text."},
  156. {0,0,0}
  157. };
  158. //----------------------------------------------------------------------------
  159. static const char * cmDocumentationSeeAlso[][3] =
  160. {
  161. {0, "ccmake", 0},
  162. {0, "ctest", 0},
  163. {0, 0, 0}
  164. };
  165. //----------------------------------------------------------------------------
  166. static const char * cmDocumentationNOTE[][3] =
  167. {
  168. {0,
  169. "CMake no longer configures a project when run with no arguments. "
  170. "In order to configure the project in the current directory, run\n"
  171. " cmake .", 0},
  172. {0,0,0}
  173. };
  174. #endif
  175. int do_cmake(int ac, char** av);
  176. static cmMakefile* cmakemainGetMakefile(void *clientdata)
  177. {
  178. cmake* cm = (cmake *)clientdata;
  179. if(cm && cm->GetDebugOutput())
  180. {
  181. cmGlobalGenerator* gg=cm->GetGlobalGenerator();
  182. if (gg)
  183. {
  184. cmLocalGenerator* lg=gg->GetCurrentLocalGenerator();
  185. if (lg)
  186. {
  187. cmMakefile* mf = lg->GetMakefile();
  188. return mf;
  189. }
  190. }
  191. }
  192. return 0;
  193. }
  194. static std::string cmakemainGetStack(void *clientdata)
  195. {
  196. std::string msg;
  197. cmMakefile* mf=cmakemainGetMakefile(clientdata);
  198. if (mf)
  199. {
  200. msg = mf->GetListFileStack();
  201. if (!msg.empty())
  202. {
  203. msg = "\n Called from: " + msg;
  204. }
  205. }
  206. return msg;
  207. }
  208. static void cmakemainErrorCallback(const char* m, const char*, bool&,
  209. void *clientdata)
  210. {
  211. std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
  212. }
  213. static void cmakemainProgressCallback(const char *m, float prog,
  214. void* clientdata)
  215. {
  216. cmMakefile* mf = cmakemainGetMakefile(clientdata);
  217. std::string dir;
  218. if ((mf) && (strstr(m, "Configuring")==m) && (prog<0))
  219. {
  220. dir = " ";
  221. dir += mf->GetCurrentDirectory();
  222. }
  223. else if ((mf) && (strstr(m, "Generating")==m))
  224. {
  225. dir = " ";
  226. dir += mf->GetCurrentOutputDirectory();
  227. }
  228. if ((prog < 0) || (!dir.empty()))
  229. {
  230. std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)<<std::endl;
  231. }
  232. std::cout.flush();
  233. }
  234. int main(int ac, char** av)
  235. {
  236. cmSystemTools::EnableMSVCDebugHook();
  237. int ret = do_cmake(ac, av);
  238. #ifdef CMAKE_BUILD_WITH_CMAKE
  239. cmDynamicLoader::FlushCache();
  240. #endif
  241. return ret;
  242. }
  243. int do_cmake(int ac, char** av)
  244. {
  245. #ifdef CMAKE_BUILD_WITH_CMAKE
  246. cmDocumentation doc;
  247. #endif
  248. int nocwd = 0;
  249. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  250. {
  251. std::cerr << "Current working directory cannot be established."
  252. << std::endl;
  253. nocwd = 1;
  254. }
  255. #ifdef CMAKE_BUILD_WITH_CMAKE
  256. if(doc.CheckOptions(ac, av) || nocwd)
  257. {
  258. // Construct and print requested documentation.
  259. cmake hcm;
  260. hcm.AddCMakePaths(av[0]);
  261. doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
  262. // the command line args are processed here so that you can do
  263. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  264. std::vector<std::string> args;
  265. for(int i =0; i < ac; ++i)
  266. {
  267. args.push_back(av[i]);
  268. }
  269. hcm.SetCacheArgs(args);
  270. const char* modulePath = hcm.GetCacheDefinition("CMAKE_MODULE_PATH");
  271. if (modulePath)
  272. {
  273. doc.SetCMakeModulePath(modulePath);
  274. }
  275. std::vector<cmDocumentationEntry> commands;
  276. std::vector<cmDocumentationEntry> compatCommands;
  277. std::map<std::string,cmDocumentationSection *> propDocs;
  278. std::vector<cmDocumentationEntry> generators;
  279. hcm.GetCommandDocumentation(commands, true, false);
  280. hcm.GetCommandDocumentation(compatCommands, false, true);
  281. hcm.GetPropertiesDocumentation(propDocs);
  282. hcm.GetGeneratorDocumentation(generators);
  283. doc.SetName("cmake");
  284. doc.SetSection("Name",cmDocumentationName);
  285. doc.SetSection("Usage",cmDocumentationUsage);
  286. doc.SetSection("Description",cmDocumentationDescription);
  287. doc.SetSection("Generators",generators);
  288. doc.SetSection("Options",cmDocumentationOptions);
  289. doc.SetSection("Commands",commands);
  290. doc.SetSection("Compatibility Commands",compatCommands);
  291. doc.SetSections(propDocs);
  292. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  293. int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
  294. // If we were run with no arguments, but a CMakeLists.txt file
  295. // exists, the user may have been trying to use the old behavior
  296. // of cmake to build a project in-source. Print a message
  297. // explaining the change to standard error and return an error
  298. // condition in case the program is running from a script.
  299. if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
  300. {
  301. doc.ClearSections();
  302. doc.SetSection("NOTE", cmDocumentationNOTE);
  303. doc.Print(cmDocumentation::UsageForm, std::cerr);
  304. return 1;
  305. }
  306. return result;
  307. }
  308. #else
  309. if ( nocwd || ac == 1 )
  310. {
  311. std::cout <<
  312. "Bootstrap CMake should not be used outside CMake build process."
  313. << std::endl;
  314. return 0;
  315. }
  316. #endif
  317. bool wiz = false;
  318. bool sysinfo = false;
  319. bool command = false;
  320. bool list_cached = false;
  321. bool list_all_cached = false;
  322. bool list_help = false;
  323. bool view_only = false;
  324. bool script_mode = false;
  325. std::vector<std::string> args;
  326. for(int i =0; i < ac; ++i)
  327. {
  328. if(strcmp(av[i], "-i") == 0)
  329. {
  330. wiz = true;
  331. }
  332. else if(!command && strcmp(av[i], "--system-information") == 0)
  333. {
  334. sysinfo = true;
  335. }
  336. // if command has already been set, then
  337. // do not eat the -E
  338. else if (!command && strcmp(av[i], "-E") == 0)
  339. {
  340. command = true;
  341. }
  342. else if (strcmp(av[i], "-N") == 0)
  343. {
  344. view_only = true;
  345. }
  346. else if (strcmp(av[i], "-L") == 0)
  347. {
  348. list_cached = true;
  349. }
  350. else if (strcmp(av[i], "-LA") == 0)
  351. {
  352. list_all_cached = true;
  353. }
  354. else if (strcmp(av[i], "-LH") == 0)
  355. {
  356. list_cached = true;
  357. list_help = true;
  358. }
  359. else if (strcmp(av[i], "-LAH") == 0)
  360. {
  361. list_all_cached = true;
  362. list_help = true;
  363. }
  364. else if (strncmp(av[i], "-P", strlen("-P")) == 0)
  365. {
  366. if ( i == ac -1 )
  367. {
  368. cmSystemTools::Error("No script specified for argument -P");
  369. }
  370. else
  371. {
  372. script_mode = true;
  373. args.push_back(av[i]);
  374. i++;
  375. args.push_back(av[i]);
  376. }
  377. }
  378. else
  379. {
  380. args.push_back(av[i]);
  381. }
  382. }
  383. if(command)
  384. {
  385. int ret = cmake::ExecuteCMakeCommand(args);
  386. return ret;
  387. }
  388. if (wiz)
  389. {
  390. cmakewizard wizard;
  391. return wizard.RunWizard(args);
  392. }
  393. if (sysinfo)
  394. {
  395. cmake cm;
  396. int ret = cm.GetSystemInformation(args);
  397. return ret;
  398. }
  399. cmake cm;
  400. cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
  401. cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
  402. cm.SetScriptMode(script_mode);
  403. int res = cm.Run(args, view_only);
  404. if ( list_cached || list_all_cached )
  405. {
  406. cmCacheManager::CacheIterator it =
  407. cm.GetCacheManager()->GetCacheIterator();
  408. std::cout << "-- Cache values" << std::endl;
  409. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  410. {
  411. cmCacheManager::CacheEntryType t = it.GetType();
  412. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  413. t != cmCacheManager::UNINITIALIZED )
  414. {
  415. bool advanced = it.PropertyExists("ADVANCED");
  416. if ( list_all_cached || !advanced)
  417. {
  418. if ( list_help )
  419. {
  420. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  421. }
  422. std::cout << it.GetName() << ":" <<
  423. cmCacheManager::TypeToString(it.GetType())
  424. << "=" << it.GetValue() << std::endl;
  425. if ( list_help )
  426. {
  427. std::cout << std::endl;
  428. }
  429. }
  430. }
  431. }
  432. }
  433. return res;
  434. }