cmExportInstallFileGenerator.cxx 17 KB

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