cmExportInstallFileGenerator.cxx 18 KB

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