cmakemain.cxx 12 KB

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