cmInstallFilesCommand.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmInstallFilesCommand.h"
  11. #include "cmInstallFilesGenerator.h"
  12. // cmExecutableCommand
  13. bool cmInstallFilesCommand
  14. ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
  15. {
  16. if(argsIn.size() < 2)
  17. {
  18. this->SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. // Enable the install target.
  22. this->Makefile->GetLocalGenerator()
  23. ->GetGlobalGenerator()->EnableInstallTarget();
  24. std::vector<std::string> args;
  25. this->Makefile->ExpandSourceListArguments(argsIn, args, 2);
  26. this->Destination = args[0];
  27. if((args.size() > 1) && (args[1] == "FILES"))
  28. {
  29. this->IsFilesForm = true;
  30. for(std::vector<std::string>::const_iterator s = args.begin()+2;
  31. s != args.end(); ++s)
  32. {
  33. // Find the source location for each file listed.
  34. std::string f = this->FindInstallSource(s->c_str());
  35. this->Files.push_back(f);
  36. }
  37. this->CreateInstallGenerator();
  38. }
  39. else
  40. {
  41. this->IsFilesForm = false;
  42. std::vector<std::string>::const_iterator s = args.begin();
  43. for (++s;s != args.end(); ++s)
  44. {
  45. this->FinalArgs.push_back(*s);
  46. }
  47. }
  48. this->Makefile->GetLocalGenerator()->GetGlobalGenerator()
  49. ->AddInstallComponent(this->Makefile->GetSafeDefinition(
  50. "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME"));
  51. return true;
  52. }
  53. void cmInstallFilesCommand::FinalPass()
  54. {
  55. // No final pass for "FILES" form of arguments.
  56. if(this->IsFilesForm)
  57. {
  58. return;
  59. }
  60. std::string testf;
  61. std::string ext = this->FinalArgs[0];
  62. // two different options
  63. if (this->FinalArgs.size() > 1)
  64. {
  65. // now put the files into the list
  66. std::vector<std::string>::iterator s = this->FinalArgs.begin();
  67. ++s;
  68. // for each argument, get the files
  69. for (;s != this->FinalArgs.end(); ++s)
  70. {
  71. // replace any variables
  72. std::string temps = *s;
  73. if (cmSystemTools::GetFilenamePath(temps).size() > 0)
  74. {
  75. testf = cmSystemTools::GetFilenamePath(temps) + "/" +
  76. cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
  77. }
  78. else
  79. {
  80. testf = cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
  81. }
  82. // add to the result
  83. this->Files.push_back(this->FindInstallSource(testf.c_str()));
  84. }
  85. }
  86. else // reg exp list
  87. {
  88. std::vector<std::string> files;
  89. std::string regex = this->FinalArgs[0];
  90. cmSystemTools::Glob(this->Makefile->GetCurrentDirectory(),
  91. regex, files);
  92. std::vector<std::string>::iterator s = files.begin();
  93. // for each argument, get the files
  94. for (;s != files.end(); ++s)
  95. {
  96. this->Files.push_back(this->FindInstallSource(s->c_str()));
  97. }
  98. }
  99. this->CreateInstallGenerator();
  100. }
  101. void cmInstallFilesCommand::CreateInstallGenerator() const
  102. {
  103. // Construct the destination. This command always installs under
  104. // the prefix. We skip the leading slash given by the user.
  105. std::string destination = this->Destination.substr(1);
  106. cmSystemTools::ConvertToUnixSlashes(destination);
  107. if(destination.empty())
  108. {
  109. destination = ".";
  110. }
  111. // Use a file install generator.
  112. const char* no_permissions = "";
  113. const char* no_rename = "";
  114. std::string no_component = this->Makefile->GetSafeDefinition(
  115. "CMAKE_INSTALL_DEFAULT_COMPONENT_NAME");
  116. std::vector<std::string> no_configurations;
  117. cmInstallGenerator::MessageLevel message =
  118. cmInstallGenerator::MessageDefault;
  119. this->Makefile->AddInstallGenerator(
  120. new cmInstallFilesGenerator(this->Makefile, this->Files,
  121. destination.c_str(), false,
  122. no_permissions, no_configurations,
  123. no_component.c_str(), message, no_rename));
  124. }
  125. /**
  126. * Find a file in the build or source tree for installation given a
  127. * relative path from the CMakeLists.txt file. This will favor files
  128. * present in the build tree. If a full path is given, it is just
  129. * returned.
  130. */
  131. std::string cmInstallFilesCommand::FindInstallSource(const char* name) const
  132. {
  133. if(cmSystemTools::FileIsFullPath(name) ||
  134. cmGeneratorExpression::Find(name) == 0)
  135. {
  136. // This is a full path.
  137. return name;
  138. }
  139. // This is a relative path.
  140. std::string tb = this->Makefile->GetCurrentOutputDirectory();
  141. tb += "/";
  142. tb += name;
  143. std::string ts = this->Makefile->GetCurrentDirectory();
  144. ts += "/";
  145. ts += name;
  146. if(cmSystemTools::FileExists(tb.c_str()))
  147. {
  148. // The file exists in the binary tree. Use it.
  149. return tb;
  150. }
  151. else if(cmSystemTools::FileExists(ts.c_str()))
  152. {
  153. // The file exists in the source tree. Use it.
  154. return ts;
  155. }
  156. else
  157. {
  158. // The file doesn't exist. Assume it will be present in the
  159. // binary tree when the install occurs.
  160. return tb;
  161. }
  162. }