cmExportFileGenerator.cxx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. std::ostringstream 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. std::ostringstream 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. bool inBinary = isSubDirectory(li->c_str(), topBinaryDir);
  253. bool inSource = isSubDirectory(li->c_str(), topSourceDir);
  254. if (isSubDirectory(li->c_str(), installDir))
  255. {
  256. // The include directory is inside the install tree. If the
  257. // install tree is not inside the source tree or build tree then
  258. // fall through to the checks below that the include directory is not
  259. // also inside the source tree or build tree.
  260. bool shouldContinue =
  261. (!inBinary || isSubDirectory(installDir, topBinaryDir)) &&
  262. (!inSource || isSubDirectory(installDir, topSourceDir));
  263. if (!shouldContinue)
  264. {
  265. switch(target->GetPolicyStatusCMP0052())
  266. {
  267. case cmPolicies::WARN:
  268. {
  269. std::ostringstream s;
  270. s << target->GetMakefile()->GetPolicies()
  271. ->GetPolicyWarning(cmPolicies::CMP0052) << "\n";
  272. s << "Directory:\n \"" << *li << "\"\nin "
  273. "INTERFACE_INCLUDE_DIRECTORIES of target \""
  274. << target->GetName() << "\" is a subdirectory of the install "
  275. "directory:\n \"" << installDir << "\"\nhowever it is also "
  276. "a subdirectory of the " << (inBinary ? "build" : "source")
  277. << " tree:\n \"" << (inBinary ? topBinaryDir : topSourceDir)
  278. << "\"" << std::endl;
  279. target->GetMakefile()->IssueMessage(cmake::AUTHOR_WARNING,
  280. s.str());
  281. }
  282. case cmPolicies::OLD:
  283. shouldContinue = true;
  284. break;
  285. case cmPolicies::REQUIRED_ALWAYS:
  286. case cmPolicies::REQUIRED_IF_USED:
  287. case cmPolicies::NEW:
  288. break;
  289. }
  290. }
  291. if (shouldContinue)
  292. {
  293. continue;
  294. }
  295. }
  296. if (inBinary)
  297. {
  298. e << "Target \"" << target->GetName() << "\" "
  299. "INTERFACE_INCLUDE_DIRECTORIES property contains path:\n"
  300. " \"" << *li << "\"\nwhich is prefixed in the build directory.";
  301. target->GetMakefile()->IssueMessage(messageType, e.str());
  302. }
  303. if (!inSourceBuild)
  304. {
  305. if (inSource)
  306. {
  307. e << "Target \"" << target->GetName() << "\" "
  308. "INTERFACE_INCLUDE_DIRECTORIES property contains path:\n"
  309. " \"" << *li << "\"\nwhich is prefixed in the source directory.";
  310. target->GetMakefile()->IssueMessage(messageType, e.str());
  311. }
  312. }
  313. }
  314. return !hadFatalError;
  315. }
  316. //----------------------------------------------------------------------------
  317. static void prefixItems(std::string &exportDirs)
  318. {
  319. std::vector<std::string> entries;
  320. cmGeneratorExpression::Split(exportDirs, entries);
  321. exportDirs = "";
  322. const char *sep = "";
  323. for(std::vector<std::string>::const_iterator ei = entries.begin();
  324. ei != entries.end(); ++ei)
  325. {
  326. exportDirs += sep;
  327. sep = ";";
  328. if (!cmSystemTools::FileIsFullPath(ei->c_str())
  329. && ei->find("${_IMPORT_PREFIX}") == std::string::npos)
  330. {
  331. exportDirs += "${_IMPORT_PREFIX}/";
  332. }
  333. exportDirs += *ei;
  334. }
  335. }
  336. //----------------------------------------------------------------------------
  337. void cmExportFileGenerator::PopulateIncludeDirectoriesInterface(
  338. cmTargetExport *tei,
  339. cmGeneratorExpression::PreprocessContext preprocessRule,
  340. ImportPropertyMap &properties,
  341. std::vector<std::string> &missingTargets)
  342. {
  343. cmTarget *target = tei->Target;
  344. assert(preprocessRule == cmGeneratorExpression::InstallInterface);
  345. const char *propName = "INTERFACE_INCLUDE_DIRECTORIES";
  346. const char *input = target->GetProperty(propName);
  347. cmGeneratorExpression ge;
  348. std::string dirs = cmGeneratorExpression::Preprocess(
  349. tei->InterfaceIncludeDirectories,
  350. preprocessRule,
  351. true);
  352. this->ReplaceInstallPrefix(dirs);
  353. cmsys::auto_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(dirs);
  354. std::string exportDirs = cge->Evaluate(target->GetMakefile(), "",
  355. false, target);
  356. if (cge->GetHadContextSensitiveCondition())
  357. {
  358. cmMakefile* mf = target->GetMakefile();
  359. std::ostringstream e;
  360. e << "Target \"" << target->GetName() << "\" is installed with "
  361. "INCLUDES DESTINATION set to a context sensitive path. Paths which "
  362. "depend on the configuration, policy values or the link interface are "
  363. "not supported. Consider using target_include_directories instead.";
  364. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  365. return;
  366. }
  367. if (!input && exportDirs.empty())
  368. {
  369. return;
  370. }
  371. if ((input && !*input) && exportDirs.empty())
  372. {
  373. // Set to empty
  374. properties[propName] = "";
  375. return;
  376. }
  377. prefixItems(exportDirs);
  378. std::string includes = (input?input:"");
  379. const char* sep = input ? ";" : "";
  380. includes += sep + exportDirs;
  381. std::string prepro = cmGeneratorExpression::Preprocess(includes,
  382. preprocessRule,
  383. true);
  384. if (!prepro.empty())
  385. {
  386. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  387. missingTargets);
  388. if (!checkInterfaceDirs(prepro, target))
  389. {
  390. return;
  391. }
  392. properties[propName] = prepro;
  393. }
  394. }
  395. //----------------------------------------------------------------------------
  396. void cmExportFileGenerator::PopulateInterfaceProperty(
  397. const std::string& propName,
  398. cmTarget *target,
  399. cmGeneratorExpression::PreprocessContext preprocessRule,
  400. ImportPropertyMap &properties,
  401. std::vector<std::string> &missingTargets)
  402. {
  403. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  404. properties, missingTargets);
  405. }
  406. //----------------------------------------------------------------------------
  407. void getPropertyContents(cmTarget const* tgt, const std::string& prop,
  408. std::set<std::string> &ifaceProperties)
  409. {
  410. const char *p = tgt->GetProperty(prop);
  411. if (!p)
  412. {
  413. return;
  414. }
  415. std::vector<std::string> content;
  416. cmSystemTools::ExpandListArgument(p, content);
  417. ifaceProperties.insert(content.begin(), content.end());
  418. }
  419. //----------------------------------------------------------------------------
  420. void getCompatibleInterfaceProperties(cmTarget *target,
  421. std::set<std::string> &ifaceProperties,
  422. const std::string& config)
  423. {
  424. cmComputeLinkInformation *info = target->GetLinkInformation(config);
  425. if (!info)
  426. {
  427. cmMakefile* mf = target->GetMakefile();
  428. std::ostringstream e;
  429. e << "Exporting the target \"" << target->GetName() << "\" is not "
  430. "allowed since its linker language cannot be determined";
  431. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  432. return;
  433. }
  434. const cmComputeLinkInformation::ItemVector &deps = info->GetItems();
  435. for(cmComputeLinkInformation::ItemVector::const_iterator li =
  436. deps.begin();
  437. li != deps.end(); ++li)
  438. {
  439. if (!li->Target)
  440. {
  441. continue;
  442. }
  443. getPropertyContents(li->Target,
  444. "COMPATIBLE_INTERFACE_BOOL",
  445. ifaceProperties);
  446. getPropertyContents(li->Target,
  447. "COMPATIBLE_INTERFACE_STRING",
  448. ifaceProperties);
  449. getPropertyContents(li->Target,
  450. "COMPATIBLE_INTERFACE_NUMBER_MIN",
  451. ifaceProperties);
  452. getPropertyContents(li->Target,
  453. "COMPATIBLE_INTERFACE_NUMBER_MAX",
  454. ifaceProperties);
  455. }
  456. }
  457. //----------------------------------------------------------------------------
  458. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  459. cmTarget *target,
  460. ImportPropertyMap &properties)
  461. {
  462. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL",
  463. target, properties);
  464. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING",
  465. target, properties);
  466. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MIN",
  467. target, properties);
  468. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MAX",
  469. target, properties);
  470. std::set<std::string> ifaceProperties;
  471. getPropertyContents(target, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  472. getPropertyContents(target, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  473. getPropertyContents(target, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  474. ifaceProperties);
  475. getPropertyContents(target, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  476. ifaceProperties);
  477. if (target->GetType() != cmTarget::INTERFACE_LIBRARY)
  478. {
  479. getCompatibleInterfaceProperties(target, ifaceProperties, "");
  480. std::vector<std::string> configNames;
  481. target->GetMakefile()->GetConfigurations(configNames);
  482. for (std::vector<std::string>::const_iterator ci = configNames.begin();
  483. ci != configNames.end(); ++ci)
  484. {
  485. getCompatibleInterfaceProperties(target, ifaceProperties, *ci);
  486. }
  487. }
  488. for (std::set<std::string>::const_iterator it = ifaceProperties.begin();
  489. it != ifaceProperties.end(); ++it)
  490. {
  491. this->PopulateInterfaceProperty("INTERFACE_" + *it,
  492. target, properties);
  493. }
  494. }
  495. //----------------------------------------------------------------------------
  496. void cmExportFileGenerator::GenerateInterfaceProperties(cmTarget const* target,
  497. std::ostream& os,
  498. const ImportPropertyMap &properties)
  499. {
  500. if (!properties.empty())
  501. {
  502. std::string targetName = this->Namespace;
  503. targetName += target->GetExportName();
  504. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  505. for(ImportPropertyMap::const_iterator pi = properties.begin();
  506. pi != properties.end(); ++pi)
  507. {
  508. os << " " << pi->first << " \"" << pi->second << "\"\n";
  509. }
  510. os << ")\n\n";
  511. }
  512. }
  513. //----------------------------------------------------------------------------
  514. bool
  515. cmExportFileGenerator::AddTargetNamespace(std::string &input,
  516. cmTarget* target,
  517. std::vector<std::string> &missingTargets)
  518. {
  519. cmMakefile *mf = target->GetMakefile();
  520. cmTarget *tgt = mf->FindTargetToUse(input);
  521. if (!tgt)
  522. {
  523. return false;
  524. }
  525. if(tgt->IsImported())
  526. {
  527. return true;
  528. }
  529. if(this->ExportedTargets.find(tgt) != this->ExportedTargets.end())
  530. {
  531. input = this->Namespace + tgt->GetExportName();
  532. }
  533. else
  534. {
  535. std::string namespacedTarget;
  536. this->HandleMissingTarget(namespacedTarget, missingTargets,
  537. mf, target, tgt);
  538. if (!namespacedTarget.empty())
  539. {
  540. input = namespacedTarget;
  541. }
  542. }
  543. return true;
  544. }
  545. //----------------------------------------------------------------------------
  546. void
  547. cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  548. std::string &input,
  549. cmTarget* target,
  550. std::vector<std::string> &missingTargets,
  551. FreeTargetsReplace replace)
  552. {
  553. if (replace == NoReplaceFreeTargets)
  554. {
  555. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  556. return;
  557. }
  558. std::vector<std::string> parts;
  559. cmGeneratorExpression::Split(input, parts);
  560. std::string sep;
  561. input = "";
  562. for(std::vector<std::string>::iterator li = parts.begin();
  563. li != parts.end(); ++li)
  564. {
  565. if (cmGeneratorExpression::Find(*li) == std::string::npos)
  566. {
  567. this->AddTargetNamespace(*li, target, missingTargets);
  568. }
  569. else
  570. {
  571. this->ResolveTargetsInGeneratorExpression(
  572. *li,
  573. target,
  574. missingTargets);
  575. }
  576. input += sep + *li;
  577. sep = ";";
  578. }
  579. }
  580. //----------------------------------------------------------------------------
  581. void
  582. cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  583. std::string &input,
  584. cmTarget* target,
  585. std::vector<std::string> &missingTargets)
  586. {
  587. std::string::size_type pos = 0;
  588. std::string::size_type lastPos = pos;
  589. cmMakefile *mf = target->GetMakefile();
  590. while((pos = input.find("$<TARGET_PROPERTY:", lastPos)) != input.npos)
  591. {
  592. std::string::size_type nameStartPos = pos +
  593. sizeof("$<TARGET_PROPERTY:") - 1;
  594. std::string::size_type closePos = input.find(">", nameStartPos);
  595. std::string::size_type commaPos = input.find(",", nameStartPos);
  596. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  597. if (commaPos == input.npos // Implied 'this' target
  598. || closePos == input.npos // Imcomplete expression.
  599. || closePos < commaPos // Implied 'this' target
  600. || nextOpenPos < commaPos) // Non-literal
  601. {
  602. lastPos = nameStartPos;
  603. continue;
  604. }
  605. std::string targetName = input.substr(nameStartPos,
  606. commaPos - nameStartPos);
  607. if (this->AddTargetNamespace(targetName, target, missingTargets))
  608. {
  609. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  610. }
  611. lastPos = nameStartPos + targetName.size() + 1;
  612. }
  613. std::string errorString;
  614. pos = 0;
  615. lastPos = pos;
  616. while((pos = input.find("$<TARGET_NAME:", lastPos)) != input.npos)
  617. {
  618. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  619. std::string::size_type endPos = input.find(">", nameStartPos);
  620. if (endPos == input.npos)
  621. {
  622. errorString = "$<TARGET_NAME:...> expression incomplete";
  623. break;
  624. }
  625. std::string targetName = input.substr(nameStartPos,
  626. endPos - nameStartPos);
  627. if(targetName.find("$<") != input.npos)
  628. {
  629. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  630. "literal.";
  631. break;
  632. }
  633. if (!this->AddTargetNamespace(targetName, target, missingTargets))
  634. {
  635. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  636. "reachable target.";
  637. break;
  638. }
  639. input.replace(pos, endPos - pos + 1, targetName);
  640. lastPos = endPos;
  641. }
  642. this->ReplaceInstallPrefix(input);
  643. if (!errorString.empty())
  644. {
  645. mf->IssueMessage(cmake::FATAL_ERROR, errorString);
  646. }
  647. }
  648. //----------------------------------------------------------------------------
  649. void
  650. cmExportFileGenerator::ReplaceInstallPrefix(std::string &)
  651. {
  652. // Do nothing
  653. }
  654. //----------------------------------------------------------------------------
  655. void
  656. cmExportFileGenerator
  657. ::SetImportLinkInterface(const std::string& config, std::string const& suffix,
  658. cmGeneratorExpression::PreprocessContext preprocessRule,
  659. cmTarget* target, ImportPropertyMap& properties,
  660. std::vector<std::string>& missingTargets)
  661. {
  662. // Add the transitive link dependencies for this configuration.
  663. cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  664. target);
  665. if (!iface)
  666. {
  667. return;
  668. }
  669. if (iface->ImplementationIsInterface)
  670. {
  671. // Policy CMP0022 must not be NEW.
  672. this->SetImportLinkProperty(suffix, target,
  673. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  674. iface->Libraries, properties, missingTargets);
  675. return;
  676. }
  677. const char *propContent;
  678. if (const char *prop_suffixed = target->GetProperty(
  679. "LINK_INTERFACE_LIBRARIES" + suffix))
  680. {
  681. propContent = prop_suffixed;
  682. }
  683. else if (const char *prop = target->GetProperty(
  684. "LINK_INTERFACE_LIBRARIES"))
  685. {
  686. propContent = prop;
  687. }
  688. else
  689. {
  690. return;
  691. }
  692. const bool newCMP0022Behavior =
  693. target->GetPolicyStatusCMP0022() != cmPolicies::WARN
  694. && target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  695. if(newCMP0022Behavior && !this->ExportOld)
  696. {
  697. cmMakefile *mf = target->GetMakefile();
  698. std::ostringstream e;
  699. e << "Target \"" << target->GetName() << "\" has policy CMP0022 enabled, "
  700. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  701. "populated, but it was exported without the "
  702. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  703. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  704. return;
  705. }
  706. if (!*propContent)
  707. {
  708. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = "";
  709. return;
  710. }
  711. std::string prepro = cmGeneratorExpression::Preprocess(propContent,
  712. preprocessRule);
  713. if (!prepro.empty())
  714. {
  715. this->ResolveTargetsInGeneratorExpressions(prepro, target,
  716. missingTargets,
  717. ReplaceFreeTargets);
  718. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  719. }
  720. }
  721. //----------------------------------------------------------------------------
  722. void
  723. cmExportFileGenerator
  724. ::SetImportDetailProperties(const std::string& config,
  725. std::string const& suffix,
  726. cmTarget* target, ImportPropertyMap& properties,
  727. std::vector<std::string>& missingTargets
  728. )
  729. {
  730. // Get the makefile in which to lookup target information.
  731. cmMakefile* mf = target->GetMakefile();
  732. // Add the soname for unix shared libraries.
  733. if(target->GetType() == cmTarget::SHARED_LIBRARY ||
  734. target->GetType() == cmTarget::MODULE_LIBRARY)
  735. {
  736. // Check whether this is a DLL platform.
  737. bool dll_platform =
  738. (mf->IsOn("WIN32") || mf->IsOn("CYGWIN") || mf->IsOn("MINGW"));
  739. if(!dll_platform)
  740. {
  741. std::string prop;
  742. std::string value;
  743. if(target->HasSOName(config))
  744. {
  745. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  746. {
  747. value = this->InstallNameDir(target, config);
  748. }
  749. prop = "IMPORTED_SONAME";
  750. value += target->GetSOName(config);
  751. }
  752. else
  753. {
  754. prop = "IMPORTED_NO_SONAME";
  755. value = "TRUE";
  756. }
  757. prop += suffix;
  758. properties[prop] = value;
  759. }
  760. }
  761. // Add the transitive link dependencies for this configuration.
  762. if(cmTarget::LinkInterface const* iface = target->GetLinkInterface(config,
  763. target))
  764. {
  765. this->SetImportLinkProperty(suffix, target,
  766. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  767. iface->Languages, properties, missingTargets);
  768. std::vector<std::string> dummy;
  769. this->SetImportLinkProperty(suffix, target,
  770. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  771. iface->SharedDeps, properties, dummy);
  772. if(iface->Multiplicity > 0)
  773. {
  774. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  775. prop += suffix;
  776. std::ostringstream m;
  777. m << iface->Multiplicity;
  778. properties[prop] = m.str();
  779. }
  780. }
  781. }
  782. //----------------------------------------------------------------------------
  783. template <typename T>
  784. void
  785. cmExportFileGenerator
  786. ::SetImportLinkProperty(std::string const& suffix,
  787. cmTarget* target,
  788. const std::string& propName,
  789. std::vector<T> const& entries,
  790. ImportPropertyMap& properties,
  791. std::vector<std::string>& missingTargets
  792. )
  793. {
  794. // Skip the property if there are no entries.
  795. if(entries.empty())
  796. {
  797. return;
  798. }
  799. // Construct the property value.
  800. std::string link_entries;
  801. const char* sep = "";
  802. for(typename std::vector<T>::const_iterator li = entries.begin();
  803. li != entries.end(); ++li)
  804. {
  805. // Separate this from the previous entry.
  806. link_entries += sep;
  807. sep = ";";
  808. std::string temp = *li;
  809. this->AddTargetNamespace(temp, target, missingTargets);
  810. link_entries += temp;
  811. }
  812. // Store the property.
  813. std::string prop = propName;
  814. prop += suffix;
  815. properties[prop] = link_entries;
  816. }
  817. //----------------------------------------------------------------------------
  818. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  819. const std::string& config)
  820. {
  821. os << "#----------------------------------------------------------------\n"
  822. << "# Generated CMake target import file";
  823. if(!config.empty())
  824. {
  825. os << " for configuration \"" << config << "\".\n";
  826. }
  827. else
  828. {
  829. os << ".\n";
  830. }
  831. os << "#----------------------------------------------------------------\n"
  832. << "\n";
  833. this->GenerateImportVersionCode(os);
  834. }
  835. //----------------------------------------------------------------------------
  836. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  837. {
  838. os << "# Commands beyond this point should not need to know the version.\n"
  839. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  840. }
  841. //----------------------------------------------------------------------------
  842. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  843. {
  844. // Store an import file format version. This will let us change the
  845. // format later while still allowing old import files to work.
  846. os << "# Commands may need to know the format version.\n"
  847. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  848. << "\n";
  849. }
  850. //----------------------------------------------------------------------------
  851. void cmExportFileGenerator::GenerateExpectedTargetsCode(std::ostream& os,
  852. const std::string &expectedTargets)
  853. {
  854. os << "# Protect against multiple inclusion, which would fail when already "
  855. "imported targets are added once more.\n"
  856. "set(_targetsDefined)\n"
  857. "set(_targetsNotDefined)\n"
  858. "set(_expectedTargets)\n"
  859. "foreach(_expectedTarget " << expectedTargets << ")\n"
  860. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  861. " if(NOT TARGET ${_expectedTarget})\n"
  862. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  863. " endif()\n"
  864. " if(TARGET ${_expectedTarget})\n"
  865. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  866. " endif()\n"
  867. "endforeach()\n"
  868. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  869. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  870. " cmake_policy(POP)\n"
  871. " return()\n"
  872. "endif()\n"
  873. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  874. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  875. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  876. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  877. "endif()\n"
  878. "unset(_targetsDefined)\n"
  879. "unset(_targetsNotDefined)\n"
  880. "unset(_expectedTargets)\n"
  881. "\n\n";
  882. }
  883. //----------------------------------------------------------------------------
  884. void
  885. cmExportFileGenerator
  886. ::GenerateImportTargetCode(std::ostream& os, cmTarget const* target)
  887. {
  888. // Construct the imported target name.
  889. std::string targetName = this->Namespace;
  890. targetName += target->GetExportName();
  891. // Create the imported target.
  892. os << "# Create imported target " << targetName << "\n";
  893. switch(target->GetType())
  894. {
  895. case cmTarget::EXECUTABLE:
  896. os << "add_executable(" << targetName << " IMPORTED)\n";
  897. break;
  898. case cmTarget::STATIC_LIBRARY:
  899. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  900. break;
  901. case cmTarget::SHARED_LIBRARY:
  902. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  903. break;
  904. case cmTarget::MODULE_LIBRARY:
  905. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  906. break;
  907. case cmTarget::UNKNOWN_LIBRARY:
  908. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  909. break;
  910. case cmTarget::INTERFACE_LIBRARY:
  911. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  912. break;
  913. default: // should never happen
  914. break;
  915. }
  916. // Mark the imported executable if it has exports.
  917. if(target->IsExecutableWithExports())
  918. {
  919. os << "set_property(TARGET " << targetName
  920. << " PROPERTY ENABLE_EXPORTS 1)\n";
  921. }
  922. // Mark the imported library if it is a framework.
  923. if(target->IsFrameworkOnApple())
  924. {
  925. os << "set_property(TARGET " << targetName
  926. << " PROPERTY FRAMEWORK 1)\n";
  927. }
  928. // Mark the imported executable if it is an application bundle.
  929. if(target->IsAppBundleOnApple())
  930. {
  931. os << "set_property(TARGET " << targetName
  932. << " PROPERTY MACOSX_BUNDLE 1)\n";
  933. }
  934. if (target->IsCFBundleOnApple())
  935. {
  936. os << "set_property(TARGET " << targetName
  937. << " PROPERTY BUNDLE 1)\n";
  938. }
  939. os << "\n";
  940. }
  941. //----------------------------------------------------------------------------
  942. void
  943. cmExportFileGenerator
  944. ::GenerateImportPropertyCode(std::ostream& os, const std::string& config,
  945. cmTarget const* target,
  946. ImportPropertyMap const& properties)
  947. {
  948. // Construct the imported target name.
  949. std::string targetName = this->Namespace;
  950. targetName += target->GetExportName();
  951. // Set the import properties.
  952. os << "# Import target \"" << targetName << "\" for configuration \""
  953. << config << "\"\n";
  954. os << "set_property(TARGET " << targetName
  955. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  956. if(!config.empty())
  957. {
  958. os << cmSystemTools::UpperCase(config);
  959. }
  960. else
  961. {
  962. os << "NOCONFIG";
  963. }
  964. os << ")\n";
  965. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  966. for(ImportPropertyMap::const_iterator pi = properties.begin();
  967. pi != properties.end(); ++pi)
  968. {
  969. os << " " << pi->first << " \"" << pi->second << "\"\n";
  970. }
  971. os << " )\n"
  972. << "\n";
  973. }
  974. //----------------------------------------------------------------------------
  975. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(std::ostream& os,
  976. const std::vector<std::string>& missingTargets)
  977. {
  978. if (missingTargets.empty())
  979. {
  980. os << "# This file does not depend on other imported targets which have\n"
  981. "# been exported from the same project but in a separate "
  982. "export set.\n\n";
  983. return;
  984. }
  985. os << "# Make sure the targets which have been exported in some other \n"
  986. "# export set exist.\n"
  987. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  988. "foreach(_target ";
  989. std::set<std::string> emitted;
  990. for(unsigned int i=0; i<missingTargets.size(); ++i)
  991. {
  992. if (emitted.insert(missingTargets[i]).second)
  993. {
  994. os << "\"" << missingTargets[i] << "\" ";
  995. }
  996. }
  997. os << ")\n"
  998. " if(NOT TARGET \"${_target}\" )\n"
  999. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  1000. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  1001. "\n"
  1002. " endif()\n"
  1003. "endforeach()\n"
  1004. "\n"
  1005. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1006. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  1007. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  1008. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  1009. "\"The following imported targets are "
  1010. "referenced, but are missing: "
  1011. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1012. " else()\n"
  1013. " message(FATAL_ERROR \"The following imported targets are "
  1014. "referenced, but are missing: "
  1015. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  1016. " endif()\n"
  1017. "endif()\n"
  1018. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  1019. "\n";
  1020. }
  1021. //----------------------------------------------------------------------------
  1022. void
  1023. cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  1024. {
  1025. // Add code which verifies at cmake time that the file which is being
  1026. // imported actually exists on disk. This should in theory always be theory
  1027. // case, but still when packages are split into normal and development
  1028. // packages this might get broken (e.g. the Config.cmake could be part of
  1029. // the non-development package, something similar happened to me without
  1030. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  1031. // but the development package was not installed.).
  1032. os << "# Loop over all imported files and verify that they actually exist\n"
  1033. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  1034. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  1035. " if(NOT EXISTS \"${file}\" )\n"
  1036. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1037. " references the file\n"
  1038. " \\\"${file}\\\"\n"
  1039. "but this file does not exist. Possible reasons include:\n"
  1040. "* The file was deleted, renamed, or moved to another location.\n"
  1041. "* An install or uninstall procedure did not complete successfully.\n"
  1042. "* The installation package was faulty and contained\n"
  1043. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1044. "but not all the files it references.\n"
  1045. "\")\n"
  1046. " endif()\n"
  1047. " endforeach()\n"
  1048. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1049. "endforeach()\n"
  1050. "unset(_IMPORT_CHECK_TARGETS)\n"
  1051. "\n";
  1052. }
  1053. //----------------------------------------------------------------------------
  1054. void
  1055. cmExportFileGenerator
  1056. ::GenerateImportedFileChecksCode(std::ostream& os, cmTarget* target,
  1057. ImportPropertyMap const& properties,
  1058. const std::set<std::string>& importedLocations)
  1059. {
  1060. // Construct the imported target name.
  1061. std::string targetName = this->Namespace;
  1062. targetName += target->GetExportName();
  1063. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName << " )\n"
  1064. "list(APPEND _IMPORT_CHECK_FILES_FOR_" << targetName << " ";
  1065. for(std::set<std::string>::const_iterator li = importedLocations.begin();
  1066. li != importedLocations.end();
  1067. ++li)
  1068. {
  1069. ImportPropertyMap::const_iterator pi = properties.find(*li);
  1070. if (pi != properties.end())
  1071. {
  1072. os << "\"" << pi->second << "\" ";
  1073. }
  1074. }
  1075. os << ")\n\n";
  1076. }