cmGeneratorTarget_IncludeDirectories.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt 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 = cmStrCat(lg->GetCurrentBinaryDirectory(), '/',
  77. lg->GetTargetDirectory(dependency));
  78. }
  79. }
  80. if (!directories.empty()) {
  81. directories += ";";
  82. }
  83. directories += value;
  84. }
  85. }
  86. }
  87. }
  88. return directories;
  89. }
  90. void AddLangSpecificImplicitIncludeDirectories(
  91. cmGeneratorTarget const* target, std::string const& lang,
  92. std::string const& config, std::string const& propertyName,
  93. IncludeDirectoryFallBack mode, EvaluatedTargetPropertyEntries& entries)
  94. {
  95. if (auto const* libraries =
  96. target->GetLinkImplementationLibraries(config, UseTo::Compile)) {
  97. cmGeneratorExpressionDAGChecker dagChecker{
  98. target,
  99. propertyName,
  100. nullptr,
  101. nullptr,
  102. target->GetLocalGenerator(),
  103. config,
  104. target->GetBacktrace(),
  105. };
  106. for (cmLinkImplItem const& library : libraries->Libraries) {
  107. if (cmGeneratorTarget const* dependency = library.Target) {
  108. if (!dependency->IsInBuildSystem()) {
  109. continue;
  110. }
  111. if (cm::contains(dependency->GetAllConfigCompileLanguages(), lang)) {
  112. auto* lg = dependency->GetLocalGenerator();
  113. EvaluatedTargetPropertyEntry entry{ library, library.Backtrace };
  114. if (lang == "Swift") {
  115. entry.Values.emplace_back(
  116. dependency->GetSwiftModuleDirectory(config));
  117. } else if (cmValue val = dependency->GetProperty(propertyName)) {
  118. entry.Values.emplace_back(*val);
  119. } else {
  120. if (mode == IncludeDirectoryFallBack::BINARY) {
  121. entry.Values.emplace_back(lg->GetCurrentBinaryDirectory());
  122. } else if (mode == IncludeDirectoryFallBack::OBJECT) {
  123. entry.Values.emplace_back(
  124. dependency->GetObjectDirectory(config));
  125. }
  126. }
  127. cmExpandList(AddLangSpecificInterfaceIncludeDirectories(
  128. target, dependency, lang, config, propertyName, mode,
  129. &dagChecker),
  130. entry.Values);
  131. entries.Entries.emplace_back(std::move(entry));
  132. }
  133. }
  134. }
  135. }
  136. }
  137. void processIncludeDirectories(cmGeneratorTarget const* tgt,
  138. EvaluatedTargetPropertyEntries& entries,
  139. std::vector<BT<std::string>>& includes,
  140. std::unordered_set<std::string>& uniqueIncludes,
  141. bool debugIncludes)
  142. {
  143. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  144. cmLinkImplItem const& item = entry.LinkImplItem;
  145. std::string const& targetName = item.AsStr();
  146. bool const fromImported = item.Target && item.Target->IsImported();
  147. std::string usedIncludes;
  148. for (std::string& entryInclude : entry.Values) {
  149. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  150. tgt->GetLocalGenerator()->IssueMessage(
  151. MessageType::FATAL_ERROR,
  152. cmStrCat(
  153. "Imported target \"", targetName,
  154. "\" includes non-existent path\n \"", entryInclude,
  155. "\"\nin its INTERFACE_INCLUDE_DIRECTORIES. Possible reasons "
  156. "include:\n"
  157. "* The path was deleted, renamed, or moved to another location.\n"
  158. "* An install or uninstall procedure did not complete "
  159. "successfully.\n"
  160. "* The installation package was faulty and references files it "
  161. "does not provide.\n"));
  162. return;
  163. }
  164. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  165. std::ostringstream e;
  166. MessageType messageType = MessageType::FATAL_ERROR;
  167. if (!targetName.empty()) {
  168. /* clang-format off */
  169. e << "Target \"" << targetName << "\" contains relative "
  170. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  171. " \"" << entryInclude << "\"";
  172. /* clang-format on */
  173. } else {
  174. e << "Found relative path while evaluating include directories of "
  175. "\""
  176. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  177. }
  178. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  179. if (messageType == MessageType::FATAL_ERROR) {
  180. return;
  181. }
  182. }
  183. if (!cmIsOff(entryInclude)) {
  184. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  185. }
  186. if (uniqueIncludes.insert(entryInclude).second) {
  187. includes.emplace_back(entryInclude, entry.Backtrace);
  188. if (debugIncludes) {
  189. usedIncludes += " * " + entryInclude + "\n";
  190. }
  191. }
  192. }
  193. if (!usedIncludes.empty()) {
  194. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  195. MessageType::LOG,
  196. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  197. usedIncludes,
  198. entry.Backtrace);
  199. }
  200. }
  201. }
  202. }
  203. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  204. std::string const& config, std::string const& lang) const
  205. {
  206. ConfigAndLanguage cacheKey(config, lang);
  207. {
  208. auto it = this->IncludeDirectoriesCache.find(cacheKey);
  209. if (it != this->IncludeDirectoriesCache.end()) {
  210. return it->second;
  211. }
  212. }
  213. std::vector<BT<std::string>> includes;
  214. std::unordered_set<std::string> uniqueIncludes;
  215. cmGeneratorExpressionDAGChecker dagChecker{
  216. this, "INCLUDE_DIRECTORIES", nullptr,
  217. nullptr, this->LocalGenerator, config,
  218. };
  219. cmList debugProperties{ this->Makefile->GetDefinition(
  220. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  221. bool debugIncludes = !this->DebugIncludesDone &&
  222. cm::contains(debugProperties, "INCLUDE_DIRECTORIES");
  223. this->DebugIncludesDone = true;
  224. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  225. this, config, lang, &dagChecker, this->IncludeDirectoriesEntries);
  226. if (lang == "Swift") {
  227. AddLangSpecificImplicitIncludeDirectories(
  228. this, lang, config, "Swift_MODULE_DIRECTORY",
  229. IncludeDirectoryFallBack::BINARY, entries);
  230. }
  231. if (this->CanCompileSources() && (lang != "Swift" && lang != "Fortran")) {
  232. std::string const propertyName = "ISPC_HEADER_DIRECTORY";
  233. // If this target has ISPC sources make sure to add the header
  234. // directory to other compilation units
  235. if (cm::contains(this->GetAllConfigCompileLanguages(), "ISPC")) {
  236. if (cmValue val = this->GetProperty(propertyName)) {
  237. includes.emplace_back(*val);
  238. } else {
  239. includes.emplace_back(this->GetObjectDirectory(config));
  240. }
  241. }
  242. AddLangSpecificImplicitIncludeDirectories(
  243. this, "ISPC", config, propertyName, IncludeDirectoryFallBack::OBJECT,
  244. entries);
  245. }
  246. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES", lang,
  247. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  248. processIncludeDirectories(this, entries, includes, uniqueIncludes,
  249. debugIncludes);
  250. if (this->IsApple()) {
  251. if (cmLinkImplementationLibraries const* impl =
  252. this->GetLinkImplementationLibraries(config, UseTo::Compile)) {
  253. for (cmLinkImplItem const& lib : impl->Libraries) {
  254. std::string libDir;
  255. if (!lib.Target) {
  256. libDir = cmSystemTools::CollapseFullPath(
  257. lib.AsStr(), this->Makefile->GetHomeOutputDirectory());
  258. } else if (lib.Target->Target->IsFrameworkOnApple() ||
  259. this->IsImportedFrameworkFolderOnApple(config)) {
  260. libDir = lib.Target->GetLocation(config);
  261. } else {
  262. continue;
  263. }
  264. auto fwDescriptor =
  265. this->GetGlobalGenerator()->SplitFrameworkPath(libDir);
  266. if (!fwDescriptor) {
  267. continue;
  268. }
  269. auto fwInclude = fwDescriptor->GetFrameworkPath();
  270. if (uniqueIncludes.insert(fwInclude).second) {
  271. includes.emplace_back(fwInclude, cmListFileBacktrace());
  272. }
  273. }
  274. }
  275. }
  276. this->IncludeDirectoriesCache.emplace(cacheKey, includes);
  277. return includes;
  278. }