cmExportFileGenerator.cxx 38 KB

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