cmInstallFilesCommand.cxx 4.6 KB

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