cmExportInstallFileGenerator.cxx 11 KB

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