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.

398 lines
12 KiB

10 years ago
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. // include these first, otherwise there will be problems on Windows
  11. // with GetCurrentDirectory() being redefined
  12. #ifdef CMAKE_BUILD_WITH_CMAKE
  13. #include "cmDocumentation.h"
  14. #include "cmDynamicLoader.h"
  15. #endif
  16. #include "cmAlgorithms.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmListFileCache.h"
  19. #include "cmLocalGenerator.h"
  20. #include "cmMakefile.h"
  21. #include "cmSourceFile.h"
  22. #include "cmState.h"
  23. #include "cmake.h"
  24. #include "cmcmd.h"
  25. #include <cmsys/Encoding.hxx>
  26. #ifdef CMAKE_BUILD_WITH_CMAKE
  27. static const char* cmDocumentationName[][2] = {
  28. { 0, " cmake - Cross-Platform Makefile Generator." },
  29. { 0, 0 }
  30. };
  31. static const char* cmDocumentationUsage[][2] = {
  32. { 0, " cmake [options] <path-to-source>\n"
  33. " cmake [options] <path-to-existing-build>" },
  34. { 0, "Specify a source directory to (re-)generate a build system for "
  35. "it in the current working directory. Specify an existing build "
  36. "directory to re-generate its build system." },
  37. { 0, 0 }
  38. };
  39. static const char* cmDocumentationUsageNote[][2] = {
  40. { 0, "Run 'cmake --help' for more information." },
  41. { 0, 0 }
  42. };
  43. #define CMAKE_BUILD_OPTIONS \
  44. " <dir> = Project binary directory to be built.\n" \
  45. " --target <tgt> = Build <tgt> instead of default targets.\n" \
  46. " May only be specified once.\n" \
  47. " --config <cfg> = For multi-configuration tools, choose <cfg>.\n" \
  48. " --clean-first = Build target 'clean' first, then build.\n" \
  49. " (To clean only, use --target 'clean'.)\n" \
  50. " --use-stderr = Ignored. Behavior is default in CMake >= 3.0.\n" \
  51. " -- = Pass remaining options to the native tool.\n"
  52. static const char* cmDocumentationOptions[][2] = {
  53. CMAKE_STANDARD_OPTIONS_TABLE,
  54. { "-E", "CMake command mode." },
  55. { "-L[A][H]", "List non-advanced cached variables." },
  56. { "--build <dir>", "Build a CMake-generated project binary tree." },
  57. { "-N", "View mode only." },
  58. { "-P <file>", "Process script mode." },
  59. { "--find-package", "Run in pkg-config like mode." },
  60. { "--graphviz=[file]", "Generate graphviz of dependencies, see "
  61. "CMakeGraphVizOptions.cmake for more." },
  62. { "--system-information [file]", "Dump information about this system." },
  63. { "--debug-trycompile", "Do not delete the try_compile build tree. Only "
  64. "useful on one try_compile at a time." },
  65. { "--debug-output", "Put cmake in a debug mode." },
  66. { "--trace", "Put cmake in trace mode." },
  67. { "--trace-expand", "Put cmake in trace mode with variable expansion." },
  68. { "--trace-source=<file>",
  69. "Trace only this CMake file/module. Multiple options allowed." },
  70. { "--warn-uninitialized", "Warn about uninitialized values." },
  71. { "--warn-unused-vars", "Warn about unused variables." },
  72. { "--no-warn-unused-cli", "Don't warn about command line options." },
  73. { "--check-system-vars", "Find problems with variable usage in system "
  74. "files." },
  75. { 0, 0 }
  76. };
  77. #endif
  78. static int do_command(int ac, char const* const* av)
  79. {
  80. std::vector<std::string> args;
  81. args.reserve(ac - 1);
  82. args.push_back(av[0]);
  83. args.insert(args.end(), av + 2, av + ac);
  84. return cmcmd::ExecuteCMakeCommand(args);
  85. }
  86. int do_cmake(int ac, char const* const* av);
  87. static int do_build(int ac, char const* const* av);
  88. static cmMakefile* cmakemainGetMakefile(void* clientdata)
  89. {
  90. cmake* cm = (cmake*)clientdata;
  91. if (cm && cm->GetDebugOutput()) {
  92. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  93. if (gg) {
  94. return gg->GetCurrentMakefile();
  95. }
  96. }
  97. return 0;
  98. }
  99. static std::string cmakemainGetStack(void* clientdata)
  100. {
  101. std::string msg;
  102. cmMakefile* mf = cmakemainGetMakefile(clientdata);
  103. if (mf) {
  104. msg = mf->FormatListFileStack();
  105. if (!msg.empty()) {
  106. msg = "\n Called from: " + msg;
  107. }
  108. }
  109. return msg;
  110. }
  111. static void cmakemainMessageCallback(const char* m, const char*, bool&,
  112. void* clientdata)
  113. {
  114. std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
  115. }
  116. static void cmakemainProgressCallback(const char* m, float prog,
  117. void* clientdata)
  118. {
  119. cmMakefile* mf = cmakemainGetMakefile(clientdata);
  120. std::string dir;
  121. if ((mf) && (strstr(m, "Configuring") == m) && (prog < 0)) {
  122. dir = " ";
  123. dir += mf->GetCurrentSourceDirectory();
  124. } else if ((mf) && (strstr(m, "Generating") == m)) {
  125. dir = " ";
  126. dir += mf->GetCurrentBinaryDirectory();
  127. }
  128. if ((prog < 0) || (!dir.empty())) {
  129. std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)
  130. << std::endl;
  131. }
  132. std::cout.flush();
  133. }
  134. int main(int ac, char const* const* av)
  135. {
  136. cmsys::Encoding::CommandLineArguments args =
  137. cmsys::Encoding::CommandLineArguments::Main(ac, av);
  138. ac = args.argc();
  139. av = args.argv();
  140. cmSystemTools::EnableMSVCDebugHook();
  141. cmSystemTools::FindCMakeResources(av[0]);
  142. if (ac > 1) {
  143. if (strcmp(av[1], "--build") == 0) {
  144. return do_build(ac, av);
  145. } else if (strcmp(av[1], "-E") == 0) {
  146. return do_command(ac, av);
  147. }
  148. }
  149. int ret = do_cmake(ac, av);
  150. #ifdef CMAKE_BUILD_WITH_CMAKE
  151. cmDynamicLoader::FlushCache();
  152. #endif
  153. return ret;
  154. }
  155. int do_cmake(int ac, char const* const* av)
  156. {
  157. if (cmSystemTools::GetCurrentWorkingDirectory().empty()) {
  158. std::cerr << "Current working directory cannot be established."
  159. << std::endl;
  160. return 1;
  161. }
  162. #ifdef CMAKE_BUILD_WITH_CMAKE
  163. cmDocumentation doc;
  164. doc.addCMakeStandardDocSections();
  165. if (doc.CheckOptions(ac, av)) {
  166. // Construct and print requested documentation.
  167. cmake hcm;
  168. hcm.SetHomeDirectory("");
  169. hcm.SetHomeOutputDirectory("");
  170. hcm.AddCMakePaths();
  171. // the command line args are processed here so that you can do
  172. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  173. std::vector<std::string> args(av, av + ac);
  174. hcm.SetCacheArgs(args);
  175. std::vector<cmDocumentationEntry> generators;
  176. hcm.GetGeneratorDocumentation(generators);
  177. doc.SetName("cmake");
  178. doc.SetSection("Name", cmDocumentationName);
  179. doc.SetSection("Usage", cmDocumentationUsage);
  180. if (ac == 1) {
  181. doc.AppendSection("Usage", cmDocumentationUsageNote);
  182. }
  183. doc.AppendSection("Generators", generators);
  184. doc.PrependSection("Options", cmDocumentationOptions);
  185. return doc.PrintRequestedDocumentation(std::cout) ? 0 : 1;
  186. }
  187. #else
  188. if (ac == 1) {
  189. std::cout
  190. << "Bootstrap CMake should not be used outside CMake build process."
  191. << std::endl;
  192. return 0;
  193. }
  194. #endif
  195. bool sysinfo = false;
  196. bool list_cached = false;
  197. bool list_all_cached = false;
  198. bool list_help = false;
  199. bool view_only = false;
  200. cmake::WorkingMode workingMode = cmake::NORMAL_MODE;
  201. std::vector<std::string> args;
  202. for (int i = 0; i < ac; ++i) {
  203. if (strcmp(av[i], "-i") == 0) {
  204. /* clang-format off */
  205. std::cerr <<
  206. "The \"cmake -i\" wizard mode is no longer supported.\n"
  207. "Use the -D option to set cache values on the command line.\n"
  208. "Use cmake-gui or ccmake for an interactive dialog.\n";
  209. /* clang-format on */
  210. return 1;
  211. } else if (strcmp(av[i], "--system-information") == 0) {
  212. sysinfo = true;
  213. } else if (strcmp(av[i], "-N") == 0) {
  214. view_only = true;
  215. } else if (strcmp(av[i], "-L") == 0) {
  216. list_cached = true;
  217. } else if (strcmp(av[i], "-LA") == 0) {
  218. list_all_cached = true;
  219. } else if (strcmp(av[i], "-LH") == 0) {
  220. list_cached = true;
  221. list_help = true;
  222. } else if (strcmp(av[i], "-LAH") == 0) {
  223. list_all_cached = true;
  224. list_help = true;
  225. } else if (cmHasLiteralPrefix(av[i], "-P")) {
  226. if (i == ac - 1) {
  227. cmSystemTools::Error("No script specified for argument -P");
  228. } else {
  229. workingMode = cmake::SCRIPT_MODE;
  230. args.push_back(av[i]);
  231. i++;
  232. args.push_back(av[i]);
  233. }
  234. } else if (cmHasLiteralPrefix(av[i], "--find-package")) {
  235. workingMode = cmake::FIND_PACKAGE_MODE;
  236. args.push_back(av[i]);
  237. } else {
  238. args.push_back(av[i]);
  239. }
  240. }
  241. if (sysinfo) {
  242. cmake cm;
  243. cm.SetHomeDirectory("");
  244. cm.SetHomeOutputDirectory("");
  245. int ret = cm.GetSystemInformation(args);
  246. return ret;
  247. }
  248. cmake cm;
  249. cm.SetHomeDirectory("");
  250. cm.SetHomeOutputDirectory("");
  251. cmSystemTools::SetMessageCallback(cmakemainMessageCallback, (void*)&cm);
  252. cm.SetProgressCallback(cmakemainProgressCallback, (void*)&cm);
  253. cm.SetWorkingMode(workingMode);
  254. int res = cm.Run(args, view_only);
  255. if (list_cached || list_all_cached) {
  256. std::cout << "-- Cache values" << std::endl;
  257. std::vector<std::string> keys = cm.GetState()->GetCacheEntryKeys();
  258. for (std::vector<std::string>::const_iterator it = keys.begin();
  259. it != keys.end(); ++it) {
  260. cmState::CacheEntryType t = cm.GetState()->GetCacheEntryType(*it);
  261. if (t != cmState::INTERNAL && t != cmState::STATIC &&
  262. t != cmState::UNINITIALIZED) {
  263. const char* advancedProp =
  264. cm.GetState()->GetCacheEntryProperty(*it, "ADVANCED");
  265. if (list_all_cached || !advancedProp) {
  266. if (list_help) {
  267. std::cout << "// "
  268. << cm.GetState()->GetCacheEntryProperty(*it,
  269. "HELPSTRING")
  270. << std::endl;
  271. }
  272. std::cout << *it << ":" << cmState::CacheEntryTypeToString(t) << "="
  273. << cm.GetState()->GetCacheEntryValue(*it) << std::endl;
  274. if (list_help) {
  275. std::cout << std::endl;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. // Always return a non-negative value. Windows tools do not always
  282. // interpret negative return values as errors.
  283. if (res != 0) {
  284. return 1;
  285. } else {
  286. return 0;
  287. }
  288. }
  289. static int do_build(int ac, char const* const* av)
  290. {
  291. #ifndef CMAKE_BUILD_WITH_CMAKE
  292. std::cerr << "This cmake does not support --build\n";
  293. return -1;
  294. #else
  295. std::string target;
  296. std::string config = "Debug";
  297. std::string dir;
  298. std::vector<std::string> nativeOptions;
  299. bool clean = false;
  300. bool hasTarget = false;
  301. enum Doing
  302. {
  303. DoingNone,
  304. DoingDir,
  305. DoingTarget,
  306. DoingConfig,
  307. DoingNative
  308. };
  309. Doing doing = DoingDir;
  310. for (int i = 2; i < ac; ++i) {
  311. if (doing == DoingNative) {
  312. nativeOptions.push_back(av[i]);
  313. } else if (strcmp(av[i], "--target") == 0) {
  314. if (!hasTarget) {
  315. doing = DoingTarget;
  316. hasTarget = true;
  317. } else {
  318. std::cerr << "'--target' may not be specified more than once.\n\n";
  319. dir = "";
  320. break;
  321. }
  322. } else if (strcmp(av[i], "--config") == 0) {
  323. doing = DoingConfig;
  324. } else if (strcmp(av[i], "--clean-first") == 0) {
  325. clean = true;
  326. doing = DoingNone;
  327. } else if (strcmp(av[i], "--use-stderr") == 0) {
  328. /* tolerate legacy option */
  329. } else if (strcmp(av[i], "--") == 0) {
  330. doing = DoingNative;
  331. } else {
  332. switch (doing) {
  333. case DoingDir:
  334. dir = cmSystemTools::CollapseFullPath(av[i]);
  335. doing = DoingNone;
  336. break;
  337. case DoingTarget:
  338. target = av[i];
  339. doing = DoingNone;
  340. break;
  341. case DoingConfig:
  342. config = av[i];
  343. doing = DoingNone;
  344. break;
  345. default:
  346. std::cerr << "Unknown argument " << av[i] << std::endl;
  347. dir = "";
  348. break;
  349. }
  350. }
  351. }
  352. if (dir.empty()) {
  353. /* clang-format off */
  354. std::cerr <<
  355. "Usage: cmake --build <dir> [options] [-- [native-options]]\n"
  356. "Options:\n"
  357. CMAKE_BUILD_OPTIONS
  358. ;
  359. /* clang-format on */
  360. return 1;
  361. }
  362. cmake cm;
  363. return cm.Build(dir, target, config, nativeOptions, clean);
  364. #endif
  365. }