cmExportFileGenerator.cxx 28 KB

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