PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/cmakemain.cxx

https://github.com/AlexeyS/cmake
C++ | 523 lines | 456 code | 31 blank | 36 comment | 81 complexity | 2978ac461e6c45427fc9ac5846c94227 MD5 | raw file
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile: cmakemain.cxx,v $
  4. Language: C++
  5. Date: $Date: 2008-09-03 13:43:18 $
  6. Version: $Revision: 1.80.2.3 $
  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. {"--trace", "Put cmake in trace mode.",
  96. "Print a trace of all calls made and from where with "
  97. "message(send_error ) calls."},
  98. {"--help-command cmd [file]", "Print help for a single command and exit.",
  99. "Full documentation specific to the given command is displayed. "
  100. "If a file is specified, the documentation is written into and the output "
  101. "format is determined depending on the filename suffix. Supported are man "
  102. "page, HTML, DocBook and plain text."},
  103. {"--help-command-list [file]", "List available listfile commands and exit.",
  104. "The list contains all commands for which help may be obtained by using "
  105. "the --help-command argument followed by a command name. "
  106. "If a file is specified, the documentation is written into and the output "
  107. "format is determined depending on the filename suffix. Supported are man "
  108. "page, HTML, DocBook and plain text."},
  109. {"--help-commands [file]", "Print help for all commands and exit.",
  110. "Full documentation specific for all current command is displayed."
  111. "If a file is specified, the documentation is written into and the output "
  112. "format is determined depending on the filename suffix. Supported are man "
  113. "page, HTML, DocBook and plain text."},
  114. {"--help-compatcommands [file]", "Print help for compatibility commands. ",
  115. "Full documentation specific for all compatibility commands is displayed."
  116. "If a file is specified, the documentation is written into and the output "
  117. "format is determined depending on the filename suffix. Supported are man "
  118. "page, HTML, DocBook and plain text."},
  119. {"--help-module module [file]", "Print help for a single module and exit.",
  120. "Full documentation specific to the given module is displayed."
  121. "If a file is specified, the documentation is written into and the output "
  122. "format is determined depending on the filename suffix. Supported are man "
  123. "page, HTML, DocBook and plain text."},
  124. {"--help-module-list [file]", "List available modules and exit.",
  125. "The list contains all modules for which help may be obtained by using "
  126. "the --help-module argument followed by a module name. "
  127. "If a file is specified, the documentation is written into and the output "
  128. "format is determined depending on the filename suffix. Supported are man "
  129. "page, HTML, DocBook and plain text."},
  130. {"--help-modules [file]", "Print help for all modules and exit.",
  131. "Full documentation for all modules is displayed. "
  132. "If a file is specified, the documentation is written into and the output "
  133. "format is determined depending on the filename suffix. Supported are man "
  134. "page, HTML, DocBook and plain text."},
  135. {"--help-custom-modules [file]" , "Print help for all custom modules and "
  136. "exit.",
  137. "Full documentation for all custom modules is displayed. "
  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, DocBook and plain text."},
  141. {"--help-policy cmp [file]",
  142. "Print help for a single policy and exit.",
  143. "Full documentation specific to the given policy is displayed."
  144. "If a file is specified, the documentation is written into and the output "
  145. "format is determined depending on the filename suffix. Supported are man "
  146. "page, HTML, DocBook and plain text."},
  147. {"--help-policies [file]", "Print help for all policies and exit.",
  148. "Full documentation for all policies is displayed."
  149. "If a file is specified, the documentation is written into and the output "
  150. "format is determined depending on the filename suffix. Supported are man "
  151. "page, HTML, DocBook and plain text."},
  152. {"--help-property prop [file]",
  153. "Print help for a single property and exit.",
  154. "Full documentation specific to the given property is displayed."
  155. "If a file is specified, the documentation is written into and the output "
  156. "format is determined depending on the filename suffix. Supported are man "
  157. "page, HTML, DocBook and plain text."},
  158. {"--help-property-list [file]", "List available properties and exit.",
  159. "The list contains all properties for which help may be obtained by using "
  160. "the --help-property argument followed by a property name. If a file is "
  161. "specified, the help is written into it."
  162. "If a file is specified, the documentation is written into and the output "
  163. "format is determined depending on the filename suffix. Supported are man "
  164. "page, HTML, DocBook and plain text."},
  165. {"--help-properties [file]", "Print help for all properties and exit.",
  166. "Full documentation for all properties is displayed."
  167. "If a file is specified, the documentation is written into and the output "
  168. "format is determined depending on the filename suffix. Supported are man "
  169. "page, HTML, DocBook and plain text."},
  170. {"--help-variable var [file]",
  171. "Print help for a single variable and exit.",
  172. "Full documentation specific to the given variable is displayed."
  173. "If a file is specified, the documentation is written into and the output "
  174. "format is determined depending on the filename suffix. Supported are man "
  175. "page, HTML, DocBook and plain text."},
  176. {"--help-variable-list [file]", "List documented variables and exit.",
  177. "The list contains all variables for which help may be obtained by using "
  178. "the --help-variable argument followed by a variable name. If a file is "
  179. "specified, the help is written into it."
  180. "If a file is specified, the documentation is written into and the output "
  181. "format is determined depending on the filename suffix. Supported are man "
  182. "page, HTML, DocBook and plain text."},
  183. {"--help-variables [file]", "Print help for all variables and exit.",
  184. "Full documentation for all variables is displayed."
  185. "If a file is specified, the documentation is written into and the output "
  186. "format is determined depending on the filename suffix. Supported are man "
  187. "page, HTML, DocBook and plain text."},
  188. {0,0,0}
  189. };
  190. //----------------------------------------------------------------------------
  191. static const char * cmDocumentationSeeAlso[][3] =
  192. {
  193. {0, "ccmake", 0},
  194. {0, "cpack", 0},
  195. {0, "ctest", 0},
  196. {0, "cmakecommands", 0},
  197. {0, "cmakecompat", 0},
  198. {0, "cmakemodules", 0},
  199. {0, "cmakeprops", 0},
  200. {0, "cmakevars", 0},
  201. {0, 0, 0}
  202. };
  203. //----------------------------------------------------------------------------
  204. static const char * cmDocumentationNOTE[][3] =
  205. {
  206. {0,
  207. "CMake no longer configures a project when run with no arguments. "
  208. "In order to configure the project in the current directory, run\n"
  209. " cmake .", 0},
  210. {0,0,0}
  211. };
  212. #endif
  213. int do_cmake(int ac, char** av);
  214. static cmMakefile* cmakemainGetMakefile(void *clientdata)
  215. {
  216. cmake* cm = (cmake *)clientdata;
  217. if(cm && cm->GetDebugOutput())
  218. {
  219. cmGlobalGenerator* gg=cm->GetGlobalGenerator();
  220. if (gg)
  221. {
  222. cmLocalGenerator* lg=gg->GetCurrentLocalGenerator();
  223. if (lg)
  224. {
  225. cmMakefile* mf = lg->GetMakefile();
  226. return mf;
  227. }
  228. }
  229. }
  230. return 0;
  231. }
  232. static std::string cmakemainGetStack(void *clientdata)
  233. {
  234. std::string msg;
  235. cmMakefile* mf=cmakemainGetMakefile(clientdata);
  236. if (mf)
  237. {
  238. msg = mf->GetListFileStack();
  239. if (!msg.empty())
  240. {
  241. msg = "\n Called from: " + msg;
  242. }
  243. }
  244. return msg;
  245. }
  246. static void cmakemainErrorCallback(const char* m, const char*, bool&,
  247. void *clientdata)
  248. {
  249. std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
  250. }
  251. static void cmakemainProgressCallback(const char *m, float prog,
  252. void* clientdata)
  253. {
  254. cmMakefile* mf = cmakemainGetMakefile(clientdata);
  255. std::string dir;
  256. if ((mf) && (strstr(m, "Configuring")==m) && (prog<0))
  257. {
  258. dir = " ";
  259. dir += mf->GetCurrentDirectory();
  260. }
  261. else if ((mf) && (strstr(m, "Generating")==m))
  262. {
  263. dir = " ";
  264. dir += mf->GetCurrentOutputDirectory();
  265. }
  266. if ((prog < 0) || (!dir.empty()))
  267. {
  268. std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)<<std::endl;
  269. }
  270. std::cout.flush();
  271. }
  272. int main(int ac, char** av)
  273. {
  274. cmSystemTools::EnableMSVCDebugHook();
  275. cmSystemTools::FindExecutableDirectory(av[0]);
  276. int ret = do_cmake(ac, av);
  277. #ifdef CMAKE_BUILD_WITH_CMAKE
  278. cmDynamicLoader::FlushCache();
  279. #endif
  280. return ret;
  281. }
  282. int do_cmake(int ac, char** av)
  283. {
  284. int nocwd = 0;
  285. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  286. {
  287. std::cerr << "Current working directory cannot be established."
  288. << std::endl;
  289. nocwd = 1;
  290. }
  291. #ifdef CMAKE_BUILD_WITH_CMAKE
  292. cmDocumentation doc;
  293. if(doc.CheckOptions(ac, av, "-E") || nocwd)
  294. {
  295. // Construct and print requested documentation.
  296. cmake hcm;
  297. hcm.AddCMakePaths();
  298. doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
  299. // the command line args are processed here so that you can do
  300. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  301. std::vector<std::string> args;
  302. for(int i =0; i < ac; ++i)
  303. {
  304. args.push_back(av[i]);
  305. }
  306. hcm.SetCacheArgs(args);
  307. const char* modulePath = hcm.GetCacheDefinition("CMAKE_MODULE_PATH");
  308. if (modulePath)
  309. {
  310. doc.SetCMakeModulePath(modulePath);
  311. }
  312. std::vector<cmDocumentationEntry> commands;
  313. std::vector<cmDocumentationEntry> policies;
  314. std::vector<cmDocumentationEntry> compatCommands;
  315. std::vector<cmDocumentationEntry> generators;
  316. std::map<std::string,cmDocumentationSection *> propDocs;
  317. hcm.GetPolicyDocumentation(policies);
  318. hcm.GetCommandDocumentation(commands, true, false);
  319. hcm.GetCommandDocumentation(compatCommands, false, true);
  320. hcm.GetPropertiesDocumentation(propDocs);
  321. hcm.GetGeneratorDocumentation(generators);
  322. doc.SetName("cmake");
  323. doc.SetSection("Name",cmDocumentationName);
  324. doc.SetSection("Usage",cmDocumentationUsage);
  325. doc.SetSection("Description",cmDocumentationDescription);
  326. doc.AppendSection("Generators",generators);
  327. doc.PrependSection("Options",cmDocumentationOptions);
  328. doc.SetSection("Commands",commands);
  329. doc.SetSection("Policies",policies);
  330. doc.AppendSection("Compatibility Commands",compatCommands);
  331. doc.SetSections(propDocs);
  332. cmDocumentationEntry e;
  333. e.Brief =
  334. "variables defined by cmake, that give information about the project, "
  335. "and cmake";
  336. doc.PrependSection("Variables that Provide Information",e);
  337. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  338. int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
  339. // If we were run with no arguments, but a CMakeLists.txt file
  340. // exists, the user may have been trying to use the old behavior
  341. // of cmake to build a project in-source. Print a message
  342. // explaining the change to standard error and return an error
  343. // condition in case the program is running from a script.
  344. if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
  345. {
  346. doc.ClearSections();
  347. doc.SetSection("NOTE", cmDocumentationNOTE);
  348. doc.Print(cmDocumentation::UsageForm, std::cerr);
  349. return 1;
  350. }
  351. return result;
  352. }
  353. #else
  354. if ( nocwd || ac == 1 )
  355. {
  356. std::cout <<
  357. "Bootstrap CMake should not be used outside CMake build process."
  358. << std::endl;
  359. return 0;
  360. }
  361. #endif
  362. bool wiz = false;
  363. bool sysinfo = false;
  364. bool command = false;
  365. bool list_cached = false;
  366. bool list_all_cached = false;
  367. bool list_help = false;
  368. bool view_only = false;
  369. bool script_mode = false;
  370. std::vector<std::string> args;
  371. for(int i =0; i < ac; ++i)
  372. {
  373. if(strcmp(av[i], "-i") == 0)
  374. {
  375. wiz = true;
  376. }
  377. else if(!command && strcmp(av[i], "--system-information") == 0)
  378. {
  379. sysinfo = true;
  380. }
  381. // if command has already been set, then
  382. // do not eat the -E
  383. else if (!command && strcmp(av[i], "-E") == 0)
  384. {
  385. command = true;
  386. }
  387. else if (!command && strcmp(av[i], "-N") == 0)
  388. {
  389. view_only = true;
  390. }
  391. else if (!command && strcmp(av[i], "-L") == 0)
  392. {
  393. list_cached = true;
  394. }
  395. else if (!command && strcmp(av[i], "-LA") == 0)
  396. {
  397. list_all_cached = true;
  398. }
  399. else if (!command && strcmp(av[i], "-LH") == 0)
  400. {
  401. list_cached = true;
  402. list_help = true;
  403. }
  404. else if (!command && strcmp(av[i], "-LAH") == 0)
  405. {
  406. list_all_cached = true;
  407. list_help = true;
  408. }
  409. else if (!command && strncmp(av[i], "-P", strlen("-P")) == 0)
  410. {
  411. if ( i == ac -1 )
  412. {
  413. cmSystemTools::Error("No script specified for argument -P");
  414. }
  415. else
  416. {
  417. script_mode = true;
  418. args.push_back(av[i]);
  419. i++;
  420. args.push_back(av[i]);
  421. }
  422. }
  423. else
  424. {
  425. args.push_back(av[i]);
  426. }
  427. }
  428. if(command)
  429. {
  430. int ret = cmake::ExecuteCMakeCommand(args);
  431. return ret;
  432. }
  433. if (wiz)
  434. {
  435. cmakewizard wizard;
  436. return wizard.RunWizard(args);
  437. }
  438. if (sysinfo)
  439. {
  440. cmake cm;
  441. int ret = cm.GetSystemInformation(args);
  442. return ret;
  443. }
  444. cmake cm;
  445. cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
  446. cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
  447. cm.SetScriptMode(script_mode);
  448. int res = cm.Run(args, view_only);
  449. if ( list_cached || list_all_cached )
  450. {
  451. cmCacheManager::CacheIterator it =
  452. cm.GetCacheManager()->GetCacheIterator();
  453. std::cout << "-- Cache values" << std::endl;
  454. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  455. {
  456. cmCacheManager::CacheEntryType t = it.GetType();
  457. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  458. t != cmCacheManager::UNINITIALIZED )
  459. {
  460. bool advanced = it.PropertyExists("ADVANCED");
  461. if ( list_all_cached || !advanced)
  462. {
  463. if ( list_help )
  464. {
  465. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  466. }
  467. std::cout << it.GetName() << ":" <<
  468. cmCacheManager::TypeToString(it.GetType())
  469. << "=" << it.GetValue() << std::endl;
  470. if ( list_help )
  471. {
  472. std::cout << std::endl;
  473. }
  474. }
  475. }
  476. }
  477. }
  478. // Always return a non-negative value. Windows tools do not always
  479. // interpret negative return values as errors.
  480. if(res != 0)
  481. {
  482. return 1;
  483. }
  484. else
  485. {
  486. return 0;
  487. }
  488. }