cmExportInstallFileGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. std::set<std::string> importedLocations;
  152. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  153. properties, importedLocations);
  154. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  155. properties, importedLocations);
  156. this->SetImportLocationProperty(config, suffix,
  157. te->RuntimeGenerator, properties,
  158. importedLocations);
  159. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  160. properties, importedLocations);
  161. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  162. properties, importedLocations);
  163. // If any file location was set for the target add it to the
  164. // import file.
  165. if(!properties.empty())
  166. {
  167. // Get the rest of the target details.
  168. this->SetImportDetailProperties(config, suffix,
  169. te->Target, properties);
  170. // TOOD: PUBLIC_HEADER_LOCATION
  171. // This should wait until the build feature propagation stuff
  172. // is done. Then this can be a propagated include directory.
  173. // this->GenerateImportProperty(config, te->HeaderGenerator,
  174. // properties);
  175. // Generate code in the export file.
  176. this->GenerateImportPropertyCode(os, config, te->Target, properties);
  177. this->GenerateImportedFileChecksCode(os, te->Target, properties,
  178. importedLocations);
  179. }
  180. }
  181. this->GenerateImportedFileCheckLoop(os);
  182. // Cleanup the import prefix variable.
  183. if(!this->ImportPrefix.empty())
  184. {
  185. os << "# Cleanup temporary variables.\n"
  186. << "SET(_IMPORT_PREFIX)\n"
  187. << "\n";
  188. }
  189. }
  190. //----------------------------------------------------------------------------
  191. void
  192. cmExportInstallFileGenerator
  193. ::SetImportLocationProperty(const char* config, std::string const& suffix,
  194. cmInstallTargetGenerator* itgen,
  195. ImportPropertyMap& properties,
  196. std::set<std::string>& importedLocations
  197. )
  198. {
  199. // Skip rules that do not match this configuration.
  200. if(!(itgen && itgen->InstallsForConfig(config)))
  201. {
  202. return;
  203. }
  204. // Get the target to be installed.
  205. cmTarget* target = itgen->GetTarget();
  206. // Construct the installed location of the target.
  207. std::string dest = itgen->GetDestination();
  208. std::string value;
  209. if(!cmSystemTools::FileIsFullPath(dest.c_str()))
  210. {
  211. // The target is installed relative to the installation prefix.
  212. if(this->ImportPrefix.empty())
  213. {
  214. this->ComplainAboutImportPrefix(itgen);
  215. }
  216. value = this->ImportPrefix;
  217. }
  218. value += dest;
  219. value += "/";
  220. if(itgen->IsImportLibrary())
  221. {
  222. // Construct the property name.
  223. std::string prop = "IMPORTED_IMPLIB";
  224. prop += suffix;
  225. // Append the installed file name.
  226. value += itgen->GetInstallFilename(target, config,
  227. cmInstallTargetGenerator::NameImplib);
  228. // Store the property.
  229. properties[prop] = value;
  230. importedLocations.insert(prop);
  231. }
  232. else
  233. {
  234. // Construct the property name.
  235. std::string prop = "IMPORTED_LOCATION";
  236. prop += suffix;
  237. // Append the installed file name.
  238. if(target->IsFrameworkOnApple())
  239. {
  240. value += itgen->GetInstallFilename(target, config);
  241. value += ".framework/";
  242. value += itgen->GetInstallFilename(target, config);
  243. }
  244. else if(target->IsCFBundleOnApple())
  245. {
  246. const char *ext = target->GetProperty("BUNDLE_EXTENSION");
  247. if (!ext)
  248. {
  249. ext = "bundle";
  250. }
  251. value += itgen->GetInstallFilename(target, config);
  252. value += ".";
  253. value += ext;
  254. value += "/";
  255. value += itgen->GetInstallFilename(target, config);
  256. }
  257. else if(target->IsAppBundleOnApple())
  258. {
  259. value += itgen->GetInstallFilename(target, config);
  260. value += ".app/Contents/MacOS/";
  261. value += itgen->GetInstallFilename(target, config);
  262. }
  263. else
  264. {
  265. value += itgen->GetInstallFilename(target, config,
  266. cmInstallTargetGenerator::NameReal);
  267. }
  268. // Store the property.
  269. properties[prop] = value;
  270. importedLocations.insert(prop);
  271. }
  272. }
  273. //----------------------------------------------------------------------------
  274. void
  275. cmExportInstallFileGenerator
  276. ::ComplainAboutImportPrefix(cmInstallTargetGenerator* itgen)
  277. {
  278. const char* installDest = this->InstallExportGenerator->GetDestination();
  279. cmOStringStream e;
  280. e << "INSTALL(EXPORT \"" << this->Name << "\") given absolute "
  281. << "DESTINATION \"" << installDest << "\" but the export "
  282. << "references an installation of target \""
  283. << itgen->GetTarget()->GetName() << "\" which has relative "
  284. << "DESTINATION \"" << itgen->GetDestination() << "\".";
  285. cmSystemTools::Error(e.str().c_str());
  286. }
  287. //----------------------------------------------------------------------------
  288. void
  289. cmExportInstallFileGenerator
  290. ::ComplainAboutMissingTarget(cmTarget* depender, cmTarget* dependee)
  291. {
  292. cmOStringStream e;
  293. e << "INSTALL(EXPORT \"" << this->Name << "\" ...) "
  294. << "includes target \"" << depender->GetName()
  295. << "\" which requires target \"" << dependee->GetName()
  296. << "\" that is not in the export set.";
  297. cmSystemTools::Error(e.str().c_str());
  298. }