cmInstallFilesCommand.cxx 4.6 KB

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