cmInstallFilesCommand.cxx 5.4 KB

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