cmExportFileGenerator.cxx 28 KB

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