cmExportFileGenerator.cxx 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. static std::string const& asString(std::string const& l)
  744. {
  745. return l;
  746. }
  747. static std::string const& asString(cmLinkItem const& l)
  748. {
  749. return l.AsStr();
  750. }
  751. template <typename T>
  752. void cmExportFileGenerator::SetImportLinkProperty(
  753. std::string const& suffix, cmGeneratorTarget* target,
  754. const std::string& propName, std::vector<T> const& entries,
  755. ImportPropertyMap& properties, std::vector<std::string>& missingTargets)
  756. {
  757. // Skip the property if there are no entries.
  758. if (entries.empty()) {
  759. return;
  760. }
  761. // Construct the property value.
  762. std::string link_entries;
  763. const char* sep = "";
  764. for (T const& l : entries) {
  765. // Separate this from the previous entry.
  766. link_entries += sep;
  767. sep = ";";
  768. std::string temp = asString(l);
  769. this->AddTargetNamespace(temp, target, missingTargets);
  770. link_entries += temp;
  771. }
  772. // Store the property.
  773. std::string prop = propName;
  774. prop += suffix;
  775. properties[prop] = link_entries;
  776. }
  777. void cmExportFileGenerator::GeneratePolicyHeaderCode(std::ostream& os)
  778. {
  779. // Protect that file against use with older CMake versions.
  780. /* clang-format off */
  781. os << "# Generated by CMake\n\n";
  782. os << "if(\"${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}\" LESS 2.5)\n"
  783. << " message(FATAL_ERROR \"CMake >= 2.6.0 required\")\n"
  784. << "endif()\n";
  785. /* clang-format on */
  786. // Isolate the file policy level.
  787. // We use 2.6 here instead of the current version because newer
  788. // versions of CMake should be able to export files imported by 2.6
  789. // until the import format changes.
  790. /* clang-format off */
  791. os << "cmake_policy(PUSH)\n"
  792. << "cmake_policy(VERSION 2.6)\n";
  793. /* clang-format on */
  794. }
  795. void cmExportFileGenerator::GeneratePolicyFooterCode(std::ostream& os)
  796. {
  797. os << "cmake_policy(POP)\n";
  798. }
  799. void cmExportFileGenerator::GenerateImportHeaderCode(std::ostream& os,
  800. const std::string& config)
  801. {
  802. os << "#----------------------------------------------------------------\n"
  803. << "# Generated CMake target import file";
  804. if (!config.empty()) {
  805. os << " for configuration \"" << config << "\".\n";
  806. } else {
  807. os << ".\n";
  808. }
  809. os << "#----------------------------------------------------------------\n"
  810. << "\n";
  811. this->GenerateImportVersionCode(os);
  812. }
  813. void cmExportFileGenerator::GenerateImportFooterCode(std::ostream& os)
  814. {
  815. os << "# Commands beyond this point should not need to know the version.\n"
  816. << "set(CMAKE_IMPORT_FILE_VERSION)\n";
  817. }
  818. void cmExportFileGenerator::GenerateImportVersionCode(std::ostream& os)
  819. {
  820. // Store an import file format version. This will let us change the
  821. // format later while still allowing old import files to work.
  822. /* clang-format off */
  823. os << "# Commands may need to know the format version.\n"
  824. << "set(CMAKE_IMPORT_FILE_VERSION 1)\n"
  825. << "\n";
  826. /* clang-format on */
  827. }
  828. void cmExportFileGenerator::GenerateExpectedTargetsCode(
  829. std::ostream& os, const std::string& expectedTargets)
  830. {
  831. /* clang-format off */
  832. os << "# Protect against multiple inclusion, which would fail when already "
  833. "imported targets are added once more.\n"
  834. "set(_targetsDefined)\n"
  835. "set(_targetsNotDefined)\n"
  836. "set(_expectedTargets)\n"
  837. "foreach(_expectedTarget " << expectedTargets << ")\n"
  838. " list(APPEND _expectedTargets ${_expectedTarget})\n"
  839. " if(NOT TARGET ${_expectedTarget})\n"
  840. " list(APPEND _targetsNotDefined ${_expectedTarget})\n"
  841. " endif()\n"
  842. " if(TARGET ${_expectedTarget})\n"
  843. " list(APPEND _targetsDefined ${_expectedTarget})\n"
  844. " endif()\n"
  845. "endforeach()\n"
  846. "if(\"${_targetsDefined}\" STREQUAL \"${_expectedTargets}\")\n"
  847. " unset(_targetsDefined)\n"
  848. " unset(_targetsNotDefined)\n"
  849. " unset(_expectedTargets)\n"
  850. " set(CMAKE_IMPORT_FILE_VERSION)\n"
  851. " cmake_policy(POP)\n"
  852. " return()\n"
  853. "endif()\n"
  854. "if(NOT \"${_targetsDefined}\" STREQUAL \"\")\n"
  855. " message(FATAL_ERROR \"Some (but not all) targets in this export "
  856. "set were already defined.\\nTargets Defined: ${_targetsDefined}\\n"
  857. "Targets not yet defined: ${_targetsNotDefined}\\n\")\n"
  858. "endif()\n"
  859. "unset(_targetsDefined)\n"
  860. "unset(_targetsNotDefined)\n"
  861. "unset(_expectedTargets)\n"
  862. "\n\n";
  863. /* clang-format on */
  864. }
  865. void cmExportFileGenerator::GenerateImportTargetCode(
  866. std::ostream& os, cmGeneratorTarget const* target,
  867. cmStateEnums::TargetType targetType)
  868. {
  869. // Construct the imported target name.
  870. std::string targetName = this->Namespace;
  871. targetName += target->GetExportName();
  872. // Create the imported target.
  873. os << "# Create imported target " << targetName << "\n";
  874. switch (targetType) {
  875. case cmStateEnums::EXECUTABLE:
  876. os << "add_executable(" << targetName << " IMPORTED)\n";
  877. break;
  878. case cmStateEnums::STATIC_LIBRARY:
  879. os << "add_library(" << targetName << " STATIC IMPORTED)\n";
  880. break;
  881. case cmStateEnums::SHARED_LIBRARY:
  882. os << "add_library(" << targetName << " SHARED IMPORTED)\n";
  883. break;
  884. case cmStateEnums::MODULE_LIBRARY:
  885. os << "add_library(" << targetName << " MODULE IMPORTED)\n";
  886. break;
  887. case cmStateEnums::UNKNOWN_LIBRARY:
  888. os << "add_library(" << targetName << " UNKNOWN IMPORTED)\n";
  889. break;
  890. case cmStateEnums::OBJECT_LIBRARY:
  891. os << "add_library(" << targetName << " OBJECT IMPORTED)\n";
  892. break;
  893. case cmStateEnums::INTERFACE_LIBRARY:
  894. os << "add_library(" << targetName << " INTERFACE IMPORTED)\n";
  895. break;
  896. default: // should never happen
  897. break;
  898. }
  899. // Mark the imported executable if it has exports.
  900. if (target->IsExecutableWithExports()) {
  901. os << "set_property(TARGET " << targetName
  902. << " PROPERTY ENABLE_EXPORTS 1)\n";
  903. }
  904. // Mark the imported library if it is a framework.
  905. if (target->IsFrameworkOnApple()) {
  906. os << "set_property(TARGET " << targetName << " PROPERTY FRAMEWORK 1)\n";
  907. }
  908. // Mark the imported executable if it is an application bundle.
  909. if (target->IsAppBundleOnApple()) {
  910. os << "set_property(TARGET " << targetName
  911. << " PROPERTY MACOSX_BUNDLE 1)\n";
  912. }
  913. if (target->IsCFBundleOnApple()) {
  914. os << "set_property(TARGET " << targetName << " PROPERTY BUNDLE 1)\n";
  915. }
  916. os << "\n";
  917. }
  918. void cmExportFileGenerator::GenerateImportPropertyCode(
  919. std::ostream& os, const std::string& config, cmGeneratorTarget const* target,
  920. ImportPropertyMap const& properties)
  921. {
  922. // Construct the imported target name.
  923. std::string targetName = this->Namespace;
  924. targetName += target->GetExportName();
  925. // Set the import properties.
  926. os << "# Import target \"" << targetName << "\" for configuration \""
  927. << config << "\"\n";
  928. os << "set_property(TARGET " << targetName
  929. << " APPEND PROPERTY IMPORTED_CONFIGURATIONS ";
  930. if (!config.empty()) {
  931. os << cmSystemTools::UpperCase(config);
  932. } else {
  933. os << "NOCONFIG";
  934. }
  935. os << ")\n";
  936. os << "set_target_properties(" << targetName << " PROPERTIES\n";
  937. for (auto const& property : properties) {
  938. os << " " << property.first << " "
  939. << cmExportFileGeneratorEscape(property.second) << "\n";
  940. }
  941. os << " )\n"
  942. << "\n";
  943. }
  944. void cmExportFileGenerator::GenerateMissingTargetsCheckCode(
  945. std::ostream& os, const std::vector<std::string>& missingTargets)
  946. {
  947. if (missingTargets.empty()) {
  948. /* clang-format off */
  949. os << "# This file does not depend on other imported targets which have\n"
  950. "# been exported from the same project but in a separate "
  951. "export set.\n\n";
  952. /* clang-format on */
  953. return;
  954. }
  955. /* clang-format off */
  956. os << "# Make sure the targets which have been exported in some other \n"
  957. "# export set exist.\n"
  958. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  959. "foreach(_target ";
  960. /* clang-format on */
  961. std::set<std::string> emitted;
  962. for (std::string const& missingTarget : missingTargets) {
  963. if (emitted.insert(missingTarget).second) {
  964. os << "\"" << missingTarget << "\" ";
  965. }
  966. }
  967. /* clang-format off */
  968. os << ")\n"
  969. " if(NOT TARGET \"${_target}\" )\n"
  970. " set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets \""
  971. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets} ${_target}\")"
  972. "\n"
  973. " endif()\n"
  974. "endforeach()\n"
  975. "\n"
  976. "if(DEFINED ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  977. " if(CMAKE_FIND_PACKAGE_NAME)\n"
  978. " set( ${CMAKE_FIND_PACKAGE_NAME}_FOUND FALSE)\n"
  979. " set( ${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE "
  980. "\"The following imported targets are "
  981. "referenced, but are missing: "
  982. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  983. " else()\n"
  984. " message(FATAL_ERROR \"The following imported targets are "
  985. "referenced, but are missing: "
  986. "${${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets}\")\n"
  987. " endif()\n"
  988. "endif()\n"
  989. "unset(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE_targets)\n"
  990. "\n";
  991. /* clang-format on */
  992. }
  993. void cmExportFileGenerator::GenerateImportedFileCheckLoop(std::ostream& os)
  994. {
  995. // Add code which verifies at cmake time that the file which is being
  996. // imported actually exists on disk. This should in theory always be theory
  997. // case, but still when packages are split into normal and development
  998. // packages this might get broken (e.g. the Config.cmake could be part of
  999. // the non-development package, something similar happened to me without
  1000. // on SUSE with a mysql pkg-config file, which claimed everything is fine,
  1001. // but the development package was not installed.).
  1002. /* clang-format off */
  1003. os << "# Loop over all imported files and verify that they actually exist\n"
  1004. "foreach(target ${_IMPORT_CHECK_TARGETS} )\n"
  1005. " foreach(file ${_IMPORT_CHECK_FILES_FOR_${target}} )\n"
  1006. " if(NOT EXISTS \"${file}\" )\n"
  1007. " message(FATAL_ERROR \"The imported target \\\"${target}\\\""
  1008. " references the file\n"
  1009. " \\\"${file}\\\"\n"
  1010. "but this file does not exist. Possible reasons include:\n"
  1011. "* The file was deleted, renamed, or moved to another location.\n"
  1012. "* An install or uninstall procedure did not complete successfully.\n"
  1013. "* The installation package was faulty and contained\n"
  1014. " \\\"${CMAKE_CURRENT_LIST_FILE}\\\"\n"
  1015. "but not all the files it references.\n"
  1016. "\")\n"
  1017. " endif()\n"
  1018. " endforeach()\n"
  1019. " unset(_IMPORT_CHECK_FILES_FOR_${target})\n"
  1020. "endforeach()\n"
  1021. "unset(_IMPORT_CHECK_TARGETS)\n"
  1022. "\n";
  1023. /* clang-format on */
  1024. }
  1025. void cmExportFileGenerator::GenerateImportedFileChecksCode(
  1026. std::ostream& os, cmGeneratorTarget* target,
  1027. ImportPropertyMap const& properties,
  1028. const std::set<std::string>& importedLocations)
  1029. {
  1030. // Construct the imported target name.
  1031. std::string targetName = this->Namespace;
  1032. targetName += target->GetExportName();
  1033. os << "list(APPEND _IMPORT_CHECK_TARGETS " << targetName
  1034. << " )\n"
  1035. "list(APPEND _IMPORT_CHECK_FILES_FOR_"
  1036. << targetName << " ";
  1037. for (std::string const& li : importedLocations) {
  1038. ImportPropertyMap::const_iterator pi = properties.find(li);
  1039. if (pi != properties.end()) {
  1040. os << cmExportFileGeneratorEscape(pi->second) << " ";
  1041. }
  1042. }
  1043. os << ")\n\n";
  1044. }
  1045. bool cmExportFileGenerator::PopulateExportProperties(
  1046. cmGeneratorTarget* gte, ImportPropertyMap& properties,
  1047. std::string& errorMessage)
  1048. {
  1049. auto& targetProperties = gte->Target->GetProperties();
  1050. const auto& exportProperties = targetProperties.find("EXPORT_PROPERTIES");
  1051. if (exportProperties != targetProperties.end()) {
  1052. std::vector<std::string> propsToExport;
  1053. cmSystemTools::ExpandListArgument(exportProperties->second.GetValue(),
  1054. propsToExport);
  1055. for (auto& prop : propsToExport) {
  1056. /* Black list reserved properties */
  1057. if (cmSystemTools::StringStartsWith(prop, "IMPORTED_") ||
  1058. cmSystemTools::StringStartsWith(prop, "INTERFACE_")) {
  1059. std::ostringstream e;
  1060. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1061. << prop << "\" in EXPORT_PROPERTIES but IMPORTED_* and INTERFACE_* "
  1062. << "properties are reserved.";
  1063. errorMessage = e.str();
  1064. return false;
  1065. }
  1066. auto propertyValue = targetProperties.GetPropertyValue(prop);
  1067. if (propertyValue == nullptr) {
  1068. // Asked to export a property that isn't defined on the target. Do not
  1069. // consider this an error, there's just nothing to export.
  1070. continue;
  1071. }
  1072. std::string evaluatedValue = cmGeneratorExpression::Preprocess(
  1073. propertyValue, cmGeneratorExpression::StripAllGeneratorExpressions);
  1074. if (evaluatedValue != propertyValue) {
  1075. std::ostringstream e;
  1076. e << "Target \"" << gte->Target->GetName() << "\" contains property \""
  1077. << prop << "\" in EXPORT_PROPERTIES but this property contains a "
  1078. << "generator expression. This is not allowed.";
  1079. errorMessage = e.str();
  1080. return false;
  1081. }
  1082. properties[prop] = propertyValue;
  1083. }
  1084. }
  1085. return true;
  1086. }