You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

712 lines
22 KiB

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
9 years ago
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmAlgorithms.h"
  4. #include "cmDocumentationEntry.h" // IWYU pragma: keep
  5. #include "cmGlobalGenerator.h"
  6. #include "cmMakefile.h"
  7. #include "cmState.h"
  8. #include "cmStateTypes.h"
  9. #include "cmSystemTools.h"
  10. #include "cmake.h"
  11. #include "cmcmd.h"
  12. #ifdef CMAKE_BUILD_WITH_CMAKE
  13. # include "cmDocumentation.h"
  14. # include "cmDynamicLoader.h"
  15. #endif
  16. #include "cm_uv.h"
  17. #include "cmsys/Encoding.hxx"
  18. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  19. # include "cmsys/ConsoleBuf.hxx"
  20. #endif
  21. #include <cassert>
  22. #include <climits>
  23. #include <ctype.h>
  24. #include <iostream>
  25. #include <string.h>
  26. #include <string>
  27. #include <vector>
  28. #ifdef CMAKE_BUILD_WITH_CMAKE
  29. static const char* cmDocumentationName[][2] = {
  30. { nullptr, " cmake - Cross-Platform Makefile Generator." },
  31. { nullptr, nullptr }
  32. };
  33. static const char* cmDocumentationUsage[][2] = {
  34. { nullptr,
  35. " cmake [options] <path-to-source>\n"
  36. " cmake [options] <path-to-existing-build>\n"
  37. " cmake [options] -S <path-to-source> -B <path-to-build>" },
  38. { nullptr,
  39. "Specify a source directory to (re-)generate a build system for "
  40. "it in the current working directory. Specify an existing build "
  41. "directory to re-generate its build system." },
  42. { nullptr, nullptr }
  43. };
  44. static const char* cmDocumentationUsageNote[][2] = {
  45. { nullptr, "Run 'cmake --help' for more information." },
  46. { nullptr, nullptr }
  47. };
  48. # define CMAKE_BUILD_OPTIONS \
  49. " <dir> = Project binary directory to be built.\n" \
  50. " --parallel [<jobs>], -j [<jobs>]\n" \
  51. " = Build in parallel using the given number of jobs. \n" \
  52. " If <jobs> is omitted the native build tool's \n" \
  53. " default number is used.\n" \
  54. " The CMAKE_BUILD_PARALLEL_LEVEL environment " \
  55. "variable\n" \
  56. " specifies a default parallel level when this " \
  57. "option\n" \
  58. " is not given.\n" \
  59. " --target <tgt>..., -t <tgt>... \n" \
  60. " = Build <tgt> instead of default targets.\n" \
  61. " --config <cfg> = For multi-configuration tools, choose <cfg>.\n" \
  62. " --clean-first = Build target 'clean' first, then build.\n" \
  63. " (To clean only, use --target 'clean'.)\n" \
  64. " --verbose, -v = Enable verbose output - if supported - including\n" \
  65. " the build commands to be executed. \n" \
  66. " -- = Pass remaining options to the native tool.\n"
  67. # define CMAKE_INSTALL_OPTIONS \
  68. " <dir> = Project binary directory to install.\n" \
  69. " --config <cfg> = For multi-configuration tools, choose <cfg>.\n" \
  70. " --component <comp> = Component-based install. Only install <comp>.\n" \
  71. " --prefix <prefix> = The installation prefix CMAKE_INSTALL_PREFIX.\n" \
  72. " --strip = Performing install/strip.\n" \
  73. " -v --verbose = Enable verbose output.\n"
  74. static const char* cmDocumentationOptions[][2] = {
  75. CMAKE_STANDARD_OPTIONS_TABLE,
  76. { "-E", "CMake command mode." },
  77. { "-L[A][H]", "List non-advanced cached variables." },
  78. { "--build <dir>", "Build a CMake-generated project binary tree." },
  79. { "--install <dir>", "Install a CMake-generated project binary tree." },
  80. { "--open <dir>", "Open generated project in the associated application." },
  81. { "-N", "View mode only." },
  82. { "-P <file>", "Process script mode." },
  83. { "--find-package", "Run in pkg-config like mode." },
  84. { "--graphviz=[file]",
  85. "Generate graphviz of dependencies, see "
  86. "CMakeGraphVizOptions.cmake for more." },
  87. { "--system-information [file]", "Dump information about this system." },
  88. { "--debug-trycompile",
  89. "Do not delete the try_compile build tree. Only "
  90. "useful on one try_compile at a time." },
  91. { "--debug-output", "Put cmake in a debug mode." },
  92. { "--trace", "Put cmake in trace mode." },
  93. { "--trace-expand", "Put cmake in trace mode with variable expansion." },
  94. { "--trace-source=<file>",
  95. "Trace only this CMake file/module. Multiple options allowed." },
  96. { "--warn-uninitialized", "Warn about uninitialized values." },
  97. { "--warn-unused-vars", "Warn about unused variables." },
  98. { "--no-warn-unused-cli", "Don't warn about command line options." },
  99. { "--check-system-vars",
  100. "Find problems with variable usage in system "
  101. "files." },
  102. { nullptr, nullptr }
  103. };
  104. #endif
  105. static int do_command(int ac, char const* const* av)
  106. {
  107. std::vector<std::string> args;
  108. args.reserve(ac - 1);
  109. args.emplace_back(av[0]);
  110. args.insert(args.end(), av + 2, av + ac);
  111. return cmcmd::ExecuteCMakeCommand(args);
  112. }
  113. int do_cmake(int ac, char const* const* av);
  114. static int do_build(int ac, char const* const* av);
  115. static int do_install(int ac, char const* const* av);
  116. static int do_open(int ac, char const* const* av);
  117. static cmMakefile* cmakemainGetMakefile(cmake* cm)
  118. {
  119. if (cm && cm->GetDebugOutput()) {
  120. cmGlobalGenerator* gg = cm->GetGlobalGenerator();
  121. if (gg) {
  122. return gg->GetCurrentMakefile();
  123. }
  124. }
  125. return nullptr;
  126. }
  127. static std::string cmakemainGetStack(cmake* cm)
  128. {
  129. std::string msg;
  130. cmMakefile* mf = cmakemainGetMakefile(cm);
  131. if (mf) {
  132. msg = mf->FormatListFileStack();
  133. if (!msg.empty()) {
  134. msg = "\n Called from: " + msg;
  135. }
  136. }
  137. return msg;
  138. }
  139. static void cmakemainMessageCallback(const std::string& m,
  140. const char* /*unused*/, cmake* cm)
  141. {
  142. std::cerr << m << cmakemainGetStack(cm) << std::endl << std::flush;
  143. }
  144. static void cmakemainProgressCallback(const std::string& m, float prog,
  145. cmake* cm)
  146. {
  147. cmMakefile* mf = cmakemainGetMakefile(cm);
  148. std::string dir;
  149. if (mf && cmHasLiteralPrefix(m, "Configuring") && (prog < 0)) {
  150. dir = " ";
  151. dir += mf->GetCurrentSourceDirectory();
  152. } else if (mf && cmHasLiteralPrefix(m, "Generating")) {
  153. dir = " ";
  154. dir += mf->GetCurrentBinaryDirectory();
  155. }
  156. if ((prog < 0) || (!dir.empty())) {
  157. std::cout << "-- " << m << dir << cmakemainGetStack(cm) << std::endl;
  158. }
  159. std::cout.flush();
  160. }
  161. int main(int ac, char const* const* av)
  162. {
  163. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  164. // Replace streambuf so we can output Unicode to console
  165. cmsys::ConsoleBuf::Manager consoleOut(std::cout);
  166. consoleOut.SetUTF8Pipes();
  167. cmsys::ConsoleBuf::Manager consoleErr(std::cerr, true);
  168. consoleErr.SetUTF8Pipes();
  169. #endif
  170. cmsys::Encoding::CommandLineArguments args =
  171. cmsys::Encoding::CommandLineArguments::Main(ac, av);
  172. ac = args.argc();
  173. av = args.argv();
  174. cmSystemTools::EnableMSVCDebugHook();
  175. cmSystemTools::InitializeLibUV();
  176. cmSystemTools::FindCMakeResources(av[0]);
  177. if (ac > 1) {
  178. if (strcmp(av[1], "--build") == 0) {
  179. return do_build(ac, av);
  180. }
  181. if (strcmp(av[1], "--install") == 0) {
  182. return do_install(ac, av);
  183. }
  184. if (strcmp(av[1], "--open") == 0) {
  185. return do_open(ac, av);
  186. }
  187. if (strcmp(av[1], "-E") == 0) {
  188. return do_command(ac, av);
  189. }
  190. }
  191. int ret = do_cmake(ac, av);
  192. #ifdef CMAKE_BUILD_WITH_CMAKE
  193. cmDynamicLoader::FlushCache();
  194. #endif
  195. uv_loop_close(uv_default_loop());
  196. return ret;
  197. }
  198. int do_cmake(int ac, char const* const* av)
  199. {
  200. if (cmSystemTools::GetCurrentWorkingDirectory().empty()) {
  201. std::cerr << "Current working directory cannot be established."
  202. << std::endl;
  203. return 1;
  204. }
  205. #ifdef CMAKE_BUILD_WITH_CMAKE
  206. cmDocumentation doc;
  207. doc.addCMakeStandardDocSections();
  208. if (doc.CheckOptions(ac, av)) {
  209. // Construct and print requested documentation.
  210. cmake hcm(cmake::RoleInternal, cmState::Unknown);
  211. hcm.SetHomeDirectory("");
  212. hcm.SetHomeOutputDirectory("");
  213. hcm.AddCMakePaths();
  214. // the command line args are processed here so that you can do
  215. // -DCMAKE_MODULE_PATH=/some/path and have this value accessible here
  216. std::vector<std::string> args(av, av + ac);
  217. hcm.SetCacheArgs(args);
  218. auto generators = hcm.GetGeneratorsDocumentation();
  219. doc.SetName("cmake");
  220. doc.SetSection("Name", cmDocumentationName);
  221. doc.SetSection("Usage", cmDocumentationUsage);
  222. if (ac == 1) {
  223. doc.AppendSection("Usage", cmDocumentationUsageNote);
  224. }
  225. doc.AppendSection("Generators", generators);
  226. doc.PrependSection("Options", cmDocumentationOptions);
  227. return doc.PrintRequestedDocumentation(std::cout) ? 0 : 1;
  228. }
  229. #else
  230. if (ac == 1) {
  231. std::cout
  232. << "Bootstrap CMake should not be used outside CMake build process."
  233. << std::endl;
  234. return 0;
  235. }
  236. #endif
  237. bool sysinfo = false;
  238. bool list_cached = false;
  239. bool list_all_cached = false;
  240. bool list_help = false;
  241. bool view_only = false;
  242. cmake::WorkingMode workingMode = cmake::NORMAL_MODE;
  243. std::vector<std::string> args;
  244. for (int i = 0; i < ac; ++i) {
  245. if (strcmp(av[i], "-i") == 0) {
  246. /* clang-format off */
  247. std::cerr <<
  248. "The \"cmake -i\" wizard mode is no longer supported.\n"
  249. "Use the -D option to set cache values on the command line.\n"
  250. "Use cmake-gui or ccmake for an interactive dialog.\n";
  251. /* clang-format on */
  252. return 1;
  253. }
  254. if (strcmp(av[i], "--system-information") == 0) {
  255. sysinfo = true;
  256. } else if (strcmp(av[i], "-N") == 0) {
  257. view_only = true;
  258. } else if (strcmp(av[i], "-L") == 0) {
  259. list_cached = true;
  260. } else if (strcmp(av[i], "-LA") == 0) {
  261. list_all_cached = true;
  262. } else if (strcmp(av[i], "-LH") == 0) {
  263. list_cached = true;
  264. list_help = true;
  265. } else if (strcmp(av[i], "-LAH") == 0) {
  266. list_all_cached = true;
  267. list_help = true;
  268. } else if (cmHasLiteralPrefix(av[i], "-P")) {
  269. if (i == ac - 1) {
  270. cmSystemTools::Error("No script specified for argument -P");
  271. return 1;
  272. }
  273. workingMode = cmake::SCRIPT_MODE;
  274. args.emplace_back(av[i]);
  275. i++;
  276. args.emplace_back(av[i]);
  277. } else if (cmHasLiteralPrefix(av[i], "--find-package")) {
  278. workingMode = cmake::FIND_PACKAGE_MODE;
  279. args.emplace_back(av[i]);
  280. } else {
  281. args.emplace_back(av[i]);
  282. }
  283. }
  284. if (sysinfo) {
  285. cmake cm(cmake::RoleProject, cmState::Project);
  286. cm.SetHomeDirectory("");
  287. cm.SetHomeOutputDirectory("");
  288. int ret = cm.GetSystemInformation(args);
  289. return ret;
  290. }
  291. cmake::Role const role =
  292. workingMode == cmake::SCRIPT_MODE ? cmake::RoleScript : cmake::RoleProject;
  293. cmState::Mode mode = cmState::Unknown;
  294. switch (workingMode) {
  295. case cmake::NORMAL_MODE:
  296. mode = cmState::Project;
  297. break;
  298. case cmake::SCRIPT_MODE:
  299. mode = cmState::Script;
  300. break;
  301. case cmake::FIND_PACKAGE_MODE:
  302. mode = cmState::FindPackage;
  303. break;
  304. }
  305. cmake cm(role, mode);
  306. cm.SetHomeDirectory("");
  307. cm.SetHomeOutputDirectory("");
  308. cmSystemTools::SetMessageCallback(
  309. [&cm](const std::string& msg, const char* title) {
  310. cmakemainMessageCallback(msg, title, &cm);
  311. });
  312. cm.SetProgressCallback([&cm](const std::string& msg, float prog) {
  313. cmakemainProgressCallback(msg, prog, &cm);
  314. });
  315. cm.SetWorkingMode(workingMode);
  316. int res = cm.Run(args, view_only);
  317. if (list_cached || list_all_cached) {
  318. std::cout << "-- Cache values" << std::endl;
  319. std::vector<std::string> keys = cm.GetState()->GetCacheEntryKeys();
  320. for (std::string const& k : keys) {
  321. cmStateEnums::CacheEntryType t = cm.GetState()->GetCacheEntryType(k);
  322. if (t != cmStateEnums::INTERNAL && t != cmStateEnums::STATIC &&
  323. t != cmStateEnums::UNINITIALIZED) {
  324. const char* advancedProp =
  325. cm.GetState()->GetCacheEntryProperty(k, "ADVANCED");
  326. if (list_all_cached || !advancedProp) {
  327. if (list_help) {
  328. std::cout << "// "
  329. << cm.GetState()->GetCacheEntryProperty(k, "HELPSTRING")
  330. << std::endl;
  331. }
  332. std::cout << k << ":" << cmState::CacheEntryTypeToString(t) << "="
  333. << cm.GetState()->GetCacheEntryValue(k) << std::endl;
  334. if (list_help) {
  335. std::cout << std::endl;
  336. }
  337. }
  338. }
  339. }
  340. }
  341. // Always return a non-negative value. Windows tools do not always
  342. // interpret negative return values as errors.
  343. if (res != 0) {
  344. return 1;
  345. }
  346. return 0;
  347. }
  348. namespace {
  349. int extract_job_number(int& index, char const* current, char const* next,
  350. int len_of_flag)
  351. {
  352. std::string command(current);
  353. std::string jobString = command.substr(len_of_flag);
  354. if (jobString.empty() && next && isdigit(next[0])) {
  355. ++index; // skip parsing the job number
  356. jobString = std::string(next);
  357. }
  358. int jobs = -1;
  359. unsigned long numJobs = 0;
  360. if (jobString.empty()) {
  361. jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL;
  362. } else if (cmSystemTools::StringToULong(jobString.c_str(), &numJobs)) {
  363. if (numJobs == 0) {
  364. std::cerr
  365. << "The <jobs> value requires a positive integer argument.\n\n";
  366. } else if (numJobs > INT_MAX) {
  367. std::cerr << "The <jobs> value is too large.\n\n";
  368. } else {
  369. jobs = int(numJobs);
  370. }
  371. } else {
  372. std::cerr << "'" << command.substr(0, len_of_flag) << "' invalid number '"
  373. << jobString << "' given.\n\n";
  374. }
  375. return jobs;
  376. }
  377. }
  378. static int do_build(int ac, char const* const* av)
  379. {
  380. #ifndef CMAKE_BUILD_WITH_CMAKE
  381. std::cerr << "This cmake does not support --build\n";
  382. return -1;
  383. #else
  384. int jobs = cmake::NO_BUILD_PARALLEL_LEVEL;
  385. std::vector<std::string> targets;
  386. std::string config = "Debug";
  387. std::string dir;
  388. std::vector<std::string> nativeOptions;
  389. bool cleanFirst = false;
  390. bool foundClean = false;
  391. bool foundNonClean = false;
  392. bool verbose = cmSystemTools::HasEnv("VERBOSE");
  393. enum Doing
  394. {
  395. DoingNone,
  396. DoingDir,
  397. DoingTarget,
  398. DoingConfig,
  399. DoingNative
  400. };
  401. Doing doing = DoingDir;
  402. for (int i = 2; i < ac; ++i) {
  403. if (doing == DoingNative) {
  404. nativeOptions.emplace_back(av[i]);
  405. } else if (cmHasLiteralPrefix(av[i], "-j")) {
  406. const char* nextArg = ((i + 1 < ac) ? av[i + 1] : nullptr);
  407. jobs = extract_job_number(i, av[i], nextArg, sizeof("-j") - 1);
  408. if (jobs < 0) {
  409. dir.clear();
  410. }
  411. doing = DoingNone;
  412. } else if (cmHasLiteralPrefix(av[i], "--parallel")) {
  413. const char* nextArg = ((i + 1 < ac) ? av[i + 1] : nullptr);
  414. jobs = extract_job_number(i, av[i], nextArg, sizeof("--parallel") - 1);
  415. if (jobs < 0) {
  416. dir.clear();
  417. }
  418. doing = DoingNone;
  419. } else if ((strcmp(av[i], "--target") == 0) ||
  420. (strcmp(av[i], "-t") == 0)) {
  421. doing = DoingTarget;
  422. } else if (strcmp(av[i], "--config") == 0) {
  423. doing = DoingConfig;
  424. } else if (strcmp(av[i], "--clean-first") == 0) {
  425. cleanFirst = true;
  426. doing = DoingNone;
  427. } else if ((strcmp(av[i], "--verbose") == 0) ||
  428. (strcmp(av[i], "-v") == 0)) {
  429. verbose = true;
  430. doing = DoingNone;
  431. } else if (strcmp(av[i], "--use-stderr") == 0) {
  432. /* tolerate legacy option */
  433. } else if (strcmp(av[i], "--") == 0) {
  434. doing = DoingNative;
  435. } else {
  436. switch (doing) {
  437. case DoingDir:
  438. dir = cmSystemTools::CollapseFullPath(av[i]);
  439. doing = DoingNone;
  440. break;
  441. case DoingTarget:
  442. if (strlen(av[i]) == 0) {
  443. std::cerr << "Warning: Argument number " << i
  444. << " after --target option is empty." << std::endl;
  445. } else {
  446. targets.emplace_back(av[i]);
  447. if (strcmp(av[i], "clean") == 0) {
  448. foundClean = true;
  449. } else {
  450. foundNonClean = true;
  451. }
  452. }
  453. if (foundClean && foundNonClean) {
  454. std::cerr << "Error: Building 'clean' and other targets together "
  455. "is not supported."
  456. << std::endl;
  457. dir.clear();
  458. }
  459. break;
  460. case DoingConfig:
  461. config = av[i];
  462. doing = DoingNone;
  463. break;
  464. default:
  465. std::cerr << "Unknown argument " << av[i] << std::endl;
  466. dir.clear();
  467. break;
  468. }
  469. }
  470. }
  471. if (jobs == cmake::NO_BUILD_PARALLEL_LEVEL) {
  472. std::string parallel;
  473. if (cmSystemTools::GetEnv("CMAKE_BUILD_PARALLEL_LEVEL", parallel)) {
  474. if (parallel.empty()) {
  475. jobs = cmake::DEFAULT_BUILD_PARALLEL_LEVEL;
  476. } else {
  477. unsigned long numJobs = 0;
  478. if (cmSystemTools::StringToULong(parallel.c_str(), &numJobs)) {
  479. if (numJobs == 0) {
  480. std::cerr << "The CMAKE_BUILD_PARALLEL_LEVEL environment variable "
  481. "requires a positive integer argument.\n\n";
  482. dir.clear();
  483. } else if (numJobs > INT_MAX) {
  484. std::cerr << "The CMAKE_BUILD_PARALLEL_LEVEL environment variable "
  485. "is too large.\n\n";
  486. dir.clear();
  487. } else {
  488. jobs = int(numJobs);
  489. }
  490. } else {
  491. std::cerr << "'CMAKE_BUILD_PARALLEL_LEVEL' environment variable\n"
  492. << "invalid number '" << parallel << "' given.\n\n";
  493. dir.clear();
  494. }
  495. }
  496. }
  497. }
  498. if (dir.empty()) {
  499. /* clang-format off */
  500. std::cerr <<
  501. "Usage: cmake --build <dir> [options] [-- [native-options]]\n"
  502. "Options:\n"
  503. CMAKE_BUILD_OPTIONS
  504. ;
  505. /* clang-format on */
  506. return 1;
  507. }
  508. cmake cm(cmake::RoleInternal, cmState::Project);
  509. cmSystemTools::SetMessageCallback(
  510. [&cm](const std::string& msg, const char* title) {
  511. cmakemainMessageCallback(msg, title, &cm);
  512. });
  513. cm.SetProgressCallback([&cm](const std::string& msg, float prog) {
  514. cmakemainProgressCallback(msg, prog, &cm);
  515. });
  516. return cm.Build(jobs, dir, targets, config, nativeOptions, cleanFirst,
  517. verbose);
  518. #endif
  519. }
  520. static int do_install(int ac, char const* const* av)
  521. {
  522. #ifndef CMAKE_BUILD_WITH_CMAKE
  523. std::cerr << "This cmake does not support --install\n";
  524. return -1;
  525. #else
  526. assert(1 < ac);
  527. std::string config;
  528. std::string component;
  529. std::string prefix;
  530. std::string dir;
  531. bool strip = false;
  532. bool verbose = cmSystemTools::HasEnv("VERBOSE");
  533. enum Doing
  534. {
  535. DoingNone,
  536. DoingDir,
  537. DoingConfig,
  538. DoingComponent,
  539. DoingPrefix,
  540. };
  541. Doing doing = DoingDir;
  542. for (int i = 2; i < ac; ++i) {
  543. if (strcmp(av[i], "--config") == 0) {
  544. doing = DoingConfig;
  545. } else if (strcmp(av[i], "--component") == 0) {
  546. doing = DoingComponent;
  547. } else if (strcmp(av[i], "--prefix") == 0) {
  548. doing = DoingPrefix;
  549. } else if (strcmp(av[i], "--strip") == 0) {
  550. strip = true;
  551. doing = DoingNone;
  552. } else if ((strcmp(av[i], "--verbose") == 0) ||
  553. (strcmp(av[i], "-v") == 0)) {
  554. verbose = true;
  555. doing = DoingNone;
  556. } else {
  557. switch (doing) {
  558. case DoingDir:
  559. dir = cmSystemTools::CollapseFullPath(av[i]);
  560. doing = DoingNone;
  561. break;
  562. case DoingConfig:
  563. config = av[i];
  564. doing = DoingNone;
  565. break;
  566. case DoingComponent:
  567. component = av[i];
  568. doing = DoingNone;
  569. break;
  570. case DoingPrefix:
  571. prefix = av[i];
  572. doing = DoingNone;
  573. break;
  574. default:
  575. std::cerr << "Unknown argument " << av[i] << std::endl;
  576. dir.clear();
  577. break;
  578. }
  579. }
  580. }
  581. if (dir.empty()) {
  582. std::cerr << "Usage: cmake --install <dir> "
  583. "[options]\nOptions:\n" CMAKE_INSTALL_OPTIONS;
  584. return 1;
  585. }
  586. cmake cm(cmake::RoleScript, cmState::Script);
  587. cmSystemTools::SetMessageCallback(
  588. [&cm](const std::string& msg, const char* title) {
  589. cmakemainMessageCallback(msg, title, &cm);
  590. });
  591. cm.SetProgressCallback([&cm](const std::string& msg, float prog) {
  592. cmakemainProgressCallback(msg, prog, &cm);
  593. });
  594. cm.SetHomeDirectory("");
  595. cm.SetHomeOutputDirectory("");
  596. cm.SetDebugOutputOn(verbose);
  597. cm.SetWorkingMode(cmake::SCRIPT_MODE);
  598. std::vector<std::string> args{ av[0] };
  599. if (!prefix.empty()) {
  600. args.emplace_back("-DCMAKE_INSTALL_PREFIX=" + prefix);
  601. }
  602. if (!component.empty()) {
  603. args.emplace_back("-DCMAKE_INSTALL_COMPONENT=" + component);
  604. }
  605. if (strip) {
  606. args.emplace_back("-DCMAKE_INSTALL_DO_STRIP=1");
  607. }
  608. if (!config.empty()) {
  609. args.emplace_back("-DCMAKE_INSTALL_CONFIG_NAME=" + config);
  610. }
  611. args.emplace_back("-P");
  612. args.emplace_back(dir + "/cmake_install.cmake");
  613. return cm.Run(args) ? 1 : 0;
  614. #endif
  615. }
  616. static int do_open(int ac, char const* const* av)
  617. {
  618. #ifndef CMAKE_BUILD_WITH_CMAKE
  619. std::cerr << "This cmake does not support --open\n";
  620. return -1;
  621. #else
  622. std::string dir;
  623. enum Doing
  624. {
  625. DoingNone,
  626. DoingDir,
  627. };
  628. Doing doing = DoingDir;
  629. for (int i = 2; i < ac; ++i) {
  630. switch (doing) {
  631. case DoingDir:
  632. dir = cmSystemTools::CollapseFullPath(av[i]);
  633. doing = DoingNone;
  634. break;
  635. default:
  636. std::cerr << "Unknown argument " << av[i] << std::endl;
  637. dir.clear();
  638. break;
  639. }
  640. }
  641. if (dir.empty()) {
  642. std::cerr << "Usage: cmake --open <dir>\n";
  643. return 1;
  644. }
  645. cmake cm(cmake::RoleInternal, cmState::Unknown);
  646. cmSystemTools::SetMessageCallback(
  647. [&cm](const std::string& msg, const char* title) {
  648. cmakemainMessageCallback(msg, title, &cm);
  649. });
  650. cm.SetProgressCallback([&cm](const std::string& msg, float prog) {
  651. cmakemainProgressCallback(msg, prog, &cm);
  652. });
  653. return cm.Open(dir, false) ? 0 : 1;
  654. #endif
  655. }