cmExportInstallFileGenerator.cxx 13 KB

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