cmExportInstallFileGenerator.cxx 17 KB

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