cmExportInstallFileGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 "cmAlgorithms.h"
  12. #include "cmExportSet.h"
  13. #include "cmExportSetMap.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmInstallExportGenerator.h"
  17. #include "cmInstallTargetGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmTargetExport.h"
  20. cmExportInstallFileGenerator
  21. ::cmExportInstallFileGenerator(cmInstallExportGenerator* iegen):
  22. IEGen(iegen)
  23. {
  24. }
  25. std::string cmExportInstallFileGenerator::GetConfigImportFileGlob()
  26. {
  27. std::string glob = this->FileBase;
  28. glob += "-*";
  29. glob += this->FileExt;
  30. return glob;
  31. }
  32. bool cmExportInstallFileGenerator::GenerateMainFile(std::ostream& os)
  33. {
  34. std::vector<cmTargetExport*> allTargets;
  35. {
  36. std::string expectedTargets;
  37. std::string sep;
  38. for(std::vector<cmTargetExport*>::const_iterator
  39. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  40. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  41. {
  42. expectedTargets +=
  43. sep + this->Namespace + (*tei)->Target->GetExportName();
  44. sep = " ";
  45. cmTargetExport * te = *tei;
  46. if(this->ExportedTargets.insert(te->Target).second)
  47. {
  48. allTargets.push_back(te);
  49. }
  50. else
  51. {
  52. std::ostringstream e;
  53. e << "install(EXPORT \""
  54. << this->IEGen->GetExportSet()->GetName()
  55. << "\" ...) " << "includes target \"" << te->Target->GetName()
  56. << "\" more than once in the export set.";
  57. cmSystemTools::Error(e.str().c_str());
  58. return false;
  59. }
  60. }
  61. this->GenerateExpectedTargetsCode(os, expectedTargets);
  62. }
  63. // Set an _IMPORT_PREFIX variable for import location properties
  64. // to reference if they are relative to the install prefix.
  65. std::string installPrefix = this->IEGen->GetLocalGenerator()
  66. ->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  67. std::string const& expDest = this->IEGen->GetDestination();
  68. if(cmSystemTools::FileIsFullPath(expDest))
  69. {
  70. // The export file is being installed to an absolute path so the
  71. // package is not relocatable. Use the configured install prefix.
  72. /* clang-format off */
  73. os <<
  74. "# The installation prefix configured by this project.\n"
  75. "set(_IMPORT_PREFIX \"" << installPrefix << "\")\n"
  76. "\n";
  77. /* clang-format on */
  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. /* clang-format off */
  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. /* clang-format on */
  106. }
  107. std::string dest = expDest;
  108. while(!dest.empty())
  109. {
  110. os <<
  111. "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" PATH)\n";
  112. dest = cmSystemTools::GetFilenamePath(dest);
  113. }
  114. os << "\n";
  115. }
  116. std::vector<std::string> missingTargets;
  117. bool require2_8_12 = false;
  118. bool require3_0_0 = false;
  119. bool require3_1_0 = false;
  120. bool requiresConfigFiles = false;
  121. // Create all the imported targets.
  122. for(std::vector<cmTargetExport*>::const_iterator
  123. tei = allTargets.begin();
  124. tei != allTargets.end(); ++tei)
  125. {
  126. cmGeneratorTarget* gt = (*tei)->Target;
  127. requiresConfigFiles = requiresConfigFiles
  128. || gt->GetType() != cmState::INTERFACE_LIBRARY;
  129. this->GenerateImportTargetCode(os, gt);
  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. gt,
  139. cmGeneratorExpression::InstallInterface,
  140. properties, missingTargets);
  141. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
  142. gt,
  143. cmGeneratorExpression::InstallInterface,
  144. properties, missingTargets);
  145. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS",
  146. gt,
  147. cmGeneratorExpression::InstallInterface,
  148. properties, missingTargets);
  149. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS",
  150. gt,
  151. cmGeneratorExpression::InstallInterface,
  152. properties, missingTargets);
  153. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES",
  154. gt,
  155. cmGeneratorExpression::InstallInterface,
  156. properties, missingTargets);
  157. const bool newCMP0022Behavior =
  158. gt->GetPolicyStatusCMP0022() != cmPolicies::WARN
  159. && gt->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  160. if (newCMP0022Behavior)
  161. {
  162. if (this->PopulateInterfaceLinkLibrariesProperty(gt,
  163. cmGeneratorExpression::InstallInterface,
  164. properties, missingTargets)
  165. && !this->ExportOld)
  166. {
  167. require2_8_12 = true;
  168. }
  169. }
  170. if (gt->GetType() == cmState::INTERFACE_LIBRARY)
  171. {
  172. require3_0_0 = true;
  173. }
  174. if(gt->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. gt, properties);
  182. this->PopulateCompatibleInterfaceProperties(gt, properties);
  183. this->GenerateInterfaceProperties(gt, 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. /* clang-format off */
  199. os << "# Load information for each installed configuration.\n"
  200. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  201. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  202. << this->GetConfigImportFileGlob() << "\")\n"
  203. << "foreach(f ${CONFIG_FILES})\n"
  204. << " include(${f})\n"
  205. << "endforeach()\n"
  206. << "\n";
  207. /* clang-format on */
  208. // Cleanup the import prefix variable.
  209. /* clang-format off */
  210. os << "# Cleanup temporary variables.\n"
  211. << "set(_IMPORT_PREFIX)\n"
  212. << "\n";
  213. /* clang-format on */
  214. this->GenerateImportedFileCheckLoop(os);
  215. bool result = true;
  216. // Generate an import file for each configuration.
  217. // Don't do this if we only export INTERFACE_LIBRARY targets.
  218. if (requiresConfigFiles)
  219. {
  220. for(std::vector<std::string>::const_iterator
  221. ci = this->Configurations.begin();
  222. ci != this->Configurations.end(); ++ci)
  223. {
  224. if(!this->GenerateImportFileConfig(*ci, missingTargets))
  225. {
  226. result = false;
  227. }
  228. }
  229. }
  230. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  231. return result;
  232. }
  233. void
  234. cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string &input)
  235. {
  236. std::string::size_type pos = 0;
  237. std::string::size_type lastPos = pos;
  238. while((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos)
  239. {
  240. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  241. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  242. lastPos = endPos;
  243. }
  244. }
  245. bool
  246. cmExportInstallFileGenerator::GenerateImportFileConfig(
  247. const std::string& config,
  248. std::vector<std::string> &missingTargets)
  249. {
  250. // Skip configurations not enabled for this export.
  251. if(!this->IEGen->InstallsForConfig(config))
  252. {
  253. return true;
  254. }
  255. // Construct the name of the file to generate.
  256. std::string fileName = this->FileDir;
  257. fileName += "/";
  258. fileName += this->FileBase;
  259. fileName += "-";
  260. if(!config.empty())
  261. {
  262. fileName += cmSystemTools::LowerCase(config);
  263. }
  264. else
  265. {
  266. fileName += "noconfig";
  267. }
  268. fileName += this->FileExt;
  269. // Open the output file to generate it.
  270. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  271. if(!exportFileStream)
  272. {
  273. std::string se = cmSystemTools::GetLastSystemError();
  274. std::ostringstream e;
  275. e << "cannot write to file \"" << fileName
  276. << "\": " << se;
  277. cmSystemTools::Error(e.str().c_str());
  278. return false;
  279. }
  280. std::ostream& os = exportFileStream;
  281. // Start with the import file header.
  282. this->GenerateImportHeaderCode(os, config);
  283. // Generate the per-config target information.
  284. this->GenerateImportConfig(os, config, missingTargets);
  285. // End with the import file footer.
  286. this->GenerateImportFooterCode(os);
  287. // Record this per-config import file.
  288. this->ConfigImportFiles[config] = fileName;
  289. return true;
  290. }
  291. void
  292. cmExportInstallFileGenerator
  293. ::GenerateImportTargetsConfig(std::ostream& os,
  294. const std::string& config,
  295. std::string const& suffix,
  296. std::vector<std::string> &missingTargets)
  297. {
  298. // Add each target in the set to the export.
  299. for(std::vector<cmTargetExport*>::const_iterator
  300. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  301. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  302. {
  303. // Collect import properties for this target.
  304. cmTargetExport const* te = *tei;
  305. if (te->Target->GetType() == cmState::INTERFACE_LIBRARY)
  306. {
  307. continue;
  308. }
  309. ImportPropertyMap properties;
  310. std::set<std::string> importedLocations;
  311. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  312. properties, importedLocations);
  313. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  314. properties, importedLocations);
  315. this->SetImportLocationProperty(config, suffix,
  316. te->RuntimeGenerator, properties,
  317. importedLocations);
  318. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  319. properties, importedLocations);
  320. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  321. properties, importedLocations);
  322. // If any file location was set for the target add it to the
  323. // import file.
  324. if(!properties.empty())
  325. {
  326. // Get the rest of the target details.
  327. cmGeneratorTarget *gtgt = te->Target;
  328. this->SetImportDetailProperties(config, suffix,
  329. gtgt, properties, missingTargets);
  330. this->SetImportLinkInterface(config, suffix,
  331. cmGeneratorExpression::InstallInterface,
  332. gtgt, properties, missingTargets);
  333. // TOOD: PUBLIC_HEADER_LOCATION
  334. // This should wait until the build feature propagation stuff
  335. // is done. Then this can be a propagated include directory.
  336. // this->GenerateImportProperty(config, te->HeaderGenerator,
  337. // properties);
  338. // Generate code in the export file.
  339. this->GenerateImportPropertyCode(os, config, gtgt, properties);
  340. this->GenerateImportedFileChecksCode(os, gtgt, properties,
  341. importedLocations);
  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. cmGeneratorTarget* target = itgen->GetTarget();
  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. void
  406. cmExportInstallFileGenerator::HandleMissingTarget(std::string& link_libs,
  407. std::vector<std::string>& missingTargets,
  408. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  409. {
  410. const std::string name = dependee->GetName();
  411. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  412. std::vector<std::string> namespaces = this->FindNamespaces(gg, 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. std::vector<std::string>
  429. cmExportInstallFileGenerator
  430. ::FindNamespaces(cmGlobalGenerator* gg, const std::string& name)
  431. {
  432. std::vector<std::string> namespaces;
  433. const cmExportSetMap& exportSets = gg->GetExportSets();
  434. for(cmExportSetMap::const_iterator expIt = exportSets.begin();
  435. expIt != exportSets.end();
  436. ++expIt)
  437. {
  438. const cmExportSet* exportSet = expIt->second;
  439. std::vector<cmTargetExport*> const* targets =
  440. exportSet->GetTargetExports();
  441. bool containsTarget = false;
  442. for(unsigned int i=0; i<targets->size(); i++)
  443. {
  444. if (name == (*targets)[i]->TargetName)
  445. {
  446. containsTarget = true;
  447. break;
  448. }
  449. }
  450. if (containsTarget)
  451. {
  452. std::vector<cmInstallExportGenerator const*> const* installs =
  453. exportSet->GetInstallations();
  454. for(unsigned int i=0; i<installs->size(); i++)
  455. {
  456. namespaces.push_back((*installs)[i]->GetNamespace());
  457. }
  458. }
  459. }
  460. return namespaces;
  461. }
  462. void
  463. cmExportInstallFileGenerator
  464. ::ComplainAboutMissingTarget(cmGeneratorTarget* depender,
  465. cmGeneratorTarget* dependee,
  466. int occurrences)
  467. {
  468. std::ostringstream e;
  469. e << "install(EXPORT \""
  470. << this->IEGen->GetExportSet()->GetName()
  471. << "\" ...) "
  472. << "includes target \"" << depender->GetName()
  473. << "\" which requires target \"" << dependee->GetName() << "\" ";
  474. if (occurrences == 0)
  475. {
  476. e << "that is not in the export set.";
  477. }
  478. else
  479. {
  480. e << "that is not in this export set, but " << occurrences
  481. << " times in others.";
  482. }
  483. cmSystemTools::Error(e.str().c_str());
  484. }
  485. std::string
  486. cmExportInstallFileGenerator::InstallNameDir(cmGeneratorTarget* target,
  487. const std::string&)
  488. {
  489. std::string install_name_dir;
  490. cmMakefile* mf = target->Target->GetMakefile();
  491. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  492. {
  493. install_name_dir =
  494. target->GetInstallNameDirForInstallTree();
  495. }
  496. return install_name_dir;
  497. }