cmExportBuildFileGenerator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "cmExportBuildFileGenerator.h"
  4. #include <algorithm>
  5. #include <map>
  6. #include <memory>
  7. #include <set>
  8. #include <sstream>
  9. #include <utility>
  10. #include <cm/string_view>
  11. #include <cmext/algorithm>
  12. #include <cmext/string_view>
  13. #include "cmExportSet.h"
  14. #include "cmFileSet.h"
  15. #include "cmGeneratorExpression.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmMakefile.h"
  20. #include "cmMessageType.h"
  21. #include "cmOutputConverter.h"
  22. #include "cmPolicies.h"
  23. #include "cmStateTypes.h"
  24. #include "cmStringAlgorithms.h"
  25. #include "cmTarget.h"
  26. #include "cmTargetExport.h"
  27. #include "cmValue.h"
  28. #include "cmake.h"
  29. class cmSourceFile;
  30. cmExportBuildFileGenerator::cmExportBuildFileGenerator()
  31. {
  32. this->LG = nullptr;
  33. this->ExportSet = nullptr;
  34. }
  35. void cmExportBuildFileGenerator::Compute(cmLocalGenerator* lg)
  36. {
  37. this->LG = lg;
  38. if (this->ExportSet) {
  39. this->ExportSet->Compute(lg);
  40. }
  41. }
  42. bool cmExportBuildFileGenerator::GenerateMainFile(std::ostream& os)
  43. {
  44. {
  45. std::string expectedTargets;
  46. std::string sep;
  47. std::vector<std::string> targets;
  48. bool generatedInterfaceRequired = false;
  49. this->GetTargets(targets);
  50. for (std::string const& tei : targets) {
  51. cmGeneratorTarget* te = this->LG->FindGeneratorTargetToUse(tei);
  52. expectedTargets += sep + this->Namespace + te->GetExportName();
  53. sep = " ";
  54. if (this->ExportedTargets.insert(te).second) {
  55. this->Exports.push_back(te);
  56. } else {
  57. std::ostringstream e;
  58. e << "given target \"" << te->GetName() << "\" more than once.";
  59. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  60. MessageType::FATAL_ERROR, e.str(),
  61. this->LG->GetMakefile()->GetBacktrace());
  62. return false;
  63. }
  64. generatedInterfaceRequired |=
  65. this->GetExportTargetType(te) == cmStateEnums::INTERFACE_LIBRARY;
  66. }
  67. if (generatedInterfaceRequired) {
  68. this->GenerateRequiredCMakeVersion(os, "3.0.0");
  69. }
  70. this->GenerateExpectedTargetsCode(os, expectedTargets);
  71. }
  72. // Create all the imported targets.
  73. for (cmGeneratorTarget* gte : this->Exports) {
  74. this->GenerateImportTargetCode(os, gte, this->GetExportTargetType(gte));
  75. gte->Target->AppendBuildInterfaceIncludes();
  76. ImportPropertyMap properties;
  77. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", gte,
  78. cmGeneratorExpression::BuildInterface,
  79. properties);
  80. this->PopulateInterfaceProperty("INTERFACE_SOURCES", gte,
  81. cmGeneratorExpression::BuildInterface,
  82. properties);
  83. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gte,
  84. cmGeneratorExpression::BuildInterface,
  85. properties);
  86. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gte,
  87. cmGeneratorExpression::BuildInterface,
  88. properties);
  89. this->PopulateInterfaceProperty("INTERFACE_PRECOMPILE_HEADERS", gte,
  90. cmGeneratorExpression::BuildInterface,
  91. properties);
  92. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gte,
  93. cmGeneratorExpression::BuildInterface,
  94. properties);
  95. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  96. cmGeneratorExpression::BuildInterface,
  97. properties);
  98. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gte,
  99. cmGeneratorExpression::BuildInterface,
  100. properties);
  101. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", gte,
  102. cmGeneratorExpression::BuildInterface,
  103. properties);
  104. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", gte,
  105. cmGeneratorExpression::BuildInterface,
  106. properties);
  107. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  108. properties);
  109. std::string errorMessage;
  110. if (!this->PopulateExportProperties(gte, properties, errorMessage)) {
  111. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  112. MessageType::FATAL_ERROR, errorMessage,
  113. this->LG->GetMakefile()->GetBacktrace());
  114. return false;
  115. }
  116. const bool newCMP0022Behavior =
  117. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  118. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  119. if (newCMP0022Behavior) {
  120. this->PopulateInterfaceLinkLibrariesProperty(
  121. gte, cmGeneratorExpression::BuildInterface, properties);
  122. }
  123. this->PopulateCompatibleInterfaceProperties(gte, properties);
  124. this->GenerateInterfaceProperties(gte, os, properties);
  125. this->GenerateTargetFileSets(gte, os);
  126. }
  127. // Generate import file content for each configuration.
  128. for (std::string const& c : this->Configurations) {
  129. this->GenerateImportConfig(os, c);
  130. }
  131. this->GenerateMissingTargetsCheckCode(os);
  132. return true;
  133. }
  134. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  135. std::ostream& os, const std::string& config, std::string const& suffix)
  136. {
  137. for (cmGeneratorTarget* target : this->Exports) {
  138. // Collect import properties for this target.
  139. ImportPropertyMap properties;
  140. if (this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY) {
  141. this->SetImportLocationProperty(config, suffix, target, properties);
  142. }
  143. if (!properties.empty()) {
  144. // Get the rest of the target details.
  145. if (this->GetExportTargetType(target) !=
  146. cmStateEnums::INTERFACE_LIBRARY) {
  147. this->SetImportDetailProperties(config, suffix, target, properties);
  148. this->SetImportLinkInterface(config, suffix,
  149. cmGeneratorExpression::BuildInterface,
  150. target, properties);
  151. }
  152. // TODO: PUBLIC_HEADER_LOCATION
  153. // This should wait until the build feature propagation stuff
  154. // is done. Then this can be a propagated include directory.
  155. // this->GenerateImportProperty(config, te->HeaderGenerator,
  156. // properties);
  157. // Generate code in the export file.
  158. this->GenerateImportPropertyCode(os, config, target, properties);
  159. }
  160. }
  161. }
  162. cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType(
  163. cmGeneratorTarget const* target) const
  164. {
  165. cmStateEnums::TargetType targetType = target->GetType();
  166. // An object library exports as an interface library if we cannot
  167. // tell clients where to find the objects. This is sufficient
  168. // to support transitive usage requirements on other targets that
  169. // use the object library.
  170. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  171. !target->Target->HasKnownObjectFileLocation(nullptr)) {
  172. targetType = cmStateEnums::INTERFACE_LIBRARY;
  173. }
  174. return targetType;
  175. }
  176. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  177. {
  178. this->ExportSet = exportSet;
  179. }
  180. void cmExportBuildFileGenerator::SetImportLocationProperty(
  181. const std::string& config, std::string const& suffix,
  182. cmGeneratorTarget* target, ImportPropertyMap& properties)
  183. {
  184. // Get the makefile in which to lookup target information.
  185. cmMakefile* mf = target->Makefile;
  186. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  187. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  188. // Compute all the object files inside this target and setup
  189. // IMPORTED_OBJECTS as a list of object files
  190. std::vector<cmSourceFile const*> objectSources;
  191. target->GetObjectSources(objectSources, config);
  192. std::string const obj_dir = target->GetObjectDirectory(config);
  193. std::vector<std::string> objects;
  194. for (cmSourceFile const* sf : objectSources) {
  195. const std::string& obj = target->GetObjectName(sf);
  196. objects.push_back(obj_dir + obj);
  197. }
  198. // Store the property.
  199. properties[prop] = cmJoin(objects, ";");
  200. } else {
  201. // Add the main target file.
  202. {
  203. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  204. std::string value;
  205. if (target->IsAppBundleOnApple()) {
  206. value =
  207. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  208. } else {
  209. value = target->GetFullPath(config,
  210. cmStateEnums::RuntimeBinaryArtifact, true);
  211. }
  212. properties[prop] = value;
  213. }
  214. // Add the import library for windows DLLs.
  215. if (target->HasImportLibrary(config)) {
  216. std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix);
  217. std::string value =
  218. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact);
  219. if (mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  220. target->GetImplibGNUtoMS(config, value, value,
  221. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  222. }
  223. properties[prop] = value;
  224. }
  225. }
  226. }
  227. void cmExportBuildFileGenerator::HandleMissingTarget(
  228. std::string& link_libs, cmGeneratorTarget const* depender,
  229. cmGeneratorTarget* dependee)
  230. {
  231. // The target is not in the export.
  232. if (!this->AppendMode) {
  233. const std::string name = dependee->GetName();
  234. cmGlobalGenerator* gg =
  235. dependee->GetLocalGenerator()->GetGlobalGenerator();
  236. auto exportInfo = this->FindBuildExportInfo(gg, name);
  237. std::vector<std::string> const& exportFiles = exportInfo.first;
  238. if (exportFiles.size() == 1) {
  239. std::string missingTarget = exportInfo.second;
  240. missingTarget += dependee->GetExportName();
  241. link_libs += missingTarget;
  242. this->MissingTargets.emplace_back(std::move(missingTarget));
  243. return;
  244. }
  245. // We are not appending, so all exported targets should be
  246. // known here. This is probably user-error.
  247. this->ComplainAboutMissingTarget(depender, dependee, exportFiles);
  248. }
  249. // Assume the target will be exported by another command.
  250. // Append it with the export namespace.
  251. link_libs += this->Namespace;
  252. link_libs += dependee->GetExportName();
  253. }
  254. void cmExportBuildFileGenerator::GetTargets(
  255. std::vector<std::string>& targets) const
  256. {
  257. if (this->ExportSet) {
  258. for (std::unique_ptr<cmTargetExport> const& te :
  259. this->ExportSet->GetTargetExports()) {
  260. if (te->NamelinkOnly) {
  261. continue;
  262. }
  263. targets.push_back(te->TargetName);
  264. }
  265. return;
  266. }
  267. targets = this->Targets;
  268. }
  269. std::pair<std::vector<std::string>, std::string>
  270. cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg,
  271. const std::string& name)
  272. {
  273. std::vector<std::string> exportFiles;
  274. std::string ns;
  275. auto& exportSets = gg->GetBuildExportSets();
  276. for (auto const& exp : exportSets) {
  277. const auto& exportSet = exp.second;
  278. std::vector<std::string> targets;
  279. exportSet->GetTargets(targets);
  280. if (cm::contains(targets, name)) {
  281. exportFiles.push_back(exp.first);
  282. ns = exportSet->GetNamespace();
  283. }
  284. }
  285. return { exportFiles, ns };
  286. }
  287. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  288. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  289. std::vector<std::string> const& exportFiles)
  290. {
  291. std::ostringstream e;
  292. e << "export called with target \"" << depender->GetName()
  293. << "\" which requires target \"" << dependee->GetName() << "\" ";
  294. if (exportFiles.empty()) {
  295. e << "that is not in any export set.";
  296. } else {
  297. e << "that is not in this export set, but in multiple other export sets: "
  298. << cmJoin(exportFiles, ", ") << ".\n";
  299. e << "An exported target cannot depend upon another target which is "
  300. "exported multiple times. Consider consolidating the exports of the "
  301. "\""
  302. << dependee->GetName() << "\" target to a single export.";
  303. }
  304. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  305. MessageType::FATAL_ERROR, e.str(),
  306. this->LG->GetMakefile()->GetBacktrace());
  307. }
  308. std::string cmExportBuildFileGenerator::InstallNameDir(
  309. cmGeneratorTarget const* target, const std::string& config)
  310. {
  311. std::string install_name_dir;
  312. cmMakefile* mf = target->Target->GetMakefile();
  313. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  314. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  315. }
  316. return install_name_dir;
  317. }
  318. namespace {
  319. bool EntryIsContextSensitive(
  320. const std::unique_ptr<cmCompiledGeneratorExpression>& cge)
  321. {
  322. return cge->GetHadContextSensitiveCondition();
  323. }
  324. }
  325. std::string cmExportBuildFileGenerator::GetFileSetDirectories(
  326. cmGeneratorTarget* gte, cmFileSet* fileSet, cmTargetExport* /*te*/)
  327. {
  328. std::vector<std::string> resultVector;
  329. auto configs =
  330. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  331. auto directoryEntries = fileSet->CompileDirectoryEntries();
  332. for (auto const& config : configs) {
  333. auto directories = fileSet->EvaluateDirectoryEntries(
  334. directoryEntries, gte->LocalGenerator, config, gte);
  335. bool const contextSensitive =
  336. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  337. EntryIsContextSensitive);
  338. auto const& type = fileSet->GetType();
  339. // C++ modules do not support interface file sets which are dependent upon
  340. // the configuration.
  341. if (contextSensitive &&
  342. (type == "CXX_MODULES"_s || type == "CXX_MODULE_HEADER_UNITS"_s)) {
  343. auto* mf = this->LG->GetMakefile();
  344. std::ostringstream e;
  345. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  346. << fileSet->GetName() << "\" of type \"" << type
  347. << "\" contains context-sensitive base directory entries which is not "
  348. "supported.";
  349. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  350. return std::string{};
  351. }
  352. for (auto const& directory : directories) {
  353. auto dest = cmOutputConverter::EscapeForCMake(
  354. directory, cmOutputConverter::WrapQuotes::NoWrap);
  355. if (contextSensitive && configs.size() != 1) {
  356. resultVector.push_back(
  357. cmStrCat("\"$<$<CONFIG:", config, ">:", dest, ">\""));
  358. } else {
  359. resultVector.push_back(cmStrCat('"', dest, '"'));
  360. break;
  361. }
  362. }
  363. }
  364. return cmJoin(resultVector, " ");
  365. }
  366. std::string cmExportBuildFileGenerator::GetFileSetFiles(cmGeneratorTarget* gte,
  367. cmFileSet* fileSet,
  368. cmTargetExport* /*te*/)
  369. {
  370. std::vector<std::string> resultVector;
  371. auto configs =
  372. gte->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  373. auto fileEntries = fileSet->CompileFileEntries();
  374. auto directoryEntries = fileSet->CompileDirectoryEntries();
  375. for (auto const& config : configs) {
  376. auto directories = fileSet->EvaluateDirectoryEntries(
  377. directoryEntries, gte->LocalGenerator, config, gte);
  378. std::map<std::string, std::vector<std::string>> files;
  379. for (auto const& entry : fileEntries) {
  380. fileSet->EvaluateFileEntry(directories, files, entry,
  381. gte->LocalGenerator, config, gte);
  382. }
  383. bool const contextSensitive =
  384. std::any_of(directoryEntries.begin(), directoryEntries.end(),
  385. EntryIsContextSensitive) ||
  386. std::any_of(fileEntries.begin(), fileEntries.end(),
  387. EntryIsContextSensitive);
  388. auto const& type = fileSet->GetType();
  389. // C++ modules do not support interface file sets which are dependent upon
  390. // the configuration.
  391. if (contextSensitive &&
  392. (type == "CXX_MODULES"_s || type == "CXX_MODULE_HEADER_UNITS"_s)) {
  393. auto* mf = this->LG->GetMakefile();
  394. std::ostringstream e;
  395. e << "The \"" << gte->GetName() << "\" target's interface file set \""
  396. << fileSet->GetName() << "\" of type \"" << type
  397. << "\" contains context-sensitive file entries which is not "
  398. "supported.";
  399. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  400. return std::string{};
  401. }
  402. for (auto const& it : files) {
  403. for (auto const& filename : it.second) {
  404. auto escapedFile = cmOutputConverter::EscapeForCMake(
  405. filename, cmOutputConverter::WrapQuotes::NoWrap);
  406. if (contextSensitive && configs.size() != 1) {
  407. resultVector.push_back(
  408. cmStrCat("\"$<$<CONFIG:", config, ">:", escapedFile, ">\""));
  409. } else {
  410. resultVector.push_back(cmStrCat('"', escapedFile, '"'));
  411. }
  412. }
  413. }
  414. if (!(contextSensitive && configs.size() != 1)) {
  415. break;
  416. }
  417. }
  418. return cmJoin(resultVector, " ");
  419. }