cmInstallProgramsCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 "cmInstallProgramsCommand.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 cmInstallProgramsCommand::InitialPass(
  13. std::vector<std::string> const& args, 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. this->FinalArgs.insert(this->FinalArgs.end(), args.begin() + 1, args.end());
  23. this->Makefile->GetGlobalGenerator()->AddInstallComponent(
  24. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
  25. return true;
  26. }
  27. void cmInstallProgramsCommand::FinalPass()
  28. {
  29. bool files_mode = false;
  30. if (!this->FinalArgs.empty() && this->FinalArgs[0] == "FILES") {
  31. files_mode = true;
  32. }
  33. // two different options
  34. if (this->FinalArgs.size() > 1 || files_mode) {
  35. // for each argument, get the programs
  36. std::vector<std::string>::iterator s = this->FinalArgs.begin();
  37. if (files_mode) {
  38. // Skip the FILES argument in files mode.
  39. ++s;
  40. }
  41. for (; s != this->FinalArgs.end(); ++s) {
  42. // add to the result
  43. this->Files.push_back(this->FindInstallSource(s->c_str()));
  44. }
  45. } else // reg exp list
  46. {
  47. std::vector<std::string> programs;
  48. cmSystemTools::Glob(this->Makefile->GetCurrentSourceDirectory(),
  49. this->FinalArgs[0], programs);
  50. std::vector<std::string>::iterator s = programs.begin();
  51. // for each argument, get the programs
  52. for (; s != programs.end(); ++s) {
  53. this->Files.push_back(this->FindInstallSource(s->c_str()));
  54. }
  55. }
  56. // Construct the destination. This command always installs under
  57. // the prefix. We skip the leading slash given by the user.
  58. std::string destination = this->Destination.substr(1);
  59. cmSystemTools::ConvertToUnixSlashes(destination);
  60. if (destination.empty()) {
  61. destination = ".";
  62. }
  63. // Use a file install generator.
  64. const char* no_permissions = "";
  65. const char* no_rename = "";
  66. bool no_exclude_from_all = false;
  67. std::string no_component =
  68. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
  69. std::vector<std::string> no_configurations;
  70. cmInstallGenerator::MessageLevel message =
  71. cmInstallGenerator::SelectMessageLevel(this->Makefile);
  72. this->Makefile->AddInstallGenerator(new cmInstallFilesGenerator(
  73. this->Files, destination.c_str(), true, no_permissions, no_configurations,
  74. no_component.c_str(), message, no_exclude_from_all, no_rename));
  75. }
  76. /**
  77. * Find a file in the build or source tree for installation given a
  78. * relative path from the CMakeLists.txt file. This will favor files
  79. * present in the build tree. If a full path is given, it is just
  80. * returned.
  81. */
  82. std::string cmInstallProgramsCommand::FindInstallSource(const char* name) const
  83. {
  84. if (cmSystemTools::FileIsFullPath(name) ||
  85. cmGeneratorExpression::Find(name) == 0) {
  86. // This is a full path.
  87. return name;
  88. }
  89. // This is a relative path.
  90. std::string tb = this->Makefile->GetCurrentBinaryDirectory();
  91. tb += "/";
  92. tb += name;
  93. std::string ts = this->Makefile->GetCurrentSourceDirectory();
  94. ts += "/";
  95. ts += name;
  96. if (cmSystemTools::FileExists(tb)) {
  97. // The file exists in the binary tree. Use it.
  98. return tb;
  99. }
  100. if (cmSystemTools::FileExists(ts)) {
  101. // The file exists in the source tree. Use it.
  102. return ts;
  103. }
  104. // The file doesn't exist. Assume it will be present in the
  105. // binary tree when the install occurs.
  106. return tb;
  107. }