cmExportBuildFileGenerator.cxx 15 KB

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