cmakemain.cxx 13 KB

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