cmInstallExportGenerator.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <stdio.h>
  14. #include "cmInstallTargetGenerator.h"
  15. #include "cmGeneratedFileStream.h"
  16. #include "cmTarget.h"
  17. #include "cmInstallExportGenerator.h"
  18. cmInstallExportGenerator::cmInstallExportGenerator(const char* destination,
  19. const char* file_permissions,
  20. std::vector<std::string> const& configurations,
  21. const char* filename, const char* prefix, const char* tempOutputDir)
  22. :cmInstallGenerator(destination)
  23. ,FilePermissions(file_permissions)
  24. ,Configurations(configurations)
  25. ,Filename(filename)
  26. ,Prefix(prefix)
  27. ,TempOutputDir(tempOutputDir)
  28. {
  29. }
  30. /* Helper function which adds the install locations from the generator
  31. to the properties for this target.
  32. */
  33. bool cmInstallExportGenerator::AddInstallLocations(cmTargetWithProperties* twp,
  34. cmInstallTargetGenerator* generator,
  35. const char* prefix)
  36. {
  37. if (generator == 0) // nothing to do
  38. {
  39. return true;
  40. }
  41. if (prefix == 0)
  42. {
  43. prefix = "";
  44. }
  45. const std::vector<std::string>& configs = generator->GetConfigurations();
  46. if (configs.empty())
  47. {
  48. std::string propertyName = prefix;
  49. propertyName += "LOCATION";
  50. // check that this property doesn't exist yet and add it then
  51. if (twp->Properties.find(propertyName.c_str())== twp->Properties.end())
  52. {
  53. std::string destinationFilename = generator->GetDestination();
  54. destinationFilename += "/";
  55. destinationFilename += generator->GetInstallFilename(0);
  56. twp->Properties[propertyName.c_str()] = destinationFilename;
  57. }
  58. else
  59. {
  60. return false;
  61. }
  62. }
  63. else
  64. {
  65. for(std::vector<std::string>::const_iterator configIt = configs.begin();
  66. configIt != configs.end();
  67. ++configIt)
  68. {
  69. std::string propertyName = configIt->c_str();
  70. propertyName += "_";
  71. propertyName += prefix;
  72. propertyName += "LOCATION";
  73. // check that this property doesn't exist yet and add it then
  74. if (twp->Properties.find(propertyName.c_str())== twp->Properties.end())
  75. {
  76. std::string destinationFilename = generator->GetDestination();
  77. destinationFilename += "/";
  78. destinationFilename +=generator->GetInstallFilename(configIt->c_str());
  79. twp->Properties[propertyName.c_str()] = destinationFilename;
  80. }
  81. else
  82. {
  83. return false;
  84. }
  85. }
  86. }
  87. return true;
  88. }
  89. bool cmInstallExportGenerator::SetExportSet(const char* name,
  90. const std::vector<cmTargetExport*>* set)
  91. {
  92. if ((name == 0) || (*name == 0) || (set==0))
  93. {
  94. return false;
  95. }
  96. this->Name = name;
  97. /* iterate over all targets in the set.
  98. If a cmTargetWithProperties with the same name already exists in this
  99. generator, add the new properties to it. If the property already exists,
  100. fail with an error.
  101. If no cmTargetWithProperties exists, create a new one.
  102. */
  103. for(std::vector<cmTargetExport*>::const_iterator it=set->begin();
  104. it != set->end();
  105. ++it)
  106. {
  107. std::string targetName = (*it)->Target->GetName();
  108. cmTargetWithProperties* targetWithProps = 0;
  109. for(unsigned int i=0; i<this->Targets.size(); i++)
  110. {
  111. if (targetName == this->Targets[i]->Target->GetName())
  112. {
  113. targetWithProps = this->Targets[i];
  114. }
  115. }
  116. if (targetWithProps == 0)
  117. {
  118. targetWithProps = new cmTargetWithProperties((*it)->Target);
  119. this->Targets.push_back(targetWithProps);
  120. }
  121. if (this->AddInstallLocations(targetWithProps, (*it)->ArchiveGenerator,
  122. "ARCHIVE_") == false)
  123. {
  124. return false;
  125. }
  126. if (this->AddInstallLocations(targetWithProps, (*it)->RuntimeGenerator,
  127. "") == false)
  128. {
  129. return false;
  130. }
  131. if (this->AddInstallLocations(targetWithProps, (*it)->LibraryGenerator,
  132. "LIBRARY_") == false)
  133. {
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. void cmInstallExportGenerator::GenerateScript(std::ostream& os)
  140. {
  141. // for the case that somebody exports the same set with the same file name
  142. // to different locations make the temp filename unique
  143. char buf[64];
  144. sprintf(buf, "%p", this);
  145. this->ExportFilename = this->TempOutputDir;
  146. this->ExportFilename += "/";
  147. this->ExportFilename += this->Filename;
  148. this->ExportFilename += ".";
  149. this->ExportFilename += buf;
  150. cmGeneratedFileStream exportFileStream(this->ExportFilename.c_str());
  151. if(!exportFileStream)
  152. {
  153. return;
  154. }
  155. /* for every target add the IMPORT statements and set the properties
  156. of the target. */
  157. for(std::vector<cmTargetWithProperties*>::const_iterator
  158. targetIt = this->Targets.begin();
  159. targetIt != this->Targets.end();
  160. ++targetIt)
  161. {
  162. switch ((*targetIt)->Target->GetType())
  163. {
  164. case cmTarget::EXECUTABLE:
  165. exportFileStream << "ADD_EXECUTABLE(" << this->Prefix.c_str()
  166. << (*targetIt)->Target->GetName()
  167. << " IMPORT )\n";
  168. break;
  169. case cmTarget::STATIC_LIBRARY:
  170. exportFileStream << "ADD_LIBRARY(" << this->Prefix.c_str()
  171. << (*targetIt)->Target->GetName()
  172. << " STATIC IMPORT )\n";
  173. break;
  174. case cmTarget::SHARED_LIBRARY:
  175. exportFileStream << "ADD_LIBRARY(" << this->Prefix.c_str()
  176. << (*targetIt)->Target->GetName()
  177. << " SHARED IMPORT )\n";
  178. break;
  179. case cmTarget::MODULE_LIBRARY:
  180. exportFileStream << "ADD_LIBRARY(" << this->Prefix.c_str()
  181. << (*targetIt)->Target->GetName()
  182. << " MODULE IMPORT )\n";
  183. break;
  184. default: // should never happen
  185. break;
  186. }
  187. exportFileStream << "SET_TARGET_PROPERTIES ( " << this->Prefix.c_str()
  188. << (*targetIt)->Target->GetName() << " PROPERTIES \n";
  189. for (std::map<std::string, std::string>::const_iterator
  190. propIt = (*targetIt)->Properties.begin();
  191. propIt != (*targetIt)->Properties.end();
  192. ++propIt)
  193. {
  194. exportFileStream << " " << propIt->first
  195. << " \"" << propIt->second << "\"\n";
  196. }
  197. exportFileStream << " )\n\n";
  198. }
  199. // install rule for the file created above
  200. std::vector<std::string> exportFile;
  201. exportFile.push_back(this->ExportFilename);
  202. this->AddInstallRule(os, this->Destination.c_str(), cmTarget::INSTALL_FILES,
  203. exportFile, false, 0,
  204. this->FilePermissions.c_str(), 0, this->Configurations,
  205. 0, this->Filename.c_str(), 0);
  206. }