cmExportInstallFileGenerator.cxx 17 KB

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