cmInstallProgramsCommand.cxx 4.2 KB

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