You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

502 lines
17 KiB

  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. int ret = do_cmake(ac, av);
  256. #ifdef CMAKE_BUILD_WITH_CMAKE
  257. cmDynamicLoader::FlushCache();
  258. #endif
  259. return ret;
  260. }
  261. int do_cmake(int ac, char** av)
  262. {
  263. #ifdef CMAKE_BUILD_WITH_CMAKE
  264. cmDocumentation doc;
  265. #endif
  266. int nocwd = 0;
  267. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  268. {
  269. std::cerr << "Current working directory cannot be established."
  270. << std::endl;
  271. nocwd = 1;
  272. }
  273. #ifdef CMAKE_BUILD_WITH_CMAKE
  274. if(doc.CheckOptions(ac, av) || nocwd)
  275. {
  276. // Construct and print requested documentation.
  277. cmake hcm;
  278. hcm.AddCMakePaths(av[0]);
  279. doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
  280. // the command line args are processed here so that you can do
  281. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  282. std::vector<std::string> args;
  283. for(int i =0; i < ac; ++i)
  284. {
  285. args.push_back(av[i]);
  286. }
  287. hcm.SetCacheArgs(args);
  288. const char* modulePath = hcm.GetCacheDefinition("CMAKE_MODULE_PATH");
  289. if (modulePath)
  290. {
  291. doc.SetCMakeModulePath(modulePath);
  292. }
  293. std::vector<cmDocumentationEntry> commands;
  294. std::vector<cmDocumentationEntry> compatCommands;
  295. std::vector<cmDocumentationEntry> generators;
  296. std::map<std::string,cmDocumentationSection *> propDocs;
  297. hcm.GetCommandDocumentation(commands, true, false);
  298. hcm.GetCommandDocumentation(compatCommands, false, true);
  299. hcm.GetPropertiesDocumentation(propDocs);
  300. hcm.GetGeneratorDocumentation(generators);
  301. doc.SetName("cmake");
  302. doc.SetSection("Name",cmDocumentationName);
  303. doc.SetSection("Usage",cmDocumentationUsage);
  304. doc.SetSection("Description",cmDocumentationDescription);
  305. doc.AppendSection("Generators",generators);
  306. doc.PrependSection("Options",cmDocumentationOptions);
  307. doc.SetSection("Commands",commands);
  308. doc.AppendSection("Compatibility Commands",compatCommands);
  309. doc.SetSections(propDocs);
  310. cmDocumentationEntry e;
  311. e.Brief =
  312. "variables defined by cmake, that give information about the project, "
  313. "and cmake";
  314. doc.PrependSection("Variables that Provide Information",e);
  315. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  316. int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
  317. // If we were run with no arguments, but a CMakeLists.txt file
  318. // exists, the user may have been trying to use the old behavior
  319. // of cmake to build a project in-source. Print a message
  320. // explaining the change to standard error and return an error
  321. // condition in case the program is running from a script.
  322. if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
  323. {
  324. doc.ClearSections();
  325. doc.SetSection("NOTE", cmDocumentationNOTE);
  326. doc.Print(cmDocumentation::UsageForm, std::cerr);
  327. return 1;
  328. }
  329. return result;
  330. }
  331. #else
  332. if ( nocwd || ac == 1 )
  333. {
  334. std::cout <<
  335. "Bootstrap CMake should not be used outside CMake build process."
  336. << std::endl;
  337. return 0;
  338. }
  339. #endif
  340. bool wiz = false;
  341. bool sysinfo = false;
  342. bool command = false;
  343. bool list_cached = false;
  344. bool list_all_cached = false;
  345. bool list_help = false;
  346. bool view_only = false;
  347. bool script_mode = false;
  348. std::vector<std::string> args;
  349. for(int i =0; i < ac; ++i)
  350. {
  351. if(strcmp(av[i], "-i") == 0)
  352. {
  353. wiz = true;
  354. }
  355. else if(!command && strcmp(av[i], "--system-information") == 0)
  356. {
  357. sysinfo = true;
  358. }
  359. // if command has already been set, then
  360. // do not eat the -E
  361. else if (!command && strcmp(av[i], "-E") == 0)
  362. {
  363. command = true;
  364. }
  365. else if (strcmp(av[i], "-N") == 0)
  366. {
  367. view_only = true;
  368. }
  369. else if (strcmp(av[i], "-L") == 0)
  370. {
  371. list_cached = true;
  372. }
  373. else if (strcmp(av[i], "-LA") == 0)
  374. {
  375. list_all_cached = true;
  376. }
  377. else if (strcmp(av[i], "-LH") == 0)
  378. {
  379. list_cached = true;
  380. list_help = true;
  381. }
  382. else if (strcmp(av[i], "-LAH") == 0)
  383. {
  384. list_all_cached = true;
  385. list_help = true;
  386. }
  387. else if (strncmp(av[i], "-P", strlen("-P")) == 0)
  388. {
  389. if ( i == ac -1 )
  390. {
  391. cmSystemTools::Error("No script specified for argument -P");
  392. }
  393. else
  394. {
  395. script_mode = true;
  396. args.push_back(av[i]);
  397. i++;
  398. args.push_back(av[i]);
  399. }
  400. }
  401. else
  402. {
  403. args.push_back(av[i]);
  404. }
  405. }
  406. if(command)
  407. {
  408. int ret = cmake::ExecuteCMakeCommand(args);
  409. return ret;
  410. }
  411. if (wiz)
  412. {
  413. cmakewizard wizard;
  414. return wizard.RunWizard(args);
  415. }
  416. if (sysinfo)
  417. {
  418. cmake cm;
  419. int ret = cm.GetSystemInformation(args);
  420. return ret;
  421. }
  422. cmake cm;
  423. cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
  424. cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
  425. cm.SetScriptMode(script_mode);
  426. int res = cm.Run(args, view_only);
  427. if ( list_cached || list_all_cached )
  428. {
  429. cmCacheManager::CacheIterator it =
  430. cm.GetCacheManager()->GetCacheIterator();
  431. std::cout << "-- Cache values" << std::endl;
  432. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  433. {
  434. cmCacheManager::CacheEntryType t = it.GetType();
  435. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  436. t != cmCacheManager::UNINITIALIZED )
  437. {
  438. bool advanced = it.PropertyExists("ADVANCED");
  439. if ( list_all_cached || !advanced)
  440. {
  441. if ( list_help )
  442. {
  443. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  444. }
  445. std::cout << it.GetName() << ":" <<
  446. cmCacheManager::TypeToString(it.GetType())
  447. << "=" << it.GetValue() << std::endl;
  448. if ( list_help )
  449. {
  450. std::cout << std::endl;
  451. }
  452. }
  453. }
  454. }
  455. }
  456. // Always return a non-negative value. Windows tools do not always
  457. // interpret negative return values as errors.
  458. if(res != 0)
  459. {
  460. return 1;
  461. }
  462. else
  463. {
  464. return 0;
  465. }
  466. }