cmExportInstallFileGenerator.cxx 19 KB

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