cmExportInstallFileGenerator.cxx 15 KB

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