cmakemain.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 "cmCacheManager.h"
  18. #include "cmListFileCache.h"
  19. #include "cmakewizard.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[][3] =
  27. {
  28. {0,
  29. " cmake - Cross-Platform Makefile Generator.", 0},
  30. {0,0,0}
  31. };
  32. //----------------------------------------------------------------------------
  33. static const char * cmDocumentationUsage[][3] =
  34. {
  35. {0,
  36. " cmake [options] <path-to-source>\n"
  37. " cmake [options] <path-to-existing-build>", 0},
  38. {0,0,0}
  39. };
  40. //----------------------------------------------------------------------------
  41. static const char * cmDocumentationDescription[][3] =
  42. {
  43. {0,
  44. "The \"cmake\" executable is the CMake command-line interface. It may "
  45. "be used to configure projects in scripts. Project configuration "
  46. "settings "
  47. "may be specified on the command line with the -D option. The -i option "
  48. "will cause cmake to interactively prompt for such settings.", 0},
  49. CMAKE_STANDARD_INTRODUCTION,
  50. {0,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 = Don't merge stdout/stderr output and pass the\n" \
  59. " original stdout/stderr handles to the native\n" \
  60. " tool so it can use the capabilities of the\n" \
  61. " calling terminal (e.g. colored output).\n" \
  62. " -- = Pass remaining options to the native tool.\n"
  63. //----------------------------------------------------------------------------
  64. static const char * cmDocumentationOptions[][3] =
  65. {
  66. CMAKE_STANDARD_OPTIONS_TABLE,
  67. {"-E", "CMake command mode.",
  68. "For true platform independence, CMake provides a list of commands "
  69. "that can be used on all systems. Run with -E help for the usage "
  70. "information. Commands available are: chdir, compare_files, copy, "
  71. "copy_directory, copy_if_different, echo, echo_append, environment, "
  72. "make_directory, md5sum, remove, remove_directory, rename, tar, time, "
  73. "touch, touch_nocreate. In addition, some platform specific commands "
  74. "are available. "
  75. "On Windows: comspec, delete_regv, write_regv. "
  76. "On UNIX: create_symlink."},
  77. {"-i", "Run in wizard mode.",
  78. "Wizard mode runs cmake interactively without a GUI. The user is "
  79. "prompted to answer questions about the project configuration. "
  80. "The answers are used to set cmake cache values."},
  81. {"-L[A][H]", "List non-advanced cached variables.",
  82. "List cache variables will run CMake and list all the variables from the "
  83. "CMake cache that are not marked as INTERNAL or ADVANCED. This will "
  84. "effectively display current CMake settings, which can then be changed "
  85. "with -D option. Changing some of the variables may result in more "
  86. "variables being created. If A is specified, then it will display also "
  87. "advanced variables. If H is specified, it will also display help for "
  88. "each variable."},
  89. {"--build <dir>", "Build a CMake-generated project binary tree.",
  90. "This abstracts a native build tool's command-line interface with the "
  91. "following options:\n"
  92. CMAKE_BUILD_OPTIONS
  93. "Run cmake --build with no options for quick help."},
  94. {"-N", "View mode only.",
  95. "Only load the cache. Do not actually run configure and generate steps."},
  96. {"-P <file>", "Process script mode.",
  97. "Process the given cmake file as a script written in the CMake language. "
  98. "No configure or generate step is performed and the cache is not"
  99. " modified. If variables are defined using -D, this must be done "
  100. "before the -P argument."},
  101. {"--find-package", "Run in pkg-config like mode.",
  102. "Search a package using find_package() and print the resulting flags "
  103. "to stdout. This can be used to use cmake instead of pkg-config to find "
  104. "installed libraries in plain Makefile-based projects or in "
  105. "autoconf-based projects (via share/aclocal/cmake.m4)."},
  106. {"--graphviz=[file]", "Generate graphviz of dependencies, see "
  107. "CMakeGraphVizOptions.cmake for more.",
  108. "Generate a graphviz input file that will contain all the library and "
  109. "executable dependencies in the project. See the documentation for "
  110. "CMakeGraphVizOptions.cmake for more details. "},
  111. {"--system-information [file]", "Dump information about this system.",
  112. "Dump a wide range of information about the current system. If run "
  113. "from the top of a binary tree for a CMake project it will dump "
  114. "additional information such as the cache, log files etc."},
  115. {"--debug-trycompile", "Do not delete the try_compile build tree. Only "
  116. "useful on one try_compile at a time.",
  117. "Do not delete the files and directories created for try_compile calls. "
  118. "This is useful in debugging failed try_compiles. It may however "
  119. "change the results of the try-compiles as old junk from a previous "
  120. "try-compile may cause a different test to either pass or fail "
  121. "incorrectly. This option is best used for one try-compile at a time, "
  122. "and only when debugging." },
  123. {"--debug-output", "Put cmake in a debug mode.",
  124. "Print extra stuff during the cmake run like stack traces with "
  125. "message(send_error ) calls."},
  126. {"--trace", "Put cmake in trace mode.",
  127. "Print a trace of all calls made and from where with "
  128. "message(send_error ) calls."},
  129. {"--warn-uninitialized", "Warn about uninitialized values.",
  130. "Print a warning when an uninitialized variable is used."},
  131. {"--warn-unused-vars", "Warn about unused variables.",
  132. "Find variables that are declared or set, but not used."},
  133. {"--no-warn-unused-cli", "Don't warn about command line options.",
  134. "Don't find variables that are declared on the command line, but not "
  135. "used."},
  136. {"--check-system-vars", "Find problems with variable usage in system "
  137. "files.", "Normally, unused and uninitialized variables are searched for "
  138. "only in CMAKE_SOURCE_DIR and CMAKE_BINARY_DIR. This flag tells CMake to "
  139. "warn about other files as well."},
  140. {"--help-command cmd [file]", "Print help for a single command and exit.",
  141. "Full documentation specific to the given command is displayed. "
  142. "If a file is specified, the documentation is written into and the output "
  143. "format is determined depending on the filename suffix. Supported are man "
  144. "page, HTML, DocBook and plain text."},
  145. {"--help-command-list [file]", "List available listfile commands and exit.",
  146. "The list contains all commands for which help may be obtained by using "
  147. "the --help-command argument followed by a command name. "
  148. "If a file is specified, the documentation is written into and the output "
  149. "format is determined depending on the filename suffix. Supported are man "
  150. "page, HTML, DocBook and plain text."},
  151. {"--help-commands [file]", "Print help for all commands and exit.",
  152. "Full documentation specific for all current commands is displayed."
  153. "If a file is specified, the documentation is written into and the output "
  154. "format is determined depending on the filename suffix. Supported are man "
  155. "page, HTML, DocBook and plain text."},
  156. {"--help-compatcommands [file]", "Print help for compatibility commands. ",
  157. "Full documentation specific for all compatibility commands is displayed."
  158. "If a file is specified, the documentation is written into and the output "
  159. "format is determined depending on the filename suffix. Supported are man "
  160. "page, HTML, DocBook and plain text."},
  161. {"--help-module module [file]", "Print help for a single module and exit.",
  162. "Full documentation specific to the given module is displayed."
  163. "If a file is specified, the documentation is written into and the output "
  164. "format is determined depending on the filename suffix. Supported are man "
  165. "page, HTML, DocBook and plain text."},
  166. {"--help-module-list [file]", "List available modules and exit.",
  167. "The list contains all modules for which help may be obtained by using "
  168. "the --help-module argument followed by a module name. "
  169. "If a file is specified, the documentation is written into and the output "
  170. "format is determined depending on the filename suffix. Supported are man "
  171. "page, HTML, DocBook and plain text."},
  172. {"--help-modules [file]", "Print help for all modules and exit.",
  173. "Full documentation for all modules is displayed. "
  174. "If a file is specified, the documentation is written into and the output "
  175. "format is determined depending on the filename suffix. Supported are man "
  176. "page, HTML, DocBook and plain text."},
  177. {"--help-custom-modules [file]" , "Print help for all custom modules and "
  178. "exit.",
  179. "Full documentation for all custom modules is displayed. "
  180. "If a file is specified, the documentation is written into and the output "
  181. "format is determined depending on the filename suffix. Supported are man "
  182. "page, HTML, DocBook and plain text."},
  183. {"--help-policy cmp [file]",
  184. "Print help for a single policy and exit.",
  185. "Full documentation specific to the given policy is displayed."
  186. "If a file is specified, the documentation is written into and the output "
  187. "format is determined depending on the filename suffix. Supported are man "
  188. "page, HTML, DocBook and plain text."},
  189. {"--help-policies [file]", "Print help for all policies and exit.",
  190. "Full documentation for all policies is displayed."
  191. "If a file is specified, the documentation is written into and the output "
  192. "format is determined depending on the filename suffix. Supported are man "
  193. "page, HTML, DocBook and plain text."},
  194. {"--help-property prop [file]",
  195. "Print help for a single property and exit.",
  196. "Full documentation specific to the given property is displayed."
  197. "If a file is specified, the documentation is written into and the output "
  198. "format is determined depending on the filename suffix. Supported are man "
  199. "page, HTML, DocBook and plain text."},
  200. {"--help-property-list [file]", "List available properties and exit.",
  201. "The list contains all properties for which help may be obtained by using "
  202. "the --help-property argument followed by a property name. If a file is "
  203. "specified, the help is written into it."
  204. "If a file is specified, the documentation is written into and the output "
  205. "format is determined depending on the filename suffix. Supported are man "
  206. "page, HTML, DocBook and plain text."},
  207. {"--help-properties [file]", "Print help for all properties and exit.",
  208. "Full documentation for all properties is displayed."
  209. "If a file is specified, the documentation is written into and the output "
  210. "format is determined depending on the filename suffix. Supported are man "
  211. "page, HTML, DocBook and plain text."},
  212. {"--help-variable var [file]",
  213. "Print help for a single variable and exit.",
  214. "Full documentation specific to the given variable is displayed."
  215. "If a file is specified, the documentation is written into and the output "
  216. "format is determined depending on the filename suffix. Supported are man "
  217. "page, HTML, DocBook and plain text."},
  218. {"--help-variable-list [file]", "List documented variables and exit.",
  219. "The list contains all variables for which help may be obtained by using "
  220. "the --help-variable argument followed by a variable name. If a file is "
  221. "specified, the help is written into it."
  222. "If a file is specified, the documentation is written into and the output "
  223. "format is determined depending on the filename suffix. Supported are man "
  224. "page, HTML, DocBook and plain text."},
  225. {"--help-variables [file]", "Print help for all variables and exit.",
  226. "Full documentation for all variables is displayed."
  227. "If a file is specified, the documentation is written into and the output "
  228. "format is determined depending on the filename suffix. Supported are man "
  229. "page, HTML, DocBook and plain text."},
  230. {0,0,0}
  231. };
  232. //----------------------------------------------------------------------------
  233. static const char * cmDocumentationSeeAlso[][3] =
  234. {
  235. {0, "ccmake", 0},
  236. {0, "cpack", 0},
  237. {0, "ctest", 0},
  238. {0, "cmakecommands", 0},
  239. {0, "cmakecompat", 0},
  240. {0, "cmakemodules", 0},
  241. {0, "cmakeprops", 0},
  242. {0, "cmakevars", 0},
  243. {0, 0, 0}
  244. };
  245. //----------------------------------------------------------------------------
  246. static const char * cmDocumentationNOTE[][3] =
  247. {
  248. {0,
  249. "CMake no longer configures a project when run with no arguments. "
  250. "In order to configure the project in the current directory, run\n"
  251. " cmake .", 0},
  252. {0,0,0}
  253. };
  254. #endif
  255. int do_cmake(int ac, char** av);
  256. static int do_build(int ac, char** av);
  257. static cmMakefile* cmakemainGetMakefile(void *clientdata)
  258. {
  259. cmake* cm = (cmake *)clientdata;
  260. if(cm && cm->GetDebugOutput())
  261. {
  262. cmGlobalGenerator* gg=cm->GetGlobalGenerator();
  263. if (gg)
  264. {
  265. cmLocalGenerator* lg=gg->GetCurrentLocalGenerator();
  266. if (lg)
  267. {
  268. cmMakefile* mf = lg->GetMakefile();
  269. return mf;
  270. }
  271. }
  272. }
  273. return 0;
  274. }
  275. static std::string cmakemainGetStack(void *clientdata)
  276. {
  277. std::string msg;
  278. cmMakefile* mf=cmakemainGetMakefile(clientdata);
  279. if (mf)
  280. {
  281. msg = mf->GetListFileStack();
  282. if (!msg.empty())
  283. {
  284. msg = "\n Called from: " + msg;
  285. }
  286. }
  287. return msg;
  288. }
  289. static void cmakemainErrorCallback(const char* m, const char*, bool&,
  290. void *clientdata)
  291. {
  292. std::cerr << m << cmakemainGetStack(clientdata) << std::endl << std::flush;
  293. }
  294. static void cmakemainProgressCallback(const char *m, float prog,
  295. void* clientdata)
  296. {
  297. cmMakefile* mf = cmakemainGetMakefile(clientdata);
  298. std::string dir;
  299. if ((mf) && (strstr(m, "Configuring")==m) && (prog<0))
  300. {
  301. dir = " ";
  302. dir += mf->GetCurrentDirectory();
  303. }
  304. else if ((mf) && (strstr(m, "Generating")==m))
  305. {
  306. dir = " ";
  307. dir += mf->GetCurrentOutputDirectory();
  308. }
  309. if ((prog < 0) || (!dir.empty()))
  310. {
  311. std::cout << "-- " << m << dir << cmakemainGetStack(clientdata)<<std::endl;
  312. }
  313. std::cout.flush();
  314. }
  315. int main(int ac, char** av)
  316. {
  317. cmSystemTools::EnableMSVCDebugHook();
  318. cmSystemTools::FindExecutableDirectory(av[0]);
  319. if(ac > 1 && strcmp(av[1], "--build") == 0)
  320. {
  321. return do_build(ac, av);
  322. }
  323. int ret = do_cmake(ac, av);
  324. #ifdef CMAKE_BUILD_WITH_CMAKE
  325. cmDynamicLoader::FlushCache();
  326. #endif
  327. return ret;
  328. }
  329. int do_cmake(int ac, char** av)
  330. {
  331. if ( cmSystemTools::GetCurrentWorkingDirectory().size() == 0 )
  332. {
  333. std::cerr << "Current working directory cannot be established."
  334. << std::endl;
  335. return 1;
  336. }
  337. #ifdef CMAKE_BUILD_WITH_CMAKE
  338. cmDocumentation doc;
  339. doc.addCMakeStandardDocSections();
  340. if(doc.CheckOptions(ac, av, "-E"))
  341. {
  342. // Construct and print requested documentation.
  343. cmake hcm;
  344. hcm.AddCMakePaths();
  345. doc.SetCMakeRoot(hcm.GetCacheDefinition("CMAKE_ROOT"));
  346. // the command line args are processed here so that you can do
  347. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  348. std::vector<std::string> args;
  349. for(int i =0; i < ac; ++i)
  350. {
  351. args.push_back(av[i]);
  352. }
  353. hcm.SetCacheArgs(args);
  354. const char* modulePath = hcm.GetCacheDefinition("CMAKE_MODULE_PATH");
  355. if (modulePath)
  356. {
  357. doc.SetCMakeModulePath(modulePath);
  358. }
  359. std::vector<cmDocumentationEntry> commands;
  360. std::vector<cmDocumentationEntry> policies;
  361. std::vector<cmDocumentationEntry> compatCommands;
  362. std::vector<cmDocumentationEntry> generators;
  363. std::map<std::string,cmDocumentationSection *> propDocs;
  364. hcm.GetPolicyDocumentation(policies);
  365. hcm.GetCommandDocumentation(commands, true, false);
  366. hcm.GetCommandDocumentation(compatCommands, false, true);
  367. hcm.GetPropertiesDocumentation(propDocs);
  368. hcm.GetGeneratorDocumentation(generators);
  369. doc.SetName("cmake");
  370. doc.SetSection("Name",cmDocumentationName);
  371. doc.SetSection("Usage",cmDocumentationUsage);
  372. doc.SetSection("Description",cmDocumentationDescription);
  373. doc.AppendSection("Generators",generators);
  374. doc.PrependSection("Options",cmDocumentationOptions);
  375. doc.SetSection("Commands",commands);
  376. doc.SetSection("Policies",policies);
  377. doc.AppendSection("Compatibility Commands",compatCommands);
  378. doc.SetSections(propDocs);
  379. cmDocumentationEntry e;
  380. e.Brief =
  381. "variables defined by cmake, that give information about the project, "
  382. "and cmake";
  383. doc.PrependSection("Variables that Provide Information",e);
  384. doc.SetSeeAlsoList(cmDocumentationSeeAlso);
  385. int result = doc.PrintRequestedDocumentation(std::cout)? 0:1;
  386. // If we were run with no arguments, but a CMakeLists.txt file
  387. // exists, the user may have been trying to use the old behavior
  388. // of cmake to build a project in-source. Print a message
  389. // explaining the change to standard error and return an error
  390. // condition in case the program is running from a script.
  391. if((ac == 1) && cmSystemTools::FileExists("CMakeLists.txt"))
  392. {
  393. doc.ClearSections();
  394. doc.SetSection("NOTE", cmDocumentationNOTE);
  395. doc.Print(cmDocumentation::UsageForm, 0, std::cerr);
  396. return 1;
  397. }
  398. return result;
  399. }
  400. #else
  401. if ( ac == 1 )
  402. {
  403. std::cout <<
  404. "Bootstrap CMake should not be used outside CMake build process."
  405. << std::endl;
  406. return 0;
  407. }
  408. #endif
  409. bool wiz = false;
  410. bool sysinfo = false;
  411. bool command = false;
  412. bool list_cached = false;
  413. bool list_all_cached = false;
  414. bool list_help = false;
  415. bool view_only = false;
  416. cmake::WorkingMode workingMode = cmake::NORMAL_MODE;
  417. std::vector<std::string> args;
  418. for(int i =0; i < ac; ++i)
  419. {
  420. if(!command && strcmp(av[i], "-i") == 0)
  421. {
  422. wiz = true;
  423. }
  424. else if(!command && strcmp(av[i], "--system-information") == 0)
  425. {
  426. sysinfo = true;
  427. }
  428. // if command has already been set, then
  429. // do not eat the -E
  430. else if (!command && strcmp(av[i], "-E") == 0)
  431. {
  432. command = true;
  433. }
  434. else if (!command && strcmp(av[i], "-N") == 0)
  435. {
  436. view_only = true;
  437. }
  438. else if (!command && strcmp(av[i], "-L") == 0)
  439. {
  440. list_cached = true;
  441. }
  442. else if (!command && strcmp(av[i], "-LA") == 0)
  443. {
  444. list_all_cached = true;
  445. }
  446. else if (!command && strcmp(av[i], "-LH") == 0)
  447. {
  448. list_cached = true;
  449. list_help = true;
  450. }
  451. else if (!command && strcmp(av[i], "-LAH") == 0)
  452. {
  453. list_all_cached = true;
  454. list_help = true;
  455. }
  456. else if (!command && strncmp(av[i], "-P", strlen("-P")) == 0)
  457. {
  458. if ( i == ac -1 )
  459. {
  460. cmSystemTools::Error("No script specified for argument -P");
  461. }
  462. else
  463. {
  464. workingMode = cmake::SCRIPT_MODE;
  465. args.push_back(av[i]);
  466. i++;
  467. args.push_back(av[i]);
  468. }
  469. }
  470. else if (!command && strncmp(av[i], "--find-package",
  471. strlen("--find-package")) == 0)
  472. {
  473. workingMode = cmake::FIND_PACKAGE_MODE;
  474. args.push_back(av[i]);
  475. }
  476. else
  477. {
  478. args.push_back(av[i]);
  479. }
  480. }
  481. if(command)
  482. {
  483. int ret = cmake::ExecuteCMakeCommand(args);
  484. return ret;
  485. }
  486. if (wiz)
  487. {
  488. cmakewizard wizard;
  489. return wizard.RunWizard(args);
  490. }
  491. if (sysinfo)
  492. {
  493. cmake cm;
  494. int ret = cm.GetSystemInformation(args);
  495. return ret;
  496. }
  497. cmake cm;
  498. cmSystemTools::SetErrorCallback(cmakemainErrorCallback, (void *)&cm);
  499. cm.SetProgressCallback(cmakemainProgressCallback, (void *)&cm);
  500. cm.SetWorkingMode(workingMode);
  501. int res = cm.Run(args, view_only);
  502. if ( list_cached || list_all_cached )
  503. {
  504. cmCacheManager::CacheIterator it =
  505. cm.GetCacheManager()->GetCacheIterator();
  506. std::cout << "-- Cache values" << std::endl;
  507. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  508. {
  509. cmCacheManager::CacheEntryType t = it.GetType();
  510. if ( t != cmCacheManager::INTERNAL && t != cmCacheManager::STATIC &&
  511. t != cmCacheManager::UNINITIALIZED )
  512. {
  513. bool advanced = it.PropertyExists("ADVANCED");
  514. if ( list_all_cached || !advanced)
  515. {
  516. if ( list_help )
  517. {
  518. std::cout << "// " << it.GetProperty("HELPSTRING") << std::endl;
  519. }
  520. std::cout << it.GetName() << ":" <<
  521. cmCacheManager::TypeToString(it.GetType())
  522. << "=" << it.GetValue() << std::endl;
  523. if ( list_help )
  524. {
  525. std::cout << std::endl;
  526. }
  527. }
  528. }
  529. }
  530. }
  531. // Always return a non-negative value. Windows tools do not always
  532. // interpret negative return values as errors.
  533. if(res != 0)
  534. {
  535. return 1;
  536. }
  537. else
  538. {
  539. return 0;
  540. }
  541. }
  542. //----------------------------------------------------------------------------
  543. static int do_build(int ac, char** av)
  544. {
  545. #ifndef CMAKE_BUILD_WITH_CMAKE
  546. std::cerr << "This cmake does not support --build\n";
  547. return -1;
  548. #else
  549. std::string target;
  550. std::string config = "Debug";
  551. std::string dir;
  552. std::vector<std::string> nativeOptions;
  553. bool clean = false;
  554. cmSystemTools::OutputOption outputflag = cmSystemTools::OUTPUT_MERGE;
  555. enum Doing { DoingNone, DoingDir, DoingTarget, DoingConfig, DoingNative};
  556. Doing doing = DoingDir;
  557. for(int i=2; i < ac; ++i)
  558. {
  559. if(doing == DoingNative)
  560. {
  561. nativeOptions.push_back(av[i]);
  562. }
  563. else if(strcmp(av[i], "--target") == 0)
  564. {
  565. doing = DoingTarget;
  566. }
  567. else if(strcmp(av[i], "--config") == 0)
  568. {
  569. doing = DoingConfig;
  570. }
  571. else if(strcmp(av[i], "--clean-first") == 0)
  572. {
  573. clean = true;
  574. doing = DoingNone;
  575. }
  576. else if(strcmp(av[i], "--use-stderr") == 0)
  577. {
  578. outputflag = cmSystemTools::OUTPUT_PASSTHROUGH;
  579. }
  580. else if(strcmp(av[i], "--") == 0)
  581. {
  582. doing = DoingNative;
  583. }
  584. else
  585. {
  586. switch (doing)
  587. {
  588. case DoingDir:
  589. dir = av[i];
  590. doing = DoingNone;
  591. break;
  592. case DoingTarget:
  593. target = av[i];
  594. doing = DoingNone;
  595. break;
  596. case DoingConfig:
  597. config = av[i];
  598. doing = DoingNone;
  599. break;
  600. default:
  601. std::cerr << "Unknown argument " << av[i] << std::endl;
  602. dir = "";
  603. break;
  604. }
  605. }
  606. }
  607. if(dir.empty())
  608. {
  609. std::cerr <<
  610. "Usage: cmake --build <dir> [options] [-- [native-options]]\n"
  611. "Options:\n"
  612. CMAKE_BUILD_OPTIONS
  613. ;
  614. return 1;
  615. }
  616. // Hack for vs6 that passes ".\Debug" as "$(IntDir)" value:
  617. //
  618. if (cmSystemTools::StringStartsWith(config.c_str(), ".\\"))
  619. {
  620. config = config.substr(2);
  621. }
  622. cmake cm;
  623. return cm.Build(dir, target, config, nativeOptions, clean, outputflag);
  624. #endif
  625. }