cmExportFileGenerator.cxx 41 KB

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