cmakemain.cxx 13 KB

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