cmExportFileGenerator.cxx 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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. cmTarget *target,
  254. cmGeneratorExpression::PreprocessContext preprocessRule,
  255. ImportPropertyMap &properties,
  256. std::vector<std::string> &missingTargets)
  257. {
  258. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  259. const char *propName = "INTERFACE_INCLUDE_DIRECTORIES";
  260. const char *input = target->GetProperty(propName);
  261. if (!input)
  262. {
  263. return;
  264. }
  265. if (!*input)
  266. {
  267. // Set to empty
  268. properties[propName] = "";
  269. return;
  270. }
  271. std::string prepro = cmGeneratorExpression::Preprocess(input,
  272. preprocessRule);
  273. if (!prepro.empty())
  274. {
  275. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  276. missingTargets);
  277. if (!checkInterfaceDirs(prepro, target))
  278. {
  279. return;
  280. }
  281. properties[propName] = prepro;
  282. }
  283. }
  284. //----------------------------------------------------------------------------
  285. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  286. cmTarget *target,
  287. cmGeneratorExpression::PreprocessContext preprocessRule,
  288. ImportPropertyMap &properties,
  289. std::vector<std::string> &missingTargets)
  290. {
  291. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  292. properties, missingTargets);
  293. }
  294. //----------------------------------------------------------------------------
  295. void getPropertyContents(cmTarget *tgt, const char *prop,
  296. std::set<std::string> &ifaceProperties)
  297. {
  298. const char *p = tgt->GetProperty(prop);
  299. if (!p)
  300. {
  301. return;
  302. }
  303. std::vector<std::string> content;
  304. cmSystemTools::ExpandListArgument(p, content);
  305. for (std::vector<std::string>::const_iterator ci = content.begin();
  306. ci != content.end(); ++ci)
  307. {
  308. ifaceProperties.insert(*ci);
  309. }
  310. }
  311. //----------------------------------------------------------------------------
  312. void getCompatibleInterfaceProperties(cmTarget *target,
  313. std::set<std::string> &ifaceProperties,
  314. const char *config)
  315. {
  316. cmComputeLinkInformation *info = target->GetLinkInformation(config);
  317. if (!info)
  318. {
  319. cmMakefile* mf = target->GetMakefile();
  320. cmOStringStream e;
  321. e << "Exporting the target \"" << target->GetName() << "\" is not "
  322. "allowed since its linker language cannot be determined";
  323. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  324. return;
  325. }
  326. const cmComputeLinkInformation::ItemVector &deps = info->GetItems();
  327. for(cmComputeLinkInformation::ItemVector::const_iterator li =
  328. deps.begin();
  329. li != deps.end(); ++li)
  330. {
  331. if (!li->Target)
  332. {
  333. continue;
  334. }
  335. getPropertyContents(li->Target,
  336. "COMPATIBLE_INTERFACE_BOOL",
  337. ifaceProperties);
  338. getPropertyContents(li->Target,
  339. "COMPATIBLE_INTERFACE_STRING",
  340. ifaceProperties);
  341. }
  342. }
  343. //----------------------------------------------------------------------------
  344. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  345. cmTarget *target,
  346. ImportPropertyMap &properties)
  347. {
  348. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL",
  349. target, properties);
  350. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING",
  351. target, properties);
  352. std::set<std::string> ifaceProperties;
  353. getPropertyContents(target, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  354. getPropertyContents(target, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  355. getCompatibleInterfaceProperties(target, ifaceProperties, 0);
  356. std::vector<std::string> configNames;
  357. target->GetMakefile()->GetConfigurations(configNames);
  358. for (std::vector<std::string>::const_iterator ci = configNames.begin();
  359. ci != configNames.end(); ++ci)
  360. {
  361. getCompatibleInterfaceProperties(target, ifaceProperties, ci->c_str());
  362. }
  363. for (std::set<std::string>::const_iterator it = ifaceProperties.begin();
  364. it != ifaceProperties.end(); ++it)
  365. {
  366. this->PopulateInterfaceProperty(("INTERFACE_" + *it).c_str(),
  367. target, properties);
  368. }
  369. }
  370. //----------------------------------------------------------------------------
  371. void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget *target,
  372. std::ostream& os,
  373. const ImportPropertyMap &properties)
  374. {
  375. if (!properties.empty())
  376. {
  377. std::string targetName = this->Namespace;
  378. targetName += target->GetExportName();
  379. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  380. for(ImportPropertyMap::const_iterator pi = properties.begin();
  381. pi != properties.end(); ++pi)
  382. {
  383. os << " " << pi->first << " \"" << pi->second << "\"\n";
  384. }
  385. os << ")\n\n";
  386. }
  387. }
  388. //----------------------------------------------------------------------------
  389. bool
  390. cmExportFileGenerator::AddTargetNamespace(std::string &input,
  391. cmTarget* target,
  392. std::vector<std::string> &missingTargets)
  393. {
  394. cmMakefile *mf = target->GetMakefile();
  395. cmTarget *tgt = mf->FindTargetToUse(input.c_str());
  396. if (!tgt)
  397. {
  398. return false;
  399. }
  400. if(tgt->IsImported())
  401. {
  402. return true;
  403. }
  404. if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  405. {
  406. input = this->Namespace + tgt->GetExportName();
  407. }
  408. else
  409. {
  410. std::string namespacedTarget;
  411. this->HandleMissingTarget(namespacedTarget, missingTargets,
  412. mf, target, tgt);
  413. if (!namespacedTarget.empty())
  414. {
  415. input = namespacedTarget;
  416. }
  417. }
  418. return true;
  419. }
  420. //----------------------------------------------------------------------------
  421. void
  422. cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  423. std::string &input,
  424. cmTarget* target,
  425. std::vector<std::string> &missingTargets,
  426. FreeTargetsReplace replace)
  427. {
  428. if (replace == NoReplaceFreeTargets)
  429. {
  430. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  431. return;
  432. }
  433. std::vector<std::string> parts;
  434. cmGeneratorExpression::Split(input, parts);
  435. std::string sep;
  436. input = "";
  437. for(std::vector<std::string>::iterator li = parts.begin();
  438. li != parts.end(); ++li)
  439. {
  440. if (cmGeneratorExpression::Find(*li) == std::string::npos)
  441. {
  442. this->AddTargetNamespace(*li, target, missingTargets);
  443. }
  444. else
  445. {
  446. this->ResolveTargetsInGeneratorExpression(
  447. *li,
  448. target,
  449. missingTargets);
  450. }
  451. input += sep + *li;
  452. sep = ";";
  453. }
  454. }
  455. //----------------------------------------------------------------------------
  456. void
  457. cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  458. std::string &input,
  459. cmTarget* target,
  460. std::vector<std::string> &missingTargets)
  461. {
  462. std::string::size_type pos = 0;
  463. std::string::size_type lastPos = pos;
  464. cmMakefile *mf = target->GetMakefile();
  465. while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
  466. {
  467. std::string::size_type nameStartPos = pos +
  468. sizeof("$<TARGET_PROPERTY:") - 1;
  469. std::string::size_type closePos = input.find(">", nameStartPos);
  470. std::string::size_type commaPos = input.find(",", nameStartPos);
  471. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  472. if (commaPos == input.npos // Implied 'this' target
  473. || closePos == input.npos // Imcomplete expression.
  474. || closePos < commaPos // Implied 'this' target
  475. || nextOpenPos < commaPos) // Non-literal
  476. {
  477. lastPos = nameStartPos;
  478. continue;
  479. }
  480. std::string targetName = input.substr(nameStartPos,
  481. commaPos - nameStartPos);
  482. if (this->AddTargetNamespace(targetName, target, missingTargets))
  483. {
  484. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  485. }
  486. lastPos = nameStartPos + targetName.size() + 1;
  487. }
  488. std::string errorString;
  489. pos = 0;
  490. lastPos = pos;
  491. while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
  492. {
  493. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  494. std::string::size_type endPos = input.find(">", nameStartPos);
  495. if (endPos == input.npos)
  496. {
  497. errorString = "$<TARGET_NAME:...> expression incomplete";
  498. break;
  499. }
  500. std::string targetName = input.substr(nameStartPos,
  501. endPos - nameStartPos);
  502. if(targetName.find("$<") != input.npos)
  503. {
  504. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  505. "literal.";
  506. break;
  507. }
  508. if (!this->AddTargetNamespace(targetName, target, missingTargets))
  509. {
  510. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  511. "reachable target.";
  512. break;
  513. }
  514. input.replace(pos, endPos - pos + 1, targetName);
  515. lastPos = endPos;
  516. }
  517. this->ReplaceInstallPrefix(input);
  518. if (!errorString.empty())
  519. {
  520. mf->IssueMessage(cmake::FATAL_ERROR, errorString);
  521. }
  522. }
  523. //----------------------------------------------------------------------------
  524. void
  525. cmExportFileGenerator::ReplaceInstallPrefix(std::string &)
  526. {
  527. // Do nothing
  528. }
  529. //----------------------------------------------------------------------------
  530. void
  531. cmExportFileGenerator
  532. ::SetImportLinkInterface(const char* config, std::string const& suffix,
  533. cmGeneratorExpression::PreprocessContext preprocessRule,
  534. cmTarget* target, ImportPropertyMap& properties,
  535. std::vector<std::string>& missingTargets)
  536. {
  537. // Add the transitive link dependencies for this configuration.
  538. cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  539. target);
  540. if (!iface)
  541. {
  542. return;
  543. }
  544. if (iface->ImplementationIsInterface)
  545. {
  546. this->SetImportLinkProperty(suffix, target,
  547. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  548. iface->Libraries, properties, missingTargets);
  549. return;
  550. }
  551. const char *propContent;
  552. if (const char *prop_suffixed = target->GetProperty(
  553. ("LINK_INTERFACE_LIBRARIES" + suffix).c_str()))
  554. {
  555. propContent = prop_suffixed;
  556. }
  557. else if (const char *prop = target->GetProperty(
  558. "LINK_INTERFACE_LIBRARIES"))
  559. {
  560. propContent = prop;
  561. }
  562. else
  563. {
  564. return;
  565. }
  566. const bool newCMP0022Behavior =
  567. target->GetPolicyStatusCMP0022() != cmPolicies::WARN
  568. && target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  569. if(newCMP0022Behavior && !this->ExportOld)
  570. {
  571. cmMakefile *mf = target->GetMakefile();
  572. cmOStringStream e;
  573. e << "Target \"" << target->GetName() << "\" has policy CMP0022 enabled, "
  574. "but also has old-style INTERFACE_LINK_LIBRARIES properties "
  575. "populated, but it was exported without the "
  576. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  577. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  578. return;
  579. }
  580. if (!*propContent)
  581. {
  582. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
  583. return;
  584. }
  585. std::string prepro = cmGeneratorExpression::Preprocess(propContent,
  586. preprocessRule);
  587. if (!prepro.empty())
  588. {
  589. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  590. missingTargets,
  591. ReplaceFreeTargets);
  592. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  593. }
  594. }
  595. //----------------------------------------------------------------------------
  596. void
  597. cmExportFileGenerator
  598. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  599. cmTarget* target, ImportPropertyMap& properties,
  600. std::vector<std::string>& missingTargets
  601. )
  602. {
  603. // Get the makefile in which to lookup target information.
  604. cmMakefile* mf = target->GetMakefile();
  605. // Add the soname for unix shared libraries.
  606. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  607. target->GetType() == cmTarget::MODULE_LIBRARY)
  608. {
  609. // Check whether this is a DLL platform.
  610. bool dll_platform =
  611. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  612. if(!dll_platform)
  613. {
  614. std::string prop;
  615. std::string value;
  616. if(target->HasSOName(config))
  617. {
  618. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  619. {
  620. value = this->InstallNameDir(target, config);
  621. }
  622. prop = "IMPORTED_SONAME";
  623. value += target->GetSOName(config);
  624. }
  625. else
  626. {
  627. prop = "IMPORTED_NO_SONAME";
  628. value = "TRUE";
  629. }
  630. prop += suffix;
  631. properties[prop] = value;
  632. }
  633. }
  634. // Add the transitive link dependencies for this configuration.
  635. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  636. target))
  637. {
  638. this->SetImportLinkProperty(suffix, target,
  639. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  640. iface->Languages, properties, missingTargets);
  641. this->SetImportLinkProperty(suffix, target,
  642. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  643. iface->SharedDeps, properties, missingTargets);
  644. if(iface->Multiplicity > 0)
  645. {
  646. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  647. prop += suffix;
  648. cmOStringStream m;
  649. m << iface->Multiplicity;
  650. properties[prop] = m.str();
  651. }
  652. }
  653. }
  654. //----------------------------------------------------------------------------
  655. void
  656. cmExportFileGenerator
  657. ::SetImportLinkProperty(std::string const& suffix,
  658. cmTarget* target,
  659. const char* propName,
  660. std::vector<std::string> const& libs,
  661. ImportPropertyMap& properties,
  662. std::vector<std::string>& missingTargets
  663. )
  664. {
  665. // Skip the property if there are no libraries.
  666. if(libs.empty())
  667. {
  668. return;
  669. }
  670. // Construct the property value.
  671. std::string link_libs;
  672. const char* sep = "";
  673. for(std::vector<std::string>::const_iterator li = libs.begin();
  674. li != libs.end(); ++li)
  675. {
  676. // Separate this from the previous entry.
  677. link_libs += sep;
  678. sep = ";";
  679. std::string temp = *li;
  680. this->AddTargetNamespace(temp, target, missingTargets);
  681. link_libs += temp;
  682. }
  683. // Store the property.
  684. std::string prop = propName;
  685. prop += suffix;
  686. properties[prop] = link_libs;
  687. }
  688. //----------------------------------------------------------------------------
  689. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  690. const char* config)
  691. {
  692. os << "#----------------------------------------------------------------\n"
  693. << "# Generated CMake target import file";
  694. if(config)
  695. {
  696. os << " for configuration \"" << config << "\".\n";
  697. }
  698. else
  699. {
  700. os << ".\n";
  701. }
  702. os << "#----------------------------------------------------------------\n"
  703. << "\n";
  704. this->GenerateImportVersionCode(os);
  705. }
  706. //----------------------------------------------------------------------------
  707. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  708. {
  709. os << "# Commands beyond this point should not need to know the version.\n"
  710. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  711. }
  712. //----------------------------------------------------------------------------
  713. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  714. {
  715. // Store an import file format version. This will let us change the
  716. // format later while still allowing old import files to work.
  717. os << "# Commands may need to know the format version.\n"
  718. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  719. << "\n";
  720. }
  721. //----------------------------------------------------------------------------
  722. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  723. const std::string &expectedTargets)
  724. {
  725. os << "# Protect against multiple inclusion, which would fail when already "
  726. "imported targets are added once more.\n"
  727. "set(_targetsDefined)\n"
  728. "set(_targetsNotDefined)\n"
  729. "set(_expectedTargets)\n"
  730. "foreach(_expectedTarget " << expectedTargets << ")\n"
  731. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  732. " if(NOT TARGET ${_expectedTarget})\n"
  733. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  734. " endif()\n"
  735. " if(TARGET ${_expectedTarget})\n"
  736. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  737. " endif()\n"
  738. "endforeach()\n"
  739. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  740. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  741. " cmake_policy(POP)\n"
  742. " return()\n"
  743. "endif()\n"
  744. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  745. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  746. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  747. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  748. "endif()\n"
  749. "unset(_targetsDefined)\n"
  750. "unset(_targetsNotDefined)\n"
  751. "unset(_expectedTargets)\n"
  752. "\n\n";
  753. }
  754. //----------------------------------------------------------------------------
  755. void
  756. cmExportFileGenerator
  757. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  758. {
  759. // Construct the imported target name.
  760. std::string targetName = this->Namespace;
  761. targetName += target->GetExportName();
  762. // Create the imported target.
  763. os << "# Create imported target " << targetName << "\n";
  764. switch(target->GetType())
  765. {
  766. case cmTarget::EXECUTABLE:
  767. os << "add_executable(" << targetName << " IMPORTED)\n";
  768. break;
  769. case cmTarget::STATIC_LIBRARY:
  770. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  771. break;
  772. case cmTarget::SHARED_LIBRARY:
  773. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  774. break;
  775. case cmTarget::MODULE_LIBRARY:
  776. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  777. break;
  778. case cmTarget::UNKNOWN_LIBRARY:
  779. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  780. break;
  781. default: // should never happen
  782. break;
  783. }
  784. // Mark the imported executable if it has exports.
  785. if(target->IsExecutableWithExports())
  786. {
  787. os << "set_property(TARGET " << targetName
  788. << " PROPERTY ENABLE_EXPORTS 1)\n";
  789. }
  790. // Mark the imported library if it is a framework.
  791. if(target->IsFrameworkOnApple())
  792. {
  793. os << "set_property(TARGET " << targetName
  794. << " PROPERTY FRAMEWORK 1)\n";
  795. }
  796. // Mark the imported executable if it is an application bundle.
  797. if(target->IsAppBundleOnApple())
  798. {
  799. os << "set_property(TARGET " << targetName
  800. << " PROPERTY MACOSX_BUNDLE 1)\n";
  801. }
  802. if (target->IsCFBundleOnApple())
  803. {
  804. os << "set_property(TARGET " << targetName
  805. << " PROPERTY BUNDLE 1)\n";
  806. }
  807. os << "\n";
  808. }
  809. //----------------------------------------------------------------------------
  810. void
  811. cmExportFileGenerator
  812. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  813. cmTarget* target,
  814. ImportPropertyMap const& properties)
  815. {
  816. // Construct the imported target name.
  817. std::string targetName = this->Namespace;
  818. targetName += target->GetExportName();
  819. // Set the import properties.
  820. os << "# Import target \"" << targetName << "\" for configuration \""
  821. << config << "\"\n";
  822. os << "set_property(TARGET " << targetName
  823. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  824. if(config && *config)
  825. {
  826. os << cmSystemTools::UpperCase(config);
  827. }
  828. else
  829. {
  830. os << "NOCONFIG";
  831. }
  832. os << ")\n";
  833. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  834. for(ImportPropertyMap::const_iterator pi = properties.begin();
  835. pi != properties.end(); ++pi)
  836. {
  837. os << " " << pi->first << " \"" << pi->second << "\"\n";
  838. }
  839. os << " )\n"
  840. << "\n";
  841. }
  842. //----------------------------------------------------------------------------
  843. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  844. const std::vector<std::string>& missingTargets)
  845. {
  846. if (missingTargets.empty())
  847. {
  848. os << "# This file does not depend on other imported targets which have\n"
  849. "# been exported from the same project but in a separate "
  850. "export set.\n\n";
  851. return;
  852. }
  853. os << "# Make sure the targets which have been exported in some other \n"
  854. "# export set exist.\n"
  855. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  856. "foreach(_target ";
  857. std::set<std::string> emitted;
  858. for(unsigned int i=0; i<missingTargets.size(); ++i)
  859. {
  860. if (emitted.insert(missingTargets[i]).second)
  861. {
  862. os << "\"" << missingTargets[i] << "\" ";
  863. }
  864. }
  865. os << ")\n"
  866. " if(NOT TARGET \"${_target}\" )\n"
  867. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  868. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  869. "\n"
  870. " endif()\n"
  871. "endforeach()\n"
  872. "\n"
  873. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  874. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  875. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  876. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  877. "\"The following imported targets are "
  878. "referenced, but are missing: "
  879. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  880. " else()\n"
  881. " message(FATAL_ERROR \"The following imported targets are "
  882. "referenced, but are missing: "
  883. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  884. " endif()\n"
  885. "endif()\n"
  886. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  887. "\n";
  888. }
  889. //----------------------------------------------------------------------------
  890. void
  891. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  892. {
  893. // Add code which verifies at cmake time that the file which is being
  894. // imported actually exists on disk. This should in theory always be theory
  895. // case, but still when packages are split into normal and development
  896. // packages this might get broken (e.g. the Config.cmake could be part of
  897. // the non-development package, something similar happened to me without
  898. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  899. // but the development package was not installed.).
  900. os << "# Loop over all imported files and verify that they actually exist\n"
  901. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  902. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  903. " if(NOT EXISTS \"${file}\" )\n"
  904. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  905. " references the file\n"
  906. " \\\"${file}\\\"\n"
  907. "but this file does not exist. Possible reasons include:\n"
  908. "* The file was deleted, renamed, or moved to another location.\n"
  909. "* An install or uninstall procedure did not complete successfully.\n"
  910. "* The installation package was faulty and contained\n"
  911. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  912. "but not all the files it references.\n"
  913. "\")\n"
  914. " endif()\n"
  915. " endforeach()\n"
  916. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  917. "endforeach()\n"
  918. "unset(_IMPORT_CHECK_TARGETS)\n"
  919. "\n";
  920. }
  921. //----------------------------------------------------------------------------
  922. void
  923. cmExportFileGenerator
  924. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  925. ImportPropertyMap const& properties,
  926. const std::set<std::string>& importedLocations)
  927. {
  928. // Construct the imported target name.
  929. std::string targetName = this->Namespace;
  930. targetName += target->GetExportName();
  931. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  932. "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  933. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  934. li != importedLocations.end();
  935. ++li)
  936. {
  937. ImportPropertyMap::const_iterator pi = properties.find(*li);
  938. if (pi != properties.end())
  939. {
  940. os << "\"" << pi->second << "\" ";
  941. }
  942. }
  943. os << ")\n\n";
  944. }