cmakemain.cxx 13 KB

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