cmExportFileGenerator.cxx 37 KB

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