cmakemain.cxx 12 KB

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