cmExportFileGenerator.cxx 32 KB

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