cmInstallExportGenerator.cxx 7.7 KB

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