cmInstallProgramsCommand.cxx 4.1 KB

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