cmExportFileGenerator.cxx 34 KB

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