cmakemain.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "cmCacheManager.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->GetCurrentDirectory();
  142. }
  143. else if ((mf) && (strstr(m, "Generating")==m))
  144. {
  145. dir = " ";
  146. dir += mf->GetCurrentOutputDirectory();
  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.AddCMakePaths();
  195. // the command line args are processed here so that you can do
  196. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  197. std::vector<std::string> args(av, av + ac);
  198. hcm.SetCacheArgs(args);
  199. std::vector<cmDocumentationEntry> generators;
  200. hcm.GetGeneratorDocumentation(generators);
  201. doc.SetName("cmake");
  202. doc.SetSection("Name",cmDocumentationName);
  203. doc.SetSection("Usage",cmDocumentationUsage);
  204. if ( ac == 1 )
  205. {
  206. doc.AppendSection("Usage",cmDocumentationUsageNote);
  207. }
  208. doc.AppendSection("Generators",generators);
  209. doc.PrependSection("Options",cmDocumentationOptions);
  210. return doc.PrintRequestedDocumentation(std::cout)? 0:1;
  211. }
  212. #else
  213. if ( ac == 1 )
  214. {
  215. std::cout <<
  216. "Bootstrap CMake should not be used outside CMake build process."
  217. << std::endl;
  218. return 0;
  219. }
  220. #endif
  221. bool sysinfo = false;
  222. bool list_cached = false;
  223. bool list_all_cached = false;
  224. bool list_help = false;
  225. bool view_only = false;
  226. cmake::WorkingMode workingMode = cmake::NORMAL_MODE;
  227. std::vector<std::string> args;
  228. for(int i =0; i < ac; ++i)
  229. {
  230. if(strcmp(av[i], "-i") == 0)
  231. {
  232. std::cerr <<
  233. "The \"cmake -i\" wizard mode is no longer supported.\n"
  234. "Use the -D option to set cache values on the command line.\n"
  235. "Use cmake-gui or ccmake for an interactive dialog.\n";
  236. return 1;
  237. }
  238. else if(strcmp(av[i], "--system-information") == 0)
  239. {
  240. sysinfo = true;
  241. }
  242. else if (strcmp(av[i], "-N") == 0)
  243. {
  244. view_only = true;
  245. }
  246. else if (strcmp(av[i], "-L") == 0)
  247. {
  248. list_cached = true;
  249. }
  250. else if (strcmp(av[i], "-LA") == 0)
  251. {
  252. list_all_cached = true;
  253. }
  254. else if (strcmp(av[i], "-LH") == 0)
  255. {
  256. list_cached = true;
  257. list_help = true;
  258. }
  259. else if (strcmp(av[i], "-LAH") == 0)
  260. {
  261. list_all_cached = true;
  262. list_help = true;
  263. }
  264. else if (cmHasLiteralPrefix(av[i], "-P"))
  265. {
  266. if ( i == ac -1 )
  267. {
  268. cmSystemTools::Error("No script specified for argument -P");
  269. }
  270. else
  271. {
  272. workingMode = cmake::SCRIPT_MODE;
  273. args.push_back(av[i]);
  274. i++;
  275. args.push_back(av[i]);
  276. }
  277. }
  278. else if (cmHasLiteralPrefix(av[i], "--find-package"))
  279. {
  280. workingMode = cmake::FIND_PACKAGE_MODE;
  281. args.push_back(av[i]);
  282. }
  283. else
  284. {
  285. args.push_back(av[i]);
  286. }
  287. }
  288. if (sysinfo)
  289. {
  290. cmake cm;
  291. int ret = cm.GetSystemInformation(args);
  292. return ret;
  293. }
  294. cmake cm;
  295. cmSystemTools::SetMessageCallback(cmakemainMessageCallback, (void *)&cm);
  296. cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
  297. cm.SetWorkingMode(workingMode);
  298. int res = cm.Run(args, view_only);
  299. if ( list_cached || list_all_cached )
  300. {
  301. std::cout << "-- Cache values" << std::endl;
  302. std::vector<std::string> keys =
  303. cm.GetCacheManager()->GetCacheEntryKeys();
  304. for (std::vector<std::string>::const_iterator it = keys.begin();
  305. it != keys.end(); ++it)
  306. {
  307. cmCacheManager::CacheEntryType t =
  308. cm.GetCacheManager()->GetCacheEntryType(*it);
  309. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  310. t != cmCacheManager::UNINITIALIZED )
  311. {
  312. const char* advancedProp =
  313. cm.GetCacheManager()->GetCacheEntryProperty(*it, "ADVANCED");
  314. if ( list_all_cached || !advancedProp)
  315. {
  316. if ( list_help )
  317. {
  318. std::cout << "// "
  319. << cm.GetCacheManager()->GetCacheEntryProperty(*it,
  320. "HELPSTRING") << std::endl;
  321. }
  322. std::cout << *it << ":" <<
  323. cmCacheManager::TypeToString(t)
  324. << "=" << cm.GetCacheManager()->GetCacheEntryValue(*it)
  325. << std::endl;
  326. if ( list_help )
  327. {
  328. std::cout << std::endl;
  329. }
  330. }
  331. }
  332. }
  333. }
  334. // Always return a non-negative value. Windows tools do not always
  335. // interpret negative return values as errors.
  336. if(res != 0)
  337. {
  338. return 1;
  339. }
  340. else
  341. {
  342. return 0;
  343. }
  344. }
  345. //----------------------------------------------------------------------------
  346. static int do_build(int ac, char const* const* av)
  347. {
  348. #ifndef CMAKE_BUILD_WITH_CMAKE
  349. std::cerr << "This cmake does not support --build\n";
  350. return -1;
  351. #else
  352. std::string target;
  353. std::string config = "Debug";
  354. std::string dir;
  355. std::vector<std::string> nativeOptions;
  356. bool clean = false;
  357. enum Doing { DoingNone, DoingDir, DoingTarget, DoingConfig, DoingNative};
  358. Doing doing = DoingDir;
  359. for(int i=2; i < ac; ++i)
  360. {
  361. if(doing == DoingNative)
  362. {
  363. nativeOptions.push_back(av[i]);
  364. }
  365. else if(strcmp(av[i], "--target") == 0)
  366. {
  367. doing = DoingTarget;
  368. }
  369. else if(strcmp(av[i], "--config") == 0)
  370. {
  371. doing = DoingConfig;
  372. }
  373. else if(strcmp(av[i], "--clean-first") == 0)
  374. {
  375. clean = true;
  376. doing = DoingNone;
  377. }
  378. else if(strcmp(av[i], "--use-stderr") == 0)
  379. {
  380. /* tolerate legacy option */
  381. }
  382. else if(strcmp(av[i], "--") == 0)
  383. {
  384. doing = DoingNative;
  385. }
  386. else
  387. {
  388. switch (doing)
  389. {
  390. case DoingDir:
  391. dir = av[i];
  392. doing = DoingNone;
  393. break;
  394. case DoingTarget:
  395. target = av[i];
  396. doing = DoingNone;
  397. break;
  398. case DoingConfig:
  399. config = av[i];
  400. doing = DoingNone;
  401. break;
  402. default:
  403. std::cerr << "Unknown argument " << av[i] << std::endl;
  404. dir = "";
  405. break;
  406. }
  407. }
  408. }
  409. if(dir.empty())
  410. {
  411. std::cerr <<
  412. "Usage: cmake --build <dir> [options] [-- [native-options]]\n"
  413. "Options:\n"
  414. CMAKE_BUILD_OPTIONS
  415. ;
  416. return 1;
  417. }
  418. // Hack for vs6 that passes ".\Debug" as "$(IntDir)" value:
  419. //
  420. if (cmSystemTools::StringStartsWith(config.c_str(), ".\\"))
  421. {
  422. config = config.substr(2);
  423. }
  424. cmake cm;
  425. return cm.Build(dir, target, config, nativeOptions, clean);
  426. #endif
  427. }