cmExportFileGenerator.cxx 41 KB

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