cmakemain.cxx 13 KB

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