cmExportFileGenerator.cxx 38 KB

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