cmExportFileGenerator.cxx 38 KB

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