cmExportInstallFileGenerator.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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).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. requiresConfigFiles = requiresConfigFiles
  127. || gt->GetType() != cmState::INTERFACE_LIBRARY;
  128. this->GenerateImportTargetCode(os, gt);
  129. ImportPropertyMap properties;
  130. this->PopulateIncludeDirectoriesInterface(*tei,
  131. cmGeneratorExpression::InstallInterface,
  132. properties, missingTargets);
  133. this->PopulateSourcesInterface(*tei,
  134. cmGeneratorExpression::InstallInterface,
  135. properties, missingTargets);
  136. this->PopulateInterfaceProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES",
  137. gt,
  138. cmGeneratorExpression::InstallInterface,
  139. properties, missingTargets);
  140. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS",
  141. gt,
  142. cmGeneratorExpression::InstallInterface,
  143. properties, missingTargets);
  144. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS",
  145. gt,
  146. cmGeneratorExpression::InstallInterface,
  147. properties, missingTargets);
  148. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS",
  149. gt,
  150. cmGeneratorExpression::InstallInterface,
  151. properties, missingTargets);
  152. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES",
  153. gt,
  154. cmGeneratorExpression::InstallInterface,
  155. properties, missingTargets);
  156. const bool newCMP0022Behavior =
  157. gt->Target->GetPolicyStatusCMP0022() != cmPolicies::WARN
  158. && gt->Target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  159. if (newCMP0022Behavior)
  160. {
  161. if (this->PopulateInterfaceLinkLibrariesProperty(gt,
  162. cmGeneratorExpression::InstallInterface,
  163. properties, missingTargets)
  164. && !this->ExportOld)
  165. {
  166. require2_8_12 = true;
  167. }
  168. }
  169. if (gt->GetType() == cmState::INTERFACE_LIBRARY)
  170. {
  171. require3_0_0 = true;
  172. }
  173. if(gt->GetProperty("INTERFACE_SOURCES"))
  174. {
  175. // We can only generate INTERFACE_SOURCES in CMake 3.3, but CMake 3.1
  176. // can consume them.
  177. require3_1_0 = true;
  178. }
  179. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE",
  180. gt, properties);
  181. this->PopulateCompatibleInterfaceProperties(gt, properties);
  182. this->GenerateInterfaceProperties(gt, os, properties);
  183. }
  184. if (require3_1_0)
  185. {
  186. this->GenerateRequiredCMakeVersion(os, "3.1.0");
  187. }
  188. else if (require3_0_0)
  189. {
  190. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  191. }
  192. else if (require2_8_12)
  193. {
  194. this->GenerateRequiredCMakeVersion(os, "2.8.12");
  195. }
  196. // Now load per-configuration properties for them.
  197. os << "# Load information for each installed configuration.\n"
  198. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  199. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  200. << this->GetConfigImportFileGlob() << "\")\n"
  201. << "foreach(f ${CONFIG_FILES})\n"
  202. << " include(${f})\n"
  203. << "endforeach()\n"
  204. << "\n";
  205. // Cleanup the import prefix variable.
  206. os << "# Cleanup temporary variables.\n"
  207. << "set(_IMPORT_PREFIX)\n"
  208. << "\n";
  209. this->GenerateImportedFileCheckLoop(os);
  210. bool result = true;
  211. // Generate an import file for each configuration.
  212. // Don't do this if we only export INTERFACE_LIBRARY targets.
  213. if (requiresConfigFiles)
  214. {
  215. for(std::vector<std::string>::const_iterator
  216. ci = this->Configurations.begin();
  217. ci != this->Configurations.end(); ++ci)
  218. {
  219. if(!this->GenerateImportFileConfig(*ci, missingTargets))
  220. {
  221. result = false;
  222. }
  223. }
  224. }
  225. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  226. return result;
  227. }
  228. //----------------------------------------------------------------------------
  229. void
  230. cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string &input)
  231. {
  232. std::string::size_type pos = 0;
  233. std::string::size_type lastPos = pos;
  234. while((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos)
  235. {
  236. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  237. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  238. lastPos = endPos;
  239. }
  240. }
  241. //----------------------------------------------------------------------------
  242. bool
  243. cmExportInstallFileGenerator::GenerateImportFileConfig(
  244. const std::string& config,
  245. std::vector<std::string> &missingTargets)
  246. {
  247. // Skip configurations not enabled for this export.
  248. if(!this->IEGen->InstallsForConfig(config))
  249. {
  250. return true;
  251. }
  252. // Construct the name of the file to generate.
  253. std::string fileName = this->FileDir;
  254. fileName += "/";
  255. fileName += this->FileBase;
  256. fileName += "-";
  257. if(!config.empty())
  258. {
  259. fileName += cmSystemTools::LowerCase(config);
  260. }
  261. else
  262. {
  263. fileName += "noconfig";
  264. }
  265. fileName += this->FileExt;
  266. // Open the output file to generate it.
  267. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  268. if(!exportFileStream)
  269. {
  270. std::string se = cmSystemTools::GetLastSystemError();
  271. std::ostringstream e;
  272. e << "cannot write to file \"" << fileName
  273. << "\": " << se;
  274. cmSystemTools::Error(e.str().c_str());
  275. return false;
  276. }
  277. std::ostream& os = exportFileStream;
  278. // Start with the import file header.
  279. this->GenerateImportHeaderCode(os, config);
  280. // Generate the per-config target information.
  281. this->GenerateImportConfig(os, config, missingTargets);
  282. // End with the import file footer.
  283. this->GenerateImportFooterCode(os);
  284. // Record this per-config import file.
  285. this->ConfigImportFiles[config] = fileName;
  286. return true;
  287. }
  288. //----------------------------------------------------------------------------
  289. void
  290. cmExportInstallFileGenerator
  291. ::GenerateImportTargetsConfig(std::ostream& os,
  292. const std::string& config,
  293. std::string const& suffix,
  294. std::vector<std::string> &missingTargets)
  295. {
  296. // Add each target in the set to the export.
  297. for(std::vector<cmTargetExport*>::const_iterator
  298. tei = this->IEGen->GetExportSet()->GetTargetExports()->begin();
  299. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei)
  300. {
  301. // Collect import properties for this target.
  302. cmTargetExport const* te = *tei;
  303. if (te->Target->GetType() == cmState::INTERFACE_LIBRARY)
  304. {
  305. continue;
  306. }
  307. ImportPropertyMap properties;
  308. std::set<std::string> importedLocations;
  309. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  310. properties, importedLocations);
  311. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  312. properties, importedLocations);
  313. this->SetImportLocationProperty(config, suffix,
  314. te->RuntimeGenerator, properties,
  315. importedLocations);
  316. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  317. properties, importedLocations);
  318. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  319. properties, importedLocations);
  320. // If any file location was set for the target add it to the
  321. // import file.
  322. if(!properties.empty())
  323. {
  324. // Get the rest of the target details.
  325. cmGeneratorTarget *gtgt = 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, gtgt->Target, properties);
  338. this->GenerateImportedFileChecksCode(os, gtgt->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. cmGeneratorTarget* 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->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. cmTarget* depender, cmTarget* dependee)
  409. {
  410. const std::string name = dependee->GetName();
  411. cmGlobalGenerator* gg = dependee->GetMakefile()->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. //----------------------------------------------------------------------------
  429. std::vector<std::string>
  430. cmExportInstallFileGenerator
  431. ::FindNamespaces(cmGlobalGenerator* gg, const std::string& name)
  432. {
  433. std::vector<std::string> namespaces;
  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(cmGeneratorTarget* target,
  489. const std::string&)
  490. {
  491. std::string install_name_dir;
  492. cmMakefile* mf = target->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. }