cmExportFileGenerator.cxx 41 KB

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