cmExportFileGenerator.cxx 40 KB

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