cmExportFileGenerator.cxx 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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 "cmExportFileGenerator.h"
  11. #include "cmExportSet.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmInstallExportGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSystemTools.h"
  18. #include "cmTarget.h"
  19. #include "cmTargetExport.h"
  20. #include "cmVersion.h"
  21. #include "cmComputeLinkInformation.h"
  22. #include <cmsys/auto_ptr.hxx>
  23. #include <assert.h>
  24. //----------------------------------------------------------------------------
  25. cmExportFileGenerator::cmExportFileGenerator()
  26. {
  27. this->AppendMode = false;
  28. this->ExportOld = false;
  29. }
  30. //----------------------------------------------------------------------------
  31. void cmExportFileGenerator::AddConfiguration(const char* config)
  32. {
  33. this->Configurations.push_back(config);
  34. }
  35. //----------------------------------------------------------------------------
  36. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  37. {
  38. this->MainImportFile = mainFile;
  39. this->FileDir =
  40. cmSystemTools::GetFilenamePath(this->MainImportFile);
  41. this->FileBase =
  42. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  43. this->FileExt =
  44. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  45. }
  46. //----------------------------------------------------------------------------
  47. bool cmExportFileGenerator::GenerateImportFile()
  48. {
  49. // Open the output file to generate it.
  50. cmsys::auto_ptr<std::ofstream> foutPtr;
  51. if(this->AppendMode)
  52. {
  53. // Open for append.
  54. cmsys::auto_ptr<std::ofstream>
  55. ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
  56. foutPtr = ap;
  57. }
  58. else
  59. {
  60. // Generate atomically and with copy-if-different.
  61. cmsys::auto_ptr<cmGeneratedFileStream>
  62. ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
  63. ap->SetCopyIfDifferent(true);
  64. foutPtr = ap;
  65. }
  66. if(!foutPtr.get() || !*foutPtr)
  67. {
  68. std::string se = cmSystemTools::GetLastSystemError();
  69. cmOStringStream e;
  70. e << "cannot write to file \"" << this->MainImportFile
  71. << "\": " << se;
  72. cmSystemTools::Error(e.str().c_str());
  73. return false;
  74. }
  75. std::ostream& os = *foutPtr;
  76. // Protect that file against use with older CMake versions.
  77. os << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
  78. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
  79. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  80. << "endif()\n";
  81. // Isolate the file policy level.
  82. // We use 2.6 here instead of the current version because newer
  83. // versions of CMake should be able to export files imported by 2.6
  84. // until the import format changes.
  85. os << "cmake_policy(PUSH)\n"
  86. << "cmake_policy(VERSION 2.6)\n";
  87. // Start with the import file header.
  88. this->GenerateImportHeaderCode(os);
  89. // Create all the imported targets.
  90. bool result = this->GenerateMainFile(os);
  91. // End with the import file footer.
  92. this->GenerateImportFooterCode(os);
  93. os << "cmake_policy(POP)\n";
  94. return result;
  95. }
  96. //----------------------------------------------------------------------------
  97. void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
  98. const char* config,
  99. std::vector<std::string> &missingTargets)
  100. {
  101. // Construct the property configuration suffix.
  102. std::string suffix = "_";
  103. if(config && *config)
  104. {
  105. suffix += cmSystemTools::UpperCase(config);
  106. }
  107. else
  108. {
  109. suffix += "NOCONFIG";
  110. }
  111. // Generate the per-config target information.
  112. this->GenerateImportTargetsConfig(os, config, suffix, missingTargets);
  113. }
  114. //----------------------------------------------------------------------------
  115. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  116. cmTarget *target,
  117. ImportPropertyMap &properties)
  118. {
  119. const char *input = target->GetProperty(propName);
  120. if (input)
  121. {
  122. properties[propName] = input;
  123. }
  124. }
  125. //----------------------------------------------------------------------------
  126. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  127. const char *outputName,
  128. cmTarget *target,
  129. cmGeneratorExpression::PreprocessContext preprocessRule,
  130. ImportPropertyMap &properties,
  131. std::vector<std::string> &missingTargets)
  132. {
  133. const char *input = target->GetProperty(propName);
  134. if (input)
  135. {
  136. if (!*input)
  137. {
  138. // Set to empty
  139. properties[outputName] = "";
  140. return;
  141. }
  142. std::string prepro = cmGeneratorExpression::Preprocess(input,
  143. preprocessRule);
  144. if (!prepro.empty())
  145. {
  146. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  147. missingTargets);
  148. properties[outputName] = prepro;
  149. }
  150. }
  151. }
  152. void cmExportFileGenerator::GenerateRequiredCMakeVersion(std::ostream& os,
  153. const char *versionString)
  154. {
  155. os << "if(CMAKE_VERSION VERSION_LESS " << versionString << ")\n"
  156. " message(FATAL_ERROR \"This file relies on consumers using "
  157. "CMake " << versionString << " or greater.\")\n"
  158. "endif()\n\n";
  159. }
  160. //----------------------------------------------------------------------------
  161. bool cmExportFileGenerator::PopulateInterfaceLinkLibrariesProperty(
  162. cmTarget *target,
  163. cmGeneratorExpression::PreprocessContext preprocessRule,
  164. ImportPropertyMap &properties,
  165. std::vector<std::string> &missingTargets)
  166. {
  167. const char *input = target->GetProperty("INTERFACE_LINK_LIBRARIES");
  168. if (input)
  169. {
  170. std::string prepro = cmGeneratorExpression::Preprocess(input,
  171. preprocessRule);
  172. if (!prepro.empty())
  173. {
  174. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  175. missingTargets,
  176. ReplaceFreeTargets);
  177. properties["INTERFACE_LINK_LIBRARIES"] = prepro;
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. //----------------------------------------------------------------------------
  184. static bool isSubDirectory(const char* a, const char* b)
  185. {
  186. return (cmSystemTools::ComparePath(a, b) ||
  187. cmSystemTools::IsSubDirectory(a, b));
  188. }
  189. //----------------------------------------------------------------------------
  190. static bool checkInterfaceDirs(const std::string &prepro,
  191. cmTarget *target)
  192. {
  193. const char* installDir =
  194. target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  195. const char* topSourceDir = target->GetMakefile()->GetHomeDirectory();
  196. const char* topBinaryDir = target->GetMakefile()->GetHomeOutputDirectory();
  197. std::vector<std::string> parts;
  198. cmGeneratorExpression::Split(prepro, parts);
  199. const bool inSourceBuild = strcmp(topSourceDir, topBinaryDir) == 0;
  200. for(std::vector<std::string>::iterator li = parts.begin();
  201. li != parts.end(); ++li)
  202. {
  203. if (cmGeneratorExpression::Find(*li) != std::string::npos)
  204. {
  205. continue;
  206. }
  207. if (strncmp(li->c_str(), "${_IMPORT_PREFIX}", 17) == 0)
  208. {
  209. continue;
  210. }
  211. if (!cmSystemTools::FileIsFullPath(li->c_str()))
  212. {
  213. cmOStringStream e;
  214. e << "Target \"" << target->GetName() << "\" "
  215. "INTERFACE_INCLUDE_DIRECTORIES property contains relative path:\n"
  216. " \"" << *li << "\"";
  217. target->GetMakefile()->IssueMessage(cmake::FATAL_ERROR,
  218. e.str().c_str());
  219. return false;
  220. }
  221. if (isSubDirectory(li->c_str(), installDir))
  222. {
  223. continue;
  224. }
  225. if (isSubDirectory(li->c_str(), topBinaryDir))
  226. {
  227. cmOStringStream e;
  228. e << "Target \"" << target->GetName() << "\" "
  229. "INTERFACE_INCLUDE_DIRECTORIES property contains path:\n"
  230. " \"" << *li << "\"\nwhich is prefixed in the build directory.";
  231. target->GetMakefile()->IssueMessage(cmake::FATAL_ERROR,
  232. e.str().c_str());
  233. return false;
  234. }
  235. if (!inSourceBuild)
  236. {
  237. if (isSubDirectory(li->c_str(), topSourceDir))
  238. {
  239. cmOStringStream e;
  240. e << "Target \"" << target->GetName() << "\" "
  241. "INTERFACE_INCLUDE_DIRECTORIES property contains path:\n"
  242. " \"" << *li << "\"\nwhich is prefixed in the source directory.";
  243. target->GetMakefile()->IssueMessage(cmake::FATAL_ERROR,
  244. e.str().c_str());
  245. return false;
  246. }
  247. }
  248. }
  249. return true;
  250. }
  251. //----------------------------------------------------------------------------
  252. void cmExportFileGenerator::PopulateIncludeDirectoriesInterface(
  253. cmTargetExport *tei,
  254. cmGeneratorExpression::PreprocessContext preprocessRule,
  255. ImportPropertyMap &properties,
  256. std::vector<std::string> &missingTargets)
  257. {
  258. cmTarget *target = tei->Target;
  259. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  260. const char *propName = "INTERFACE_INCLUDE_DIRECTORIES";
  261. const char *input = target->GetProperty(propName);
  262. cmListFileBacktrace lfbt;
  263. cmGeneratorExpression ge(lfbt);
  264. std::string dirs = tei->InterfaceIncludeDirectories;
  265. this->ReplaceInstallPrefix(dirs);
  266. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(dirs);
  267. std::string exportDirs = cge->Evaluate(target->GetMakefile(), 0,
  268. false, target);
  269. if (cge->GetHadContextSensitiveCondition())
  270. {
  271. cmMakefile* mf = target->GetMakefile();
  272. cmOStringStream e;
  273. e << "Target \"" << target->GetName() << "\" is installed with "
  274. "INCLUDES DESTINATION set to a context sensitive path. Paths which "
  275. "depend on the configuration, policy values or the link interface are "
  276. "not supported. Consider using target_include_directories instead.";
  277. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  278. return;
  279. }
  280. if (!input && exportDirs.empty())
  281. {
  282. return;
  283. }
  284. if ((input && !*input) && exportDirs.empty())
  285. {
  286. // Set to empty
  287. properties[propName] = "";
  288. return;
  289. }
  290. std::string includes = (input?input:"");
  291. const char* sep = input ? ";" : "";
  292. includes += sep + exportDirs;
  293. std::string prepro = cmGeneratorExpression::Preprocess(includes,
  294. preprocessRule,
  295. true);
  296. if (!prepro.empty())
  297. {
  298. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  299. missingTargets);
  300. if (!checkInterfaceDirs(prepro, target))
  301. {
  302. return;
  303. }
  304. properties[propName] = prepro;
  305. }
  306. }
  307. //----------------------------------------------------------------------------
  308. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  309. cmTarget *target,
  310. cmGeneratorExpression::PreprocessContext preprocessRule,
  311. ImportPropertyMap &properties,
  312. std::vector<std::string> &missingTargets)
  313. {
  314. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  315. properties, missingTargets);
  316. }
  317. //----------------------------------------------------------------------------
  318. void getPropertyContents(cmTarget *tgt, const char *prop,
  319. std::set<std::string> &ifaceProperties)
  320. {
  321. const char *p = tgt->GetProperty(prop);
  322. if (!p)
  323. {
  324. return;
  325. }
  326. std::vector<std::string> content;
  327. cmSystemTools::ExpandListArgument(p, content);
  328. for (std::vector<std::string>::const_iterator ci = content.begin();
  329. ci != content.end(); ++ci)
  330. {
  331. ifaceProperties.insert(*ci);
  332. }
  333. }
  334. //----------------------------------------------------------------------------
  335. void getCompatibleInterfaceProperties(cmTarget *target,
  336. std::set<std::string> &ifaceProperties,
  337. const char *config)
  338. {
  339. cmComputeLinkInformation *info = target->GetLinkInformation(config);
  340. if (!info)
  341. {
  342. cmMakefile* mf = target->GetMakefile();
  343. cmOStringStream e;
  344. e << "Exporting the target \"" << target->GetName() << "\" is not "
  345. "allowed since its linker language cannot be determined";
  346. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  347. return;
  348. }
  349. const cmComputeLinkInformation::ItemVector &deps = info->GetItems();
  350. for(cmComputeLinkInformation::ItemVector::const_iterator li =
  351. deps.begin();
  352. li != deps.end(); ++li)
  353. {
  354. if (!li->Target)
  355. {
  356. continue;
  357. }
  358. getPropertyContents(li->Target,
  359. "COMPATIBLE_INTERFACE_BOOL",
  360. ifaceProperties);
  361. getPropertyContents(li->Target,
  362. "COMPATIBLE_INTERFACE_STRING",
  363. ifaceProperties);
  364. }
  365. }
  366. //----------------------------------------------------------------------------
  367. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  368. cmTarget *target,
  369. ImportPropertyMap &properties)
  370. {
  371. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL",
  372. target, properties);
  373. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING",
  374. target, properties);
  375. std::set<std::string> ifaceProperties;
  376. getPropertyContents(target, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  377. getPropertyContents(target, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  378. getCompatibleInterfaceProperties(target, ifaceProperties, 0);
  379. std::vector<std::string> configNames;
  380. target->GetMakefile()->GetConfigurations(configNames);
  381. for (std::vector<std::string>::const_iterator ci = configNames.begin();
  382. ci != configNames.end(); ++ci)
  383. {
  384. getCompatibleInterfaceProperties(target, ifaceProperties, ci->c_str());
  385. }
  386. for (std::set<std::string>::const_iterator it = ifaceProperties.begin();
  387. it != ifaceProperties.end(); ++it)
  388. {
  389. this->PopulateInterfaceProperty(("INTERFACE_" + *it).c_str(),
  390. target, properties);
  391. }
  392. }
  393. //----------------------------------------------------------------------------
  394. void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget *target,
  395. std::ostream& os,
  396. const ImportPropertyMap &properties)
  397. {
  398. if (!properties.empty())
  399. {
  400. std::string targetName = this->Namespace;
  401. targetName += target->GetExportName();
  402. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  403. for(ImportPropertyMap::const_iterator pi = properties.begin();
  404. pi != properties.end(); ++pi)
  405. {
  406. os << " " << pi->first << " \"" << pi->second << "\"\n";
  407. }
  408. os << ")\n\n";
  409. }
  410. }
  411. //----------------------------------------------------------------------------
  412. bool
  413. cmExportFileGenerator::AddTargetNamespace(std::string &input,
  414. cmTarget* target,
  415. std::vector<std::string> &missingTargets)
  416. {
  417. cmMakefile *mf = target->GetMakefile();
  418. cmTarget *tgt = mf->FindTargetToUse(input.c_str());
  419. if (!tgt)
  420. {
  421. return false;
  422. }
  423. if(tgt->IsImported())
  424. {
  425. return true;
  426. }
  427. if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  428. {
  429. input = this->Namespace + tgt->GetExportName();
  430. }
  431. else
  432. {
  433. std::string namespacedTarget;
  434. this->HandleMissingTarget(namespacedTarget, missingTargets,
  435. mf, target, tgt);
  436. if (!namespacedTarget.empty())
  437. {
  438. input = namespacedTarget;
  439. }
  440. }
  441. return true;
  442. }
  443. //----------------------------------------------------------------------------
  444. void
  445. cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  446. std::string &input,
  447. cmTarget* target,
  448. std::vector<std::string> &missingTargets,
  449. FreeTargetsReplace replace)
  450. {
  451. if (replace == NoReplaceFreeTargets)
  452. {
  453. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  454. return;
  455. }
  456. std::vector<std::string> parts;
  457. cmGeneratorExpression::Split(input, parts);
  458. std::string sep;
  459. input = "";
  460. for(std::vector<std::string>::iterator li = parts.begin();
  461. li != parts.end(); ++li)
  462. {
  463. if (cmGeneratorExpression::Find(*li) == std::string::npos)
  464. {
  465. this->AddTargetNamespace(*li, target, missingTargets);
  466. }
  467. else
  468. {
  469. this->ResolveTargetsInGeneratorExpression(
  470. *li,
  471. target,
  472. missingTargets);
  473. }
  474. input += sep + *li;
  475. sep = ";";
  476. }
  477. }
  478. //----------------------------------------------------------------------------
  479. void
  480. cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  481. std::string &input,
  482. cmTarget* target,
  483. std::vector<std::string> &missingTargets)
  484. {
  485. std::string::size_type pos = 0;
  486. std::string::size_type lastPos = pos;
  487. cmMakefile *mf = target->GetMakefile();
  488. while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
  489. {
  490. std::string::size_type nameStartPos = pos +
  491. sizeof("$<TARGET_PROPERTY:") - 1;
  492. std::string::size_type closePos = input.find(">", nameStartPos);
  493. std::string::size_type commaPos = input.find(",", nameStartPos);
  494. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  495. if (commaPos == input.npos // Implied 'this' target
  496. || closePos == input.npos // Imcomplete expression.
  497. || closePos < commaPos // Implied 'this' target
  498. || nextOpenPos < commaPos) // Non-literal
  499. {
  500. lastPos = nameStartPos;
  501. continue;
  502. }
  503. std::string targetName = input.substr(nameStartPos,
  504. commaPos - nameStartPos);
  505. if (this->AddTargetNamespace(targetName, target, missingTargets))
  506. {
  507. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  508. }
  509. lastPos = nameStartPos + targetName.size() + 1;
  510. }
  511. std::string errorString;
  512. pos = 0;
  513. lastPos = pos;
  514. while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
  515. {
  516. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  517. std::string::size_type endPos = input.find(">", nameStartPos);
  518. if (endPos == input.npos)
  519. {
  520. errorString = "$<TARGET_NAME:...> expression incomplete";
  521. break;
  522. }
  523. std::string targetName = input.substr(nameStartPos,
  524. endPos - nameStartPos);
  525. if(targetName.find("$<") != input.npos)
  526. {
  527. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  528. "literal.";
  529. break;
  530. }
  531. if (!this->AddTargetNamespace(targetName, target, missingTargets))
  532. {
  533. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  534. "reachable target.";
  535. break;
  536. }
  537. input.replace(pos, endPos - pos + 1, targetName);
  538. lastPos = endPos;
  539. }
  540. this->ReplaceInstallPrefix(input);
  541. if (!errorString.empty())
  542. {
  543. mf->IssueMessage(cmake::FATAL_ERROR, errorString);
  544. }
  545. }
  546. //----------------------------------------------------------------------------
  547. void
  548. cmExportFileGenerator::ReplaceInstallPrefix(std::string &)
  549. {
  550. // Do nothing
  551. }
  552. //----------------------------------------------------------------------------
  553. void
  554. cmExportFileGenerator
  555. ::SetImportLinkInterface(const char* config, std::string const& suffix,
  556. cmGeneratorExpression::PreprocessContext preprocessRule,
  557. cmTarget* target, ImportPropertyMap& properties,
  558. std::vector<std::string>& missingTargets)
  559. {
  560. // Add the transitive link dependencies for this configuration.
  561. cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  562. target);
  563. if (!iface)
  564. {
  565. return;
  566. }
  567. if (iface->ImplementationIsInterface)
  568. {
  569. this->SetImportLinkProperty(suffix, target,
  570. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  571. iface->Libraries, properties, missingTargets);
  572. return;
  573. }
  574. const char *propContent;
  575. if (const char *prop_suffixed = target->GetProperty(
  576. ("LINK_INTERFACE_LIBRARIES" + suffix).c_str()))
  577. {
  578. propContent = prop_suffixed;
  579. }
  580. else if (const char *prop = target->GetProperty(
  581. "LINK_INTERFACE_LIBRARIES"))
  582. {
  583. propContent = prop;
  584. }
  585. else
  586. {
  587. return;
  588. }
  589. const bool newCMP0022Behavior =
  590. target->GetPolicyStatusCMP0022() != cmPolicies::WARN
  591. && target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  592. if(newCMP0022Behavior && !this->ExportOld)
  593. {
  594. cmMakefile *mf = target->GetMakefile();
  595. cmOStringStream e;
  596. e << "Target \"" << target->GetName() << "\" has policy CMP0022 enabled, "
  597. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  598. "populated, but it was exported without the "
  599. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  600. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  601. return;
  602. }
  603. if (!*propContent)
  604. {
  605. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
  606. return;
  607. }
  608. std::string prepro = cmGeneratorExpression::Preprocess(propContent,
  609. preprocessRule);
  610. if (!prepro.empty())
  611. {
  612. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  613. missingTargets,
  614. ReplaceFreeTargets);
  615. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  616. }
  617. }
  618. //----------------------------------------------------------------------------
  619. void
  620. cmExportFileGenerator
  621. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  622. cmTarget* target, ImportPropertyMap& properties,
  623. std::vector<std::string>& missingTargets
  624. )
  625. {
  626. // Get the makefile in which to lookup target information.
  627. cmMakefile* mf = target->GetMakefile();
  628. // Add the soname for unix shared libraries.
  629. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  630. target->GetType() == cmTarget::MODULE_LIBRARY)
  631. {
  632. // Check whether this is a DLL platform.
  633. bool dll_platform =
  634. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  635. if(!dll_platform)
  636. {
  637. std::string prop;
  638. std::string value;
  639. if(target->HasSOName(config))
  640. {
  641. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  642. {
  643. value = this->InstallNameDir(target, config);
  644. }
  645. prop = "IMPORTED_SONAME";
  646. value += target->GetSOName(config);
  647. }
  648. else
  649. {
  650. prop = "IMPORTED_NO_SONAME";
  651. value = "TRUE";
  652. }
  653. prop += suffix;
  654. properties[prop] = value;
  655. }
  656. }
  657. // Add the transitive link dependencies for this configuration.
  658. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  659. target))
  660. {
  661. this->SetImportLinkProperty(suffix, target,
  662. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  663. iface->Languages, properties, missingTargets);
  664. this->SetImportLinkProperty(suffix, target,
  665. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  666. iface->SharedDeps, properties, missingTargets);
  667. if(iface->Multiplicity > 0)
  668. {
  669. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  670. prop += suffix;
  671. cmOStringStream m;
  672. m << iface->Multiplicity;
  673. properties[prop] = m.str();
  674. }
  675. }
  676. }
  677. //----------------------------------------------------------------------------
  678. void
  679. cmExportFileGenerator
  680. ::SetImportLinkProperty(std::string const& suffix,
  681. cmTarget* target,
  682. const char* propName,
  683. std::vector<std::string> const& libs,
  684. ImportPropertyMap& properties,
  685. std::vector<std::string>& missingTargets
  686. )
  687. {
  688. // Skip the property if there are no libraries.
  689. if(libs.empty())
  690. {
  691. return;
  692. }
  693. // Construct the property value.
  694. std::string link_libs;
  695. const char* sep = "";
  696. for(std::vector<std::string>::const_iterator li = libs.begin();
  697. li != libs.end(); ++li)
  698. {
  699. // Separate this from the previous entry.
  700. link_libs += sep;
  701. sep = ";";
  702. std::string temp = *li;
  703. this->AddTargetNamespace(temp, target, missingTargets);
  704. link_libs += temp;
  705. }
  706. // Store the property.
  707. std::string prop = propName;
  708. prop += suffix;
  709. properties[prop] = link_libs;
  710. }
  711. //----------------------------------------------------------------------------
  712. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  713. const char* config)
  714. {
  715. os << "#----------------------------------------------------------------\n"
  716. << "# Generated CMake target import file";
  717. if(config)
  718. {
  719. os << " for configuration \"" << config << "\".\n";
  720. }
  721. else
  722. {
  723. os << ".\n";
  724. }
  725. os << "#----------------------------------------------------------------\n"
  726. << "\n";
  727. this->GenerateImportVersionCode(os);
  728. }
  729. //----------------------------------------------------------------------------
  730. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  731. {
  732. os << "# Commands beyond this point should not need to know the version.\n"
  733. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  734. }
  735. //----------------------------------------------------------------------------
  736. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  737. {
  738. // Store an import file format version. This will let us change the
  739. // format later while still allowing old import files to work.
  740. os << "# Commands may need to know the format version.\n"
  741. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  742. << "\n";
  743. }
  744. //----------------------------------------------------------------------------
  745. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  746. const std::string &expectedTargets)
  747. {
  748. os << "# Protect against multiple inclusion, which would fail when already "
  749. "imported targets are added once more.\n"
  750. "set(_targetsDefined)\n"
  751. "set(_targetsNotDefined)\n"
  752. "set(_expectedTargets)\n"
  753. "foreach(_expectedTarget " << expectedTargets << ")\n"
  754. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  755. " if(NOT TARGET ${_expectedTarget})\n"
  756. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  757. " endif()\n"
  758. " if(TARGET ${_expectedTarget})\n"
  759. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  760. " endif()\n"
  761. "endforeach()\n"
  762. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  763. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  764. " cmake_policy(POP)\n"
  765. " return()\n"
  766. "endif()\n"
  767. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  768. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  769. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  770. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  771. "endif()\n"
  772. "unset(_targetsDefined)\n"
  773. "unset(_targetsNotDefined)\n"
  774. "unset(_expectedTargets)\n"
  775. "\n\n";
  776. }
  777. //----------------------------------------------------------------------------
  778. void
  779. cmExportFileGenerator
  780. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  781. {
  782. // Construct the imported target name.
  783. std::string targetName = this->Namespace;
  784. targetName += target->GetExportName();
  785. // Create the imported target.
  786. os << "# Create imported target " << targetName << "\n";
  787. switch(target->GetType())
  788. {
  789. case cmTarget::EXECUTABLE:
  790. os << "add_executable(" << targetName << " IMPORTED)\n";
  791. break;
  792. case cmTarget::STATIC_LIBRARY:
  793. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  794. break;
  795. case cmTarget::SHARED_LIBRARY:
  796. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  797. break;
  798. case cmTarget::MODULE_LIBRARY:
  799. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  800. break;
  801. case cmTarget::UNKNOWN_LIBRARY:
  802. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  803. break;
  804. default: // should never happen
  805. break;
  806. }
  807. // Mark the imported executable if it has exports.
  808. if(target->IsExecutableWithExports())
  809. {
  810. os << "set_property(TARGET " << targetName
  811. << " PROPERTY ENABLE_EXPORTS 1)\n";
  812. }
  813. // Mark the imported library if it is a framework.
  814. if(target->IsFrameworkOnApple())
  815. {
  816. os << "set_property(TARGET " << targetName
  817. << " PROPERTY FRAMEWORK 1)\n";
  818. }
  819. // Mark the imported executable if it is an application bundle.
  820. if(target->IsAppBundleOnApple())
  821. {
  822. os << "set_property(TARGET " << targetName
  823. << " PROPERTY MACOSX_BUNDLE 1)\n";
  824. }
  825. if (target->IsCFBundleOnApple())
  826. {
  827. os << "set_property(TARGET " << targetName
  828. << " PROPERTY BUNDLE 1)\n";
  829. }
  830. os << "\n";
  831. }
  832. //----------------------------------------------------------------------------
  833. void
  834. cmExportFileGenerator
  835. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  836. cmTarget* target,
  837. ImportPropertyMap const& properties)
  838. {
  839. // Construct the imported target name.
  840. std::string targetName = this->Namespace;
  841. targetName += target->GetExportName();
  842. // Set the import properties.
  843. os << "# Import target \"" << targetName << "\" for configuration \""
  844. << config << "\"\n";
  845. os << "set_property(TARGET " << targetName
  846. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  847. if(config && *config)
  848. {
  849. os << cmSystemTools::UpperCase(config);
  850. }
  851. else
  852. {
  853. os << "NOCONFIG";
  854. }
  855. os << ")\n";
  856. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  857. for(ImportPropertyMap::const_iterator pi = properties.begin();
  858. pi != properties.end(); ++pi)
  859. {
  860. os << " " << pi->first << " \"" << pi->second << "\"\n";
  861. }
  862. os << " )\n"
  863. << "\n";
  864. }
  865. //----------------------------------------------------------------------------
  866. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  867. const std::vector<std::string>& missingTargets)
  868. {
  869. if (missingTargets.empty())
  870. {
  871. os << "# This file does not depend on other imported targets which have\n"
  872. "# been exported from the same project but in a separate "
  873. "export set.\n\n";
  874. return;
  875. }
  876. os << "# Make sure the targets which have been exported in some other \n"
  877. "# export set exist.\n"
  878. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  879. "foreach(_target ";
  880. std::set<std::string> emitted;
  881. for(unsigned int i=0; i<missingTargets.size(); ++i)
  882. {
  883. if (emitted.insert(missingTargets[i]).second)
  884. {
  885. os << "\"" << missingTargets[i] << "\" ";
  886. }
  887. }
  888. os << ")\n"
  889. " if(NOT TARGET \"${_target}\" )\n"
  890. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  891. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  892. "\n"
  893. " endif()\n"
  894. "endforeach()\n"
  895. "\n"
  896. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  897. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  898. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  899. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  900. "\"The following imported targets are "
  901. "referenced, but are missing: "
  902. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  903. " else()\n"
  904. " message(FATAL_ERROR \"The following imported targets are "
  905. "referenced, but are missing: "
  906. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  907. " endif()\n"
  908. "endif()\n"
  909. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  910. "\n";
  911. }
  912. //----------------------------------------------------------------------------
  913. void
  914. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  915. {
  916. // Add code which verifies at cmake time that the file which is being
  917. // imported actually exists on disk. This should in theory always be theory
  918. // case, but still when packages are split into normal and development
  919. // packages this might get broken (e.g. the Config.cmake could be part of
  920. // the non-development package, something similar happened to me without
  921. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  922. // but the development package was not installed.).
  923. os << "# Loop over all imported files and verify that they actually exist\n"
  924. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  925. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  926. " if(NOT EXISTS \"${file}\" )\n"
  927. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  928. " references the file\n"
  929. " \\\"${file}\\\"\n"
  930. "but this file does not exist. Possible reasons include:\n"
  931. "* The file was deleted, renamed, or moved to another location.\n"
  932. "* An install or uninstall procedure did not complete successfully.\n"
  933. "* The installation package was faulty and contained\n"
  934. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  935. "but not all the files it references.\n"
  936. "\")\n"
  937. " endif()\n"
  938. " endforeach()\n"
  939. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  940. "endforeach()\n"
  941. "unset(_IMPORT_CHECK_TARGETS)\n"
  942. "\n";
  943. }
  944. //----------------------------------------------------------------------------
  945. void
  946. cmExportFileGenerator
  947. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  948. ImportPropertyMap const& properties,
  949. const std::set<std::string>& importedLocations)
  950. {
  951. // Construct the imported target name.
  952. std::string targetName = this->Namespace;
  953. targetName += target->GetExportName();
  954. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  955. "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  956. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  957. li != importedLocations.end();
  958. ++li)
  959. {
  960. ImportPropertyMap::const_iterator pi = properties.find(*li);
  961. if (pi != properties.end())
  962. {
  963. os << "\"" << pi->second << "\" ";
  964. }
  965. }
  966. os << ")\n\n";
  967. }