cmExportFileGenerator.cxx 25 KB

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