cmInstallExportGenerator.cxx 8.5 KB

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