cmExportFileGenerator.cxx 23 KB

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