cmGeneratorTarget_IncludeDirectories.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. /* clang-format off */
  4. #include "cmGeneratorTarget.h"
  5. /* clang-format on */
  6. #include "cmConfigure.h"
  7. #include <set>
  8. #include <sstream>
  9. #include <string>
  10. #include <unordered_set>
  11. #include <utility>
  12. #include <vector>
  13. #include <cm/optional>
  14. #include <cmext/algorithm>
  15. #include "cmEvaluatedTargetProperty.h"
  16. #include "cmGeneratorExpressionDAGChecker.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmLinkItem.h"
  19. #include "cmList.h"
  20. #include "cmListFileCache.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmMessageType.h"
  24. #include "cmStringAlgorithms.h"
  25. #include "cmSystemTools.h"
  26. #include "cmTarget.h"
  27. #include "cmValue.h"
  28. #include "cmake.h"
  29. namespace {
  30. using UseTo = cmGeneratorTarget::UseTo;
  31. enum class IncludeDirectoryFallBack
  32. {
  33. BINARY,
  34. OBJECT
  35. };
  36. std::string AddLangSpecificInterfaceIncludeDirectories(
  37. cmGeneratorTarget const* root, cmGeneratorTarget const* target,
  38. std::string const& lang, std::string const& config,
  39. std::string const& propertyName, IncludeDirectoryFallBack mode,
  40. cmGeneratorExpressionDAGChecker* context)
  41. {
  42. cmGeneratorExpressionDAGChecker dagChecker{
  43. target,
  44. propertyName,
  45. nullptr,
  46. context,
  47. target->GetLocalGenerator(),
  48. config,
  49. target->GetBacktrace(),
  50. };
  51. switch (dagChecker.Check()) {
  52. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  53. dagChecker.ReportError(
  54. nullptr, "$<TARGET_PROPERTY:" + target->GetName() + ",propertyName");
  55. CM_FALLTHROUGH;
  56. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  57. // No error. We just skip cyclic references.
  58. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  59. // No error. We have already seen this transitive property.
  60. return "";
  61. case cmGeneratorExpressionDAGChecker::DAG:
  62. break;
  63. }
  64. std::string directories;
  65. if (auto const* link_interface =
  66. target->GetLinkInterfaceLibraries(config, root, UseTo::Compile)) {
  67. for (cmLinkItem const& library : link_interface->Libraries) {
  68. if (cmGeneratorTarget const* dependency = library.Target) {
  69. if (cm::contains(dependency->GetAllConfigCompileLanguages(), lang)) {
  70. auto* lg = dependency->GetLocalGenerator();
  71. std::string value = dependency->GetSafeProperty(propertyName);
  72. if (value.empty()) {
  73. if (mode == IncludeDirectoryFallBack::BINARY) {
  74. value = lg->GetCurrentBinaryDirectory();
  75. } else if (mode == IncludeDirectoryFallBack::OBJECT) {
  76. value = dependency->GetSupportDirectory();
  77. }
  78. }
  79. if (!directories.empty()) {
  80. directories += ";";
  81. }
  82. directories += value;
  83. }
  84. }
  85. }
  86. }
  87. return directories;
  88. }
  89. void AddLangSpecificImplicitIncludeDirectories(
  90. cmGeneratorTarget const* target, std::string const& lang,
  91. std::string const& config, std::string const& propertyName,
  92. IncludeDirectoryFallBack mode, EvaluatedTargetPropertyEntries& entries)
  93. {
  94. if (auto const* libraries =
  95. target->GetLinkImplementationLibraries(config, UseTo::Compile)) {
  96. cmGeneratorExpressionDAGChecker dagChecker{
  97. target,
  98. propertyName,
  99. nullptr,
  100. nullptr,
  101. target->GetLocalGenerator(),
  102. config,
  103. target->GetBacktrace(),
  104. };
  105. for (cmLinkImplItem const& library : libraries->Libraries) {
  106. if (cmGeneratorTarget const* dependency = library.Target) {
  107. if (!dependency->IsInBuildSystem()) {
  108. continue;
  109. }
  110. if (cm::contains(dependency->GetAllConfigCompileLanguages(), lang)) {
  111. auto* lg = dependency->GetLocalGenerator();
  112. EvaluatedTargetPropertyEntry entry{ library, library.Backtrace };
  113. if (lang == "Swift") {
  114. entry.Values.emplace_back(
  115. dependency->GetSwiftModuleDirectory(config));
  116. } else if (cmValue val = dependency->GetProperty(propertyName)) {
  117. entry.Values.emplace_back(*val);
  118. } else {
  119. if (mode == IncludeDirectoryFallBack::BINARY) {
  120. entry.Values.emplace_back(lg->GetCurrentBinaryDirectory());
  121. } else if (mode == IncludeDirectoryFallBack::OBJECT) {
  122. entry.Values.emplace_back(
  123. dependency->GetObjectDirectory(config));
  124. }
  125. }
  126. cmExpandList(AddLangSpecificInterfaceIncludeDirectories(
  127. target, dependency, lang, config, propertyName, mode,
  128. &dagChecker),
  129. entry.Values);
  130. entries.Entries.emplace_back(std::move(entry));
  131. }
  132. }
  133. }
  134. }
  135. }
  136. void processIncludeDirectories(cmGeneratorTarget const* tgt,
  137. EvaluatedTargetPropertyEntries& entries,
  138. std::vector<BT<std::string>>& includes,
  139. std::unordered_set<std::string>& uniqueIncludes,
  140. bool debugIncludes)
  141. {
  142. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  143. cmLinkImplItem const& item = entry.LinkImplItem;
  144. std::string const& targetName = item.AsStr();
  145. bool const fromImported = item.Target && item.Target->IsImported();
  146. std::string usedIncludes;
  147. for (std::string& entryInclude : entry.Values) {
  148. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  149. tgt->GetLocalGenerator()->IssueMessage(
  150. MessageType::FATAL_ERROR,
  151. cmStrCat(
  152. "Imported target \"", targetName,
  153. "\" includes non-existent path\n \"", entryInclude,
  154. "\"\nin its INTERFACE_INCLUDE_DIRECTORIES. Possible reasons "
  155. "include:\n"
  156. "* The path was deleted, renamed, or moved to another location.\n"
  157. "* An install or uninstall procedure did not complete "
  158. "successfully.\n"
  159. "* The installation package was faulty and references files it "
  160. "does not provide.\n"));
  161. return;
  162. }
  163. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  164. std::ostringstream e;
  165. MessageType messageType = MessageType::FATAL_ERROR;
  166. if (!targetName.empty()) {
  167. /* clang-format off */
  168. e << "Target \"" << targetName << "\" contains relative "
  169. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  170. " \"" << entryInclude << "\"";
  171. /* clang-format on */
  172. } else {
  173. e << "Found relative path while evaluating include directories of "
  174. "\""
  175. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  176. }
  177. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  178. if (messageType == MessageType::FATAL_ERROR) {
  179. return;
  180. }
  181. }
  182. if (!cmIsOff(entryInclude)) {
  183. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  184. }
  185. if (uniqueIncludes.insert(entryInclude).second) {
  186. includes.emplace_back(entryInclude, entry.Backtrace);
  187. if (debugIncludes) {
  188. usedIncludes += " * " + entryInclude + "\n";
  189. }
  190. }
  191. }
  192. if (!usedIncludes.empty()) {
  193. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  194. MessageType::LOG,
  195. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  196. usedIncludes,
  197. entry.Backtrace);
  198. }
  199. }
  200. }
  201. }
  202. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  203. std::string const& config, std::string const& lang) const
  204. {
  205. ConfigAndLanguage cacheKey(config, lang);
  206. {
  207. auto it = this->IncludeDirectoriesCache.find(cacheKey);
  208. if (it != this->IncludeDirectoriesCache.end()) {
  209. return it->second;
  210. }
  211. }
  212. std::vector<BT<std::string>> includes;
  213. std::unordered_set<std::string> uniqueIncludes;
  214. cmGeneratorExpressionDAGChecker dagChecker{
  215. this, "INCLUDE_DIRECTORIES", nullptr,
  216. nullptr, this->LocalGenerator, config,
  217. };
  218. cmList debugProperties{ this->Makefile->GetDefinition(
  219. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  220. bool debugIncludes = !this->DebugIncludesDone &&
  221. cm::contains(debugProperties, "INCLUDE_DIRECTORIES");
  222. this->DebugIncludesDone = true;
  223. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  224. this, config, lang, &dagChecker, this->IncludeDirectoriesEntries);
  225. if (lang == "Swift") {
  226. AddLangSpecificImplicitIncludeDirectories(
  227. this, lang, config, "Swift_MODULE_DIRECTORY",
  228. IncludeDirectoryFallBack::BINARY, entries);
  229. }
  230. if (this->CanCompileSources() && (lang != "Swift" && lang != "Fortran")) {
  231. std::string const propertyName = "ISPC_HEADER_DIRECTORY";
  232. // If this target has ISPC sources make sure to add the header
  233. // directory to other compilation units
  234. if (cm::contains(this->GetAllConfigCompileLanguages(), "ISPC")) {
  235. if (cmValue val = this->GetProperty(propertyName)) {
  236. includes.emplace_back(*val);
  237. } else {
  238. includes.emplace_back(this->GetObjectDirectory(config));
  239. }
  240. }
  241. AddLangSpecificImplicitIncludeDirectories(
  242. this, "ISPC", config, propertyName, IncludeDirectoryFallBack::OBJECT,
  243. entries);
  244. }
  245. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES", lang,
  246. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  247. processIncludeDirectories(this, entries, includes, uniqueIncludes,
  248. debugIncludes);
  249. if (this->IsApple()) {
  250. if (cmLinkImplementationLibraries const* impl =
  251. this->GetLinkImplementationLibraries(config, UseTo::Compile)) {
  252. for (cmLinkImplItem const& lib : impl->Libraries) {
  253. std::string libDir;
  254. if (!lib.Target) {
  255. libDir = cmSystemTools::CollapseFullPath(
  256. lib.AsStr(), this->Makefile->GetHomeOutputDirectory());
  257. } else if (lib.Target->Target->IsFrameworkOnApple() ||
  258. this->IsImportedFrameworkFolderOnApple(config)) {
  259. libDir = lib.Target->GetLocation(config);
  260. } else {
  261. continue;
  262. }
  263. auto fwDescriptor =
  264. this->GetGlobalGenerator()->SplitFrameworkPath(libDir);
  265. if (!fwDescriptor) {
  266. continue;
  267. }
  268. auto fwInclude = fwDescriptor->GetFrameworkPath();
  269. if (uniqueIncludes.insert(fwInclude).second) {
  270. includes.emplace_back(fwInclude, cmListFileBacktrace());
  271. }
  272. }
  273. }
  274. }
  275. this->IncludeDirectoriesCache.emplace(cacheKey, includes);
  276. return includes;
  277. }