cmExportFileGenerator.cxx 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  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. // Policy CMP0022 must not be NEW.
  570. this->SetImportLinkProperty(suffix, target,
  571. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  572. iface->Libraries, properties, missingTargets);
  573. return;
  574. }
  575. const char *propContent;
  576. if (const char *prop_suffixed = target->GetProperty(
  577. ("LINK_INTERFACE_LIBRARIES" + suffix).c_str()))
  578. {
  579. propContent = prop_suffixed;
  580. }
  581. else if (const char *prop = target->GetProperty(
  582. "LINK_INTERFACE_LIBRARIES"))
  583. {
  584. propContent = prop;
  585. }
  586. else
  587. {
  588. return;
  589. }
  590. const bool newCMP0022Behavior =
  591. target->GetPolicyStatusCMP0022() != cmPolicies::WARN
  592. && target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  593. if(newCMP0022Behavior && !this->ExportOld)
  594. {
  595. cmMakefile *mf = target->GetMakefile();
  596. cmOStringStream e;
  597. e << "Target \"" << target->GetName() << "\" has policy CMP0022 enabled, "
  598. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  599. "populated, but it was exported without the "
  600. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  601. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  602. return;
  603. }
  604. if (!*propContent)
  605. {
  606. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
  607. return;
  608. }
  609. std::string prepro = cmGeneratorExpression::Preprocess(propContent,
  610. preprocessRule);
  611. if (!prepro.empty())
  612. {
  613. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  614. missingTargets,
  615. ReplaceFreeTargets);
  616. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  617. }
  618. }
  619. //----------------------------------------------------------------------------
  620. void
  621. cmExportFileGenerator
  622. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  623. cmTarget* target, ImportPropertyMap& properties,
  624. std::vector<std::string>& missingTargets
  625. )
  626. {
  627. // Get the makefile in which to lookup target information.
  628. cmMakefile* mf = target->GetMakefile();
  629. // Add the soname for unix shared libraries.
  630. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  631. target->GetType() == cmTarget::MODULE_LIBRARY)
  632. {
  633. // Check whether this is a DLL platform.
  634. bool dll_platform =
  635. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  636. if(!dll_platform)
  637. {
  638. std::string prop;
  639. std::string value;
  640. if(target->HasSOName(config))
  641. {
  642. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  643. {
  644. value = this->InstallNameDir(target, config);
  645. }
  646. prop = "IMPORTED_SONAME";
  647. value += target->GetSOName(config);
  648. }
  649. else
  650. {
  651. prop = "IMPORTED_NO_SONAME";
  652. value = "TRUE";
  653. }
  654. prop += suffix;
  655. properties[prop] = value;
  656. }
  657. }
  658. // Add the transitive link dependencies for this configuration.
  659. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  660. target))
  661. {
  662. this->SetImportLinkProperty(suffix, target,
  663. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  664. iface->Languages, properties, missingTargets);
  665. this->SetImportLinkProperty(suffix, target,
  666. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  667. iface->SharedDeps, properties, missingTargets);
  668. if(iface->Multiplicity > 0)
  669. {
  670. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  671. prop += suffix;
  672. cmOStringStream m;
  673. m << iface->Multiplicity;
  674. properties[prop] = m.str();
  675. }
  676. }
  677. }
  678. //----------------------------------------------------------------------------
  679. void
  680. cmExportFileGenerator
  681. ::SetImportLinkProperty(std::string const& suffix,
  682. cmTarget* target,
  683. const char* propName,
  684. std::vector<std::string> const& libs,
  685. ImportPropertyMap& properties,
  686. std::vector<std::string>& missingTargets
  687. )
  688. {
  689. // Skip the property if there are no libraries.
  690. if(libs.empty())
  691. {
  692. return;
  693. }
  694. // Construct the property value.
  695. std::string link_libs;
  696. const char* sep = "";
  697. for(std::vector<std::string>::const_iterator li = libs.begin();
  698. li != libs.end(); ++li)
  699. {
  700. // Separate this from the previous entry.
  701. link_libs += sep;
  702. sep = ";";
  703. std::string temp = *li;
  704. this->AddTargetNamespace(temp, target, missingTargets);
  705. link_libs += temp;
  706. }
  707. // Store the property.
  708. std::string prop = propName;
  709. prop += suffix;
  710. properties[prop] = link_libs;
  711. }
  712. //----------------------------------------------------------------------------
  713. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  714. const char* config)
  715. {
  716. os << "#----------------------------------------------------------------\n"
  717. << "# Generated CMake target import file";
  718. if(config)
  719. {
  720. os << " for configuration \"" << config << "\".\n";
  721. }
  722. else
  723. {
  724. os << ".\n";
  725. }
  726. os << "#----------------------------------------------------------------\n"
  727. << "\n";
  728. this->GenerateImportVersionCode(os);
  729. }
  730. //----------------------------------------------------------------------------
  731. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  732. {
  733. os << "# Commands beyond this point should not need to know the version.\n"
  734. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  735. }
  736. //----------------------------------------------------------------------------
  737. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  738. {
  739. // Store an import file format version. This will let us change the
  740. // format later while still allowing old import files to work.
  741. os << "# Commands may need to know the format version.\n"
  742. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  743. << "\n";
  744. }
  745. //----------------------------------------------------------------------------
  746. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  747. const std::string &expectedTargets)
  748. {
  749. os << "# Protect against multiple inclusion, which would fail when already "
  750. "imported targets are added once more.\n"
  751. "set(_targetsDefined)\n"
  752. "set(_targetsNotDefined)\n"
  753. "set(_expectedTargets)\n"
  754. "foreach(_expectedTarget " << expectedTargets << ")\n"
  755. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  756. " if(NOT TARGET ${_expectedTarget})\n"
  757. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  758. " endif()\n"
  759. " if(TARGET ${_expectedTarget})\n"
  760. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  761. " endif()\n"
  762. "endforeach()\n"
  763. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  764. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  765. " cmake_policy(POP)\n"
  766. " return()\n"
  767. "endif()\n"
  768. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  769. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  770. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  771. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  772. "endif()\n"
  773. "unset(_targetsDefined)\n"
  774. "unset(_targetsNotDefined)\n"
  775. "unset(_expectedTargets)\n"
  776. "\n\n";
  777. }
  778. //----------------------------------------------------------------------------
  779. void
  780. cmExportFileGenerator
  781. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  782. {
  783. // Construct the imported target name.
  784. std::string targetName = this->Namespace;
  785. targetName += target->GetExportName();
  786. // Create the imported target.
  787. os << "# Create imported target " << targetName << "\n";
  788. switch(target->GetType())
  789. {
  790. case cmTarget::EXECUTABLE:
  791. os << "add_executable(" << targetName << " IMPORTED)\n";
  792. break;
  793. case cmTarget::STATIC_LIBRARY:
  794. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  795. break;
  796. case cmTarget::SHARED_LIBRARY:
  797. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  798. break;
  799. case cmTarget::MODULE_LIBRARY:
  800. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  801. break;
  802. case cmTarget::UNKNOWN_LIBRARY:
  803. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  804. break;
  805. default: // should never happen
  806. break;
  807. }
  808. // Mark the imported executable if it has exports.
  809. if(target->IsExecutableWithExports())
  810. {
  811. os << "set_property(TARGET " << targetName
  812. << " PROPERTY ENABLE_EXPORTS 1)\n";
  813. }
  814. // Mark the imported library if it is a framework.
  815. if(target->IsFrameworkOnApple())
  816. {
  817. os << "set_property(TARGET " << targetName
  818. << " PROPERTY FRAMEWORK 1)\n";
  819. }
  820. // Mark the imported executable if it is an application bundle.
  821. if(target->IsAppBundleOnApple())
  822. {
  823. os << "set_property(TARGET " << targetName
  824. << " PROPERTY MACOSX_BUNDLE 1)\n";
  825. }
  826. if (target->IsCFBundleOnApple())
  827. {
  828. os << "set_property(TARGET " << targetName
  829. << " PROPERTY BUNDLE 1)\n";
  830. }
  831. os << "\n";
  832. }
  833. //----------------------------------------------------------------------------
  834. void
  835. cmExportFileGenerator
  836. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  837. cmTarget* target,
  838. ImportPropertyMap const& properties)
  839. {
  840. // Construct the imported target name.
  841. std::string targetName = this->Namespace;
  842. targetName += target->GetExportName();
  843. // Set the import properties.
  844. os << "# Import target \"" << targetName << "\" for configuration \""
  845. << config << "\"\n";
  846. os << "set_property(TARGET " << targetName
  847. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  848. if(config && *config)
  849. {
  850. os << cmSystemTools::UpperCase(config);
  851. }
  852. else
  853. {
  854. os << "NOCONFIG";
  855. }
  856. os << ")\n";
  857. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  858. for(ImportPropertyMap::const_iterator pi = properties.begin();
  859. pi != properties.end(); ++pi)
  860. {
  861. os << " " << pi->first << " \"" << pi->second << "\"\n";
  862. }
  863. os << " )\n"
  864. << "\n";
  865. }
  866. //----------------------------------------------------------------------------
  867. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  868. const std::vector<std::string>& missingTargets)
  869. {
  870. if (missingTargets.empty())
  871. {
  872. os << "# This file does not depend on other imported targets which have\n"
  873. "# been exported from the same project but in a separate "
  874. "export set.\n\n";
  875. return;
  876. }
  877. os << "# Make sure the targets which have been exported in some other \n"
  878. "# export set exist.\n"
  879. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  880. "foreach(_target ";
  881. std::set<std::string> emitted;
  882. for(unsigned int i=0; i<missingTargets.size(); ++i)
  883. {
  884. if (emitted.insert(missingTargets[i]).second)
  885. {
  886. os << "\"" << missingTargets[i] << "\" ";
  887. }
  888. }
  889. os << ")\n"
  890. " if(NOT TARGET \"${_target}\" )\n"
  891. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  892. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  893. "\n"
  894. " endif()\n"
  895. "endforeach()\n"
  896. "\n"
  897. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  898. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  899. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  900. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  901. "\"The following imported targets are "
  902. "referenced, but are missing: "
  903. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  904. " else()\n"
  905. " message(FATAL_ERROR \"The following imported targets are "
  906. "referenced, but are missing: "
  907. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  908. " endif()\n"
  909. "endif()\n"
  910. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  911. "\n";
  912. }
  913. //----------------------------------------------------------------------------
  914. void
  915. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  916. {
  917. // Add code which verifies at cmake time that the file which is being
  918. // imported actually exists on disk. This should in theory always be theory
  919. // case, but still when packages are split into normal and development
  920. // packages this might get broken (e.g. the Config.cmake could be part of
  921. // the non-development package, something similar happened to me without
  922. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  923. // but the development package was not installed.).
  924. os << "# Loop over all imported files and verify that they actually exist\n"
  925. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  926. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  927. " if(NOT EXISTS \"${file}\" )\n"
  928. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  929. " references the file\n"
  930. " \\\"${file}\\\"\n"
  931. "but this file does not exist. Possible reasons include:\n"
  932. "* The file was deleted, renamed, or moved to another location.\n"
  933. "* An install or uninstall procedure did not complete successfully.\n"
  934. "* The installation package was faulty and contained\n"
  935. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  936. "but not all the files it references.\n"
  937. "\")\n"
  938. " endif()\n"
  939. " endforeach()\n"
  940. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  941. "endforeach()\n"
  942. "unset(_IMPORT_CHECK_TARGETS)\n"
  943. "\n";
  944. }
  945. //----------------------------------------------------------------------------
  946. void
  947. cmExportFileGenerator
  948. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  949. ImportPropertyMap const& properties,
  950. const std::set<std::string>& importedLocations)
  951. {
  952. // Construct the imported target name.
  953. std::string targetName = this->Namespace;
  954. targetName += target->GetExportName();
  955. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  956. "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  957. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  958. li != importedLocations.end();
  959. ++li)
  960. {
  961. ImportPropertyMap::const_iterator pi = properties.find(*li);
  962. if (pi != properties.end())
  963. {
  964. os << "\"" << pi->second << "\" ";
  965. }
  966. }
  967. os << ")\n\n";
  968. }