cmakemain.cxx 12 KB

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