cmExportFileGenerator.cxx 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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. #include <assert.h>
  24. //----------------------------------------------------------------------------
  25. cmExportFileGenerator::cmExportFileGenerator()
  26. {
  27. this->AppendMode = false;
  28. this->ExportOld = false;
  29. }
  30. //----------------------------------------------------------------------------
  31. void cmExportFileGenerator::AddConfiguration(const char* config)
  32. {
  33. this->Configurations.push_back(config);
  34. }
  35. //----------------------------------------------------------------------------
  36. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  37. {
  38. this->MainImportFile = mainFile;
  39. this->FileDir =
  40. cmSystemTools::GetFilenamePath(this->MainImportFile);
  41. this->FileBase =
  42. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  43. this->FileExt =
  44. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  45. }
  46. //----------------------------------------------------------------------------
  47. bool cmExportFileGenerator::GenerateImportFile()
  48. {
  49. // Open the output file to generate it.
  50. cmsys::auto_ptr<std::ofstream> foutPtr;
  51. if(this->AppendMode)
  52. {
  53. // Open for append.
  54. cmsys::auto_ptr<std::ofstream>
  55. ap(new std::ofstream(this->MainImportFile.c_str(), std::ios::app));
  56. foutPtr = ap;
  57. }
  58. else
  59. {
  60. // Generate atomically and with copy-if-different.
  61. cmsys::auto_ptr<cmGeneratedFileStream>
  62. ap(new cmGeneratedFileStream(this->MainImportFile.c_str(), true));
  63. ap->SetCopyIfDifferent(true);
  64. foutPtr = ap;
  65. }
  66. if(!foutPtr.get() || !*foutPtr)
  67. {
  68. std::string se = cmSystemTools::GetLastSystemError();
  69. cmOStringStream e;
  70. e << "cannot write to file \"" << this->MainImportFile
  71. << "\": " << se;
  72. cmSystemTools::Error(e.str().c_str());
  73. return false;
  74. }
  75. std::ostream& os = *foutPtr;
  76. // Protect that file against use with older CMake versions.
  77. os << "# Generated by CMake " << cmVersion::GetCMakeVersion() << "\n\n";
  78. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
  79. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  80. << "endif()\n";
  81. // Isolate the file policy level.
  82. // We use 2.6 here instead of the current version because newer
  83. // versions of CMake should be able to export files imported by 2.6
  84. // until the import format changes.
  85. os << "cmake_policy(PUSH)\n"
  86. << "cmake_policy(VERSION 2.6)\n";
  87. // Start with the import file header.
  88. this->GenerateImportHeaderCode(os);
  89. // Create all the imported targets.
  90. bool result = this->GenerateMainFile(os);
  91. // End with the import file footer.
  92. this->GenerateImportFooterCode(os);
  93. os << "cmake_policy(POP)\n";
  94. return result;
  95. }
  96. //----------------------------------------------------------------------------
  97. void cmExportFileGenerator::GenerateImportConfig(std::ostream& os,
  98. const char* config,
  99. std::vector<std::string> &missingTargets)
  100. {
  101. // Construct the property configuration suffix.
  102. std::string suffix = "_";
  103. if(config && *config)
  104. {
  105. suffix += cmSystemTools::UpperCase(config);
  106. }
  107. else
  108. {
  109. suffix += "NOCONFIG";
  110. }
  111. // Generate the per-config target information.
  112. this->GenerateImportTargetsConfig(os, config, suffix, missingTargets);
  113. }
  114. //----------------------------------------------------------------------------
  115. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  116. cmTarget *target,
  117. ImportPropertyMap &properties)
  118. {
  119. const char *input = target->GetProperty(propName);
  120. if (input)
  121. {
  122. properties[propName] = input;
  123. }
  124. }
  125. //----------------------------------------------------------------------------
  126. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  127. const char *outputName,
  128. cmTarget *target,
  129. cmGeneratorExpression::PreprocessContext preprocessRule,
  130. ImportPropertyMap &properties,
  131. std::vector<std::string> &missingTargets)
  132. {
  133. const char *input = target->GetProperty(propName);
  134. if (input)
  135. {
  136. if (!*input)
  137. {
  138. // Set to empty
  139. properties[outputName] = "";
  140. return;
  141. }
  142. std::string prepro = cmGeneratorExpression::Preprocess(input,
  143. preprocessRule);
  144. if (!prepro.empty())
  145. {
  146. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  147. missingTargets);
  148. properties[outputName] = prepro;
  149. }
  150. }
  151. }
  152. void cmExportFileGenerator::GenerateRequiredCMakeVersion(std::ostream& os,
  153. const char *versionString)
  154. {
  155. os << "if(CMAKE_VERSION VERSION_LESS " << versionString << ")\n"
  156. " message(FATAL_ERROR \"This file relies on consumers using "
  157. "CMake " << versionString << " or greater.\")\n"
  158. "endif()\n\n";
  159. }
  160. //----------------------------------------------------------------------------
  161. bool cmExportFileGenerator::PopulateInterfaceLinkLibrariesProperty(
  162. cmTarget *target,
  163. cmGeneratorExpression::PreprocessContext preprocessRule,
  164. ImportPropertyMap &properties,
  165. std::vector<std::string> &missingTargets)
  166. {
  167. const char *input = target->GetProperty("INTERFACE_LINK_LIBRARIES");
  168. if (input)
  169. {
  170. std::string prepro = cmGeneratorExpression::Preprocess(input,
  171. preprocessRule);
  172. if (!prepro.empty())
  173. {
  174. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  175. missingTargets,
  176. ReplaceFreeTargets);
  177. properties["INTERFACE_LINK_LIBRARIES"] = prepro;
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. //----------------------------------------------------------------------------
  184. static bool isSubDirectory(const char* a, const char* b)
  185. {
  186. return (cmSystemTools::ComparePath(a, b) ||
  187. cmSystemTools::IsSubDirectory(a, b));
  188. }
  189. //----------------------------------------------------------------------------
  190. static bool checkInterfaceDirs(const std::string &prepro,
  191. cmTarget *target)
  192. {
  193. const char* installDir =
  194. target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  195. const char* topSourceDir = target->GetMakefile()->GetHomeDirectory();
  196. const char* topBinaryDir = target->GetMakefile()->GetHomeOutputDirectory();
  197. std::vector<std::string> parts;
  198. cmGeneratorExpression::Split(prepro, parts);
  199. const bool inSourceBuild = strcmp(topSourceDir, topBinaryDir) == 0;
  200. for(std::vector<std::string>::iterator li = parts.begin();
  201. li != parts.end(); ++li)
  202. {
  203. if (cmGeneratorExpression::Find(*li) != std::string::npos)
  204. {
  205. continue;
  206. }
  207. if (strncmp(li->c_str(), "${_IMPORT_PREFIX}", 17) == 0)
  208. {
  209. continue;
  210. }
  211. if (!cmSystemTools::FileIsFullPath(li->c_str()))
  212. {
  213. cmOStringStream e;
  214. e << "Target \"" << target->GetName() << "\" "
  215. "INTERFACE_INCLUDE_DIRECTORIES property contains relative path:\n"
  216. " \"" << *li << "\"";
  217. target->GetMakefile()->IssueMessage(cmake::FATAL_ERROR,
  218. e.str().c_str());
  219. return false;
  220. }
  221. if (isSubDirectory(li->c_str(), installDir))
  222. {
  223. continue;
  224. }
  225. if (isSubDirectory(li->c_str(), topBinaryDir))
  226. {
  227. cmOStringStream e;
  228. e << "Target \"" << target->GetName() << "\" "
  229. "INTERFACE_INCLUDE_DIRECTORIES property contains path:\n"
  230. " \"" << *li << "\"\nwhich is prefixed in the build directory.";
  231. target->GetMakefile()->IssueMessage(cmake::FATAL_ERROR,
  232. e.str().c_str());
  233. return false;
  234. }
  235. if (!inSourceBuild)
  236. {
  237. if (isSubDirectory(li->c_str(), topSourceDir))
  238. {
  239. cmOStringStream e;
  240. e << "Target \"" << target->GetName() << "\" "
  241. "INTERFACE_INCLUDE_DIRECTORIES property contains path:\n"
  242. " \"" << *li << "\"\nwhich is prefixed in the source directory.";
  243. target->GetMakefile()->IssueMessage(cmake::FATAL_ERROR,
  244. e.str().c_str());
  245. return false;
  246. }
  247. }
  248. }
  249. return true;
  250. }
  251. //----------------------------------------------------------------------------
  252. void cmExportFileGenerator::PopulateIncludeDirectoriesInterface(
  253. cmTargetExport *tei,
  254. cmGeneratorExpression::PreprocessContext preprocessRule,
  255. ImportPropertyMap &properties,
  256. std::vector<std::string> &missingTargets)
  257. {
  258. cmTarget *target = tei->Target;
  259. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  260. const char *propName = "INTERFACE_INCLUDE_DIRECTORIES";
  261. const char *input = target->GetProperty(propName);
  262. if (!input && tei->InterfaceIncludeDirectories.empty())
  263. {
  264. return;
  265. }
  266. if ((input && !*input) && tei->InterfaceIncludeDirectories.empty())
  267. {
  268. // Set to empty
  269. properties[propName] = "";
  270. return;
  271. }
  272. std::string includes = (input?input:"");
  273. const char* sep = input ? ";" : "";
  274. includes += sep + tei->InterfaceIncludeDirectories;
  275. std::string prepro = cmGeneratorExpression::Preprocess(includes,
  276. preprocessRule,
  277. true);
  278. if (!prepro.empty())
  279. {
  280. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  281. missingTargets);
  282. if (!checkInterfaceDirs(prepro, target))
  283. {
  284. return;
  285. }
  286. properties[propName] = prepro;
  287. }
  288. }
  289. //----------------------------------------------------------------------------
  290. void cmExportFileGenerator::PopulateInterfaceProperty(const char *propName,
  291. cmTarget *target,
  292. cmGeneratorExpression::PreprocessContext preprocessRule,
  293. ImportPropertyMap &properties,
  294. std::vector<std::string> &missingTargets)
  295. {
  296. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  297. properties, missingTargets);
  298. }
  299. //----------------------------------------------------------------------------
  300. void getPropertyContents(cmTarget *tgt, const char *prop,
  301. std::set<std::string> &ifaceProperties)
  302. {
  303. const char *p = tgt->GetProperty(prop);
  304. if (!p)
  305. {
  306. return;
  307. }
  308. std::vector<std::string> content;
  309. cmSystemTools::ExpandListArgument(p, content);
  310. for (std::vector<std::string>::const_iterator ci = content.begin();
  311. ci != content.end(); ++ci)
  312. {
  313. ifaceProperties.insert(*ci);
  314. }
  315. }
  316. //----------------------------------------------------------------------------
  317. void getCompatibleInterfaceProperties(cmTarget *target,
  318. std::set<std::string> &ifaceProperties,
  319. const char *config)
  320. {
  321. cmComputeLinkInformation *info = target->GetLinkInformation(config);
  322. if (!info)
  323. {
  324. cmMakefile* mf = target->GetMakefile();
  325. cmOStringStream e;
  326. e << "Exporting the target \"" << target->GetName() << "\" is not "
  327. "allowed since its linker language cannot be determined";
  328. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  329. return;
  330. }
  331. const cmComputeLinkInformation::ItemVector &deps = info->GetItems();
  332. for(cmComputeLinkInformation::ItemVector::const_iterator li =
  333. deps.begin();
  334. li != deps.end(); ++li)
  335. {
  336. if (!li->Target)
  337. {
  338. continue;
  339. }
  340. getPropertyContents(li->Target,
  341. "COMPATIBLE_INTERFACE_BOOL",
  342. ifaceProperties);
  343. getPropertyContents(li->Target,
  344. "COMPATIBLE_INTERFACE_STRING",
  345. ifaceProperties);
  346. }
  347. }
  348. //----------------------------------------------------------------------------
  349. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  350. cmTarget *target,
  351. ImportPropertyMap &properties)
  352. {
  353. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL",
  354. target, properties);
  355. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING",
  356. target, properties);
  357. std::set<std::string> ifaceProperties;
  358. getPropertyContents(target, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  359. getPropertyContents(target, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  360. getCompatibleInterfaceProperties(target, ifaceProperties, 0);
  361. std::vector<std::string> configNames;
  362. target->GetMakefile()->GetConfigurations(configNames);
  363. for (std::vector<std::string>::const_iterator ci = configNames.begin();
  364. ci != configNames.end(); ++ci)
  365. {
  366. getCompatibleInterfaceProperties(target, ifaceProperties, ci->c_str());
  367. }
  368. for (std::set<std::string>::const_iterator it = ifaceProperties.begin();
  369. it != ifaceProperties.end(); ++it)
  370. {
  371. this->PopulateInterfaceProperty(("INTERFACE_" + *it).c_str(),
  372. target, properties);
  373. }
  374. }
  375. //----------------------------------------------------------------------------
  376. void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget *target,
  377. std::ostream& os,
  378. const ImportPropertyMap &properties)
  379. {
  380. if (!properties.empty())
  381. {
  382. std::string targetName = this->Namespace;
  383. targetName += target->GetExportName();
  384. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  385. for(ImportPropertyMap::const_iterator pi = properties.begin();
  386. pi != properties.end(); ++pi)
  387. {
  388. os << " " << pi->first << " \"" << pi->second << "\"\n";
  389. }
  390. os << ")\n\n";
  391. }
  392. }
  393. //----------------------------------------------------------------------------
  394. bool
  395. cmExportFileGenerator::AddTargetNamespace(std::string &input,
  396. cmTarget* target,
  397. std::vector<std::string> &missingTargets)
  398. {
  399. cmMakefile *mf = target->GetMakefile();
  400. cmTarget *tgt = mf->FindTargetToUse(input.c_str());
  401. if (!tgt)
  402. {
  403. return false;
  404. }
  405. if(tgt->IsImported())
  406. {
  407. return true;
  408. }
  409. if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  410. {
  411. input = this->Namespace + tgt->GetExportName();
  412. }
  413. else
  414. {
  415. std::string namespacedTarget;
  416. this->HandleMissingTarget(namespacedTarget, missingTargets,
  417. mf, target, tgt);
  418. if (!namespacedTarget.empty())
  419. {
  420. input = namespacedTarget;
  421. }
  422. }
  423. return true;
  424. }
  425. //----------------------------------------------------------------------------
  426. void
  427. cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  428. std::string &input,
  429. cmTarget* target,
  430. std::vector<std::string> &missingTargets,
  431. FreeTargetsReplace replace)
  432. {
  433. if (replace == NoReplaceFreeTargets)
  434. {
  435. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  436. return;
  437. }
  438. std::vector<std::string> parts;
  439. cmGeneratorExpression::Split(input, parts);
  440. std::string sep;
  441. input = "";
  442. for(std::vector<std::string>::iterator li = parts.begin();
  443. li != parts.end(); ++li)
  444. {
  445. if (cmGeneratorExpression::Find(*li) == std::string::npos)
  446. {
  447. this->AddTargetNamespace(*li, target, missingTargets);
  448. }
  449. else
  450. {
  451. this->ResolveTargetsInGeneratorExpression(
  452. *li,
  453. target,
  454. missingTargets);
  455. }
  456. input += sep + *li;
  457. sep = ";";
  458. }
  459. }
  460. //----------------------------------------------------------------------------
  461. void
  462. cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  463. std::string &input,
  464. cmTarget* target,
  465. std::vector<std::string> &missingTargets)
  466. {
  467. std::string::size_type pos = 0;
  468. std::string::size_type lastPos = pos;
  469. cmMakefile *mf = target->GetMakefile();
  470. while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
  471. {
  472. std::string::size_type nameStartPos = pos +
  473. sizeof("$<TARGET_PROPERTY:") - 1;
  474. std::string::size_type closePos = input.find(">", nameStartPos);
  475. std::string::size_type commaPos = input.find(",", nameStartPos);
  476. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  477. if (commaPos == input.npos // Implied 'this' target
  478. || closePos == input.npos // Imcomplete expression.
  479. || closePos < commaPos // Implied 'this' target
  480. || nextOpenPos < commaPos) // Non-literal
  481. {
  482. lastPos = nameStartPos;
  483. continue;
  484. }
  485. std::string targetName = input.substr(nameStartPos,
  486. commaPos - nameStartPos);
  487. if (this->AddTargetNamespace(targetName, target, missingTargets))
  488. {
  489. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  490. }
  491. lastPos = nameStartPos + targetName.size() + 1;
  492. }
  493. std::string errorString;
  494. pos = 0;
  495. lastPos = pos;
  496. while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
  497. {
  498. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  499. std::string::size_type endPos = input.find(">", nameStartPos);
  500. if (endPos == input.npos)
  501. {
  502. errorString = "$<TARGET_NAME:...> expression incomplete";
  503. break;
  504. }
  505. std::string targetName = input.substr(nameStartPos,
  506. endPos - nameStartPos);
  507. if(targetName.find("$<") != input.npos)
  508. {
  509. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  510. "literal.";
  511. break;
  512. }
  513. if (!this->AddTargetNamespace(targetName, target, missingTargets))
  514. {
  515. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  516. "reachable target.";
  517. break;
  518. }
  519. input.replace(pos, endPos - pos + 1, targetName);
  520. lastPos = endPos;
  521. }
  522. this->ReplaceInstallPrefix(input);
  523. if (!errorString.empty())
  524. {
  525. mf->IssueMessage(cmake::FATAL_ERROR, errorString);
  526. }
  527. }
  528. //----------------------------------------------------------------------------
  529. void
  530. cmExportFileGenerator::ReplaceInstallPrefix(std::string &)
  531. {
  532. // Do nothing
  533. }
  534. //----------------------------------------------------------------------------
  535. void
  536. cmExportFileGenerator
  537. ::SetImportLinkInterface(const char* config, std::string const& suffix,
  538. cmGeneratorExpression::PreprocessContext preprocessRule,
  539. cmTarget* target, ImportPropertyMap& properties,
  540. std::vector<std::string>& missingTargets)
  541. {
  542. // Add the transitive link dependencies for this configuration.
  543. cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  544. target);
  545. if (!iface)
  546. {
  547. return;
  548. }
  549. if (iface->ImplementationIsInterface)
  550. {
  551. this->SetImportLinkProperty(suffix, target,
  552. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  553. iface->Libraries, properties, missingTargets);
  554. return;
  555. }
  556. const char *propContent;
  557. if (const char *prop_suffixed = target->GetProperty(
  558. ("LINK_INTERFACE_LIBRARIES" + suffix).c_str()))
  559. {
  560. propContent = prop_suffixed;
  561. }
  562. else if (const char *prop = target->GetProperty(
  563. "LINK_INTERFACE_LIBRARIES"))
  564. {
  565. propContent = prop;
  566. }
  567. else
  568. {
  569. return;
  570. }
  571. const bool newCMP0022Behavior =
  572. target->GetPolicyStatusCMP0022() != cmPolicies::WARN
  573. && target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  574. if(newCMP0022Behavior && !this->ExportOld)
  575. {
  576. cmMakefile *mf = target->GetMakefile();
  577. cmOStringStream e;
  578. e << "Target \"" << target->GetName() << "\" has policy CMP0022 enabled, "
  579. "but also has old-style INTERFACE_LINK_LIBRARIES properties "
  580. "populated, but it was exported without the "
  581. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  582. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  583. return;
  584. }
  585. if (!*propContent)
  586. {
  587. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
  588. return;
  589. }
  590. std::string prepro = cmGeneratorExpression::Preprocess(propContent,
  591. preprocessRule);
  592. if (!prepro.empty())
  593. {
  594. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  595. missingTargets,
  596. ReplaceFreeTargets);
  597. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  598. }
  599. }
  600. //----------------------------------------------------------------------------
  601. void
  602. cmExportFileGenerator
  603. ::SetImportDetailProperties(const char* config, std::string const& suffix,
  604. cmTarget* target, ImportPropertyMap& properties,
  605. std::vector<std::string>& missingTargets
  606. )
  607. {
  608. // Get the makefile in which to lookup target information.
  609. cmMakefile* mf = target->GetMakefile();
  610. // Add the soname for unix shared libraries.
  611. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  612. target->GetType() == cmTarget::MODULE_LIBRARY)
  613. {
  614. // Check whether this is a DLL platform.
  615. bool dll_platform =
  616. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  617. if(!dll_platform)
  618. {
  619. std::string prop;
  620. std::string value;
  621. if(target->HasSOName(config))
  622. {
  623. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  624. {
  625. value = this->InstallNameDir(target, config);
  626. }
  627. prop = "IMPORTED_SONAME";
  628. value += target->GetSOName(config);
  629. }
  630. else
  631. {
  632. prop = "IMPORTED_NO_SONAME";
  633. value = "TRUE";
  634. }
  635. prop += suffix;
  636. properties[prop] = value;
  637. }
  638. }
  639. // Add the transitive link dependencies for this configuration.
  640. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  641. target))
  642. {
  643. this->SetImportLinkProperty(suffix, target,
  644. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  645. iface->Languages, properties, missingTargets);
  646. this->SetImportLinkProperty(suffix, target,
  647. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  648. iface->SharedDeps, properties, missingTargets);
  649. if(iface->Multiplicity > 0)
  650. {
  651. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  652. prop += suffix;
  653. cmOStringStream m;
  654. m << iface->Multiplicity;
  655. properties[prop] = m.str();
  656. }
  657. }
  658. }
  659. //----------------------------------------------------------------------------
  660. void
  661. cmExportFileGenerator
  662. ::SetImportLinkProperty(std::string const& suffix,
  663. cmTarget* target,
  664. const char* propName,
  665. std::vector<std::string> const& libs,
  666. ImportPropertyMap& properties,
  667. std::vector<std::string>& missingTargets
  668. )
  669. {
  670. // Skip the property if there are no libraries.
  671. if(libs.empty())
  672. {
  673. return;
  674. }
  675. // Construct the property value.
  676. std::string link_libs;
  677. const char* sep = "";
  678. for(std::vector<std::string>::const_iterator li = libs.begin();
  679. li != libs.end(); ++li)
  680. {
  681. // Separate this from the previous entry.
  682. link_libs += sep;
  683. sep = ";";
  684. std::string temp = *li;
  685. this->AddTargetNamespace(temp, target, missingTargets);
  686. link_libs += temp;
  687. }
  688. // Store the property.
  689. std::string prop = propName;
  690. prop += suffix;
  691. properties[prop] = link_libs;
  692. }
  693. //----------------------------------------------------------------------------
  694. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  695. const char* config)
  696. {
  697. os << "#----------------------------------------------------------------\n"
  698. << "# Generated CMake target import file";
  699. if(config)
  700. {
  701. os << " for configuration \"" << config << "\".\n";
  702. }
  703. else
  704. {
  705. os << ".\n";
  706. }
  707. os << "#----------------------------------------------------------------\n"
  708. << "\n";
  709. this->GenerateImportVersionCode(os);
  710. }
  711. //----------------------------------------------------------------------------
  712. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  713. {
  714. os << "# Commands beyond this point should not need to know the version.\n"
  715. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  716. }
  717. //----------------------------------------------------------------------------
  718. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  719. {
  720. // Store an import file format version. This will let us change the
  721. // format later while still allowing old import files to work.
  722. os << "# Commands may need to know the format version.\n"
  723. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  724. << "\n";
  725. }
  726. //----------------------------------------------------------------------------
  727. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  728. const std::string &expectedTargets)
  729. {
  730. os << "# Protect against multiple inclusion, which would fail when already "
  731. "imported targets are added once more.\n"
  732. "set(_targetsDefined)\n"
  733. "set(_targetsNotDefined)\n"
  734. "set(_expectedTargets)\n"
  735. "foreach(_expectedTarget " << expectedTargets << ")\n"
  736. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  737. " if(NOT TARGET ${_expectedTarget})\n"
  738. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  739. " endif()\n"
  740. " if(TARGET ${_expectedTarget})\n"
  741. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  742. " endif()\n"
  743. "endforeach()\n"
  744. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  745. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  746. " cmake_policy(POP)\n"
  747. " return()\n"
  748. "endif()\n"
  749. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  750. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  751. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  752. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  753. "endif()\n"
  754. "unset(_targetsDefined)\n"
  755. "unset(_targetsNotDefined)\n"
  756. "unset(_expectedTargets)\n"
  757. "\n\n";
  758. }
  759. //----------------------------------------------------------------------------
  760. void
  761. cmExportFileGenerator
  762. ::GenerateImportTargetCode(std::ostream& os, cmTarget* target)
  763. {
  764. // Construct the imported target name.
  765. std::string targetName = this->Namespace;
  766. targetName += target->GetExportName();
  767. // Create the imported target.
  768. os << "# Create imported target " << targetName << "\n";
  769. switch(target->GetType())
  770. {
  771. case cmTarget::EXECUTABLE:
  772. os << "add_executable(" << targetName << " IMPORTED)\n";
  773. break;
  774. case cmTarget::STATIC_LIBRARY:
  775. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  776. break;
  777. case cmTarget::SHARED_LIBRARY:
  778. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  779. break;
  780. case cmTarget::MODULE_LIBRARY:
  781. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  782. break;
  783. case cmTarget::UNKNOWN_LIBRARY:
  784. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  785. break;
  786. default: // should never happen
  787. break;
  788. }
  789. // Mark the imported executable if it has exports.
  790. if(target->IsExecutableWithExports())
  791. {
  792. os << "set_property(TARGET " << targetName
  793. << " PROPERTY ENABLE_EXPORTS 1)\n";
  794. }
  795. // Mark the imported library if it is a framework.
  796. if(target->IsFrameworkOnApple())
  797. {
  798. os << "set_property(TARGET " << targetName
  799. << " PROPERTY FRAMEWORK 1)\n";
  800. }
  801. // Mark the imported executable if it is an application bundle.
  802. if(target->IsAppBundleOnApple())
  803. {
  804. os << "set_property(TARGET " << targetName
  805. << " PROPERTY MACOSX_BUNDLE 1)\n";
  806. }
  807. if (target->IsCFBundleOnApple())
  808. {
  809. os << "set_property(TARGET " << targetName
  810. << " PROPERTY BUNDLE 1)\n";
  811. }
  812. os << "\n";
  813. }
  814. //----------------------------------------------------------------------------
  815. void
  816. cmExportFileGenerator
  817. ::GenerateImportPropertyCode(std::ostream& os, const char* config,
  818. cmTarget* target,
  819. ImportPropertyMap const& properties)
  820. {
  821. // Construct the imported target name.
  822. std::string targetName = this->Namespace;
  823. targetName += target->GetExportName();
  824. // Set the import properties.
  825. os << "# Import target \"" << targetName << "\" for configuration \""
  826. << config << "\"\n";
  827. os << "set_property(TARGET " << targetName
  828. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  829. if(config && *config)
  830. {
  831. os << cmSystemTools::UpperCase(config);
  832. }
  833. else
  834. {
  835. os << "NOCONFIG";
  836. }
  837. os << ")\n";
  838. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  839. for(ImportPropertyMap::const_iterator pi = properties.begin();
  840. pi != properties.end(); ++pi)
  841. {
  842. os << " " << pi->first << " \"" << pi->second << "\"\n";
  843. }
  844. os << " )\n"
  845. << "\n";
  846. }
  847. //----------------------------------------------------------------------------
  848. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  849. const std::vector<std::string>& missingTargets)
  850. {
  851. if (missingTargets.empty())
  852. {
  853. os << "# This file does not depend on other imported targets which have\n"
  854. "# been exported from the same project but in a separate "
  855. "export set.\n\n";
  856. return;
  857. }
  858. os << "# Make sure the targets which have been exported in some other \n"
  859. "# export set exist.\n"
  860. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  861. "foreach(_target ";
  862. std::set<std::string> emitted;
  863. for(unsigned int i=0; i<missingTargets.size(); ++i)
  864. {
  865. if (emitted.insert(missingTargets[i]).second)
  866. {
  867. os << "\"" << missingTargets[i] << "\" ";
  868. }
  869. }
  870. os << ")\n"
  871. " if(NOT TARGET \"${_target}\" )\n"
  872. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  873. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  874. "\n"
  875. " endif()\n"
  876. "endforeach()\n"
  877. "\n"
  878. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  879. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  880. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  881. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  882. "\"The following imported targets are "
  883. "referenced, but are missing: "
  884. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  885. " else()\n"
  886. " message(FATAL_ERROR \"The following imported targets are "
  887. "referenced, but are missing: "
  888. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  889. " endif()\n"
  890. "endif()\n"
  891. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  892. "\n";
  893. }
  894. //----------------------------------------------------------------------------
  895. void
  896. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  897. {
  898. // Add code which verifies at cmake time that the file which is being
  899. // imported actually exists on disk. This should in theory always be theory
  900. // case, but still when packages are split into normal and development
  901. // packages this might get broken (e.g. the Config.cmake could be part of
  902. // the non-development package, something similar happened to me without
  903. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  904. // but the development package was not installed.).
  905. os << "# Loop over all imported files and verify that they actually exist\n"
  906. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  907. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  908. " if(NOT EXISTS \"${file}\" )\n"
  909. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  910. " references the file\n"
  911. " \\\"${file}\\\"\n"
  912. "but this file does not exist. Possible reasons include:\n"
  913. "* The file was deleted, renamed, or moved to another location.\n"
  914. "* An install or uninstall procedure did not complete successfully.\n"
  915. "* The installation package was faulty and contained\n"
  916. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  917. "but not all the files it references.\n"
  918. "\")\n"
  919. " endif()\n"
  920. " endforeach()\n"
  921. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  922. "endforeach()\n"
  923. "unset(_IMPORT_CHECK_TARGETS)\n"
  924. "\n";
  925. }
  926. //----------------------------------------------------------------------------
  927. void
  928. cmExportFileGenerator
  929. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  930. ImportPropertyMap const& properties,
  931. const std::set<std::string>& importedLocations)
  932. {
  933. // Construct the imported target name.
  934. std::string targetName = this->Namespace;
  935. targetName += target->GetExportName();
  936. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  937. "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  938. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  939. li != importedLocations.end();
  940. ++li)
  941. {
  942. ImportPropertyMap::const_iterator pi = properties.find(*li);
  943. if (pi != properties.end())
  944. {
  945. os << "\"" << pi->second << "\" ";
  946. }
  947. }
  948. os << ")\n\n";
  949. }