cmExportFileGenerator.cxx 43 KB

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