cmakemain.cxx 16 KB

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