cmExportInstallFileGenerator.cxx 17 KB

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