cmInstallFilesCommand.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // cmExecutableCommand
  15. bool cmInstallFilesCommand
  16. ::InitialPass(std::vector<std::string> const& argsIn)
  17. {
  18. if(argsIn.size() < 2)
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. // Enable the install target.
  24. this->Makefile->GetLocalGenerator()
  25. ->GetGlobalGenerator()->EnableInstallTarget();
  26. std::vector<std::string> args;
  27. this->Makefile->ExpandSourceListArguments(argsIn, args, 2);
  28. // Create an INSTALL_FILES target specifically for this path.
  29. this->TargetName = "INSTALL_FILES_"+args[0];
  30. cmTarget& target = this->Makefile->GetTargets()[this->TargetName];
  31. target.SetInAll(false);
  32. target.SetType(cmTarget::INSTALL_FILES, this->TargetName.c_str());
  33. target.SetInstallPath(args[0].c_str());
  34. if((args.size() > 1) && (args[1] == "FILES"))
  35. {
  36. this->IsFilesForm = true;
  37. for(std::vector<std::string>::const_iterator s = args.begin()+2;
  38. s != args.end(); ++s)
  39. {
  40. // Find the source location for each file listed.
  41. std::string f = this->FindInstallSource(s->c_str());
  42. target.GetSourceLists().push_back(f);
  43. }
  44. }
  45. else
  46. {
  47. this->IsFilesForm = false;
  48. std::vector<std::string>::const_iterator s = args.begin();
  49. for (++s;s != args.end(); ++s)
  50. {
  51. this->FinalArgs.push_back(*s);
  52. }
  53. }
  54. return true;
  55. }
  56. void cmInstallFilesCommand::FinalPass()
  57. {
  58. // No final pass for "FILES" form of arguments.
  59. if(this->IsFilesForm)
  60. {
  61. return;
  62. }
  63. std::string testf;
  64. std::string ext = this->FinalArgs[0];
  65. std::vector<std::string>& targetSourceLists =
  66. this->Makefile->GetTargets()[this->TargetName].GetSourceLists();
  67. // two different options
  68. if (this->FinalArgs.size() > 1)
  69. {
  70. // now put the files into the list
  71. std::vector<std::string>::iterator s = this->FinalArgs.begin();
  72. ++s;
  73. // for each argument, get the files
  74. for (;s != this->FinalArgs.end(); ++s)
  75. {
  76. // replace any variables
  77. std::string temps = *s;
  78. if (cmSystemTools::GetFilenamePath(temps).size() > 0)
  79. {
  80. testf = cmSystemTools::GetFilenamePath(temps) + "/" +
  81. cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
  82. }
  83. else
  84. {
  85. testf = cmSystemTools::GetFilenameWithoutLastExtension(temps) + ext;
  86. }
  87. // add to the result
  88. targetSourceLists.push_back(this->FindInstallSource(testf.c_str()));
  89. }
  90. }
  91. else // reg exp list
  92. {
  93. std::vector<std::string> files;
  94. std::string regex = this->FinalArgs[0].c_str();
  95. cmSystemTools::Glob(this->Makefile->GetCurrentDirectory(),
  96. regex.c_str(), files);
  97. std::vector<std::string>::iterator s = files.begin();
  98. // for each argument, get the files
  99. for (;s != files.end(); ++s)
  100. {
  101. targetSourceLists.push_back(this->FindInstallSource(s->c_str()));
  102. }
  103. }
  104. }
  105. /**
  106. * Find a file in the build or source tree for installation given a
  107. * relative path from the CMakeLists.txt file. This will favor files
  108. * present in the build tree. If a full path is given, it is just
  109. * returned.
  110. */
  111. std::string cmInstallFilesCommand::FindInstallSource(const char* name) const
  112. {
  113. if(cmSystemTools::FileIsFullPath(name))
  114. {
  115. // This is a full path.
  116. return name;
  117. }
  118. // This is a relative path.
  119. std::string tb = this->Makefile->GetCurrentOutputDirectory();
  120. tb += "/";
  121. tb += name;
  122. std::string ts = this->Makefile->GetCurrentDirectory();
  123. ts += "/";
  124. ts += name;
  125. if(cmSystemTools::FileExists(tb.c_str()))
  126. {
  127. // The file exists in the binary tree. Use it.
  128. return tb;
  129. }
  130. else if(cmSystemTools::FileExists(ts.c_str()))
  131. {
  132. // The file exists in the source tree. Use it.
  133. return ts;
  134. }
  135. else
  136. {
  137. // The file doesn't exist. Assume it will be present in the
  138. // binary tree when the install occurs.
  139. return tb;
  140. }
  141. }