cmInstallFilesCommand.cxx 4.7 KB

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