cmakemain.cxx 12 KB

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