cmakemain.cxx 13 KB

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