cmakemain.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 "cmConfigure.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmDocumentationEntry.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmState.h"
  9. #include "cmStateTypes.h"
  10. #include "cmSystemTools.h"
  11. #include "cmake.h"
  12. #include "cmcmd.h"
  13. #ifdef CMAKE_BUILD_WITH_CMAKE
  14. #include "cmDocumentation.h"
  15. #include "cmDynamicLoader.h"
  16. #ifdef _WIN32
  17. #include <fcntl.h> /* _O_TEXT */
  18. #include <stdlib.h> /* _set_fmode, _fmode */
  19. #endif
  20. #include "cm_uv.h"
  21. #endif
  22. #include "cmsys/Encoding.hxx"
  23. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  24. #include "cmsys/ConsoleBuf.hxx"
  25. #endif
  26. #include <iostream>
  27. #include <string.h>
  28. #include <string>
  29. #include <vector>
  30. #ifdef CMAKE_BUILD_WITH_CMAKE
  31. static const char* cmDocumentationName[][2] = {
  32. { nullptr, " cmake - Cross-Platform Makefile Generator." },
  33. { nullptr, nullptr }
  34. };
  35. static const char* cmDocumentationUsage[][2] = {
  36. { nullptr, " cmake [options] <path-to-source>\n"
  37. " cmake [options] <path-to-existing-build>" },
  38. { nullptr, "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. { nullptr, nullptr }
  42. };
  43. static const char* cmDocumentationUsageNote[][2] = {
  44. { nullptr, "Run 'cmake --help' for more information." },
  45. { nullptr, 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. { nullptr, 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 = reinterpret_cast<cmake*>(clientdata);
  95. if (cm && cm->GetDebugOutput()) {
  96. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  97. if (gg) {
  98. return gg->GetCurrentMakefile();
  99. }
  100. }
  101. return 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. consoleOut.SetUTF8Pipes();
  144. cmsys::ConsoleBuf::Manager consoleErr(std::cerr, true);
  145. consoleErr.SetUTF8Pipes();
  146. #endif
  147. cmsys::Encoding::CommandLineArguments args =
  148. cmsys::Encoding::CommandLineArguments::Main(ac, av);
  149. ac = args.argc();
  150. av = args.argv();
  151. #if defined(CMAKE_BUILD_WITH_CMAKE) && defined(_WIN32)
  152. // Perform libuv one-time initialization now, and then un-do its
  153. // global _fmode setting so that using libuv does not change the
  154. // default file text/binary mode. See libuv issue 840.
  155. uv_loop_close(uv_default_loop());
  156. #ifdef _MSC_VER
  157. _set_fmode(_O_TEXT);
  158. #else
  159. _fmode = _O_TEXT;
  160. #endif
  161. #endif
  162. cmSystemTools::EnableMSVCDebugHook();
  163. cmSystemTools::FindCMakeResources(av[0]);
  164. if (ac > 1) {
  165. if (strcmp(av[1], "--build") == 0) {
  166. return do_build(ac, av);
  167. }
  168. if (strcmp(av[1], "-E") == 0) {
  169. return do_command(ac, av);
  170. }
  171. }
  172. int ret = do_cmake(ac, av);
  173. #ifdef CMAKE_BUILD_WITH_CMAKE
  174. cmDynamicLoader::FlushCache();
  175. uv_loop_close(uv_default_loop());
  176. #endif
  177. return ret;
  178. }
  179. int do_cmake(int ac, char const* const* av)
  180. {
  181. if (cmSystemTools::GetCurrentWorkingDirectory().empty()) {
  182. std::cerr << "Current working directory cannot be established."
  183. << std::endl;
  184. return 1;
  185. }
  186. #ifdef CMAKE_BUILD_WITH_CMAKE
  187. cmDocumentation doc;
  188. doc.addCMakeStandardDocSections();
  189. if (doc.CheckOptions(ac, av)) {
  190. // Construct and print requested documentation.
  191. cmake hcm(cmake::RoleInternal);
  192. hcm.SetHomeDirectory("");
  193. hcm.SetHomeOutputDirectory("");
  194. hcm.AddCMakePaths();
  195. // the command line args are processed here so that you can do
  196. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  197. std::vector<std::string> args(av, av + ac);
  198. hcm.SetCacheArgs(args);
  199. std::vector<cmDocumentationEntry> generators;
  200. hcm.GetGeneratorDocumentation(generators);
  201. doc.SetName("cmake");
  202. doc.SetSection("Name", cmDocumentationName);
  203. doc.SetSection("Usage", cmDocumentationUsage);
  204. if (ac == 1) {
  205. doc.AppendSection("Usage", cmDocumentationUsageNote);
  206. }
  207. doc.AppendSection("Generators", generators);
  208. doc.PrependSection("Options", cmDocumentationOptions);
  209. return doc.PrintRequestedDocumentation(std::cout) ? 0 : 1;
  210. }
  211. #else
  212. if (ac == 1) {
  213. std::cout
  214. << "Bootstrap CMake should not be used outside CMake build process."
  215. << std::endl;
  216. return 0;
  217. }
  218. #endif
  219. bool sysinfo = false;
  220. bool list_cached = false;
  221. bool list_all_cached = false;
  222. bool list_help = false;
  223. bool view_only = false;
  224. cmake::WorkingMode workingMode = cmake::NORMAL_MODE;
  225. std::vector<std::string> args;
  226. for (int i = 0; i < ac; ++i) {
  227. if (strcmp(av[i], "-i") == 0) {
  228. /* clang-format off */
  229. std::cerr <<
  230. "The \"cmake -i\" wizard mode is no longer supported.\n"
  231. "Use the -D option to set cache values on the command line.\n"
  232. "Use cmake-gui or ccmake for an interactive dialog.\n";
  233. /* clang-format on */
  234. return 1;
  235. }
  236. if (strcmp(av[i], "--system-information") == 0) {
  237. sysinfo = true;
  238. } else if (strcmp(av[i], "-N") == 0) {
  239. view_only = true;
  240. } else if (strcmp(av[i], "-L") == 0) {
  241. list_cached = true;
  242. } else if (strcmp(av[i], "-LA") == 0) {
  243. list_all_cached = true;
  244. } else if (strcmp(av[i], "-LH") == 0) {
  245. list_cached = true;
  246. list_help = true;
  247. } else if (strcmp(av[i], "-LAH") == 0) {
  248. list_all_cached = true;
  249. list_help = true;
  250. } else if (cmHasLiteralPrefix(av[i], "-P")) {
  251. if (i == ac - 1) {
  252. cmSystemTools::Error("No script specified for argument -P");
  253. } else {
  254. workingMode = cmake::SCRIPT_MODE;
  255. args.push_back(av[i]);
  256. i++;
  257. args.push_back(av[i]);
  258. }
  259. } else if (cmHasLiteralPrefix(av[i], "--find-package")) {
  260. workingMode = cmake::FIND_PACKAGE_MODE;
  261. args.push_back(av[i]);
  262. } else {
  263. args.push_back(av[i]);
  264. }
  265. }
  266. if (sysinfo) {
  267. cmake cm(cmake::RoleProject);
  268. cm.SetHomeDirectory("");
  269. cm.SetHomeOutputDirectory("");
  270. int ret = cm.GetSystemInformation(args);
  271. return ret;
  272. }
  273. cmake::Role const role =
  274. workingMode == cmake::NORMAL_MODE ? cmake::RoleProject : cmake::RoleScript;
  275. cmake cm(role);
  276. cm.SetHomeDirectory("");
  277. cm.SetHomeOutputDirectory("");
  278. cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
  279. cm.SetProgressCallback(cmakemainProgressCallback, &cm);
  280. cm.SetWorkingMode(workingMode);
  281. int res = cm.Run(args, view_only);
  282. if (list_cached || list_all_cached) {
  283. std::cout << "-- Cache values" << std::endl;
  284. std::vector<std::string> keys = cm.GetState()->GetCacheEntryKeys();
  285. for (std::vector<std::string>::const_iterator it = keys.begin();
  286. it != keys.end(); ++it) {
  287. cmStateEnums::CacheEntryType t = cm.GetState()->GetCacheEntryType(*it);
  288. if (t != cmStateEnums::INTERNAL && t != cmStateEnums::STATIC &&
  289. t != cmStateEnums::UNINITIALIZED) {
  290. const char* advancedProp =
  291. cm.GetState()->GetCacheEntryProperty(*it, "ADVANCED");
  292. if (list_all_cached || !advancedProp) {
  293. if (list_help) {
  294. std::cout << "// "
  295. << cm.GetState()->GetCacheEntryProperty(*it,
  296. "HELPSTRING")
  297. << std::endl;
  298. }
  299. std::cout << *it << ":" << cmState::CacheEntryTypeToString(t) << "="
  300. << cm.GetState()->GetCacheEntryValue(*it) << std::endl;
  301. if (list_help) {
  302. std::cout << std::endl;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. // Always return a non-negative value. Windows tools do not always
  309. // interpret negative return values as errors.
  310. if (res != 0) {
  311. return 1;
  312. }
  313. return 0;
  314. }
  315. static int do_build(int ac, char const* const* av)
  316. {
  317. #ifndef CMAKE_BUILD_WITH_CMAKE
  318. std::cerr << "This cmake does not support --build\n";
  319. return -1;
  320. #else
  321. std::string target;
  322. std::string config = "Debug";
  323. std::string dir;
  324. std::vector<std::string> nativeOptions;
  325. bool clean = false;
  326. bool hasTarget = false;
  327. enum Doing
  328. {
  329. DoingNone,
  330. DoingDir,
  331. DoingTarget,
  332. DoingConfig,
  333. DoingNative
  334. };
  335. Doing doing = DoingDir;
  336. for (int i = 2; i < ac; ++i) {
  337. if (doing == DoingNative) {
  338. nativeOptions.push_back(av[i]);
  339. } else if (strcmp(av[i], "--target") == 0) {
  340. if (!hasTarget) {
  341. doing = DoingTarget;
  342. hasTarget = true;
  343. } else {
  344. std::cerr << "'--target' may not be specified more than once.\n\n";
  345. dir = "";
  346. break;
  347. }
  348. } else if (strcmp(av[i], "--config") == 0) {
  349. doing = DoingConfig;
  350. } else if (strcmp(av[i], "--clean-first") == 0) {
  351. clean = true;
  352. doing = DoingNone;
  353. } else if (strcmp(av[i], "--use-stderr") == 0) {
  354. /* tolerate legacy option */
  355. } else if (strcmp(av[i], "--") == 0) {
  356. doing = DoingNative;
  357. } else {
  358. switch (doing) {
  359. case DoingDir:
  360. dir = cmSystemTools::CollapseFullPath(av[i]);
  361. doing = DoingNone;
  362. break;
  363. case DoingTarget:
  364. target = av[i];
  365. doing = DoingNone;
  366. break;
  367. case DoingConfig:
  368. config = av[i];
  369. doing = DoingNone;
  370. break;
  371. default:
  372. std::cerr << "Unknown argument " << av[i] << std::endl;
  373. dir = "";
  374. break;
  375. }
  376. }
  377. }
  378. if (dir.empty()) {
  379. /* clang-format off */
  380. std::cerr <<
  381. "Usage: cmake --build <dir> [options] [-- [native-options]]\n"
  382. "Options:\n"
  383. CMAKE_BUILD_OPTIONS
  384. ;
  385. /* clang-format on */
  386. return 1;
  387. }
  388. cmake cm(cmake::RoleInternal);
  389. cmSystemTools::SetMessageCallback(cmakemainMessageCallback, &cm);
  390. cm.SetProgressCallback(cmakemainProgressCallback, &cm);
  391. return cm.Build(dir, target, config, nativeOptions, clean);
  392. #endif
  393. }