cmExportFileGenerator.cxx 37 KB

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