cmExportInstallFileGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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<cmTargetExport*> 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 * te = *tei;
  47. if(this->ExportedTargets.insert(te->Target).second)
  48. {
  49. allTargets.push_back(te);
  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(cmHasLiteralPrefix(absDestS.c_str(), "/lib/") ||
  77. cmHasLiteralPrefix(absDestS.c_str(), "/lib64/") ||
  78. cmHasLiteralPrefix(absDestS.c_str(), "/usr/lib/") ||
  79. cmHasLiteralPrefix(absDestS.c_str(), "/usr/lib64/"))
  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. bool require2_8_12 = false;
  106. bool require3_0_0 = false;
  107. bool requiresConfigFiles = false;
  108. // Create all the imported targets.
  109. for(std::vector<cmTargetExport*>::const_iterator
  110. tei = allTargets.begin();
  111. tei != allTargets.end(); ++tei)
  112. {
  113. cmTarget* te = (*tei)->Target;
  114. if (te->GetProperty("INTERFACE_SOURCES"))
  115. {
  116. cmOStringStream e;
  117. e << "Target \""
  118. << te->GetName()
  119. << "\" has a populated INTERFACE_SOURCES property. This is not "
  120. "currently supported.";
  121. cmSystemTools::Error(e.str().c_str());
  122. return false;
  123. }
  124. requiresConfigFiles = requiresConfigFiles
  125. || te->GetType() != cmTarget::INTERFACE_LIBRARY;
  126. this->GenerateImportTargetCode(os, te);
  127. ImportPropertyMap properties;
  128. this->PopulateIncludeDirectoriesInterface(*tei,
  129. cmGeneratorExpression::InstallInterface,
  130. properties, missingTargets);
  131. this->PopulateInterfaceProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES",
  132. te,
  133. cmGeneratorExpression::InstallInterface,
  134. properties, missingTargets);
  135. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
  136. te,
  137. cmGeneratorExpression::InstallInterface,
  138. properties, missingTargets);
  139. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS",
  140. te,
  141. cmGeneratorExpression::InstallInterface,
  142. properties, missingTargets);
  143. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS",
  144. te,
  145. cmGeneratorExpression::InstallInterface,
  146. properties, missingTargets);
  147. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES",
  148. te,
  149. cmGeneratorExpression::InstallInterface,
  150. properties, missingTargets);
  151. const bool newCMP0022Behavior =
  152. te->GetPolicyStatusCMP0022() != cmPolicies::WARN
  153. && te->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  154. if (newCMP0022Behavior)
  155. {
  156. if (this->PopulateInterfaceLinkLibrariesProperty(te,
  157. cmGeneratorExpression::InstallInterface,
  158. properties, missingTargets)
  159. && !this->ExportOld)
  160. {
  161. require2_8_12 = true;
  162. }
  163. }
  164. if (te->GetType() == cmTarget::INTERFACE_LIBRARY)
  165. {
  166. require3_0_0 = true;
  167. }
  168. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
  169. te, properties);
  170. this->PopulateCompatibleInterfaceProperties(te, properties);
  171. this->GenerateInterfaceProperties(te, os, properties);
  172. }
  173. if (require3_0_0)
  174. {
  175. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  176. }
  177. else if (require2_8_12)
  178. {
  179. this->GenerateRequiredCMakeVersion(os, "2.8.12");
  180. }
  181. // Now load per-configuration properties for them.
  182. os << "# Load information for each installed configuration.\n"
  183. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  184. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  185. << this->GetConfigImportFileGlob() << "\")\n"
  186. << "foreach(f ${CONFIG_FILES})\n"
  187. << " include(${f})\n"
  188. << "endforeach()\n"
  189. << "\n";
  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. this->GenerateImportedFileCheckLoop(os);
  198. bool result = true;
  199. // Generate an import file for each configuration.
  200. // Don't do this if we only export INTERFACE_LIBRARY targets.
  201. if (requiresConfigFiles)
  202. {
  203. for(std::vector<std::string>::const_iterator
  204. ci = this->Configurations.begin();
  205. ci != this->Configurations.end(); ++ci)
  206. {
  207. if(!this->GenerateImportFileConfig(*ci, missingTargets))
  208. {
  209. result = false;
  210. }
  211. }
  212. }
  213. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  214. return result;
  215. }
  216. //----------------------------------------------------------------------------
  217. void
  218. cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string &input)
  219. {
  220. std::string::size_type pos = 0;
  221. std::string::size_type lastPos = pos;
  222. while((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos)
  223. {
  224. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  225. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  226. lastPos = endPos;
  227. }
  228. }
  229. //----------------------------------------------------------------------------
  230. bool
  231. cmExportInstallFileGenerator::GenerateImportFileConfig(
  232. const std::string& config,
  233. std::vector<std::string> &missingTargets)
  234. {
  235. // Skip configurations not enabled for this export.
  236. if(!this->IEGen->InstallsForConfig(config))
  237. {
  238. return true;
  239. }
  240. // Construct the name of the file to generate.
  241. std::string fileName = this->FileDir;
  242. fileName += "/";
  243. fileName += this->FileBase;
  244. fileName += "-";
  245. if(!config.empty())
  246. {
  247. fileName += cmSystemTools::LowerCase(config);
  248. }
  249. else
  250. {
  251. fileName += "noconfig";
  252. }
  253. fileName += this->FileExt;
  254. // Open the output file to generate it.
  255. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  256. if(!exportFileStream)
  257. {
  258. std::string se = cmSystemTools::GetLastSystemError();
  259. cmOStringStream e;
  260. e << "cannot write to file \"" << fileName
  261. << "\": " << se;
  262. cmSystemTools::Error(e.str().c_str());
  263. return false;
  264. }
  265. std::ostream& os = exportFileStream;
  266. // Start with the import file header.
  267. this->GenerateImportHeaderCode(os, config);
  268. // Generate the per-config target information.
  269. this->GenerateImportConfig(os, config, missingTargets);
  270. // End with the import file footer.
  271. this->GenerateImportFooterCode(os);
  272. // Record this per-config import file.
  273. this->ConfigImportFiles[config] = fileName;
  274. return true;
  275. }
  276. //----------------------------------------------------------------------------
  277. void
  278. cmExportInstallFileGenerator
  279. ::GenerateImportTargetsConfig(std::ostream& os,
  280. const std::string& config,
  281. std::string const& suffix,
  282. std::vector<std::string> &missingTargets)
  283. {
  284. // Add each target in the set to the export.
  285. for(std::vector<cmTargetExport*>::const_iterator
  286. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  287. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  288. {
  289. // Collect import properties for this target.
  290. cmTargetExport const* te = *tei;
  291. if (te->Target->GetType() == cmTarget::INTERFACE_LIBRARY)
  292. {
  293. continue;
  294. }
  295. ImportPropertyMap properties;
  296. std::set<std::string> importedLocations;
  297. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  298. properties, importedLocations);
  299. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  300. properties, importedLocations);
  301. this->SetImportLocationProperty(config, suffix,
  302. te->RuntimeGenerator, properties,
  303. importedLocations);
  304. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  305. properties, importedLocations);
  306. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  307. properties, importedLocations);
  308. // If any file location was set for the target add it to the
  309. // import file.
  310. if(!properties.empty())
  311. {
  312. // Get the rest of the target details.
  313. this->SetImportDetailProperties(config, suffix,
  314. te->Target, properties, missingTargets);
  315. this->SetImportLinkInterface(config, suffix,
  316. cmGeneratorExpression::InstallInterface,
  317. te->Target, properties, missingTargets);
  318. // TOOD: PUBLIC_HEADER_LOCATION
  319. // This should wait until the build feature propagation stuff
  320. // is done. Then this can be a propagated include directory.
  321. // this->GenerateImportProperty(config, te->HeaderGenerator,
  322. // properties);
  323. // Generate code in the export file.
  324. this->GenerateImportPropertyCode(os, config, te->Target, properties);
  325. this->GenerateImportedFileChecksCode(os, te->Target, properties,
  326. importedLocations);
  327. }
  328. }
  329. }
  330. //----------------------------------------------------------------------------
  331. void
  332. cmExportInstallFileGenerator
  333. ::SetImportLocationProperty(const std::string& config,
  334. std::string const& suffix,
  335. cmInstallTargetGenerator* itgen,
  336. ImportPropertyMap& properties,
  337. std::set<std::string>& importedLocations
  338. )
  339. {
  340. // Skip rules that do not match this configuration.
  341. if(!(itgen && itgen->InstallsForConfig(config)))
  342. {
  343. return;
  344. }
  345. // Get the target to be installed.
  346. cmTarget* target = itgen->GetTarget();
  347. // Construct the installed location of the target.
  348. std::string dest = itgen->GetDestination();
  349. std::string value;
  350. if(!cmSystemTools::FileIsFullPath(dest.c_str()))
  351. {
  352. // The target is installed relative to the installation prefix.
  353. if(this->ImportPrefix.empty())
  354. {
  355. this->ComplainAboutImportPrefix(itgen);
  356. }
  357. value = this->ImportPrefix;
  358. }
  359. value += dest;
  360. value += "/";
  361. if(itgen->IsImportLibrary())
  362. {
  363. // Construct the property name.
  364. std::string prop = "IMPORTED_IMPLIB";
  365. prop += suffix;
  366. // Append the installed file name.
  367. value += itgen->GetInstallFilename(target, config,
  368. cmInstallTargetGenerator::NameImplib);
  369. // Store the property.
  370. properties[prop] = value;
  371. importedLocations.insert(prop);
  372. }
  373. else
  374. {
  375. // Construct the property name.
  376. std::string prop = "IMPORTED_LOCATION";
  377. prop += suffix;
  378. // Append the installed file name.
  379. if(target->IsAppBundleOnApple())
  380. {
  381. value += itgen->GetInstallFilename(target, config);
  382. value += ".app/Contents/MacOS/";
  383. value += itgen->GetInstallFilename(target, config);
  384. }
  385. else
  386. {
  387. value += itgen->GetInstallFilename(target, config,
  388. cmInstallTargetGenerator::NameReal);
  389. }
  390. // Store the property.
  391. properties[prop] = value;
  392. importedLocations.insert(prop);
  393. }
  394. }
  395. //----------------------------------------------------------------------------
  396. void
  397. cmExportInstallFileGenerator::HandleMissingTarget(
  398. std::string& link_libs, std::vector<std::string>& missingTargets,
  399. cmMakefile* mf, cmTarget* depender, cmTarget* dependee)
  400. {
  401. const std::string name = dependee->GetName();
  402. std::vector<std::string> namespaces = this->FindNamespaces(mf, name);
  403. int targetOccurrences = (int)namespaces.size();
  404. if (targetOccurrences == 1)
  405. {
  406. std::string missingTarget = namespaces[0];
  407. missingTarget += dependee->GetExportName();
  408. link_libs += missingTarget;
  409. missingTargets.push_back(missingTarget);
  410. }
  411. else
  412. {
  413. // All exported targets should be known here and should be unique.
  414. // This is probably user-error.
  415. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  416. }
  417. }
  418. //----------------------------------------------------------------------------
  419. std::vector<std::string>
  420. cmExportInstallFileGenerator
  421. ::FindNamespaces(cmMakefile* mf, const std::string& name)
  422. {
  423. std::vector<std::string> namespaces;
  424. cmGlobalGenerator* gg = mf->GetLocalGenerator()->GetGlobalGenerator();
  425. const cmExportSetMap& exportSets = gg->GetExportSets();
  426. for(cmExportSetMap::const_iterator expIt = exportSets.begin();
  427. expIt != exportSets.end();
  428. ++expIt)
  429. {
  430. const cmExportSet* exportSet = expIt->second;
  431. std::vector<cmTargetExport*> const* targets =
  432. exportSet->GetTargetExports();
  433. bool containsTarget = false;
  434. for(unsigned int i=0; i<targets->size(); i++)
  435. {
  436. if (name == (*targets)[i]->Target->GetName())
  437. {
  438. containsTarget = true;
  439. break;
  440. }
  441. }
  442. if (containsTarget)
  443. {
  444. std::vector<cmInstallExportGenerator const*> const* installs =
  445. exportSet->GetInstallations();
  446. for(unsigned int i=0; i<installs->size(); i++)
  447. {
  448. namespaces.push_back((*installs)[i]->GetNamespace());
  449. }
  450. }
  451. }
  452. return namespaces;
  453. }
  454. //----------------------------------------------------------------------------
  455. void
  456. cmExportInstallFileGenerator
  457. ::ComplainAboutImportPrefix(cmInstallTargetGenerator* itgen)
  458. {
  459. const char* installDest = this->IEGen->GetDestination();
  460. cmOStringStream e;
  461. e << "install(EXPORT \""
  462. << this->IEGen->GetExportSet()->GetName()
  463. << "\") given absolute "
  464. << "DESTINATION \"" << installDest << "\" but the export "
  465. << "references an installation of target \""
  466. << itgen->GetTarget()->GetName() << "\" which has relative "
  467. << "DESTINATION \"" << itgen->GetDestination() << "\".";
  468. cmSystemTools::Error(e.str().c_str());
  469. }
  470. //----------------------------------------------------------------------------
  471. void
  472. cmExportInstallFileGenerator
  473. ::ComplainAboutMissingTarget(cmTarget* depender,
  474. cmTarget* dependee,
  475. int occurrences)
  476. {
  477. cmOStringStream e;
  478. e << "install(EXPORT \""
  479. << this->IEGen->GetExportSet()->GetName()
  480. << "\" ...) "
  481. << "includes target \"" << depender->GetName()
  482. << "\" which requires target \"" << dependee->GetName() << "\" ";
  483. if (occurrences == 0)
  484. {
  485. e << "that is not in the export set.";
  486. }
  487. else
  488. {
  489. e << "that is not in this export set, but " << occurrences
  490. << " times in others.";
  491. }
  492. cmSystemTools::Error(e.str().c_str());
  493. }
  494. std::string
  495. cmExportInstallFileGenerator::InstallNameDir(cmTarget* target,
  496. const std::string&)
  497. {
  498. std::string install_name_dir;
  499. cmMakefile* mf = target->GetMakefile();
  500. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  501. {
  502. install_name_dir =
  503. target->GetInstallNameDirForInstallTree();
  504. }
  505. return install_name_dir;
  506. }