cmExportInstallFileGenerator.cxx 17 KB

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