cmakemain.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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,
  199. bool& nomore, void *clientdata)
  200. {
  201. std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
  202. }
  203. static void cmakemainProgressCallback(const char *m, float prog,
  204. void* clientdata)
  205. {
  206. cmMakefile* mf = cmakemainGetMakefile(clientdata);
  207. std::string dir;
  208. if ((mf) && (strstr(m, "Configuring")==m))
  209. {
  210. dir = " ";
  211. dir += mf->GetCurrentDirectory();
  212. }
  213. else if ((mf) && (strstr(m, "Generating")==m))
  214. {
  215. dir = " ";
  216. dir += mf->GetCurrentOutputDirectory();
  217. }
  218. if ((prog < 0) || (!dir.empty()))
  219. {
  220. std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)<<std::endl;
  221. }
  222. std::cout.flush();
  223. }
  224. int main(int ac, char** av)
  225. {
  226. cmSystemTools::EnableMSVCDebugHook();
  227. int ret = do_cmake(ac, av);
  228. #ifdef CMAKE_BUILD_WITH_CMAKE
  229. cmDynamicLoader::FlushCache();
  230. #endif
  231. return ret;
  232. }
  233. int do_cmake(int ac, char** av)
  234. {
  235. #ifdef CMAKE_BUILD_WITH_CMAKE
  236. cmDocumentation doc;
  237. #endif
  238. int nocwd = 0;
  239. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  240. {
  241. std::cerr << "Current working directory cannot be established."
  242. << std::endl;
  243. nocwd = 1;
  244. }
  245. #ifdef CMAKE_BUILD_WITH_CMAKE
  246. if(doc.CheckOptions(ac, av) || nocwd)
  247. {
  248. // Construct and print requested documentation.
  249. cmake hcm;
  250. hcm.AddCMakePaths(av[0]);
  251. doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
  252. std::vector<cmDocumentationEntry> commands;
  253. std::vector<cmDocumentationEntry> compatCommands;
  254. std::vector<cmDocumentationEntry> globalProperties;
  255. std::vector<cmDocumentationEntry> dirProperties;
  256. std::vector<cmDocumentationEntry> targetProperties;
  257. std::vector<cmDocumentationEntry> testProperties;
  258. std::vector<cmDocumentationEntry> sourceFileProperties;
  259. std::vector<cmDocumentationEntry> variableProperties;
  260. std::vector<cmDocumentationEntry> cachedVariableProperties;
  261. std::vector<cmDocumentationEntry> generators;
  262. hcm.GetCommandDocumentation(commands, true, false);
  263. hcm.GetCommandDocumentation(compatCommands, false, true);
  264. hcm.GetPropertiesDocumentation(globalProperties, cmProperty::GLOBAL);
  265. hcm.GetPropertiesDocumentation(dirProperties, cmProperty::DIRECTORY);
  266. hcm.GetPropertiesDocumentation(targetProperties, cmProperty::TARGET);
  267. hcm.GetPropertiesDocumentation(testProperties, cmProperty::TEST);
  268. hcm.GetPropertiesDocumentation(sourceFileProperties,
  269. cmProperty::SOURCE_FILE);
  270. hcm.GetPropertiesDocumentation(variableProperties, cmProperty::VARIABLE);
  271. hcm.GetPropertiesDocumentation(cachedVariableProperties,
  272. cmProperty::CACHED_VARIABLE);
  273. hcm.GetGeneratorDocumentation(generators);
  274. doc.SetName("cmake");
  275. doc.SetNameSection(cmDocumentationName);
  276. doc.SetUsageSection(cmDocumentationUsage);
  277. doc.SetDescriptionSection(cmDocumentationDescription);
  278. doc.SetGeneratorsSection(&generators[0]);
  279. doc.SetOptionsSection(cmDocumentationOptions);
  280. doc.SetCommandsSection(&commands[0]);
  281. doc.SetCompatCommandsSection(&compatCommands[0]);
  282. doc.SetPropertiesSection(&globalProperties[0], cmProperty::GLOBAL);
  283. doc.SetPropertiesSection(&dirProperties[0], cmProperty::DIRECTORY);
  284. doc.SetPropertiesSection(&targetProperties[0], cmProperty::TARGET);
  285. doc.SetPropertiesSection(&testProperties[0], cmProperty::TEST);
  286. doc.SetPropertiesSection(&sourceFileProperties[0],cmProperty::SOURCE_FILE);
  287. doc.SetPropertiesSection(&variableProperties[0],cmProperty::VARIABLE);
  288. doc.SetPropertiesSection(&cachedVariableProperties[0],
  289. cmProperty::CACHED_VARIABLE);
  290. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  291. int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
  292. // If we were run with no arguments, but a CMakeLists.txt file
  293. // exists, the user may have been trying to use the old behavior
  294. // of cmake to build a project in-source. Print a message
  295. // explaining the change to standard error and return an error
  296. // condition in case the program is running from a script.
  297. if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
  298. {
  299. doc.ClearSections();
  300. doc.AddSection("NOTE", cmDocumentationNOTE);
  301. doc.Print(cmDocumentation::UsageForm, std::cerr);
  302. return 1;
  303. }
  304. return result;
  305. }
  306. #else
  307. if ( nocwd || ac == 1 )
  308. {
  309. std::cout <<
  310. "Bootstrap CMake should not be used outside CMake build process."
  311. << std::endl;
  312. return 0;
  313. }
  314. #endif
  315. bool wiz = false;
  316. bool sysinfo = false;
  317. bool command = false;
  318. bool list_cached = false;
  319. bool list_all_cached = false;
  320. bool list_help = false;
  321. bool view_only = false;
  322. bool script_mode = false;
  323. std::vector<std::string> args;
  324. for(int i =0; i < ac; ++i)
  325. {
  326. if(strcmp(av[i], "-i") == 0)
  327. {
  328. wiz = true;
  329. }
  330. else if(!command && strcmp(av[i], "--system-information") == 0)
  331. {
  332. sysinfo = true;
  333. }
  334. // if command has already been set, then
  335. // do not eat the -E
  336. else if (!command && strcmp(av[i], "-E") == 0)
  337. {
  338. command = true;
  339. }
  340. else if (strcmp(av[i], "-N") == 0)
  341. {
  342. view_only = true;
  343. }
  344. else if (strcmp(av[i], "-L") == 0)
  345. {
  346. list_cached = true;
  347. }
  348. else if (strcmp(av[i], "-LA") == 0)
  349. {
  350. list_all_cached = true;
  351. }
  352. else if (strcmp(av[i], "-LH") == 0)
  353. {
  354. list_cached = true;
  355. list_help = true;
  356. }
  357. else if (strcmp(av[i], "-LAH") == 0)
  358. {
  359. list_all_cached = true;
  360. list_help = true;
  361. }
  362. else if (strncmp(av[i], "-P", strlen("-P")) == 0)
  363. {
  364. if ( i == ac -1 )
  365. {
  366. cmSystemTools::Error("No script specified for argument -P");
  367. }
  368. else
  369. {
  370. script_mode = true;
  371. args.push_back(av[i]);
  372. i++;
  373. args.push_back(av[i]);
  374. }
  375. }
  376. else
  377. {
  378. args.push_back(av[i]);
  379. }
  380. }
  381. if(command)
  382. {
  383. int ret = cmake::ExecuteCMakeCommand(args);
  384. return ret;
  385. }
  386. if (wiz)
  387. {
  388. cmakewizard wizard;
  389. return wizard.RunWizard(args);
  390. }
  391. if (sysinfo)
  392. {
  393. cmake cm;
  394. int ret = cm.GetSystemInformation(args);
  395. return ret;
  396. }
  397. cmake cm;
  398. cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
  399. cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
  400. cm.SetScriptMode(script_mode);
  401. int res = cm.Run(args, view_only);
  402. if ( list_cached || list_all_cached )
  403. {
  404. cmCacheManager::CacheIterator it =
  405. cm.GetCacheManager()->GetCacheIterator();
  406. std::cout << "-- Cache values" << std::endl;
  407. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  408. {
  409. cmCacheManager::CacheEntryType t = it.GetType();
  410. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  411. t != cmCacheManager::UNINITIALIZED )
  412. {
  413. bool advanced = it.PropertyExists("ADVANCED");
  414. if ( list_all_cached || !advanced)
  415. {
  416. if ( list_help )
  417. {
  418. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  419. }
  420. std::cout << it.GetName() << ":" <<
  421. cmCacheManager::TypeToString(it.GetType())
  422. << "=" << it.GetValue() << std::endl;
  423. if ( list_help )
  424. {
  425. std::cout << std::endl;
  426. }
  427. }
  428. }
  429. }
  430. }
  431. return res;
  432. }