1
0

cmExportInstallFileGenerator.cxx 17 KB

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