cmExportBuildFileGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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 <map>
  5. #include <memory>
  6. #include <set>
  7. #include <sstream>
  8. #include <utility>
  9. #include <cmext/algorithm>
  10. #include "cmExportSet.h"
  11. #include "cmFileSet.h"
  12. #include "cmGeneratorExpression.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmListFileCache.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. std::vector<std::string> missingTargets;
  71. // Create all the imported targets.
  72. for (cmGeneratorTarget* gte : this->Exports) {
  73. this->GenerateImportTargetCode(os, gte, this->GetExportTargetType(gte));
  74. gte->Target->AppendBuildInterfaceIncludes();
  75. ImportPropertyMap properties;
  76. this->PopulateInterfaceProperty("INTERFACE_INCLUDE_DIRECTORIES", gte,
  77. cmGeneratorExpression::BuildInterface,
  78. properties, missingTargets);
  79. this->PopulateInterfaceProperty("INTERFACE_SOURCES", gte,
  80. cmGeneratorExpression::BuildInterface,
  81. properties, missingTargets);
  82. this->PopulateInterfaceProperty("INTERFACE_COMPILE_DEFINITIONS", gte,
  83. cmGeneratorExpression::BuildInterface,
  84. properties, missingTargets);
  85. this->PopulateInterfaceProperty("INTERFACE_COMPILE_OPTIONS", gte,
  86. cmGeneratorExpression::BuildInterface,
  87. properties, missingTargets);
  88. this->PopulateInterfaceProperty("INTERFACE_PRECOMPILE_HEADERS", gte,
  89. cmGeneratorExpression::BuildInterface,
  90. properties, missingTargets);
  91. this->PopulateInterfaceProperty("INTERFACE_AUTOUIC_OPTIONS", gte,
  92. cmGeneratorExpression::BuildInterface,
  93. properties, missingTargets);
  94. this->PopulateInterfaceProperty("INTERFACE_COMPILE_FEATURES", gte,
  95. cmGeneratorExpression::BuildInterface,
  96. properties, missingTargets);
  97. this->PopulateInterfaceProperty("INTERFACE_LINK_OPTIONS", gte,
  98. cmGeneratorExpression::BuildInterface,
  99. properties, missingTargets);
  100. this->PopulateInterfaceProperty("INTERFACE_LINK_DIRECTORIES", gte,
  101. cmGeneratorExpression::BuildInterface,
  102. properties, missingTargets);
  103. this->PopulateInterfaceProperty("INTERFACE_LINK_DEPENDS", gte,
  104. cmGeneratorExpression::BuildInterface,
  105. properties, missingTargets);
  106. this->PopulateInterfaceProperty("INTERFACE_POSITION_INDEPENDENT_CODE", gte,
  107. properties);
  108. std::string errorMessage;
  109. if (!this->PopulateExportProperties(gte, properties, errorMessage)) {
  110. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  111. MessageType::FATAL_ERROR, errorMessage,
  112. this->LG->GetMakefile()->GetBacktrace());
  113. return false;
  114. }
  115. const bool newCMP0022Behavior =
  116. gte->GetPolicyStatusCMP0022() != cmPolicies::WARN &&
  117. gte->GetPolicyStatusCMP0022() != cmPolicies::OLD;
  118. if (newCMP0022Behavior) {
  119. this->PopulateInterfaceLinkLibrariesProperty(
  120. gte, cmGeneratorExpression::BuildInterface, properties,
  121. missingTargets);
  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, missingTargets);
  130. }
  131. this->GenerateMissingTargetsCheckCode(os, missingTargets);
  132. return true;
  133. }
  134. void cmExportBuildFileGenerator::GenerateImportTargetsConfig(
  135. std::ostream& os, const std::string& config, std::string const& suffix,
  136. std::vector<std::string>& missingTargets)
  137. {
  138. for (cmGeneratorTarget* target : this->Exports) {
  139. // Collect import properties for this target.
  140. ImportPropertyMap properties;
  141. if (this->GetExportTargetType(target) != cmStateEnums::INTERFACE_LIBRARY) {
  142. this->SetImportLocationProperty(config, suffix, target, properties);
  143. }
  144. if (!properties.empty()) {
  145. // Get the rest of the target details.
  146. if (this->GetExportTargetType(target) !=
  147. cmStateEnums::INTERFACE_LIBRARY) {
  148. this->SetImportDetailProperties(config, suffix, target, properties,
  149. missingTargets);
  150. this->SetImportLinkInterface(config, suffix,
  151. cmGeneratorExpression::BuildInterface,
  152. target, properties, missingTargets);
  153. }
  154. // TODO: PUBLIC_HEADER_LOCATION
  155. // This should wait until the build feature propagation stuff
  156. // is done. Then this can be a propagated include directory.
  157. // this->GenerateImportProperty(config, te->HeaderGenerator,
  158. // properties);
  159. // Generate code in the export file.
  160. this->GenerateImportPropertyCode(os, config, target, properties);
  161. }
  162. }
  163. }
  164. cmStateEnums::TargetType cmExportBuildFileGenerator::GetExportTargetType(
  165. cmGeneratorTarget const* target) const
  166. {
  167. cmStateEnums::TargetType targetType = target->GetType();
  168. // An object library exports as an interface library if we cannot
  169. // tell clients where to find the objects. This is sufficient
  170. // to support transitive usage requirements on other targets that
  171. // use the object library.
  172. if (targetType == cmStateEnums::OBJECT_LIBRARY &&
  173. !target->Target->HasKnownObjectFileLocation(nullptr)) {
  174. targetType = cmStateEnums::INTERFACE_LIBRARY;
  175. }
  176. return targetType;
  177. }
  178. void cmExportBuildFileGenerator::SetExportSet(cmExportSet* exportSet)
  179. {
  180. this->ExportSet = exportSet;
  181. }
  182. void cmExportBuildFileGenerator::SetImportLocationProperty(
  183. const std::string& config, std::string const& suffix,
  184. cmGeneratorTarget* target, ImportPropertyMap& properties)
  185. {
  186. // Get the makefile in which to lookup target information.
  187. cmMakefile* mf = target->Makefile;
  188. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  189. std::string prop = cmStrCat("IMPORTED_OBJECTS", suffix);
  190. // Compute all the object files inside this target and setup
  191. // IMPORTED_OBJECTS as a list of object files
  192. std::vector<cmSourceFile const*> objectSources;
  193. target->GetObjectSources(objectSources, config);
  194. std::string const obj_dir = target->GetObjectDirectory(config);
  195. std::vector<std::string> objects;
  196. for (cmSourceFile const* sf : objectSources) {
  197. const std::string& obj = target->GetObjectName(sf);
  198. objects.push_back(obj_dir + obj);
  199. }
  200. // Store the property.
  201. properties[prop] = cmJoin(objects, ";");
  202. } else {
  203. // Add the main target file.
  204. {
  205. std::string prop = cmStrCat("IMPORTED_LOCATION", suffix);
  206. std::string value;
  207. if (target->IsAppBundleOnApple()) {
  208. value =
  209. target->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  210. } else {
  211. value = target->GetFullPath(config,
  212. cmStateEnums::RuntimeBinaryArtifact, true);
  213. }
  214. properties[prop] = value;
  215. }
  216. // Add the import library for windows DLLs.
  217. if (target->HasImportLibrary(config)) {
  218. std::string prop = cmStrCat("IMPORTED_IMPLIB", suffix);
  219. std::string value =
  220. target->GetFullPath(config, cmStateEnums::ImportLibraryArtifact);
  221. if (mf->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  222. target->GetImplibGNUtoMS(config, value, value,
  223. "${CMAKE_IMPORT_LIBRARY_SUFFIX}");
  224. }
  225. properties[prop] = value;
  226. }
  227. }
  228. }
  229. void cmExportBuildFileGenerator::HandleMissingTarget(
  230. std::string& link_libs, std::vector<std::string>& missingTargets,
  231. cmGeneratorTarget const* depender, cmGeneratorTarget* dependee)
  232. {
  233. // The target is not in the export.
  234. if (!this->AppendMode) {
  235. const std::string name = dependee->GetName();
  236. cmGlobalGenerator* gg =
  237. dependee->GetLocalGenerator()->GetGlobalGenerator();
  238. auto exportInfo = this->FindBuildExportInfo(gg, name);
  239. std::vector<std::string> const& exportFiles = exportInfo.first;
  240. if (exportFiles.size() == 1) {
  241. std::string missingTarget = exportInfo.second;
  242. missingTarget += dependee->GetExportName();
  243. link_libs += missingTarget;
  244. missingTargets.push_back(std::move(missingTarget));
  245. return;
  246. }
  247. // We are not appending, so all exported targets should be
  248. // known here. This is probably user-error.
  249. this->ComplainAboutMissingTarget(depender, dependee, exportFiles);
  250. }
  251. // Assume the target will be exported by another command.
  252. // Append it with the export namespace.
  253. link_libs += this->Namespace;
  254. link_libs += dependee->GetExportName();
  255. }
  256. void cmExportBuildFileGenerator::GetTargets(
  257. std::vector<std::string>& targets) const
  258. {
  259. if (this->ExportSet) {
  260. for (std::unique_ptr<cmTargetExport> const& te :
  261. this->ExportSet->GetTargetExports()) {
  262. if (te->NamelinkOnly) {
  263. continue;
  264. }
  265. targets.push_back(te->TargetName);
  266. }
  267. return;
  268. }
  269. targets = this->Targets;
  270. }
  271. std::pair<std::vector<std::string>, std::string>
  272. cmExportBuildFileGenerator::FindBuildExportInfo(cmGlobalGenerator* gg,
  273. const std::string& name)
  274. {
  275. std::vector<std::string> exportFiles;
  276. std::string ns;
  277. auto& exportSets = gg->GetBuildExportSets();
  278. for (auto const& exp : exportSets) {
  279. const auto& exportSet = exp.second;
  280. std::vector<std::string> targets;
  281. exportSet->GetTargets(targets);
  282. if (cm::contains(targets, name)) {
  283. exportFiles.push_back(exp.first);
  284. ns = exportSet->GetNamespace();
  285. }
  286. }
  287. return { exportFiles, ns };
  288. }
  289. void cmExportBuildFileGenerator::ComplainAboutMissingTarget(
  290. cmGeneratorTarget const* depender, cmGeneratorTarget const* dependee,
  291. std::vector<std::string> const& exportFiles)
  292. {
  293. std::ostringstream e;
  294. e << "export called with target \"" << depender->GetName()
  295. << "\" which requires target \"" << dependee->GetName() << "\" ";
  296. if (exportFiles.empty()) {
  297. e << "that is not in any export set.";
  298. } else {
  299. e << "that is not in this export set, but in multiple other export sets: "
  300. << cmJoin(exportFiles, ", ") << ".\n";
  301. e << "An exported target cannot depend upon another target which is "
  302. "exported multiple times. Consider consolidating the exports of the "
  303. "\""
  304. << dependee->GetName() << "\" target to a single export.";
  305. }
  306. this->LG->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  307. MessageType::FATAL_ERROR, e.str(),
  308. this->LG->GetMakefile()->GetBacktrace());
  309. }
  310. std::string cmExportBuildFileGenerator::InstallNameDir(
  311. cmGeneratorTarget const* target, const std::string& config)
  312. {
  313. std::string install_name_dir;
  314. cmMakefile* mf = target->Target->GetMakefile();
  315. if (mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  316. install_name_dir = target->GetInstallNameDirForBuildTree(config);
  317. }
  318. return install_name_dir;
  319. }
  320. std::string cmExportBuildFileGenerator::GetFileSetDirectories(
  321. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport* /*te*/)
  322. {
  323. return cmOutputConverter::EscapeForCMake(
  324. cmJoin(fileSet->GetDirectoryEntries(), ";"));
  325. }
  326. std::string cmExportBuildFileGenerator::GetFileSetFiles(
  327. cmGeneratorTarget* /*gte*/, cmFileSet* fileSet, cmTargetExport* /*te*/)
  328. {
  329. return cmOutputConverter::EscapeForCMake(
  330. cmJoin(fileSet->GetFileEntries(), ";"));
  331. }