cmakemain.cxx 12 KB

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