cmExportInstallFileGenerator.cxx 16 KB

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