cmExportFileGenerator.cxx 25 KB

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