cmakemain.cxx 17 KB

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