cmExportFileGenerator.cxx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  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 "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmGeneratedFileStream.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmLinkItem.h"
  9. #include "cmLocalGenerator.h"
  10. #include "cmMakefile.h"
  11. #include "cmOutputConverter.h"
  12. #include "cmPolicies.h"
  13. #include "cmProperty.h"
  14. #include "cmPropertyMap.h"
  15. #include "cmStateTypes.h"
  16. #include "cmSystemTools.h"
  17. #include "cmTarget.h"
  18. #include "cmTargetExport.h"
  19. #include "cmake.h"
  20. #include "cmsys/FStream.hxx"
  21. #include <assert.h>
  22. #include <memory> // IWYU pragma: keep
  23. #include <sstream>
  24. #include <string.h>
  25. #include <utility>
  26. static std::string cmExportFileGeneratorEscape(std::string const& str)
  27. {
  28. // Escape a property value for writing into a .cmake file.
  29. std::string result = cmOutputConverter::EscapeForCMake(str);
  30. // Un-escape variable references generated by our own export code.
  31. cmSystemTools::ReplaceString(result, "\\${_IMPORT_PREFIX}",
  32. "${_IMPORT_PREFIX}");
  33. cmSystemTools::ReplaceString(result, "\\${CMAKE_IMPORT_LIBRARY_SUFFIX}",
  34. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  35. return result;
  36. }
  37. cmExportFileGenerator::cmExportFileGenerator()
  38. {
  39. this->AppendMode = false;
  40. this->ExportOld = false;
  41. }
  42. void cmExportFileGenerator::AddConfiguration(const std::string& config)
  43. {
  44. this->Configurations.push_back(config);
  45. }
  46. void cmExportFileGenerator::SetExportFile(const char* mainFile)
  47. {
  48. this->MainImportFile = mainFile;
  49. this->FileDir = cmSystemTools::GetFilenamePath(this->MainImportFile);
  50. this->FileBase =
  51. cmSystemTools::GetFilenameWithoutLastExtension(this->MainImportFile);
  52. this->FileExt =
  53. cmSystemTools::GetFilenameLastExtension(this->MainImportFile);
  54. }
  55. const char* cmExportFileGenerator::GetMainExportFileName() const
  56. {
  57. return this->MainImportFile.c_str();
  58. }
  59. bool cmExportFileGenerator::GenerateImportFile()
  60. {
  61. // Open the output file to generate it.
  62. std::unique_ptr<cmsys::ofstream> foutPtr;
  63. if (this->AppendMode) {
  64. // Open for append.
  65. foutPtr = cm::make_unique<cmsys::ofstream>(this->MainImportFile.c_str(),
  66. std::ios::app);
  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.get() || !*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().c_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. const char* 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. cmake::MessageType messageType = cmake::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 = cmake::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(cmake::AUTHOR_WARNING,
  253. 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(cmake::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::PopulateInterfaceProperty(
  406. const std::string& propName, cmGeneratorTarget* target,
  407. cmGeneratorExpression::PreprocessContext preprocessRule,
  408. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  409. {
  410. this->PopulateInterfaceProperty(propName, propName, target, preprocessRule,
  411. properties, missingTargets);
  412. }
  413. void getPropertyContents(cmGeneratorTarget const* tgt, const std::string& prop,
  414. std::set<std::string>& ifaceProperties)
  415. {
  416. const char* p = tgt->GetProperty(prop);
  417. if (!p) {
  418. return;
  419. }
  420. std::vector<std::string> content;
  421. cmSystemTools::ExpandListArgument(p, content);
  422. ifaceProperties.insert(content.begin(), content.end());
  423. }
  424. void getCompatibleInterfaceProperties(cmGeneratorTarget* target,
  425. std::set<std::string>& ifaceProperties,
  426. const std::string& config)
  427. {
  428. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  429. // object libraries have no link information, so nothing to compute
  430. return;
  431. }
  432. cmComputeLinkInformation* info = target->GetLinkInformation(config);
  433. if (!info) {
  434. cmLocalGenerator* lg = target->GetLocalGenerator();
  435. std::ostringstream e;
  436. e << "Exporting the target \"" << target->GetName()
  437. << "\" is not "
  438. "allowed since its linker language cannot be determined";
  439. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  440. return;
  441. }
  442. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  443. for (auto const& dep : deps) {
  444. if (!dep.Target) {
  445. continue;
  446. }
  447. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_BOOL",
  448. ifaceProperties);
  449. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_STRING",
  450. ifaceProperties);
  451. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  452. ifaceProperties);
  453. getPropertyContents(dep.Target, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  454. ifaceProperties);
  455. }
  456. }
  457. void cmExportFileGenerator::PopulateCompatibleInterfaceProperties(
  458. cmGeneratorTarget* gtarget, ImportPropertyMap& properties)
  459. {
  460. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_BOOL", gtarget,
  461. properties);
  462. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_STRING", gtarget,
  463. properties);
  464. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MIN", gtarget,
  465. properties);
  466. this->PopulateInterfaceProperty("COMPATIBLE_INTERFACE_NUMBER_MAX", gtarget,
  467. properties);
  468. std::set<std::string> ifaceProperties;
  469. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_BOOL", ifaceProperties);
  470. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_STRING", ifaceProperties);
  471. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MIN",
  472. ifaceProperties);
  473. getPropertyContents(gtarget, "COMPATIBLE_INTERFACE_NUMBER_MAX",
  474. ifaceProperties);
  475. if (gtarget->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  476. getCompatibleInterfaceProperties(gtarget, ifaceProperties, "");
  477. std::vector<std::string> configNames;
  478. gtarget->Target->GetMakefile()->GetConfigurations(configNames);
  479. for (std::string const& cn : configNames) {
  480. getCompatibleInterfaceProperties(gtarget, ifaceProperties, cn);
  481. }
  482. }
  483. for (std::string const& ip : ifaceProperties) {
  484. this->PopulateInterfaceProperty("INTERFACE_" + ip, gtarget, properties);
  485. }
  486. }
  487. void cmExportFileGenerator::GenerateInterfaceProperties(
  488. const cmGeneratorTarget* target, std::ostream& os,
  489. const ImportPropertyMap& properties)
  490. {
  491. if (!properties.empty()) {
  492. std::string targetName = this->Namespace;
  493. targetName += target->GetExportName();
  494. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  495. for (auto const& property : properties) {
  496. os << " " << property.first << " "
  497. << cmExportFileGeneratorEscape(property.second) << "\n";
  498. }
  499. os << ")\n\n";
  500. }
  501. }
  502. bool cmExportFileGenerator::AddTargetNamespace(
  503. std::string& input, cmGeneratorTarget* target,
  504. std::vector<std::string>& missingTargets)
  505. {
  506. cmLocalGenerator* lg = target->GetLocalGenerator();
  507. cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(input);
  508. if (!tgt) {
  509. return false;
  510. }
  511. if (tgt->IsImported()) {
  512. return true;
  513. }
  514. if (this->ExportedTargets.find(tgt) != this->ExportedTargets.end()) {
  515. input = this->Namespace + tgt->GetExportName();
  516. } else {
  517. std::string namespacedTarget;
  518. this->HandleMissingTarget(namespacedTarget, missingTargets, target, tgt);
  519. if (!namespacedTarget.empty()) {
  520. input = namespacedTarget;
  521. }
  522. }
  523. return true;
  524. }
  525. void cmExportFileGenerator::ResolveTargetsInGeneratorExpressions(
  526. std::string& input, cmGeneratorTarget* target,
  527. std::vector<std::string>& missingTargets, FreeTargetsReplace replace)
  528. {
  529. if (replace == NoReplaceFreeTargets) {
  530. this->ResolveTargetsInGeneratorExpression(input, target, missingTargets);
  531. return;
  532. }
  533. std::vector<std::string> parts;
  534. cmGeneratorExpression::Split(input, parts);
  535. std::string sep;
  536. input.clear();
  537. for (std::string& li : parts) {
  538. if (cmGeneratorExpression::Find(li) == std::string::npos) {
  539. this->AddTargetNamespace(li, target, missingTargets);
  540. } else {
  541. this->ResolveTargetsInGeneratorExpression(li, target, missingTargets);
  542. }
  543. input += sep + li;
  544. sep = ";";
  545. }
  546. }
  547. void cmExportFileGenerator::ResolveTargetsInGeneratorExpression(
  548. std::string& input, cmGeneratorTarget* target,
  549. std::vector<std::string>& missingTargets)
  550. {
  551. std::string::size_type pos = 0;
  552. std::string::size_type lastPos = pos;
  553. while ((pos = input.find("$<TARGET_PROPERTY:", lastPos)) !=
  554. std::string::npos) {
  555. std::string::size_type nameStartPos =
  556. pos + sizeof("$<TARGET_PROPERTY:") - 1;
  557. std::string::size_type closePos = input.find('>', nameStartPos);
  558. std::string::size_type commaPos = input.find(',', nameStartPos);
  559. std::string::size_type nextOpenPos = input.find("$<", nameStartPos);
  560. if (commaPos == std::string::npos // Implied 'this' target
  561. || closePos == std::string::npos // Incomplete expression.
  562. || closePos < commaPos // Implied 'this' target
  563. || nextOpenPos < commaPos) // Non-literal
  564. {
  565. lastPos = nameStartPos;
  566. continue;
  567. }
  568. std::string targetName =
  569. input.substr(nameStartPos, commaPos - nameStartPos);
  570. if (this->AddTargetNamespace(targetName, target, missingTargets)) {
  571. input.replace(nameStartPos, commaPos - nameStartPos, targetName);
  572. }
  573. lastPos = nameStartPos + targetName.size() + 1;
  574. }
  575. std::string errorString;
  576. pos = 0;
  577. lastPos = pos;
  578. while ((pos = input.find("$<TARGET_NAME:", lastPos)) != std::string::npos) {
  579. std::string::size_type nameStartPos = pos + sizeof("$<TARGET_NAME:") - 1;
  580. std::string::size_type endPos = input.find('>', nameStartPos);
  581. if (endPos == std::string::npos) {
  582. errorString = "$<TARGET_NAME:...> expression incomplete";
  583. break;
  584. }
  585. std::string targetName = input.substr(nameStartPos, endPos - nameStartPos);
  586. if (targetName.find("$<") != std::string::npos) {
  587. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  588. "literal.";
  589. break;
  590. }
  591. if (!this->AddTargetNamespace(targetName, target, missingTargets)) {
  592. errorString = "$<TARGET_NAME:...> requires its parameter to be a "
  593. "reachable target.";
  594. break;
  595. }
  596. input.replace(pos, endPos - pos + 1, targetName);
  597. lastPos = endPos;
  598. }
  599. pos = 0;
  600. lastPos = pos;
  601. while (errorString.empty() &&
  602. (pos = input.find("$<LINK_ONLY:", lastPos)) != std::string::npos) {
  603. std::string::size_type nameStartPos = pos + sizeof("$<LINK_ONLY:") - 1;
  604. std::string::size_type endPos = input.find('>', nameStartPos);
  605. if (endPos == std::string::npos) {
  606. errorString = "$<LINK_ONLY:...> expression incomplete";
  607. break;
  608. }
  609. std::string libName = input.substr(nameStartPos, endPos - nameStartPos);
  610. if (cmGeneratorExpression::IsValidTargetName(libName) &&
  611. this->AddTargetNamespace(libName, target, missingTargets)) {
  612. input.replace(nameStartPos, endPos - nameStartPos, libName);
  613. }
  614. lastPos = nameStartPos + libName.size() + 1;
  615. }
  616. this->ReplaceInstallPrefix(input);
  617. if (!errorString.empty()) {
  618. target->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, errorString);
  619. }
  620. }
  621. void cmExportFileGenerator::ReplaceInstallPrefix(std::string& /*unused*/)
  622. {
  623. // Do nothing
  624. }
  625. void cmExportFileGenerator::SetImportLinkInterface(
  626. const std::string& config, std::string const& suffix,
  627. cmGeneratorExpression::PreprocessContext preprocessRule,
  628. cmGeneratorTarget* target, ImportPropertyMap& properties,
  629. std::vector<std::string>& missingTargets)
  630. {
  631. // Add the transitive link dependencies for this configuration.
  632. cmLinkInterface const* iface = target->GetLinkInterface(config, target);
  633. if (!iface) {
  634. return;
  635. }
  636. if (iface->ImplementationIsInterface) {
  637. // Policy CMP0022 must not be NEW.
  638. this->SetImportLinkProperty(suffix, target,
  639. "IMPORTED_LINK_INTERFACE_LIBRARIES",
  640. iface->Libraries, properties, missingTargets);
  641. return;
  642. }
  643. const char* propContent;
  644. if (const char* prop_suffixed =
  645. target->GetProperty("LINK_INTERFACE_LIBRARIES" + suffix)) {
  646. propContent = prop_suffixed;
  647. } else if (const char* prop =
  648. target->GetProperty("LINK_INTERFACE_LIBRARIES")) {
  649. propContent = prop;
  650. } else {
  651. return;
  652. }
  653. const bool newCMP0022Behavior =
  654. target->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  655. target->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  656. if (newCMP0022Behavior && !this->ExportOld) {
  657. cmLocalGenerator* lg = target->GetLocalGenerator();
  658. std::ostringstream e;
  659. e << "Target \"" << target->GetName()
  660. << "\" has policy CMP0022 enabled, "
  661. "but also has old-style LINK_INTERFACE_LIBRARIES properties "
  662. "populated, but it was exported without the "
  663. "EXPORT_LINK_INTERFACE_LIBRARIES to export the old-style properties";
  664. lg->IssueMessage(cmake::FATAL_ERROR, e.str());
  665. return;
  666. }
  667. if (!*propContent) {
  668. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix].clear();
  669. return;
  670. }
  671. std::string prepro =
  672. cmGeneratorExpression::Preprocess(propContent, preprocessRule);
  673. if (!prepro.empty()) {
  674. this->ResolveTargetsInGeneratorExpressions(prepro, target, missingTargets,
  675. ReplaceFreeTargets);
  676. properties["IMPORTED_LINK_INTERFACE_LIBRARIES" + suffix] = prepro;
  677. }
  678. }
  679. void cmExportFileGenerator::SetImportDetailProperties(
  680. const std::string& config, std::string const& suffix,
  681. cmGeneratorTarget* target, ImportPropertyMap& properties,
  682. std::vector<std::string>& missingTargets)
  683. {
  684. // Get the makefile in which to lookup target information.
  685. cmMakefile* mf = target->Makefile;
  686. // Add the soname for unix shared libraries.
  687. if (target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  688. target->GetType() == cmStateEnums::MODULE_LIBRARY) {
  689. if (!target->IsDLLPlatform()) {
  690. std::string prop;
  691. std::string value;
  692. if (target->HasSOName(config)) {
  693. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  694. value = this->InstallNameDir(target, config);
  695. }
  696. prop = "IMPORTED_SONAME";
  697. value += target->GetSOName(config);
  698. } else {
  699. prop = "IMPORTED_NO_SONAME";
  700. value = "TRUE";
  701. }
  702. prop += suffix;
  703. properties[prop] = value;
  704. }
  705. }
  706. // Add the transitive link dependencies for this configuration.
  707. if (cmLinkInterface const* iface =
  708. target->GetLinkInterface(config, target)) {
  709. this->SetImportLinkProperty(suffix, target,
  710. "IMPORTED_LINK_INTERFACE_LANGUAGES",
  711. iface->Languages, properties, missingTargets);
  712. std::vector<std::string> dummy;
  713. this->SetImportLinkProperty(suffix, target,
  714. "IMPORTED_LINK_DEPENDENT_LIBRARIES",
  715. iface->SharedDeps, properties, dummy);
  716. if (iface->Multiplicity > 0) {
  717. std::string prop = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  718. prop += suffix;
  719. std::ostringstream m;
  720. m << iface->Multiplicity;
  721. properties[prop] = m.str();
  722. }
  723. }
  724. // Add information if this target is a managed target
  725. if (target->GetManagedType(config) !=
  726. cmGeneratorTarget::ManagedType::Native) {
  727. std::string prop = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  728. prop += suffix;
  729. std::string propval;
  730. if (auto* p = target->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  731. propval = p;
  732. } else if (target->HasLanguage("CSharp", config)) {
  733. // C# projects do not have the /clr flag, so we set the property
  734. // here to mark the target as (only) managed (i.e. no .lib file
  735. // to link to). Otherwise the COMMON_LANGUAGE_RUNTIME target
  736. // property would have to be set manually for C# targets to make
  737. // exporting/importing work.
  738. propval = "CSharp";
  739. }
  740. properties[prop] = propval;
  741. }
  742. }
  743. template <typename T>
  744. void cmExportFileGenerator::SetImportLinkProperty(
  745. std::string const& suffix, cmGeneratorTarget* target,
  746. const std::string& propName, std::vector<T> const& entries,
  747. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  748. {
  749. // Skip the property if there are no entries.
  750. if (entries.empty()) {
  751. return;
  752. }
  753. // Construct the property value.
  754. std::string link_entries;
  755. const char* sep = "";
  756. for (T const& l : entries) {
  757. // Separate this from the previous entry.
  758. link_entries += sep;
  759. sep = ";";
  760. std::string temp = l;
  761. this->AddTargetNamespace(temp, target, missingTargets);
  762. link_entries += temp;
  763. }
  764. // Store the property.
  765. std::string prop = propName;
  766. prop += suffix;
  767. properties[prop] = link_entries;
  768. }
  769. void cmExportFileGenerator::GeneratePolicyHeaderCode(std::ostream& os)
  770. {
  771. // Protect that file against use with older CMake versions.
  772. /* clang-format off */
  773. os << "# Generated by CMake\n\n";
  774. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
  775. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  776. << "endif()\n";
  777. /* clang-format on */
  778. // Isolate the file policy level.
  779. // We use 2.6 here instead of the current version because newer
  780. // versions of CMake should be able to export files imported by 2.6
  781. // until the import format changes.
  782. /* clang-format off */
  783. os << "cmake_policy(PUSH)\n"
  784. << "cmake_policy(VERSION 2.6)\n";
  785. /* clang-format on */
  786. }
  787. void cmExportFileGenerator::GeneratePolicyFooterCode(std::ostream& os)
  788. {
  789. os << "cmake_policy(POP)\n";
  790. }
  791. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  792. const std::string& config)
  793. {
  794. os << "#----------------------------------------------------------------\n"
  795. << "# Generated CMake target import file";
  796. if (!config.empty()) {
  797. os << " for configuration \"" << config << "\".\n";
  798. } else {
  799. os << ".\n";
  800. }
  801. os << "#----------------------------------------------------------------\n"
  802. << "\n";
  803. this->GenerateImportVersionCode(os);
  804. }
  805. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  806. {
  807. os << "# Commands beyond this point should not need to know the version.\n"
  808. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  809. }
  810. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  811. {
  812. // Store an import file format version. This will let us change the
  813. // format later while still allowing old import files to work.
  814. /* clang-format off */
  815. os << "# Commands may need to know the format version.\n"
  816. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  817. << "\n";
  818. /* clang-format on */
  819. }
  820. void cmExportFileGenerator::GenerateExpectedTargetsCode(
  821. std::ostream& os, const std::string& expectedTargets)
  822. {
  823. /* clang-format off */
  824. os << "# Protect against multiple inclusion, which would fail when already "
  825. "imported targets are added once more.\n"
  826. "set(_targetsDefined)\n"
  827. "set(_targetsNotDefined)\n"
  828. "set(_expectedTargets)\n"
  829. "foreach(_expectedTarget " << expectedTargets << ")\n"
  830. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  831. " if(NOT TARGET ${_expectedTarget})\n"
  832. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  833. " endif()\n"
  834. " if(TARGET ${_expectedTarget})\n"
  835. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  836. " endif()\n"
  837. "endforeach()\n"
  838. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  839. " unset(_targetsDefined)\n"
  840. " unset(_targetsNotDefined)\n"
  841. " unset(_expectedTargets)\n"
  842. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  843. " cmake_policy(POP)\n"
  844. " return()\n"
  845. "endif()\n"
  846. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  847. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  848. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  849. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  850. "endif()\n"
  851. "unset(_targetsDefined)\n"
  852. "unset(_targetsNotDefined)\n"
  853. "unset(_expectedTargets)\n"
  854. "\n\n";
  855. /* clang-format on */
  856. }
  857. void cmExportFileGenerator::GenerateImportTargetCode(
  858. std::ostream& os, cmGeneratorTarget const* target,
  859. cmStateEnums::TargetType targetType)
  860. {
  861. // Construct the imported target name.
  862. std::string targetName = this->Namespace;
  863. targetName += target->GetExportName();
  864. // Create the imported target.
  865. os << "# Create imported target " << targetName << "\n";
  866. switch (targetType) {
  867. case cmStateEnums::EXECUTABLE:
  868. os << "add_executable(" << targetName << " IMPORTED)\n";
  869. break;
  870. case cmStateEnums::STATIC_LIBRARY:
  871. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  872. break;
  873. case cmStateEnums::SHARED_LIBRARY:
  874. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  875. break;
  876. case cmStateEnums::MODULE_LIBRARY:
  877. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  878. break;
  879. case cmStateEnums::UNKNOWN_LIBRARY:
  880. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  881. break;
  882. case cmStateEnums::OBJECT_LIBRARY:
  883. os << "add_library(" << targetName << " OBJECT IMPORTED)\n";
  884. break;
  885. case cmStateEnums::INTERFACE_LIBRARY:
  886. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  887. break;
  888. default: // should never happen
  889. break;
  890. }
  891. // Mark the imported executable if it has exports.
  892. if (target->IsExecutableWithExports()) {
  893. os << "set_property(TARGET " << targetName
  894. << " PROPERTY ENABLE_EXPORTS 1)\n";
  895. }
  896. // Mark the imported library if it is a framework.
  897. if (target->IsFrameworkOnApple()) {
  898. os << "set_property(TARGET " << targetName << " PROPERTY FRAMEWORK 1)\n";
  899. }
  900. // Mark the imported executable if it is an application bundle.
  901. if (target->IsAppBundleOnApple()) {
  902. os << "set_property(TARGET " << targetName
  903. << " PROPERTY MACOSX_BUNDLE 1)\n";
  904. }
  905. if (target->IsCFBundleOnApple()) {
  906. os << "set_property(TARGET " << targetName << " PROPERTY BUNDLE 1)\n";
  907. }
  908. os << "\n";
  909. }
  910. void cmExportFileGenerator::GenerateImportPropertyCode(
  911. std::ostream& os, const std::string& config, cmGeneratorTarget const* target,
  912. ImportPropertyMap const& properties)
  913. {
  914. // Construct the imported target name.
  915. std::string targetName = this->Namespace;
  916. targetName += target->GetExportName();
  917. // Set the import properties.
  918. os << "# Import target \"" << targetName << "\" for configuration \""
  919. << config << "\"\n";
  920. os << "set_property(TARGET " << targetName
  921. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  922. if (!config.empty()) {
  923. os << cmSystemTools::UpperCase(config);
  924. } else {
  925. os << "NOCONFIG";
  926. }
  927. os << ")\n";
  928. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  929. for (auto const& property : properties) {
  930. os << " " << property.first << " "
  931. << cmExportFileGeneratorEscape(property.second) << "\n";
  932. }
  933. os << " )\n"
  934. << "\n";
  935. }
  936. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(
  937. std::ostream& os, const std::vector<std::string>& missingTargets)
  938. {
  939. if (missingTargets.empty()) {
  940. /* clang-format off */
  941. os << "# This file does not depend on other imported targets which have\n"
  942. "# been exported from the same project but in a separate "
  943. "export set.\n\n";
  944. /* clang-format on */
  945. return;
  946. }
  947. /* clang-format off */
  948. os << "# Make sure the targets which have been exported in some other \n"
  949. "# export set exist.\n"
  950. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  951. "foreach(_target ";
  952. /* clang-format on */
  953. std::set<std::string> emitted;
  954. for (std::string const& missingTarget : missingTargets) {
  955. if (emitted.insert(missingTarget).second) {
  956. os << "\"" << missingTarget << "\" ";
  957. }
  958. }
  959. /* clang-format off */
  960. os << ")\n"
  961. " if(NOT TARGET \"${_target}\" )\n"
  962. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  963. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  964. "\n"
  965. " endif()\n"
  966. "endforeach()\n"
  967. "\n"
  968. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  969. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  970. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  971. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  972. "\"The following imported targets are "
  973. "referenced, but are missing: "
  974. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  975. " else()\n"
  976. " message(FATAL_ERROR \"The following imported targets are "
  977. "referenced, but are missing: "
  978. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  979. " endif()\n"
  980. "endif()\n"
  981. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  982. "\n";
  983. /* clang-format on */
  984. }
  985. void cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  986. {
  987. // Add code which verifies at cmake time that the file which is being
  988. // imported actually exists on disk. This should in theory always be theory
  989. // case, but still when packages are split into normal and development
  990. // packages this might get broken (e.g. the Config.cmake could be part of
  991. // the non-development package, something similar happened to me without
  992. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  993. // but the development package was not installed.).
  994. /* clang-format off */
  995. os << "# Loop over all imported files and verify that they actually exist\n"
  996. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  997. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  998. " if(NOT EXISTS \"${file}\" )\n"
  999. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1000. " references the file\n"
  1001. " \\\"${file}\\\"\n"
  1002. "but this file does not exist. Possible reasons include:\n"
  1003. "* The file was deleted, renamed, or moved to another location.\n"
  1004. "* An install or uninstall procedure did not complete successfully.\n"
  1005. "* The installation package was faulty and contained\n"
  1006. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1007. "but not all the files it references.\n"
  1008. "\")\n"
  1009. " endif()\n"
  1010. " endforeach()\n"
  1011. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1012. "endforeach()\n"
  1013. "unset(_IMPORT_CHECK_TARGETS)\n"
  1014. "\n";
  1015. /* clang-format on */
  1016. }
  1017. void cmExportFileGenerator::GenerateImportedFileChecksCode(
  1018. std::ostream& os, cmGeneratorTarget* target,
  1019. ImportPropertyMap const& properties,
  1020. const std::set<std::string>& importedLocations)
  1021. {
  1022. // Construct the imported target name.
  1023. std::string targetName = this->Namespace;
  1024. targetName += target->GetExportName();
  1025. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName
  1026. << " )\n"
  1027. "list(APPEND _IMPORT_CHECK_FILES_FOR_"
  1028. << targetName << " ";
  1029. for (std::string const& li : importedLocations) {
  1030. ImportPropertyMap::const_iterator pi = properties.find(li);
  1031. if (pi != properties.end()) {
  1032. os << cmExportFileGeneratorEscape(pi->second) << " ";
  1033. }
  1034. }
  1035. os << ")\n\n";
  1036. }
  1037. bool cmExportFileGenerator::PopulateExportProperties(
  1038. cmGeneratorTarget* gte, ImportPropertyMap& properties,
  1039. std::string& errorMessage)
  1040. {
  1041. auto& targetProperties = gte->Target->GetProperties();
  1042. const auto& exportProperties = targetProperties.find("EXPORT_PROPERTIES");
  1043. if (exportProperties != targetProperties.end()) {
  1044. std::vector<std::string> propsToExport;
  1045. cmSystemTools::ExpandListArgument(exportProperties->second.GetValue(),
  1046. propsToExport);
  1047. for (auto& prop : propsToExport) {
  1048. /* Black list reserved properties */
  1049. if (cmSystemTools::StringStartsWith(prop, "IMPORTED_") ||
  1050. cmSystemTools::StringStartsWith(prop, "INTERFACE_")) {
  1051. std::ostringstream e;
  1052. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1053. << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
  1054. << "properties are reserved.";
  1055. errorMessage = e.str();
  1056. return false;
  1057. }
  1058. auto propertyValue = targetProperties.GetPropertyValue(prop);
  1059. std::string evaluatedValue = cmGeneratorExpression::Preprocess(
  1060. propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
  1061. if (evaluatedValue != propertyValue) {
  1062. std::ostringstream e;
  1063. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1064. << prop << "\" in EXPORT_PROPERTIES but this property contains a "
  1065. << "generator expression. This is not allowed.";
  1066. errorMessage = e.str();
  1067. return false;
  1068. }
  1069. properties[prop] = propertyValue;
  1070. }
  1071. }
  1072. return true;
  1073. }