cmExportInstallFileGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 "cmStateTypes.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() != cmStateEnums::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() == cmStateEnums::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(), "/libx32/") ||
  175. cmHasLiteralPrefix(absDestS.c_str(), "/usr/lib/") ||
  176. cmHasLiteralPrefix(absDestS.c_str(), "/usr/lib64/") ||
  177. cmHasLiteralPrefix(absDestS.c_str(), "/usr/libx32/")) {
  178. // Handle "/usr move" symlinks created by some Linux distros.
  179. /* clang-format off */
  180. os <<
  181. "# Use original install prefix when loaded through a\n"
  182. "# cross-prefix symbolic link such as /lib -> /usr/lib.\n"
  183. "get_filename_component(_realCurr \"${_IMPORT_PREFIX}\" REALPATH)\n"
  184. "get_filename_component(_realOrig \"" << absDest << "\" REALPATH)\n"
  185. "if(_realCurr STREQUAL _realOrig)\n"
  186. " set(_IMPORT_PREFIX \"" << absDest << "\")\n"
  187. "endif()\n"
  188. "unset(_realOrig)\n"
  189. "unset(_realCurr)\n";
  190. /* clang-format on */
  191. }
  192. std::string dest = expDest;
  193. while (!dest.empty()) {
  194. os << "get_filename_component(_IMPORT_PREFIX \"${_IMPORT_PREFIX}\" "
  195. "PATH)\n";
  196. dest = cmSystemTools::GetFilenamePath(dest);
  197. }
  198. os << "if(_IMPORT_PREFIX STREQUAL \"/\")\n"
  199. << " set(_IMPORT_PREFIX \"\")\n"
  200. << "endif()\n"
  201. << "\n";
  202. }
  203. }
  204. void cmExportInstallFileGenerator::CleanupTemporaryVariables(std::ostream& os)
  205. {
  206. /* clang-format off */
  207. os << "# Cleanup temporary variables.\n"
  208. << "set(_IMPORT_PREFIX)\n"
  209. << "\n";
  210. /* clang-format on */
  211. }
  212. void cmExportInstallFileGenerator::LoadConfigFiles(std::ostream& os)
  213. {
  214. // Now load per-configuration properties for them.
  215. /* clang-format off */
  216. os << "# Load information for each installed configuration.\n"
  217. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  218. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  219. << this->GetConfigImportFileGlob() << "\")\n"
  220. << "foreach(f ${CONFIG_FILES})\n"
  221. << " include(${f})\n"
  222. << "endforeach()\n"
  223. << "\n";
  224. /* clang-format on */
  225. }
  226. void cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string& input)
  227. {
  228. std::string::size_type pos = 0;
  229. std::string::size_type lastPos = pos;
  230. while ((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos) {
  231. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  232. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  233. lastPos = endPos;
  234. }
  235. }
  236. bool cmExportInstallFileGenerator::GenerateImportFileConfig(
  237. const std::string& config, std::vector<std::string>& missingTargets)
  238. {
  239. // Skip configurations not enabled for this export.
  240. if (!this->IEGen->InstallsForConfig(config)) {
  241. return true;
  242. }
  243. // Construct the name of the file to generate.
  244. std::string fileName = this->FileDir;
  245. fileName += "/";
  246. fileName += this->FileBase;
  247. fileName += "-";
  248. if (!config.empty()) {
  249. fileName += cmSystemTools::LowerCase(config);
  250. } else {
  251. fileName += "noconfig";
  252. }
  253. fileName += this->FileExt;
  254. // Open the output file to generate it.
  255. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  256. if (!exportFileStream) {
  257. std::string se = cmSystemTools::GetLastSystemError();
  258. std::ostringstream e;
  259. e << "cannot write to file \"" << fileName << "\": " << se;
  260. cmSystemTools::Error(e.str().c_str());
  261. return false;
  262. }
  263. std::ostream& os = exportFileStream;
  264. // Start with the import file header.
  265. this->GenerateImportHeaderCode(os, config);
  266. // Generate the per-config target information.
  267. this->GenerateImportConfig(os, config, missingTargets);
  268. // End with the import file footer.
  269. this->GenerateImportFooterCode(os);
  270. // Record this per-config import file.
  271. this->ConfigImportFiles[config] = fileName;
  272. return true;
  273. }
  274. void cmExportInstallFileGenerator::GenerateImportTargetsConfig(
  275. std::ostream& os, const std::string& config, std::string const& suffix,
  276. std::vector<std::string>& missingTargets)
  277. {
  278. // Add each target in the set to the export.
  279. for (std::vector<cmTargetExport*>::const_iterator tei =
  280. this->IEGen->GetExportSet()->GetTargetExports()->begin();
  281. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei) {
  282. // Collect import properties for this target.
  283. cmTargetExport const* te = *tei;
  284. if (te->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  285. continue;
  286. }
  287. ImportPropertyMap properties;
  288. std::set<std::string> importedLocations;
  289. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  290. properties, importedLocations);
  291. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  292. properties, importedLocations);
  293. this->SetImportLocationProperty(config, suffix, te->RuntimeGenerator,
  294. properties, importedLocations);
  295. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  296. properties, importedLocations);
  297. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  298. properties, importedLocations);
  299. // If any file location was set for the target add it to the
  300. // import file.
  301. if (!properties.empty()) {
  302. // Get the rest of the target details.
  303. cmGeneratorTarget* gtgt = te->Target;
  304. this->SetImportDetailProperties(config, suffix, gtgt, properties,
  305. missingTargets);
  306. this->SetImportLinkInterface(config, suffix,
  307. cmGeneratorExpression::InstallInterface,
  308. gtgt, properties, missingTargets);
  309. // TOOD: PUBLIC_HEADER_LOCATION
  310. // This should wait until the build feature propagation stuff
  311. // is done. Then this can be a propagated include directory.
  312. // this->GenerateImportProperty(config, te->HeaderGenerator,
  313. // properties);
  314. // Generate code in the export file.
  315. this->GenerateImportPropertyCode(os, config, gtgt, properties);
  316. this->GenerateImportedFileChecksCode(os, gtgt, properties,
  317. importedLocations);
  318. }
  319. }
  320. }
  321. void cmExportInstallFileGenerator::SetImportLocationProperty(
  322. const std::string& config, std::string const& suffix,
  323. cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,
  324. std::set<std::string>& importedLocations)
  325. {
  326. // Skip rules that do not match this configuration.
  327. if (!(itgen && itgen->InstallsForConfig(config))) {
  328. return;
  329. }
  330. // Get the target to be installed.
  331. cmGeneratorTarget* target = itgen->GetTarget();
  332. // Construct the installed location of the target.
  333. std::string dest = itgen->GetDestination(config);
  334. std::string value;
  335. if (!cmSystemTools::FileIsFullPath(dest.c_str())) {
  336. // The target is installed relative to the installation prefix.
  337. value = "${_IMPORT_PREFIX}/";
  338. }
  339. value += dest;
  340. value += "/";
  341. if (itgen->IsImportLibrary()) {
  342. // Construct the property name.
  343. std::string prop = "IMPORTED_IMPLIB";
  344. prop += suffix;
  345. // Append the installed file name.
  346. value += itgen->GetInstallFilename(target, config,
  347. cmInstallTargetGenerator::NameImplib);
  348. // Store the property.
  349. properties[prop] = value;
  350. importedLocations.insert(prop);
  351. } else {
  352. // Construct the property name.
  353. std::string prop = "IMPORTED_LOCATION";
  354. prop += suffix;
  355. // Append the installed file name.
  356. if (target->IsAppBundleOnApple()) {
  357. value += itgen->GetInstallFilename(target, config);
  358. value += ".app/Contents/MacOS/";
  359. value += itgen->GetInstallFilename(target, config);
  360. } else {
  361. value += itgen->GetInstallFilename(target, config,
  362. cmInstallTargetGenerator::NameReal);
  363. }
  364. // Store the property.
  365. properties[prop] = value;
  366. importedLocations.insert(prop);
  367. }
  368. }
  369. void cmExportInstallFileGenerator::HandleMissingTarget(
  370. std::string& link_libs, std::vector<std::string>& missingTargets,
  371. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  372. {
  373. const std::string name = dependee->GetName();
  374. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  375. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  376. int targetOccurrences = (int)namespaces.size();
  377. if (targetOccurrences == 1) {
  378. std::string missingTarget = namespaces[0];
  379. missingTarget += dependee->GetExportName();
  380. link_libs += missingTarget;
  381. missingTargets.push_back(missingTarget);
  382. } else {
  383. // All exported targets should be known here and should be unique.
  384. // This is probably user-error.
  385. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  386. }
  387. }
  388. std::vector<std::string> cmExportInstallFileGenerator::FindNamespaces(
  389. cmGlobalGenerator* gg, const std::string& name)
  390. {
  391. std::vector<std::string> namespaces;
  392. const cmExportSetMap& exportSets = gg->GetExportSets();
  393. for (cmExportSetMap::const_iterator expIt = exportSets.begin();
  394. expIt != exportSets.end(); ++expIt) {
  395. const cmExportSet* exportSet = expIt->second;
  396. std::vector<cmTargetExport*> const* targets =
  397. exportSet->GetTargetExports();
  398. bool containsTarget = false;
  399. for (unsigned int i = 0; i < targets->size(); i++) {
  400. if (name == (*targets)[i]->TargetName) {
  401. containsTarget = true;
  402. break;
  403. }
  404. }
  405. if (containsTarget) {
  406. std::vector<cmInstallExportGenerator const*> const* installs =
  407. exportSet->GetInstallations();
  408. for (unsigned int i = 0; i < installs->size(); i++) {
  409. namespaces.push_back((*installs)[i]->GetNamespace());
  410. }
  411. }
  412. }
  413. return namespaces;
  414. }
  415. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  416. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  417. {
  418. std::ostringstream e;
  419. e << "install(EXPORT \"" << this->IEGen->GetExportSet()->GetName()
  420. << "\" ...) "
  421. << "includes target \"" << depender->GetName()
  422. << "\" which requires target \"" << dependee->GetName() << "\" ";
  423. if (occurrences == 0) {
  424. e << "that is not in the export set.";
  425. } else {
  426. e << "that is not in this export set, but " << occurrences
  427. << " times in others.";
  428. }
  429. cmSystemTools::Error(e.str().c_str());
  430. }
  431. std::string cmExportInstallFileGenerator::InstallNameDir(
  432. cmGeneratorTarget* target, const std::string& /*config*/)
  433. {
  434. std::string install_name_dir;
  435. cmMakefile* mf = target->Target->GetMakefile();
  436. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  437. install_name_dir = target->GetInstallNameDirForInstallTree();
  438. }
  439. return install_name_dir;
  440. }