1
0

cmGeneratorTarget_IncludeDirectories.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 dagChecker{
  44. target->GetBacktrace(), target, propertyName, nullptr, context,
  45. target->GetLocalGenerator(), config,
  46. };
  47. switch (dagChecker.Check()) {
  48. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  49. dagChecker.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 dagChecker{
  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(AddLangSpecificInterfaceIncludeDirectories(
  119. target, dependency, lang, config, propertyName, mode,
  120. &dagChecker),
  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::REQUIRED_ALWAYS:
  153. case cmPolicies::REQUIRED_IF_USED:
  154. case cmPolicies::NEW:
  155. break;
  156. }
  157. }
  158. /* clang-format off */
  159. e << "Imported target \"" << targetName << "\" includes "
  160. "non-existent path\n \"" << entryInclude << "\"\nin its "
  161. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  162. "* The path was deleted, renamed, or moved to another "
  163. "location.\n"
  164. "* An install or uninstall procedure did not complete "
  165. "successfully.\n"
  166. "* The installation package was faulty and references files it "
  167. "does not provide.\n";
  168. /* clang-format on */
  169. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  170. return;
  171. }
  172. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  173. std::ostringstream e;
  174. bool noMessage = false;
  175. MessageType messageType = MessageType::FATAL_ERROR;
  176. if (!targetName.empty()) {
  177. /* clang-format off */
  178. e << "Target \"" << targetName << "\" contains relative "
  179. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  180. " \"" << entryInclude << "\"";
  181. /* clang-format on */
  182. } else {
  183. switch (tgt->GetPolicyStatusCMP0021()) {
  184. case cmPolicies::WARN: {
  185. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  186. messageType = MessageType::AUTHOR_WARNING;
  187. } break;
  188. case cmPolicies::OLD:
  189. noMessage = true;
  190. break;
  191. case cmPolicies::REQUIRED_IF_USED:
  192. case cmPolicies::REQUIRED_ALWAYS:
  193. case cmPolicies::NEW:
  194. // Issue the fatal message.
  195. break;
  196. }
  197. e << "Found relative path while evaluating include directories of "
  198. "\""
  199. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  200. }
  201. if (!noMessage) {
  202. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  203. if (messageType == MessageType::FATAL_ERROR) {
  204. return;
  205. }
  206. }
  207. }
  208. if (!cmIsOff(entryInclude)) {
  209. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  210. }
  211. if (uniqueIncludes.insert(entryInclude).second) {
  212. includes.emplace_back(entryInclude, entry.Backtrace);
  213. if (debugIncludes) {
  214. usedIncludes += " * " + entryInclude + "\n";
  215. }
  216. }
  217. }
  218. if (!usedIncludes.empty()) {
  219. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  220. MessageType::LOG,
  221. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  222. usedIncludes,
  223. entry.Backtrace);
  224. }
  225. }
  226. }
  227. }
  228. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  229. const std::string& config, const std::string& lang) const
  230. {
  231. ConfigAndLanguage cacheKey(config, lang);
  232. {
  233. auto it = this->IncludeDirectoriesCache.find(cacheKey);
  234. if (it != this->IncludeDirectoriesCache.end()) {
  235. return it->second;
  236. }
  237. }
  238. std::vector<BT<std::string>> includes;
  239. std::unordered_set<std::string> uniqueIncludes;
  240. cmGeneratorExpressionDAGChecker dagChecker{
  241. this, "INCLUDE_DIRECTORIES", nullptr,
  242. nullptr, this->LocalGenerator, config,
  243. };
  244. cmList debugProperties{ this->Makefile->GetDefinition(
  245. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  246. bool debugIncludes = !this->DebugIncludesDone &&
  247. cm::contains(debugProperties, "INCLUDE_DIRECTORIES");
  248. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  249. this->DebugIncludesDone = true;
  250. }
  251. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  252. this, config, lang, &dagChecker, this->IncludeDirectoriesEntries);
  253. if (lang == "Swift") {
  254. AddLangSpecificImplicitIncludeDirectories(
  255. this, lang, config, "Swift_MODULE_DIRECTORY",
  256. IncludeDirectoryFallBack::BINARY, entries);
  257. }
  258. if (this->CanCompileSources() && (lang != "Swift" && lang != "Fortran")) {
  259. const std::string propertyName = "ISPC_HEADER_DIRECTORY";
  260. // If this target has ISPC sources make sure to add the header
  261. // directory to other compilation units
  262. if (cm::contains(this->GetAllConfigCompileLanguages(), "ISPC")) {
  263. if (cmValue val = this->GetProperty(propertyName)) {
  264. includes.emplace_back(*val);
  265. } else {
  266. includes.emplace_back(this->GetObjectDirectory(config));
  267. }
  268. }
  269. AddLangSpecificImplicitIncludeDirectories(
  270. this, "ISPC", config, propertyName, IncludeDirectoryFallBack::OBJECT,
  271. entries);
  272. }
  273. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES", lang,
  274. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  275. processIncludeDirectories(this, entries, includes, uniqueIncludes,
  276. debugIncludes);
  277. if (this->IsApple()) {
  278. if (cmLinkImplementationLibraries const* impl =
  279. this->GetLinkImplementationLibraries(config, UseTo::Compile)) {
  280. for (cmLinkImplItem const& lib : impl->Libraries) {
  281. std::string libDir;
  282. if (!lib.Target) {
  283. libDir = cmSystemTools::CollapseFullPath(
  284. lib.AsStr(), this->Makefile->GetHomeOutputDirectory());
  285. } else if (lib.Target->Target->IsFrameworkOnApple() ||
  286. this->IsImportedFrameworkFolderOnApple(config)) {
  287. libDir = lib.Target->GetLocation(config);
  288. } else {
  289. continue;
  290. }
  291. auto fwDescriptor =
  292. this->GetGlobalGenerator()->SplitFrameworkPath(libDir);
  293. if (!fwDescriptor) {
  294. continue;
  295. }
  296. auto fwInclude = fwDescriptor->GetFrameworkPath();
  297. if (uniqueIncludes.insert(fwInclude).second) {
  298. includes.emplace_back(fwInclude, cmListFileBacktrace());
  299. }
  300. }
  301. }
  302. }
  303. this->IncludeDirectoriesCache.emplace(cacheKey, includes);
  304. return includes;
  305. }