cmInstallFilesCommand.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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("Unspecified");
  50. return true;
  51. }
  52. void cmInstallFilesCommand::FinalPass()
  53. {
  54. // No final pass for "FILES" form of arguments.
  55. if(this->IsFilesForm)
  56. {
  57. return;
  58. }
  59. std::string testf;
  60. std::string ext = this->FinalArgs[0];
  61. // two different options
  62. if (this->FinalArgs.size() > 1)
  63. {
  64. // now put the files into the list
  65. std::vector<std::string>::iterator s = this->FinalArgs.begin();
  66. ++s;
  67. // for each argument, get the files
  68. for (;s != this->FinalArgs.end(); ++s)
  69. {
  70. // replace any variables
  71. std::string temps = *s;
  72. if (cmSystemTools::GetFilenamePath(temps).size() > 0)
  73. {
  74. testf = cmSystemTools::GetFilenamePath(temps) + "/" +
  75. cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
  76. }
  77. else
  78. {
  79. testf = cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
  80. }
  81. // add to the result
  82. this->Files.push_back(this->FindInstallSource(testf.c_str()));
  83. }
  84. }
  85. else // reg exp list
  86. {
  87. std::vector<std::string> files;
  88. std::string regex = this->FinalArgs[0].c_str();
  89. cmSystemTools::Glob(this->Makefile->GetCurrentDirectory(),
  90. regex.c_str(), files);
  91. std::vector<std::string>::iterator s = files.begin();
  92. // for each argument, get the files
  93. for (;s != files.end(); ++s)
  94. {
  95. this->Files.push_back(this->FindInstallSource(s->c_str()));
  96. }
  97. }
  98. this->CreateInstallGenerator();
  99. }
  100. void cmInstallFilesCommand::CreateInstallGenerator() const
  101. {
  102. // Construct the destination. This command always installs under
  103. // the prefix. We skip the leading slash given by the user.
  104. std::string destination = this->Destination.substr(1);
  105. cmSystemTools::ConvertToUnixSlashes(destination);
  106. if(destination.empty())
  107. {
  108. destination = ".";
  109. }
  110. // Use a file install generator.
  111. const char* no_permissions = "";
  112. const char* no_rename = "";
  113. const char* no_component = "Unspecified";
  114. std::vector<std::string> no_configurations;
  115. this->Makefile->AddInstallGenerator(
  116. new cmInstallFilesGenerator(this->Files,
  117. destination.c_str(), false,
  118. no_permissions, no_configurations,
  119. no_component, no_rename));
  120. }
  121. /**
  122. * Find a file in the build or source tree for installation given a
  123. * relative path from the CMakeLists.txt file. This will favor files
  124. * present in the build tree. If a full path is given, it is just
  125. * returned.
  126. */
  127. std::string cmInstallFilesCommand::FindInstallSource(const char* name) const
  128. {
  129. if(cmSystemTools::FileIsFullPath(name))
  130. {
  131. // This is a full path.
  132. return name;
  133. }
  134. // This is a relative path.
  135. std::string tb = this->Makefile->GetCurrentOutputDirectory();
  136. tb += "/";
  137. tb += name;
  138. std::string ts = this->Makefile->GetCurrentDirectory();
  139. ts += "/";
  140. ts += name;
  141. if(cmSystemTools::FileExists(tb.c_str()))
  142. {
  143. // The file exists in the binary tree. Use it.
  144. return tb;
  145. }
  146. else if(cmSystemTools::FileExists(ts.c_str()))
  147. {
  148. // The file exists in the source tree. Use it.
  149. return ts;
  150. }
  151. else
  152. {
  153. // The file doesn't exist. Assume it will be present in the
  154. // binary tree when the install occurs.
  155. return tb;
  156. }
  157. }