cmExportInstallFileGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 << "if(_IMPORT_PREFIX STREQUAL \"/\")\n"
  117. << " set(_IMPORT_PREFIX \"\")\n"
  118. << "endif()\n"
  119. << "\n";
  120. }
  121. std::vector<std::string> missingTargets;
  122. bool require2_8_12 = false;
  123. bool require3_0_0 = false;
  124. bool require3_1_0 = false;
  125. bool requiresConfigFiles = false;
  126. // Create all the imported targets.
  127. for (std::vector<cmTargetExport*>::const_iterator tei = allTargets.begin();
  128. tei != allTargets.end(); ++tei) {
  129. cmGeneratorTarget* gt = (*tei)->Target;
  130. requiresConfigFiles =
  131. requiresConfigFiles || gt->GetType() != cmState::INTERFACE_LIBRARY;
  132. this->GenerateImportTargetCode(os, gt);
  133. ImportPropertyMap properties;
  134. this->PopulateIncludeDirectoriesInterface(
  135. *tei, cmGeneratorExpression::InstallInterface, properties,
  136. missingTargets);
  137. this->PopulateSourcesInterface(*tei,
  138. cmGeneratorExpression::InstallInterface,
  139. properties, missingTargets);
  140. this->PopulateInterfaceProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES", gt,
  141. cmGeneratorExpression::InstallInterface,
  142. properties, missingTargets);
  143. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gt,
  144. cmGeneratorExpression::InstallInterface,
  145. properties, missingTargets);
  146. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gt,
  147. cmGeneratorExpression::InstallInterface,
  148. properties, missingTargets);
  149. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gt,
  150. cmGeneratorExpression::InstallInterface,
  151. properties, missingTargets);
  152. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gt,
  153. cmGeneratorExpression::InstallInterface,
  154. properties, missingTargets);
  155. const bool newCMP0022Behavior =
  156. gt->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  157. gt->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  158. if (newCMP0022Behavior) {
  159. if (this->PopulateInterfaceLinkLibrariesProperty(
  160. gt, cmGeneratorExpression::InstallInterface, properties,
  161. missingTargets) &&
  162. !this->ExportOld) {
  163. require2_8_12 = true;
  164. }
  165. }
  166. if (gt->GetType() == cmState::INTERFACE_LIBRARY) {
  167. require3_0_0 = true;
  168. }
  169. if (gt->GetProperty("INTERFACE_SOURCES")) {
  170. // We can only generate INTERFACE_SOURCES in CMake 3.3, but CMake 3.1
  171. // can consume them.
  172. require3_1_0 = true;
  173. }
  174. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gt,
  175. properties);
  176. this->PopulateCompatibleInterfaceProperties(gt, properties);
  177. this->GenerateInterfaceProperties(gt, os, properties);
  178. }
  179. if (require3_1_0) {
  180. this->GenerateRequiredCMakeVersion(os, "3.1.0");
  181. } else if (require3_0_0) {
  182. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  183. } else if (require2_8_12) {
  184. this->GenerateRequiredCMakeVersion(os, "2.8.12");
  185. }
  186. // Now load per-configuration properties for them.
  187. /* clang-format off */
  188. os << "# Load information for each installed configuration.\n"
  189. << "get_filename_component(_DIR \"${CMAKE_CURRENT_LIST_FILE}\" PATH)\n"
  190. << "file(GLOB CONFIG_FILES \"${_DIR}/"
  191. << this->GetConfigImportFileGlob() << "\")\n"
  192. << "foreach(f ${CONFIG_FILES})\n"
  193. << " include(${f})\n"
  194. << "endforeach()\n"
  195. << "\n";
  196. /* clang-format on */
  197. // Cleanup the import prefix variable.
  198. /* clang-format off */
  199. os << "# Cleanup temporary variables.\n"
  200. << "set(_IMPORT_PREFIX)\n"
  201. << "\n";
  202. /* clang-format on */
  203. this->GenerateImportedFileCheckLoop(os);
  204. bool result = true;
  205. // Generate an import file for each configuration.
  206. // Don't do this if we only export INTERFACE_LIBRARY targets.
  207. if (requiresConfigFiles) {
  208. for (std::vector<std::string>::const_iterator ci =
  209. this->Configurations.begin();
  210. ci != this->Configurations.end(); ++ci) {
  211. if (!this->GenerateImportFileConfig(*ci, missingTargets)) {
  212. result = false;
  213. }
  214. }
  215. }
  216. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  217. return result;
  218. }
  219. void cmExportInstallFileGenerator::ReplaceInstallPrefix(std::string& input)
  220. {
  221. std::string::size_type pos = 0;
  222. std::string::size_type lastPos = pos;
  223. while ((pos = input.find("$<INSTALL_PREFIX>", lastPos)) != input.npos) {
  224. std::string::size_type endPos = pos + sizeof("$<INSTALL_PREFIX>") - 1;
  225. input.replace(pos, endPos - pos, "${_IMPORT_PREFIX}");
  226. lastPos = endPos;
  227. }
  228. }
  229. bool cmExportInstallFileGenerator::GenerateImportFileConfig(
  230. const std::string& config, std::vector<std::string>& missingTargets)
  231. {
  232. // Skip configurations not enabled for this export.
  233. if (!this->IEGen->InstallsForConfig(config)) {
  234. return true;
  235. }
  236. // Construct the name of the file to generate.
  237. std::string fileName = this->FileDir;
  238. fileName += "/";
  239. fileName += this->FileBase;
  240. fileName += "-";
  241. if (!config.empty()) {
  242. fileName += cmSystemTools::LowerCase(config);
  243. } else {
  244. fileName += "noconfig";
  245. }
  246. fileName += this->FileExt;
  247. // Open the output file to generate it.
  248. cmGeneratedFileStream exportFileStream(fileName.c_str(), true);
  249. if (!exportFileStream) {
  250. std::string se = cmSystemTools::GetLastSystemError();
  251. std::ostringstream e;
  252. e << "cannot write to file \"" << fileName << "\": " << se;
  253. cmSystemTools::Error(e.str().c_str());
  254. return false;
  255. }
  256. std::ostream& os = exportFileStream;
  257. // Start with the import file header.
  258. this->GenerateImportHeaderCode(os, config);
  259. // Generate the per-config target information.
  260. this->GenerateImportConfig(os, config, missingTargets);
  261. // End with the import file footer.
  262. this->GenerateImportFooterCode(os);
  263. // Record this per-config import file.
  264. this->ConfigImportFiles[config] = fileName;
  265. return true;
  266. }
  267. void cmExportInstallFileGenerator::GenerateImportTargetsConfig(
  268. std::ostream& os, const std::string& config, std::string const& suffix,
  269. std::vector<std::string>& missingTargets)
  270. {
  271. // Add each target in the set to the export.
  272. for (std::vector<cmTargetExport*>::const_iterator tei =
  273. this->IEGen->GetExportSet()->GetTargetExports()->begin();
  274. tei != this->IEGen->GetExportSet()->GetTargetExports()->end(); ++tei) {
  275. // Collect import properties for this target.
  276. cmTargetExport const* te = *tei;
  277. if (te->Target->GetType() == cmState::INTERFACE_LIBRARY) {
  278. continue;
  279. }
  280. ImportPropertyMap properties;
  281. std::set<std::string> importedLocations;
  282. this->SetImportLocationProperty(config, suffix, te->ArchiveGenerator,
  283. properties, importedLocations);
  284. this->SetImportLocationProperty(config, suffix, te->LibraryGenerator,
  285. properties, importedLocations);
  286. this->SetImportLocationProperty(config, suffix, te->RuntimeGenerator,
  287. properties, importedLocations);
  288. this->SetImportLocationProperty(config, suffix, te->FrameworkGenerator,
  289. properties, importedLocations);
  290. this->SetImportLocationProperty(config, suffix, te->BundleGenerator,
  291. properties, importedLocations);
  292. // If any file location was set for the target add it to the
  293. // import file.
  294. if (!properties.empty()) {
  295. // Get the rest of the target details.
  296. cmGeneratorTarget* gtgt = te->Target;
  297. this->SetImportDetailProperties(config, suffix, gtgt, properties,
  298. missingTargets);
  299. this->SetImportLinkInterface(config, suffix,
  300. cmGeneratorExpression::InstallInterface,
  301. gtgt, properties, missingTargets);
  302. // TOOD: PUBLIC_HEADER_LOCATION
  303. // This should wait until the build feature propagation stuff
  304. // is done. Then this can be a propagated include directory.
  305. // this->GenerateImportProperty(config, te->HeaderGenerator,
  306. // properties);
  307. // Generate code in the export file.
  308. this->GenerateImportPropertyCode(os, config, gtgt, properties);
  309. this->GenerateImportedFileChecksCode(os, gtgt, properties,
  310. importedLocations);
  311. }
  312. }
  313. }
  314. void cmExportInstallFileGenerator::SetImportLocationProperty(
  315. const std::string& config, std::string const& suffix,
  316. cmInstallTargetGenerator* itgen, ImportPropertyMap& properties,
  317. std::set<std::string>& importedLocations)
  318. {
  319. // Skip rules that do not match this configuration.
  320. if (!(itgen && itgen->InstallsForConfig(config))) {
  321. return;
  322. }
  323. // Get the target to be installed.
  324. cmGeneratorTarget* target = itgen->GetTarget();
  325. // Construct the installed location of the target.
  326. std::string dest = itgen->GetDestination(config);
  327. std::string value;
  328. if (!cmSystemTools::FileIsFullPath(dest.c_str())) {
  329. // The target is installed relative to the installation prefix.
  330. value = "${_IMPORT_PREFIX}/";
  331. }
  332. value += dest;
  333. value += "/";
  334. if (itgen->IsImportLibrary()) {
  335. // Construct the property name.
  336. std::string prop = "IMPORTED_IMPLIB";
  337. prop += suffix;
  338. // Append the installed file name.
  339. value += itgen->GetInstallFilename(target, config,
  340. cmInstallTargetGenerator::NameImplib);
  341. // Store the property.
  342. properties[prop] = value;
  343. importedLocations.insert(prop);
  344. } else {
  345. // Construct the property name.
  346. std::string prop = "IMPORTED_LOCATION";
  347. prop += suffix;
  348. // Append the installed file name.
  349. if (target->IsAppBundleOnApple()) {
  350. value += itgen->GetInstallFilename(target, config);
  351. value += ".app/Contents/MacOS/";
  352. value += itgen->GetInstallFilename(target, config);
  353. } else {
  354. value += itgen->GetInstallFilename(target, config,
  355. cmInstallTargetGenerator::NameReal);
  356. }
  357. // Store the property.
  358. properties[prop] = value;
  359. importedLocations.insert(prop);
  360. }
  361. }
  362. void cmExportInstallFileGenerator::HandleMissingTarget(
  363. std::string& link_libs, std::vector<std::string>& missingTargets,
  364. cmGeneratorTarget* depender, cmGeneratorTarget* dependee)
  365. {
  366. const std::string name = dependee->GetName();
  367. cmGlobalGenerator* gg = dependee->GetLocalGenerator()->GetGlobalGenerator();
  368. std::vector<std::string> namespaces = this->FindNamespaces(gg, name);
  369. int targetOccurrences = (int)namespaces.size();
  370. if (targetOccurrences == 1) {
  371. std::string missingTarget = namespaces[0];
  372. missingTarget += dependee->GetExportName();
  373. link_libs += missingTarget;
  374. missingTargets.push_back(missingTarget);
  375. } else {
  376. // All exported targets should be known here and should be unique.
  377. // This is probably user-error.
  378. this->ComplainAboutMissingTarget(depender, dependee, targetOccurrences);
  379. }
  380. }
  381. std::vector<std::string> cmExportInstallFileGenerator::FindNamespaces(
  382. cmGlobalGenerator* gg, const std::string& name)
  383. {
  384. std::vector<std::string> namespaces;
  385. const cmExportSetMap& exportSets = gg->GetExportSets();
  386. for (cmExportSetMap::const_iterator expIt = exportSets.begin();
  387. expIt != exportSets.end(); ++expIt) {
  388. const cmExportSet* exportSet = expIt->second;
  389. std::vector<cmTargetExport*> const* targets =
  390. exportSet->GetTargetExports();
  391. bool containsTarget = false;
  392. for (unsigned int i = 0; i < targets->size(); i++) {
  393. if (name == (*targets)[i]->TargetName) {
  394. containsTarget = true;
  395. break;
  396. }
  397. }
  398. if (containsTarget) {
  399. std::vector<cmInstallExportGenerator const*> const* installs =
  400. exportSet->GetInstallations();
  401. for (unsigned int i = 0; i < installs->size(); i++) {
  402. namespaces.push_back((*installs)[i]->GetNamespace());
  403. }
  404. }
  405. }
  406. return namespaces;
  407. }
  408. void cmExportInstallFileGenerator::ComplainAboutMissingTarget(
  409. cmGeneratorTarget* depender, cmGeneratorTarget* dependee, int occurrences)
  410. {
  411. std::ostringstream e;
  412. e << "install(EXPORT \"" << this->IEGen->GetExportSet()->GetName()
  413. << "\" ...) "
  414. << "includes target \"" << depender->GetName()
  415. << "\" which requires target \"" << dependee->GetName() << "\" ";
  416. if (occurrences == 0) {
  417. e << "that is not in the export set.";
  418. } else {
  419. e << "that is not in this export set, but " << occurrences
  420. << " times in others.";
  421. }
  422. cmSystemTools::Error(e.str().c_str());
  423. }
  424. std::string cmExportInstallFileGenerator::InstallNameDir(
  425. cmGeneratorTarget* target, const std::string& /*config*/)
  426. {
  427. std::string install_name_dir;
  428. cmMakefile* mf = target->Target->GetMakefile();
  429. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  430. install_name_dir = target->GetInstallNameDirForInstallTree();
  431. }
  432. return install_name_dir;
  433. }