cmExportFileGenerator.cxx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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, propName,
  146. missingTargets);
  147. properties[outputName] = prepro;
  148. }
  149. }
  150. }
  151. //----------------------------------------------------------------------------
  152. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  153. cmTarget *target,
  154. cmGeneratorExpression::PreprocessContext preprocessRule,
  155. ImportPropertyMap &properties,
  156. std::vector<std::string> &missingTargets)
  157. {
  158. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  159. properties, missingTargets);
  160. }
  161. //----------------------------------------------------------------------------
  162. void getPropertyContents(cmTarget *tgt, const char *prop,
  163. std::set<std::string> &ifaceProperties)
  164. {
  165. const char *p = tgt->GetProperty(prop);
  166. if (!p)
  167. {
  168. return;
  169. }
  170. std::vector<std::string> content;
  171. cmSystemTools::ExpandListArgument(p, content);
  172. for (std::vector<std::string>::const_iterator ci = content.begin();
  173. ci != content.end(); ++ci)
  174. {
  175. ifaceProperties.insert(*ci);
  176. }
  177. }
  178. //----------------------------------------------------------------------------
  179. void getCompatibleInterfaceProperties(cmTarget *target,
  180. std::set<std::string> &ifaceProperties,
  181. const char *config)
  182. {
  183. cmComputeLinkInformation *info = target->GetLinkInformation(config);
  184. const cmComputeLinkInformation::ItemVector &deps = info->GetItems();
  185. for(cmComputeLinkInformation::ItemVector::const_iterator li =
  186. deps.begin();
  187. li != deps.end(); ++li)
  188. {
  189. if (!li->Target)
  190. {
  191. continue;
  192. }
  193. getPropertyContents(li->Target,
  194. "COMPATIBLE_INTERFACE_BOOL",
  195. ifaceProperties);
  196. getPropertyContents(li->Target,
  197. "COMPATIBLE_INTERFACE_STRING",
  198. ifaceProperties);
  199. }
  200. }
  201. //----------------------------------------------------------------------------
  202. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  203. cmTarget *target,
  204. ImportPropertyMap &properties)
  205. {
  206. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL",
  207. target, properties);
  208. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING",
  209. target, properties);
  210. std::set<std::string> ifaceProperties;
  211. getPropertyContents(target, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  212. getPropertyContents(target, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  213. getCompatibleInterfaceProperties(target, ifaceProperties, 0);
  214. std::vector<std::string> configNames;
  215. target->GetMakefile()->GetConfigurations(configNames);
  216. for (std::vector<std::string>::const_iterator ci = configNames.begin();
  217. ci != configNames.end(); ++ci)
  218. {
  219. getCompatibleInterfaceProperties(target, ifaceProperties, ci->c_str());
  220. }
  221. for (std::set<std::string>::const_iterator it = ifaceProperties.begin();
  222. it != ifaceProperties.end(); ++it)
  223. {
  224. this->PopulateInterfaceProperty(("INTERFACE_" + *it).c_str(),
  225. target, properties);
  226. }
  227. }
  228. //----------------------------------------------------------------------------
  229. void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget *target,
  230. std::ostream& os,
  231. const ImportPropertyMap &properties)
  232. {
  233. if (!properties.empty())
  234. {
  235. os << "if(NOT ${CMAKE_FIND_PACKAGE_NAME}_NO_INTERFACES)\n";
  236. std::string targetName = this->Namespace;
  237. targetName += target->GetName();
  238. os << " set_target_properties(" << targetName << " PROPERTIES\n";
  239. for(ImportPropertyMap::const_iterator pi = properties.begin();
  240. pi != properties.end(); ++pi)
  241. {
  242. os << " " << pi->first << " \"" << pi->second << "\"\n";
  243. }
  244. os << " )\nendif()\n\n";
  245. }
  246. }
  247. //----------------------------------------------------------------------------
  248. bool
  249. cmExportFileGenerator::AddTargetNamespace(std::string &input,
  250. cmTarget* target,
  251. std::vector<std::string> &missingTargets)
  252. {
  253. cmMakefile *mf = target->GetMakefile();
  254. cmTarget *tgt = mf->FindTargetToUse(input.c_str());
  255. if (!tgt)
  256. {
  257. return false;
  258. }
  259. if(tgt->IsImported())
  260. {
  261. return true;
  262. }
  263. if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  264. {
  265. input = this->Namespace + input;
  266. }
  267. else
  268. {
  269. std::string namespacedTarget;
  270. this->HandleMissingTarget(namespacedTarget, missingTargets,
  271. mf, target, tgt);
  272. if (!namespacedTarget.empty())
  273. {
  274. input = namespacedTarget;
  275. }
  276. }
  277. return true;
  278. }
  279. //----------------------------------------------------------------------------
  280. static bool isGeneratorExpression(const std::string &lib)
  281. {
  282. const std::string::size_type openpos = lib.find("$<");
  283. return (openpos != std::string::npos)
  284. && (lib.find(">", openpos) != std::string::npos);
  285. }
  286. //----------------------------------------------------------------------------
  287. void
  288. cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  289. std::string &input,
  290. cmTarget* target, const char *propName,
  291. std::vector<std::string> &missingTargets,
  292. FreeTargetsReplace replace)
  293. {
  294. if (replace == NoReplaceFreeTargets)
  295. {
  296. this->ResolveTargetsInGeneratorExpression(input, target, propName,
  297. missingTargets);
  298. return;
  299. }
  300. std::vector<std::string> parts;
  301. cmGeneratorExpression::Split(input, parts);
  302. std::string sep;
  303. input = "";
  304. for(std::vector<std::string>::iterator li = parts.begin();
  305. li != parts.end(); ++li)
  306. {
  307. if (!isGeneratorExpression(*li))
  308. {
  309. this->AddTargetNamespace(*li, target, missingTargets);
  310. }
  311. else
  312. {
  313. this->ResolveTargetsInGeneratorExpression(
  314. *li,
  315. target, propName,
  316. missingTargets);
  317. }
  318. input += sep + *li;
  319. sep = ";";
  320. }
  321. }
  322. //----------------------------------------------------------------------------
  323. void
  324. cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  325. std::string &input,
  326. cmTarget* target, const char *propName,
  327. std::vector<std::string> &missingTargets)
  328. {
  329. std::string::size_type pos = 0;
  330. std::string::size_type lastPos = pos;
  331. cmMakefile *mf = target->GetMakefile();
  332. while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
  333. {
  334. std::string::size_type nameStartPos = pos +
  335. sizeof("$<TARGET_PROPERTY:") - 1;
  336. std::string::size_type closePos = input.find(">", nameStartPos);
  337. std::string::size_type commaPos = input.find(",", nameStartPos);
  338. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  339. if (commaPos == input.npos // Implied 'this' target
  340. || closePos == input.npos // Imcomplete expression.
  341. || closePos < commaPos // Implied 'this' target
  342. || nextOpenPos < commaPos) // Non-literal
  343. {
  344. lastPos = nameStartPos;
  345. continue;
  346. }
  347. std::string targetName = input.substr(nameStartPos,
  348. commaPos - nameStartPos);
  349. if (this->AddTargetNamespace(targetName, target, missingTargets))
  350. {
  351. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  352. }
  353. lastPos = nameStartPos + targetName.size() + 1;
  354. }
  355. std::string errorString;
  356. pos = 0;
  357. lastPos = pos;
  358. while((pos = input.find("$<LINKED:", lastPos)) != input.npos)
  359. {
  360. std::string::size_type nameStartPos = pos + sizeof("$<LINKED:") - 1;
  361. std::string::size_type endPos = input.find(">", nameStartPos);
  362. if (endPos == input.npos)
  363. {
  364. errorString = "$<LINKED:...> expression incomplete";
  365. break;
  366. }
  367. std::string targetName = input.substr(nameStartPos,
  368. endPos - nameStartPos);
  369. if(targetName.find("$<") != input.npos)
  370. {
  371. errorString = "$<LINKED:...> requires its parameter to be a "
  372. "literal.";
  373. break;
  374. }
  375. if (this->AddTargetNamespace(targetName, target, missingTargets))
  376. {
  377. assert(propName); // The link libraries strings will
  378. // never contain $<LINKED>
  379. std::string replacement = "$<TARGET_PROPERTY:"
  380. + targetName + "," + propName;
  381. input.replace(pos, endPos - pos, replacement);
  382. lastPos = pos + replacement.size() + 1;
  383. }
  384. else
  385. {
  386. if (pos != 0)
  387. {
  388. if (input[pos - 1] == ';')
  389. {
  390. --pos;
  391. }
  392. }
  393. else if (input[endPos + 1] == ';')
  394. {
  395. ++endPos;
  396. }
  397. input.replace(pos, endPos - pos + 1, "");
  398. lastPos = pos;
  399. }
  400. }
  401. if (!errorString.empty())
  402. {
  403. mf->IssueMessage(cmake::FATAL_ERROR, errorString);
  404. return;
  405. }
  406. pos = 0;
  407. lastPos = pos;
  408. while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
  409. {
  410. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  411. std::string::size_type endPos = input.find(">", nameStartPos);
  412. if (endPos == input.npos)
  413. {
  414. errorString = "$<TARGET_NAME:...> expression incomplete";
  415. break;
  416. }
  417. std::string targetName = input.substr(nameStartPos,
  418. endPos - nameStartPos);
  419. if(targetName.find("$<") != input.npos)
  420. {
  421. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  422. "literal.";
  423. break;
  424. }
  425. if (!this->AddTargetNamespace(targetName, target, missingTargets))
  426. {
  427. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  428. "reachable target.";
  429. break;
  430. }
  431. input.replace(pos, endPos - pos + 1, targetName);
  432. lastPos = endPos;
  433. }
  434. this->ReplaceInstallPrefix(input);
  435. if (!errorString.empty())
  436. {
  437. mf->IssueMessage(cmake::FATAL_ERROR, errorString);
  438. }
  439. }
  440. //----------------------------------------------------------------------------
  441. void
  442. cmExportFileGenerator::ReplaceInstallPrefix(std::string &)
  443. {
  444. // Do nothing
  445. }
  446. //----------------------------------------------------------------------------
  447. void
  448. cmExportFileGenerator
  449. ::SetImportLinkInterface(const char* config, std::string const& suffix,
  450. cmGeneratorExpression::PreprocessContext preprocessRule,
  451. cmTarget* target, ImportPropertyMap& properties,
  452. std::vector<std::string>& missingTargets)
  453. {
  454. // Add the transitive link dependencies for this configuration.
  455. cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  456. target);
  457. if (!iface)
  458. {
  459. return;
  460. }
  461. if (iface->ImplementationIsInterface)
  462. {
  463. this->SetImportLinkProperty(suffix, target,
  464. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  465. iface->Libraries, properties, missingTargets);
  466. return;
  467. }
  468. const char *propContent;
  469. if (const char *prop_suffixed = target->GetProperty(
  470. ("LINK_INTERFACE_LIBRARIES" + suffix).c_str()))
  471. {
  472. propContent = prop_suffixed;
  473. }
  474. else if (const char *prop = target->GetProperty(
  475. "LINK_INTERFACE_LIBRARIES"))
  476. {
  477. propContent = prop;
  478. }
  479. else
  480. {
  481. return;
  482. }
  483. if (!*propContent)
  484. {
  485. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
  486. return;
  487. }
  488. std::string prepro = cmGeneratorExpression::Preprocess(propContent,
  489. preprocessRule);
  490. if (!prepro.empty())
  491. {
  492. this->ResolveTargetsInGeneratorExpressions(prepro, target, 0,
  493. missingTargets,
  494. ReplaceFreeTargets);
  495. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  496. }
  497. }
  498. //----------------------------------------------------------------------------
  499. void
  500. cmExportFileGenerator
  501. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  502. cmTarget* target, ImportPropertyMap& properties,
  503. std::vector<std::string>& missingTargets
  504. )
  505. {
  506. // Get the makefile in which to lookup target information.
  507. cmMakefile* mf = target->GetMakefile();
  508. // Add the soname for unix shared libraries.
  509. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  510. target->GetType() == cmTarget::MODULE_LIBRARY)
  511. {
  512. // Check whether this is a DLL platform.
  513. bool dll_platform =
  514. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  515. if(!dll_platform)
  516. {
  517. std::string prop;
  518. std::string value;
  519. if(target->HasSOName(config))
  520. {
  521. prop = "IMPORTED_SONAME";
  522. value = target->GetSOName(config);
  523. }
  524. else
  525. {
  526. prop = "IMPORTED_NO_SONAME";
  527. value = "TRUE";
  528. }
  529. prop += suffix;
  530. properties[prop] = value;
  531. }
  532. }
  533. // Add the transitive link dependencies for this configuration.
  534. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  535. target))
  536. {
  537. this->SetImportLinkProperty(suffix, target,
  538. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  539. iface->Languages, properties, missingTargets);
  540. this->SetImportLinkProperty(suffix, target,
  541. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  542. iface->SharedDeps, properties, missingTargets);
  543. if(iface->Multiplicity > 0)
  544. {
  545. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  546. prop += suffix;
  547. cmOStringStream m;
  548. m << iface->Multiplicity;
  549. properties[prop] = m.str();
  550. }
  551. }
  552. }
  553. //----------------------------------------------------------------------------
  554. void
  555. cmExportFileGenerator
  556. ::SetImportLinkProperty(std::string const& suffix,
  557. cmTarget* target,
  558. const char* propName,
  559. std::vector<std::string> const& libs,
  560. ImportPropertyMap& properties,
  561. std::vector<std::string>& missingTargets
  562. )
  563. {
  564. // Skip the property if there are no libraries.
  565. if(libs.empty())
  566. {
  567. return;
  568. }
  569. // Construct the property value.
  570. std::string link_libs;
  571. const char* sep = "";
  572. for(std::vector<std::string>::const_iterator li = libs.begin();
  573. li != libs.end(); ++li)
  574. {
  575. // Separate this from the previous entry.
  576. link_libs += sep;
  577. sep = ";";
  578. std::string temp = *li;
  579. this->AddTargetNamespace(temp, target, missingTargets);
  580. link_libs += temp;
  581. }
  582. // Store the property.
  583. std::string prop = propName;
  584. prop += suffix;
  585. properties[prop] = link_libs;
  586. }
  587. //----------------------------------------------------------------------------
  588. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  589. const char* config)
  590. {
  591. os << "#----------------------------------------------------------------\n"
  592. << "# Generated CMake target import file";
  593. if(config)
  594. {
  595. os << " for configuration \"" << config << "\".\n";
  596. }
  597. else
  598. {
  599. os << ".\n";
  600. }
  601. os << "#----------------------------------------------------------------\n"
  602. << "\n";
  603. this->GenerateImportVersionCode(os);
  604. }
  605. //----------------------------------------------------------------------------
  606. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  607. {
  608. os << "# Commands beyond this point should not need to know the version.\n"
  609. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  610. }
  611. //----------------------------------------------------------------------------
  612. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  613. {
  614. // Store an import file format version. This will let us change the
  615. // format later while still allowing old import files to work.
  616. os << "# Commands may need to know the format version.\n"
  617. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  618. << "\n";
  619. }
  620. //----------------------------------------------------------------------------
  621. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  622. const std::string &expectedTargets)
  623. {
  624. os << "set(_targetsDefined)\n"
  625. "set(_targetsNotDefined)\n"
  626. "set(_expectedTargets)\n"
  627. "foreach(_expectedTarget " << expectedTargets << ")\n"
  628. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  629. " if(NOT TARGET ${_expectedTarget})\n"
  630. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  631. " endif()\n"
  632. " if(TARGET ${_expectedTarget})\n"
  633. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  634. " endif()\n"
  635. "endforeach()\n"
  636. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  637. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  638. " cmake_policy(POP)\n"
  639. " return()\n"
  640. "endif()\n"
  641. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  642. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  643. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  644. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  645. "endif()\n"
  646. "unset(_targetsDefined)\n"
  647. "unset(_targetsNotDefined)\n"
  648. "unset(_expectedTargets)\n"
  649. "\n\n";
  650. }
  651. //----------------------------------------------------------------------------
  652. void
  653. cmExportFileGenerator
  654. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  655. {
  656. // Construct the imported target name.
  657. std::string targetName = this->Namespace;
  658. targetName += target->GetName();
  659. // Create the imported target.
  660. os << "# Create imported target " << targetName << "\n";
  661. switch(target->GetType())
  662. {
  663. case cmTarget::EXECUTABLE:
  664. os << "add_executable(" << targetName << " IMPORTED)\n";
  665. break;
  666. case cmTarget::STATIC_LIBRARY:
  667. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  668. break;
  669. case cmTarget::SHARED_LIBRARY:
  670. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  671. break;
  672. case cmTarget::MODULE_LIBRARY:
  673. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  674. break;
  675. default: // should never happen
  676. break;
  677. }
  678. // Mark the imported executable if it has exports.
  679. if(target->IsExecutableWithExports())
  680. {
  681. os << "set_property(TARGET " << targetName
  682. << " PROPERTY ENABLE_EXPORTS 1)\n";
  683. }
  684. // Mark the imported library if it is a framework.
  685. if(target->IsFrameworkOnApple())
  686. {
  687. os << "set_property(TARGET " << targetName
  688. << " PROPERTY FRAMEWORK 1)\n";
  689. }
  690. // Mark the imported executable if it is an application bundle.
  691. if(target->IsAppBundleOnApple())
  692. {
  693. os << "set_property(TARGET " << targetName
  694. << " PROPERTY MACOSX_BUNDLE 1)\n";
  695. }
  696. if (target->IsCFBundleOnApple())
  697. {
  698. os << "set_property(TARGET " << targetName
  699. << " PROPERTY BUNDLE 1)\n";
  700. }
  701. os << "\n";
  702. }
  703. //----------------------------------------------------------------------------
  704. void
  705. cmExportFileGenerator
  706. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  707. cmTarget* target,
  708. ImportPropertyMap const& properties)
  709. {
  710. // Construct the imported target name.
  711. std::string targetName = this->Namespace;
  712. targetName += target->GetName();
  713. // Set the import properties.
  714. os << "# Import target \"" << targetName << "\" for configuration \""
  715. << config << "\"\n";
  716. os << "set_property(TARGET " << targetName
  717. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  718. if(config && *config)
  719. {
  720. os << cmSystemTools::UpperCase(config);
  721. }
  722. else
  723. {
  724. os << "NOCONFIG";
  725. }
  726. os << ")\n";
  727. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  728. for(ImportPropertyMap::const_iterator pi = properties.begin();
  729. pi != properties.end(); ++pi)
  730. {
  731. os << " " << pi->first << " \"" << pi->second << "\"\n";
  732. }
  733. os << " )\n"
  734. << "\n";
  735. }
  736. //----------------------------------------------------------------------------
  737. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  738. const std::vector<std::string>& missingTargets)
  739. {
  740. if (missingTargets.empty())
  741. {
  742. return;
  743. }
  744. os << "# Make sure the targets which have been exported in some other \n"
  745. "# export set exist.\n";
  746. std::set<std::string> emitted;
  747. for(unsigned int i=0; i<missingTargets.size(); ++i)
  748. {
  749. if (emitted.insert(missingTargets[i]).second)
  750. {
  751. os << "if(NOT TARGET \"" << missingTargets[i] << "\" )\n"
  752. << " if(CMAKE_FIND_PACKAGE_NAME)\n"
  753. << " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  754. << " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  755. << "\"Required imported target \\\"" << missingTargets[i]
  756. << "\\\" not found ! \")\n"
  757. << " else()\n"
  758. << " message(FATAL_ERROR \"Required imported target \\\""
  759. << missingTargets[i] << "\\\" not found ! \")\n"
  760. << " endif()\n"
  761. << "endif()\n";
  762. }
  763. }
  764. os << "\n";
  765. }
  766. //----------------------------------------------------------------------------
  767. void
  768. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  769. {
  770. // Add code which verifies at cmake time that the file which is being
  771. // imported actually exists on disk. This should in theory always be theory
  772. // case, but still when packages are split into normal and development
  773. // packages this might get broken (e.g. the Config.cmake could be part of
  774. // the non-development package, something similar happened to me without
  775. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  776. // but the development package was not installed.).
  777. os << "# Loop over all imported files and verify that they actually exist\n"
  778. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  779. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  780. " if(NOT EXISTS \"${file}\" )\n"
  781. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  782. " references the file\n"
  783. " \\\"${file}\\\"\n"
  784. "but this file does not exist. Possible reasons include:\n"
  785. "* The file was deleted, renamed, or moved to another location.\n"
  786. "* An install or uninstall procedure did not complete successfully.\n"
  787. "* The installation package was faulty and contained\n"
  788. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  789. "but not all the files it references.\n"
  790. "\")\n"
  791. " endif()\n"
  792. " endforeach()\n"
  793. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  794. "endforeach()\n"
  795. "unset(_IMPORT_CHECK_TARGETS)\n"
  796. "\n";
  797. }
  798. //----------------------------------------------------------------------------
  799. void
  800. cmExportFileGenerator
  801. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  802. ImportPropertyMap const& properties,
  803. const std::set<std::string>& importedLocations)
  804. {
  805. // Construct the imported target name.
  806. std::string targetName = this->Namespace;
  807. targetName += target->GetName();
  808. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  809. "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  810. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  811. li != importedLocations.end();
  812. ++li)
  813. {
  814. ImportPropertyMap::const_iterator pi = properties.find(*li);
  815. if (pi != properties.end())
  816. {
  817. os << "\"" << pi->second << "\" ";
  818. }
  819. }
  820. os << ")\n\n";
  821. }