cmakemain.cxx 11 KB

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