cmExportFileGenerator.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. this->SetImportLinkProperty(suffix, target,
  330. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  331. iface->Libraries, properties, missingTargets);
  332. }
  333. //----------------------------------------------------------------------------
  334. void
  335. cmExportFileGenerator
  336. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  337. cmTarget* target, ImportPropertyMap& properties,
  338. std::vector<std::string>& missingTargets
  339. )
  340. {
  341. // Get the makefile in which to lookup target information.
  342. cmMakefile* mf = target->GetMakefile();
  343. // Add the soname for unix shared libraries.
  344. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  345. target->GetType() == cmTarget::MODULE_LIBRARY)
  346. {
  347. // Check whether this is a DLL platform.
  348. bool dll_platform =
  349. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  350. if(!dll_platform)
  351. {
  352. std::string prop;
  353. std::string value;
  354. if(target->HasSOName(config))
  355. {
  356. prop = "IMPORTED_SONAME";
  357. value = target->GetSOName(config);
  358. }
  359. else
  360. {
  361. prop = "IMPORTED_NO_SONAME";
  362. value = "TRUE";
  363. }
  364. prop += suffix;
  365. properties[prop] = value;
  366. }
  367. }
  368. // Add the transitive link dependencies for this configuration.
  369. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  370. target))
  371. {
  372. this->SetImportLinkProperty(suffix, target,
  373. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  374. iface->Languages, properties, missingTargets);
  375. this->SetImportLinkProperty(suffix, target,
  376. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  377. iface->SharedDeps, properties, missingTargets);
  378. if(iface->Multiplicity > 0)
  379. {
  380. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  381. prop += suffix;
  382. cmOStringStream m;
  383. m << iface->Multiplicity;
  384. properties[prop] = m.str();
  385. }
  386. }
  387. }
  388. //----------------------------------------------------------------------------
  389. void
  390. cmExportFileGenerator
  391. ::SetImportLinkProperty(std::string const& suffix,
  392. cmTarget* target,
  393. const char* propName,
  394. std::vector<std::string> const& libs,
  395. ImportPropertyMap& properties,
  396. std::vector<std::string>& missingTargets
  397. )
  398. {
  399. // Skip the property if there are no libraries.
  400. if(libs.empty())
  401. {
  402. return;
  403. }
  404. // Construct the property value.
  405. std::string link_libs;
  406. const char* sep = "";
  407. for(std::vector<std::string>::const_iterator li = libs.begin();
  408. li != libs.end(); ++li)
  409. {
  410. // Separate this from the previous entry.
  411. link_libs += sep;
  412. sep = ";";
  413. std::string temp = *li;
  414. this->AddTargetNamespace(temp, target, missingTargets);
  415. link_libs += temp;
  416. }
  417. // Store the property.
  418. std::string prop = propName;
  419. prop += suffix;
  420. properties[prop] = link_libs;
  421. }
  422. //----------------------------------------------------------------------------
  423. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  424. const char* config)
  425. {
  426. os << "#----------------------------------------------------------------\n"
  427. << "# Generated CMake target import file";
  428. if(config)
  429. {
  430. os << " for configuration \"" << config << "\".\n";
  431. }
  432. else
  433. {
  434. os << ".\n";
  435. }
  436. os << "#----------------------------------------------------------------\n"
  437. << "\n";
  438. this->GenerateImportVersionCode(os);
  439. }
  440. //----------------------------------------------------------------------------
  441. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  442. {
  443. os << "# Commands beyond this point should not need to know the version.\n"
  444. << "SET(CMAKE_IMPORT_FILE_VERSION)\n";
  445. }
  446. //----------------------------------------------------------------------------
  447. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  448. {
  449. // Store an import file format version. This will let us change the
  450. // format later while still allowing old import files to work.
  451. os << "# Commands may need to know the format version.\n"
  452. << "SET(CMAKE_IMPORT_FILE_VERSION 1)\n"
  453. << "\n";
  454. }
  455. //----------------------------------------------------------------------------
  456. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  457. const std::string &expectedTargets)
  458. {
  459. os << "SET(_targetsDefined)\n"
  460. "SET(_targetsNotDefined)\n"
  461. "SET(_expectedTargets)\n"
  462. "FOREACH(_expectedTarget " << expectedTargets << ")\n"
  463. " LIST(APPEND _expectedTargets ${_expectedTarget})\n"
  464. " IF(NOT TARGET ${_expectedTarget})\n"
  465. " LIST(APPEND _targetsNotDefined ${_expectedTarget})\n"
  466. " ENDIF(NOT TARGET ${_expectedTarget})\n"
  467. " IF(TARGET ${_expectedTarget})\n"
  468. " LIST(APPEND _targetsDefined ${_expectedTarget})\n"
  469. " ENDIF(TARGET ${_expectedTarget})\n"
  470. "ENDFOREACH(_expectedTarget)\n"
  471. "IF(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  472. " SET(CMAKE_IMPORT_FILE_VERSION)\n"
  473. " CMAKE_POLICY(POP)\n"
  474. " RETURN()\n"
  475. "ENDIF(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  476. "IF(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  477. " MESSAGE(FATAL_ERROR \"Some (but not all) targets in this export "
  478. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  479. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  480. "ENDIF(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  481. "UNSET(_targetsDefined)\n"
  482. "UNSET(_targetsNotDefined)\n"
  483. "UNSET(_expectedTargets)\n"
  484. "\n\n";
  485. }
  486. //----------------------------------------------------------------------------
  487. void
  488. cmExportFileGenerator
  489. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  490. {
  491. // Construct the imported target name.
  492. std::string targetName = this->Namespace;
  493. targetName += target->GetName();
  494. // Create the imported target.
  495. os << "# Create imported target " << targetName << "\n";
  496. switch(target->GetType())
  497. {
  498. case cmTarget::EXECUTABLE:
  499. os << "ADD_EXECUTABLE(" << targetName << " IMPORTED)\n";
  500. break;
  501. case cmTarget::STATIC_LIBRARY:
  502. os << "ADD_LIBRARY(" << targetName << " STATIC IMPORTED)\n";
  503. break;
  504. case cmTarget::SHARED_LIBRARY:
  505. os << "ADD_LIBRARY(" << targetName << " SHARED IMPORTED)\n";
  506. break;
  507. case cmTarget::MODULE_LIBRARY:
  508. os << "ADD_LIBRARY(" << targetName << " MODULE IMPORTED)\n";
  509. break;
  510. default: // should never happen
  511. break;
  512. }
  513. // Mark the imported executable if it has exports.
  514. if(target->IsExecutableWithExports())
  515. {
  516. os << "SET_PROPERTY(TARGET " << targetName
  517. << " PROPERTY ENABLE_EXPORTS 1)\n";
  518. }
  519. // Mark the imported library if it is a framework.
  520. if(target->IsFrameworkOnApple())
  521. {
  522. os << "SET_PROPERTY(TARGET " << targetName
  523. << " PROPERTY FRAMEWORK 1)\n";
  524. }
  525. // Mark the imported executable if it is an application bundle.
  526. if(target->IsAppBundleOnApple())
  527. {
  528. os << "SET_PROPERTY(TARGET " << targetName
  529. << " PROPERTY MACOSX_BUNDLE 1)\n";
  530. }
  531. if (target->IsCFBundleOnApple())
  532. {
  533. os << "SET_PROPERTY(TARGET " << targetName
  534. << " PROPERTY BUNDLE 1)\n";
  535. }
  536. os << "\n";
  537. }
  538. //----------------------------------------------------------------------------
  539. void
  540. cmExportFileGenerator
  541. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  542. cmTarget* target,
  543. ImportPropertyMap const& properties)
  544. {
  545. // Construct the imported target name.
  546. std::string targetName = this->Namespace;
  547. targetName += target->GetName();
  548. // Set the import properties.
  549. os << "# Import target \"" << targetName << "\" for configuration \""
  550. << config << "\"\n";
  551. os << "SET_PROPERTY(TARGET " << targetName
  552. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  553. if(config && *config)
  554. {
  555. os << cmSystemTools::UpperCase(config);
  556. }
  557. else
  558. {
  559. os << "NOCONFIG";
  560. }
  561. os << ")\n";
  562. os << "SET_TARGET_PROPERTIES(" << targetName << " PROPERTIES\n";
  563. for(ImportPropertyMap::const_iterator pi = properties.begin();
  564. pi != properties.end(); ++pi)
  565. {
  566. os << " " << pi->first << " \"" << pi->second << "\"\n";
  567. }
  568. os << " )\n"
  569. << "\n";
  570. }
  571. //----------------------------------------------------------------------------
  572. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  573. const std::vector<std::string>& missingTargets)
  574. {
  575. os << "# Make sure the targets which have been exported in some other \n"
  576. "# export set exist.\n";
  577. for(unsigned int i=0; i<missingTargets.size(); ++i)
  578. {
  579. os << "IF(NOT TARGET \"" << missingTargets[i] << "\" )\n"
  580. << " IF(CMAKE_FIND_PACKAGE_NAME)\n"
  581. << " SET( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  582. << " SET( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  583. << "\"Required imported target \\\"" << missingTargets[i]
  584. << "\\\" not found ! \")\n"
  585. << " ELSE()\n"
  586. << " MESSAGE(FATAL_ERROR \"Required imported target \\\""
  587. << missingTargets[i] << "\\\" not found ! \")\n"
  588. << " ENDIF()\n"
  589. << "ENDIF()\n";
  590. }
  591. os << "\n";
  592. }
  593. //----------------------------------------------------------------------------
  594. void
  595. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  596. {
  597. // Add code which verifies at cmake time that the file which is being
  598. // imported actually exists on disk. This should in theory always be theory
  599. // case, but still when packages are split into normal and development
  600. // packages this might get broken (e.g. the Config.cmake could be part of
  601. // the non-development package, something similar happened to me without
  602. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  603. // but the development package was not installed.).
  604. os << "# Loop over all imported files and verify that they actually exist\n"
  605. "FOREACH(target ${_IMPORT_CHECK_TARGETS} )\n"
  606. " FOREACH(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  607. " IF(NOT EXISTS \"${file}\" )\n"
  608. " MESSAGE(FATAL_ERROR \"The imported target \\\"${target}\\\""
  609. " references the file\n"
  610. " \\\"${file}\\\"\n"
  611. "but this file does not exist. Possible reasons include:\n"
  612. "* The file was deleted, renamed, or moved to another location.\n"
  613. "* An install or uninstall procedure did not complete successfully.\n"
  614. "* The installation package was faulty and contained\n"
  615. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  616. "but not all the files it references.\n"
  617. "\")\n"
  618. " ENDIF()\n"
  619. " ENDFOREACH()\n"
  620. " UNSET(_IMPORT_CHECK_FILES_FOR_${target})\n"
  621. "ENDFOREACH()\n"
  622. "UNSET(_IMPORT_CHECK_TARGETS)\n"
  623. "\n";
  624. }
  625. //----------------------------------------------------------------------------
  626. void
  627. cmExportFileGenerator
  628. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  629. ImportPropertyMap const& properties,
  630. const std::set<std::string>& importedLocations)
  631. {
  632. // Construct the imported target name.
  633. std::string targetName = this->Namespace;
  634. targetName += target->GetName();
  635. os << "LIST(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  636. "LIST(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  637. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  638. li != importedLocations.end();
  639. ++li)
  640. {
  641. ImportPropertyMap::const_iterator pi = properties.find(*li);
  642. if (pi != properties.end())
  643. {
  644. os << "\"" << pi->second << "\" ";
  645. }
  646. }
  647. os << ")\n\n";
  648. }