1
0

cmGeneratorTarget_IncludeDirectories.cxx 12 KB

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