cmakemain.cxx 12 KB

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