cmGeneratorTarget_IncludeDirectories.cxx 12 KB

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