cmExportInstallFileGenerator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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)) !=
  231. std::string::npos) {
  232. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  233. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  234. lastPos = endPos;
  235. }
  236. }
  237. bool cmExportInstallFileGenerator::GenerateImportFileConfig(
  238. const std::string& config, std::vector<std::string>& missingTargets)
  239. {
  240. // Skip configurations not enabled for this export.
  241. if (!this->IEGen->InstallsForConfig(config)) {
  242. return true;
  243. }
  244. // Construct the name of the file to generate.
  245. std::string fileName = this->FileDir;
  246. fileName += "/";
  247. fileName += this->FileBase;
  248. fileName += "-";
  249. if (!config.empty()) {
  250. fileName += cmSystemTools::LowerCase(config);
  251. } else {
  252. fileName += "noconfig";
  253. }
  254. fileName += this->FileExt;
  255. // Open the output file to generate it.
  256. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  257. if (!exportFileStream) {
  258. std::string se = cmSystemTools::GetLastSystemError();
  259. std::ostringstream e;
  260. e << "cannot write to file \"" << fileName << "\": " << se;
  261. cmSystemTools::Error(e.str().c_str());
  262. return false;
  263. }
  264. std::ostream& os = exportFileStream;
  265. // Start with the import file header.
  266. this->GenerateImportHeaderCode(os, config);
  267. // Generate the per-config target information.
  268. this->GenerateImportConfig(os, config, missingTargets);
  269. // End with the import file footer.
  270. this->GenerateImportFooterCode(os);
  271. // Record this per-config import file.
  272. this->ConfigImportFiles[config] = fileName;
  273. return true;
  274. }
  275. void cmExportInstallFileGenerator::GenerateImportTargetsConfig(
  276. std::ostream& os, const std::string& config, std::string const& suffix,
  277. std::vector<std::string>& missingTargets)
  278. {
  279. // Add each target in the set to the export.
  280. for (std::vector<cmTargetExport*>::const_iterator tei =
  281. this->IEGen->GetExportSet()->GetTargetExports()->begin();
  282. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei) {
  283. // Collect import properties for this target.
  284. cmTargetExport const* te = *tei;
  285. if (te->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  286. continue;
  287. }
  288. ImportPropertyMap properties;
  289. std::set<std::string> importedLocations;
  290. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  291. properties, importedLocations);
  292. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  293. properties, importedLocations);
  294. this->SetImportLocationProperty(config, suffix, te->RuntimeGenerator,
  295. properties, importedLocations);
  296. this->SetImportLocationProperty(config, suffix, te->ObjectsGenerator,
  297. properties, importedLocations);
  298. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  299. properties, importedLocations);
  300. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  301. properties, importedLocations);
  302. // If any file location was set for the target add it to the
  303. // import file.
  304. if (!properties.empty()) {
  305. // Get the rest of the target details.
  306. cmGeneratorTarget* gtgt = te->Target;
  307. this->SetImportDetailProperties(config, suffix, gtgt, properties,
  308. missingTargets);
  309. this->SetImportLinkInterface(config, suffix,
  310. cmGeneratorExpression::InstallInterface,
  311. gtgt, properties, missingTargets);
  312. // TOOD: PUBLIC_HEADER_LOCATION
  313. // This should wait until the build feature propagation stuff
  314. // is done. Then this can be a propagated include directory.
  315. // this->GenerateImportProperty(config, te->HeaderGenerator,
  316. // properties);
  317. // Generate code in the export file.
  318. this->GenerateImportPropertyCode(os, config, gtgt, properties);
  319. this->GenerateImportedFileChecksCode(os, gtgt, properties,
  320. importedLocations);
  321. }
  322. }
  323. }
  324. void cmExportInstallFileGenerator::SetImportLocationProperty(
  325. const std::string& config, std::string const& suffix,
  326. cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,
  327. std::set<std::string>& importedLocations)
  328. {
  329. // Skip rules that do not match this configuration.
  330. if (!(itgen && itgen->InstallsForConfig(config))) {
  331. return;
  332. }
  333. // Get the target to be installed.
  334. cmGeneratorTarget* target = itgen->GetTarget();
  335. // Construct the installed location of the target.
  336. std::string dest = itgen->GetDestination(config);
  337. std::string value;
  338. if (!cmSystemTools::FileIsFullPath(dest.c_str())) {
  339. // The target is installed relative to the installation prefix.
  340. value = "${_IMPORT_PREFIX}/";
  341. }
  342. value += dest;
  343. value += "/";
  344. if (itgen->IsImportLibrary()) {
  345. // Construct the property name.
  346. std::string prop = "IMPORTED_IMPLIB";
  347. prop += suffix;
  348. // Append the installed file name.
  349. value += itgen->GetInstallFilename(target, config,
  350. cmInstallTargetGenerator::NameImplib);
  351. // Store the property.
  352. properties[prop] = value;
  353. importedLocations.insert(prop);
  354. } else if (itgen->GetTarget()->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  355. // Construct the property name.
  356. std::string prop = "IMPORTED_OBJECTS";
  357. prop += suffix;
  358. // Compute all the object files inside this target and setup
  359. // IMPORTED_OBJECTS as a list of object files
  360. std::vector<std::string> objects;
  361. itgen->GetInstallObjectNames(config, objects);
  362. for (std::vector<std::string>::iterator i = objects.begin();
  363. i != objects.end(); ++i) {
  364. *i = value + *i;
  365. }
  366. // Store the property.
  367. properties[prop] = cmJoin(objects, ";");
  368. importedLocations.insert(prop);
  369. } else {
  370. // Construct the property name.
  371. std::string prop = "IMPORTED_LOCATION";
  372. prop += suffix;
  373. // Append the installed file name.
  374. if (target->IsAppBundleOnApple()) {
  375. value += itgen->GetInstallFilename(target, config);
  376. value += ".app/Contents/MacOS/";
  377. value += itgen->GetInstallFilename(target, config);
  378. } else {
  379. value += itgen->GetInstallFilename(target, config,
  380. cmInstallTargetGenerator::NameReal);
  381. }
  382. // Store the property.
  383. properties[prop] = value;
  384. importedLocations.insert(prop);
  385. }
  386. }
  387. void cmExportInstallFileGenerator::HandleMissingTarget(
  388. std::string& link_libs, std::vector<std::string>& missingTargets,
  389. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  390. {
  391. const std::string name = dependee->GetName();
  392. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  393. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  394. int targetOccurrences = (int)namespaces.size();
  395. if (targetOccurrences == 1) {
  396. std::string missingTarget = namespaces[0];
  397. missingTarget += dependee->GetExportName();
  398. link_libs += missingTarget;
  399. missingTargets.push_back(missingTarget);
  400. } else {
  401. // All exported targets should be known here and should be unique.
  402. // This is probably user-error.
  403. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  404. }
  405. }
  406. std::vector<std::string> cmExportInstallFileGenerator::FindNamespaces(
  407. cmGlobalGenerator* gg, const std::string& name)
  408. {
  409. std::vector<std::string> namespaces;
  410. const cmExportSetMap& exportSets = gg->GetExportSets();
  411. for (cmExportSetMap::const_iterator expIt = exportSets.begin();
  412. expIt != exportSets.end(); ++expIt) {
  413. const cmExportSet* exportSet = expIt->second;
  414. std::vector<cmTargetExport*> const* targets =
  415. exportSet->GetTargetExports();
  416. bool containsTarget = false;
  417. for (unsigned int i = 0; i < targets->size(); i++) {
  418. if (name == (*targets)[i]->TargetName) {
  419. containsTarget = true;
  420. break;
  421. }
  422. }
  423. if (containsTarget) {
  424. std::vector<cmInstallExportGenerator const*> const* installs =
  425. exportSet->GetInstallations();
  426. for (unsigned int i = 0; i < installs->size(); i++) {
  427. namespaces.push_back((*installs)[i]->GetNamespace());
  428. }
  429. }
  430. }
  431. return namespaces;
  432. }
  433. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  434. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  435. {
  436. std::ostringstream e;
  437. e << "install(EXPORT \"" << this->IEGen->GetExportSet()->GetName()
  438. << "\" ...) "
  439. << "includes target \"" << depender->GetName()
  440. << "\" which requires target \"" << dependee->GetName() << "\" ";
  441. if (occurrences == 0) {
  442. e << "that is not in the export set.";
  443. } else {
  444. e << "that is not in this export set, but " << occurrences
  445. << " times in others.";
  446. }
  447. cmSystemTools::Error(e.str().c_str());
  448. }
  449. std::string cmExportInstallFileGenerator::InstallNameDir(
  450. cmGeneratorTarget* target, const std::string& /*config*/)
  451. {
  452. std::string install_name_dir;
  453. cmMakefile* mf = target->Target->GetMakefile();
  454. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  455. install_name_dir = target->GetInstallNameDirForInstallTree();
  456. }
  457. return install_name_dir;
  458. }