cmInstallFilesCommand.cxx 5.2 KB

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