cmInstallProgramsCommand.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "cmInstallProgramsCommand.h"
  4. #include <cm/memory>
  5. #include "cmExecutionStatus.h"
  6. #include "cmGeneratorExpression.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmInstallFilesGenerator.h"
  9. #include "cmInstallGenerator.h"
  10. #include "cmLocalGenerator.h"
  11. #include "cmMakefile.h"
  12. #include "cmStringAlgorithms.h"
  13. #include "cmSystemTools.h"
  14. class cmListFileBacktrace;
  15. static void FinalAction(cmMakefile& makefile, std::string const& dest,
  16. std::vector<std::string> const& args);
  17. static std::string FindInstallSource(cmMakefile& makefile, const char* name);
  18. bool cmInstallProgramsCommand(std::vector<std::string> const& args,
  19. cmExecutionStatus& status)
  20. {
  21. if (args.size() < 2) {
  22. status.SetError("called with incorrect number of arguments");
  23. return false;
  24. }
  25. cmMakefile& mf = status.GetMakefile();
  26. // Enable the install target.
  27. mf.GetGlobalGenerator()->EnableInstallTarget();
  28. mf.GetGlobalGenerator()->AddInstallComponent(
  29. mf.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
  30. std::string const& dest = args[0];
  31. std::vector<std::string> const finalArgs(args.begin() + 1, args.end());
  32. mf.AddGeneratorAction(
  33. [dest, finalArgs](cmLocalGenerator& lg, const cmListFileBacktrace&) {
  34. FinalAction(*lg.GetMakefile(), dest, finalArgs);
  35. });
  36. return true;
  37. }
  38. static void FinalAction(cmMakefile& makefile, std::string const& dest,
  39. std::vector<std::string> const& args)
  40. {
  41. bool files_mode = false;
  42. if (!args.empty() && args[0] == "FILES") {
  43. files_mode = true;
  44. }
  45. std::vector<std::string> files;
  46. // two different options
  47. if (args.size() > 1 || files_mode) {
  48. // for each argument, get the programs
  49. auto s = args.begin();
  50. if (files_mode) {
  51. // Skip the FILES argument in files mode.
  52. ++s;
  53. }
  54. for (; s != args.end(); ++s) {
  55. // add to the result
  56. files.push_back(FindInstallSource(makefile, s->c_str()));
  57. }
  58. } else // reg exp list
  59. {
  60. std::vector<std::string> programs;
  61. cmSystemTools::Glob(makefile.GetCurrentSourceDirectory(), args[0],
  62. programs);
  63. auto s = programs.begin();
  64. // for each argument, get the programs
  65. for (; s != programs.end(); ++s) {
  66. files.push_back(FindInstallSource(makefile, s->c_str()));
  67. }
  68. }
  69. // Construct the destination. This command always installs under
  70. // the prefix. We skip the leading slash given by the user.
  71. std::string destination = dest.substr(1);
  72. cmSystemTools::ConvertToUnixSlashes(destination);
  73. if (destination.empty()) {
  74. destination = ".";
  75. }
  76. // Use a file install generator.
  77. const char* no_permissions = "";
  78. const char* no_rename = "";
  79. bool no_exclude_from_all = false;
  80. std::string no_component =
  81. makefile.GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
  82. std::vector<std::string> no_configurations;
  83. cmInstallGenerator::MessageLevel message =
  84. cmInstallGenerator::SelectMessageLevel(&makefile);
  85. makefile.AddInstallGenerator(cm::make_unique<cmInstallFilesGenerator>(
  86. files, destination.c_str(), true, no_permissions, no_configurations,
  87. no_component.c_str(), message, no_exclude_from_all, no_rename));
  88. }
  89. /**
  90. * Find a file in the build or source tree for installation given a
  91. * relative path from the CMakeLists.txt file. This will favor files
  92. * present in the build tree. If a full path is given, it is just
  93. * returned.
  94. */
  95. static std::string FindInstallSource(cmMakefile& makefile, const char* name)
  96. {
  97. if (cmSystemTools::FileIsFullPath(name) ||
  98. cmGeneratorExpression::Find(name) == 0) {
  99. // This is a full path.
  100. return name;
  101. }
  102. // This is a relative path.
  103. std::string tb = cmStrCat(makefile.GetCurrentBinaryDirectory(), '/', name);
  104. std::string ts = cmStrCat(makefile.GetCurrentSourceDirectory(), '/', name);
  105. if (cmSystemTools::FileExists(tb)) {
  106. // The file exists in the binary tree. Use it.
  107. return tb;
  108. }
  109. if (cmSystemTools::FileExists(ts)) {
  110. // The file exists in the source tree. Use it.
  111. return ts;
  112. }
  113. // The file doesn't exist. Assume it will be present in the
  114. // binary tree when the install occurs.
  115. return tb;
  116. }