cmExportFileGenerator.cxx 29 KB

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