/src/CMake/Source/cmGeneratorTarget_LinkDirectories.cxx
Line | Count | Source |
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 | | |
7 | | #include <map> |
8 | | #include <string> |
9 | | #include <unordered_set> |
10 | | #include <utility> |
11 | | #include <vector> |
12 | | |
13 | | #include <cmext/algorithm> |
14 | | |
15 | | #include "cmEvaluatedTargetProperty.h" |
16 | | #include "cmGenExContext.h" |
17 | | #include "cmGeneratorExpressionDAGChecker.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 "cmValue.h" |
28 | | #include "cmake.h" |
29 | | |
30 | | namespace { |
31 | | void processLinkDirectories(cmGeneratorTarget const* tgt, |
32 | | cm::EvaluatedTargetPropertyEntries& entries, |
33 | | std::vector<BT<std::string>>& directories, |
34 | | std::unordered_set<std::string>& uniqueDirectories, |
35 | | bool debugDirectories) |
36 | 0 | { |
37 | 0 | for (cm::EvaluatedTargetPropertyEntry& entry : entries.Entries) { |
38 | 0 | cmLinkItem const& item = entry.LinkItem; |
39 | 0 | std::string const& targetName = item.AsStr(); |
40 | |
|
41 | 0 | std::string usedDirectories; |
42 | 0 | for (std::string& entryDirectory : entry.Values) { |
43 | 0 | if (!cmSystemTools::FileIsFullPath(entryDirectory)) { |
44 | 0 | if (!targetName.empty()) { |
45 | 0 | tgt->GetLocalGenerator()->IssueMessage( |
46 | 0 | MessageType::FATAL_ERROR, |
47 | 0 | cmStrCat("Target \"", targetName, |
48 | 0 | "\" contains relative path" |
49 | 0 | " in its INTERFACE_LINK_DIRECTORIES:\n \"", |
50 | 0 | entryDirectory, "\"")); |
51 | 0 | return; |
52 | 0 | } |
53 | 0 | switch (tgt->GetPolicyStatusCMP0081()) { |
54 | 0 | case cmPolicies::WARN: |
55 | 0 | tgt->GetLocalGenerator()->IssuePolicyWarning( |
56 | 0 | cmPolicies::CMP0081, {}, |
57 | 0 | cmStrCat("Found relative path while evaluating" |
58 | 0 | " link directories of \"", |
59 | 0 | tgt->GetName(), "\":\n \"", entryDirectory, "\"\n")); |
60 | 0 | break; |
61 | 0 | case cmPolicies::OLD: |
62 | 0 | break; |
63 | 0 | case cmPolicies::NEW: |
64 | 0 | tgt->GetLocalGenerator()->IssueMessage( |
65 | 0 | MessageType::FATAL_ERROR, |
66 | 0 | cmStrCat("Found relative path while evaluating" |
67 | 0 | " link directories of \"", |
68 | 0 | tgt->GetName(), "\":\n \"", entryDirectory, "\"\n")); |
69 | 0 | return; |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | | // Sanitize the path the same way the link_directories command does |
74 | | // in case projects set the LINK_DIRECTORIES property directly. |
75 | 0 | cmSystemTools::ConvertToUnixSlashes(entryDirectory); |
76 | 0 | if (uniqueDirectories.insert(entryDirectory).second) { |
77 | 0 | directories.emplace_back(entryDirectory, entry.Backtrace); |
78 | 0 | if (debugDirectories) { |
79 | 0 | usedDirectories += cmStrCat(" * ", entryDirectory, '\n'); |
80 | 0 | } |
81 | 0 | } |
82 | 0 | } |
83 | 0 | if (!usedDirectories.empty()) { |
84 | 0 | tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage( |
85 | 0 | MessageType::LOG, |
86 | 0 | cmStrCat("Used link directories for target ", tgt->GetName(), ":\n", |
87 | 0 | usedDirectories), |
88 | 0 | entry.Backtrace); |
89 | 0 | } |
90 | 0 | } |
91 | 0 | } |
92 | | } |
93 | | |
94 | | void cmGeneratorTarget::GetLinkDirectories(std::vector<std::string>& result, |
95 | | std::string const& config, |
96 | | std::string const& language) const |
97 | 0 | { |
98 | 0 | std::vector<BT<std::string>> tmp = |
99 | 0 | this->GetLinkDirectories(config, language); |
100 | 0 | result.reserve(tmp.size()); |
101 | 0 | for (BT<std::string>& v : tmp) { |
102 | 0 | result.emplace_back(std::move(v.Value)); |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDirectories( |
107 | | std::string const& config, std::string const& language) const |
108 | 0 | { |
109 | 0 | ConfigAndLanguage cacheKey( |
110 | 0 | config, cmStrCat(language, this->IsDeviceLink() ? "-device" : "")); |
111 | 0 | { |
112 | 0 | auto it = this->LinkDirectoriesCache.find(cacheKey); |
113 | 0 | if (it != this->LinkDirectoriesCache.end()) { |
114 | 0 | return it->second; |
115 | 0 | } |
116 | 0 | } |
117 | 0 | std::vector<BT<std::string>> result; |
118 | 0 | std::unordered_set<std::string> uniqueDirectories; |
119 | |
|
120 | 0 | cm::GenEx::Context context(this->LocalGenerator, config, language); |
121 | 0 | cmGeneratorExpressionDAGChecker dagChecker{ |
122 | 0 | this, "LINK_DIRECTORIES", nullptr, nullptr, context, |
123 | 0 | }; |
124 | |
|
125 | 0 | cmList debugProperties{ this->Makefile->GetDefinition( |
126 | 0 | "CMAKE_DEBUG_TARGET_PROPERTIES") }; |
127 | 0 | bool debugDirectories = !this->DebugLinkDirectoriesDone && |
128 | 0 | cm::contains(debugProperties, "LINK_DIRECTORIES"); |
129 | |
|
130 | 0 | this->DebugLinkDirectoriesDone = true; |
131 | |
|
132 | 0 | cm::EvaluatedTargetPropertyEntries entries = |
133 | 0 | cm::EvaluateTargetPropertyEntries(this, context, &dagChecker, |
134 | 0 | this->LinkDirectoriesEntries); |
135 | |
|
136 | 0 | AddInterfaceEntries(this, "INTERFACE_LINK_DIRECTORIES", context, &dagChecker, |
137 | 0 | entries, cm::IncludeRuntimeInterface::Yes, |
138 | 0 | this->GetPolicyStatusCMP0099() == cmPolicies::NEW |
139 | 0 | ? UseTo::Link |
140 | 0 | : UseTo::Compile); |
141 | |
|
142 | 0 | processLinkDirectories(this, entries, result, uniqueDirectories, |
143 | 0 | debugDirectories); |
144 | |
|
145 | 0 | this->LinkDirectoriesCache.emplace(cacheKey, result); |
146 | 0 | return result; |
147 | 0 | } |