cmExportInstallFileGenerator.cxx 19 KB

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