cmExportInstallFileGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "cmExportInstallFileGenerator.h"
  11. #include "cmGeneratedFileStream.h"
  12. #include "cmInstallExportGenerator.h"
  13. #include "cmInstallTargetGenerator.h"
  14. //----------------------------------------------------------------------------
  15. cmExportInstallFileGenerator
  16. ::cmExportInstallFileGenerator(cmInstallExportGenerator* iegen):
  17. InstallExportGenerator(iegen)
  18. {
  19. }
  20. //----------------------------------------------------------------------------
  21. std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
  22. {
  23. std::string glob = this->FileBase;
  24. glob += "-*";
  25. glob += this->FileExt;
  26. return glob;
  27. }
  28. //----------------------------------------------------------------------------
  29. bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
  30. {
  31. // Create all the imported targets.
  32. for(std::vector<cmTargetExport*>::const_iterator
  33. tei = this->ExportSet->begin();
  34. tei != this->ExportSet->end(); ++tei)
  35. {
  36. cmTargetExport* te = *tei;
  37. if(this->ExportedTargets.insert(te->Target).second)
  38. {
  39. this->GenerateImportTargetCode(os, te->Target);
  40. }
  41. else
  42. {
  43. cmOStringStream e;
  44. e << "INSTALL(EXPORT \"" << this->Name << "\" ...) "
  45. << "includes target \"" << te->Target->GetName()
  46. << "\" more than once in the export set.";
  47. cmSystemTools::Error(e.str().c_str());
  48. return false;
  49. }
  50. }
  51. // Now load per-configuration properties for them.
  52. os << "# Load information for each installed configuration.\n"
  53. << "GET_FILENAME_COMPONENT(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  54. << "FILE(GLOB CONFIG_FILES \"${_DIR}/"
  55. << this->GetConfigImportFileGlob() << "\")\n"
  56. << "FOREACH(f ${CONFIG_FILES})\n"
  57. << " INCLUDE(${f})\n"
  58. << "ENDFOREACH(f)\n"
  59. << "\n";
  60. // Generate an import file for each configuration.
  61. bool result = true;
  62. for(std::vector<std::string>::const_iterator
  63. ci = this->Configurations.begin();
  64. ci != this->Configurations.end(); ++ci)
  65. {
  66. if(!this->GenerateImportFileConfig(ci->c_str()))
  67. {
  68. result = false;
  69. }
  70. }
  71. return result;
  72. }
  73. //----------------------------------------------------------------------------
  74. bool
  75. cmExportInstallFileGenerator::GenerateImportFileConfig(const char* config)
  76. {
  77. // Skip configurations not enabled for this export.
  78. if(!this->InstallExportGenerator->InstallsForConfig(config))
  79. {
  80. return true;
  81. }
  82. // Construct the name of the file to generate.
  83. std::string fileName = this->FileDir;
  84. fileName += "/";
  85. fileName += this->FileBase;
  86. fileName += "-";
  87. if(config && *config)
  88. {
  89. fileName += cmSystemTools::LowerCase(config);
  90. }
  91. else
  92. {
  93. fileName += "noconfig";
  94. }
  95. fileName += this->FileExt;
  96. // Open the output file to generate it.
  97. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  98. if(!exportFileStream)
  99. {
  100. std::string se = cmSystemTools::GetLastSystemError();
  101. cmOStringStream e;
  102. e << "cannot write to file \"" << fileName.c_str()
  103. << "\": " << se;
  104. cmSystemTools::Error(e.str().c_str());
  105. return false;
  106. }
  107. std::ostream& os = exportFileStream;
  108. // Start with the import file header.
  109. this->GenerateImportHeaderCode(os, config);
  110. // Generate the per-config target information.
  111. this->GenerateImportConfig(os, config);
  112. // End with the import file footer.
  113. this->GenerateImportFooterCode(os);
  114. // Record this per-config import file.
  115. this->ConfigImportFiles[config] = fileName;
  116. return true;
  117. }
  118. //----------------------------------------------------------------------------
  119. void
  120. cmExportInstallFileGenerator
  121. ::GenerateImportTargetsConfig(std::ostream& os,
  122. const char* config, std::string const& suffix)
  123. {
  124. // Add code to compute the installation prefix relative to the
  125. // import file location.
  126. const char* installDest = this->InstallExportGenerator->GetDestination();
  127. if(!cmSystemTools::FileIsFullPath(installDest))
  128. {
  129. std::string dest = installDest;
  130. os << "# Compute the installation prefix relative to this file.\n"
  131. << "GET_FILENAME_COMPONENT(_IMPORT_PREFIX "
  132. << "\"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n";
  133. while(!dest.empty())
  134. {
  135. os <<
  136. "GET_FILENAME_COMPONENT(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" PATH)\n";
  137. dest = cmSystemTools::GetFilenamePath(dest);
  138. }
  139. os << "\n";
  140. // Import location properties may reference this variable.
  141. this->ImportPrefix = "${_IMPORT_PREFIX}/";
  142. }
  143. // Add each target in the set to the export.
  144. for(std::vector<cmTargetExport*>::const_iterator
  145. tei = this->ExportSet->begin();
  146. tei != this->ExportSet->end(); ++tei)
  147. {
  148. // Collect import properties for this target.
  149. cmTargetExport* te = *tei;
  150. ImportPropertyMap properties;
  151. this->SetImportLocationProperty(config, suffix,
  152. te->ArchiveGenerator, properties);
  153. this->SetImportLocationProperty(config, suffix,
  154. te->LibraryGenerator, properties);
  155. this->SetImportLocationProperty(config, suffix,
  156. te->RuntimeGenerator, properties);
  157. this->SetImportLocationProperty(config, suffix,
  158. te->FrameworkGenerator, properties);
  159. this->SetImportLocationProperty(config, suffix,
  160. te->BundleGenerator, properties);
  161. // If any file location was set for the target add it to the
  162. // import file.
  163. if(!properties.empty())
  164. {
  165. // Get the rest of the target details.
  166. this->SetImportDetailProperties(config, suffix,
  167. te->Target, properties);
  168. // TOOD: PUBLIC_HEADER_LOCATION
  169. // This should wait until the build feature propagation stuff
  170. // is done. Then this can be a propagated include directory.
  171. // this->GenerateImportProperty(config, te->HeaderGenerator,
  172. // properties);
  173. // Generate code in the export file.
  174. this->GenerateImportPropertyCode(os, config, te->Target, properties);
  175. }
  176. }
  177. // Cleanup the import prefix variable.
  178. if(!this->ImportPrefix.empty())
  179. {
  180. os << "# Cleanup temporary variables.\n"
  181. << "SET(_IMPORT_PREFIX)\n"
  182. << "\n";
  183. }
  184. }
  185. //----------------------------------------------------------------------------
  186. void
  187. cmExportInstallFileGenerator
  188. ::SetImportLocationProperty(const char* config, std::string const& suffix,
  189. cmInstallTargetGenerator* itgen,
  190. ImportPropertyMap& properties)
  191. {
  192. // Skip rules that do not match this configuration.
  193. if(!(itgen && itgen->InstallsForConfig(config)))
  194. {
  195. return;
  196. }
  197. // Get the target to be installed.
  198. cmTarget* target = itgen->GetTarget();
  199. // Construct the installed location of the target.
  200. std::string dest = itgen->GetDestination();
  201. std::string value;
  202. if(!cmSystemTools::FileIsFullPath(dest.c_str()))
  203. {
  204. // The target is installed relative to the installation prefix.
  205. if(this->ImportPrefix.empty())
  206. {
  207. this->ComplainAboutImportPrefix(itgen);
  208. }
  209. value = this->ImportPrefix;
  210. }
  211. value += dest;
  212. value += "/";
  213. if(itgen->IsImportLibrary())
  214. {
  215. // Construct the property name.
  216. std::string prop = "IMPORTED_IMPLIB";
  217. prop += suffix;
  218. // Append the installed file name.
  219. value += itgen->GetInstallFilename(target, config,
  220. cmInstallTargetGenerator::NameImplib);
  221. // Store the property.
  222. properties[prop] = value;
  223. }
  224. else
  225. {
  226. // Construct the property name.
  227. std::string prop = "IMPORTED_LOCATION";
  228. prop += suffix;
  229. // Append the installed file name.
  230. if(target->IsFrameworkOnApple())
  231. {
  232. value += itgen->GetInstallFilename(target, config);
  233. value += ".framework/";
  234. value += itgen->GetInstallFilename(target, config);
  235. }
  236. else if(target->IsCFBundleOnApple())
  237. {
  238. const char *ext = target->GetProperty("BUNDLE_EXTENSION");
  239. if (!ext)
  240. {
  241. ext = "bundle";
  242. }
  243. value += itgen->GetInstallFilename(target, config);
  244. value += ".";
  245. value += ext;
  246. value += "/";
  247. value += itgen->GetInstallFilename(target, config);
  248. }
  249. else if(target->IsAppBundleOnApple())
  250. {
  251. value += itgen->GetInstallFilename(target, config);
  252. value += ".app/Contents/MacOS/";
  253. value += itgen->GetInstallFilename(target, config);
  254. }
  255. else
  256. {
  257. value += itgen->GetInstallFilename(target, config,
  258. cmInstallTargetGenerator::NameReal);
  259. }
  260. // Store the property.
  261. properties[prop] = value;
  262. }
  263. }
  264. //----------------------------------------------------------------------------
  265. void
  266. cmExportInstallFileGenerator
  267. ::ComplainAboutImportPrefix(cmInstallTargetGenerator* itgen)
  268. {
  269. const char* installDest = this->InstallExportGenerator->GetDestination();
  270. cmOStringStream e;
  271. e << "INSTALL(EXPORT \"" << this->Name << "\") given absolute "
  272. << "DESTINATION \"" << installDest << "\" but the export "
  273. << "references an installation of target \""
  274. << itgen->GetTarget()->GetName() << "\" which has relative "
  275. << "DESTINATION \"" << itgen->GetDestination() << "\".";
  276. cmSystemTools::Error(e.str().c_str());
  277. }
  278. //----------------------------------------------------------------------------
  279. void
  280. cmExportInstallFileGenerator
  281. ::ComplainAboutMissingTarget(cmTarget* depender, cmTarget* dependee)
  282. {
  283. cmOStringStream e;
  284. e << "INSTALL(EXPORT \"" << this->Name << "\" ...) "
  285. << "includes target \"" << depender->GetName()
  286. << "\" which requires target \"" << dependee->GetName()
  287. << "\" that is not in the export set.";
  288. cmSystemTools::Error(e.str().c_str());
  289. }