cmInstallFilesCommand.cxx 5.3 KB

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