cmInstallFilesCommand.cxx 5.2 KB

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