cmExportFileGenerator.cxx 28 KB

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