cmakemain.cxx 12 KB

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