cmInstallFilesCommand.cxx 4.6 KB

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