cmExportInstallFileGenerator.cxx 15 KB

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