cmInstallFilesCommand.cxx 4.3 KB

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