cmakemain.cxx 12 KB

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