cmExportFileGenerator.cxx 34 KB

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