cmInstallFilesCommand.cxx 5.2 KB

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