/src/CMake/Source/cmCxxModuleUsageEffects.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 | | #include "cmCxxModuleUsageEffects.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <set> |
7 | | #include <vector> |
8 | | |
9 | | #include <cm/string_view> |
10 | | |
11 | | #include "cmCryptoHash.h" |
12 | | #include "cmGeneratorTarget.h" |
13 | | #include "cmListFileCache.h" |
14 | | #include "cmMakefile.h" |
15 | | #include "cmTarget.h" |
16 | | #include "cmValue.h" |
17 | | |
18 | | namespace { |
19 | | |
20 | | bool IsMSVCWarning(cm::string_view flag) |
21 | 0 | { |
22 | 0 | if (flag.size() < 2) { |
23 | 0 | return false; |
24 | 0 | } |
25 | | |
26 | 0 | if (flag.front() != '/' && flag.front() != '-') { |
27 | 0 | return false; |
28 | 0 | } |
29 | | |
30 | 0 | flag.remove_prefix(1); |
31 | |
|
32 | 0 | if (flag.front() == 'w' || flag.front() == 'W') { |
33 | 0 | return true; |
34 | 0 | } |
35 | | |
36 | 0 | if (flag.substr(0, 9) == "external:") { |
37 | 0 | return true; |
38 | 0 | } |
39 | | |
40 | 0 | return false; |
41 | 0 | } |
42 | | |
43 | | bool IsGNUWarning(cm::string_view flag) |
44 | 0 | { |
45 | 0 | if (flag.size() < 2) { |
46 | 0 | return false; |
47 | 0 | } |
48 | | |
49 | 0 | if (flag.front() != '-') { |
50 | 0 | return false; |
51 | 0 | } |
52 | | |
53 | 0 | flag.remove_prefix(1); |
54 | |
|
55 | 0 | if (flag.front() == 'w' || flag.front() == 'W') { |
56 | 0 | return true; |
57 | 0 | } |
58 | | |
59 | 0 | if (flag.front() == '-') { |
60 | 0 | flag.remove_prefix(1); |
61 | 0 | } |
62 | |
|
63 | 0 | static std::set<cm::string_view> const long_names{ |
64 | 0 | "pedantic", "pedantic-errors", "all-warnings", "extra-warnings", |
65 | 0 | "no-warnings" |
66 | 0 | }; |
67 | |
|
68 | 0 | return long_names.find(flag) != long_names.end(); |
69 | 0 | } |
70 | | |
71 | | bool UnknownFilter(cm::string_view) |
72 | 0 | { |
73 | 0 | return false; |
74 | 0 | } |
75 | | |
76 | | using FlagFilter = bool (*)(cm::string_view flag); |
77 | | |
78 | | FlagFilter GetFlagFilter(cmGeneratorTarget const* gt) |
79 | 0 | { |
80 | 0 | static FlagFilter filter = nullptr; |
81 | 0 | if (filter) { |
82 | 0 | return filter; |
83 | 0 | } |
84 | | |
85 | 0 | cmValue frontendVariant = |
86 | 0 | gt->Makefile->GetDefinition("CMAKE_CXX_COMPILER_FRONTEND_VARIANT"); |
87 | |
|
88 | 0 | if (frontendVariant == "GNU") { |
89 | 0 | filter = IsGNUWarning; |
90 | 0 | } else if (frontendVariant == "MSVC") { |
91 | 0 | filter = IsMSVCWarning; |
92 | 0 | } else { |
93 | 0 | filter = UnknownFilter; |
94 | 0 | } |
95 | |
|
96 | 0 | return filter; |
97 | 0 | } |
98 | | |
99 | | template <typename Range> |
100 | | void AppendUsageEntries(std::string& usageHashInput, Range const& entries) |
101 | 0 | { |
102 | 0 | std::vector<std::string> entryValues; |
103 | 0 | for (auto const& entry : entries) { |
104 | 0 | entryValues.push_back(entry.Value); |
105 | 0 | } |
106 | 0 | std::sort(entryValues.begin(), entryValues.end()); |
107 | |
|
108 | 0 | for (auto const& entryValue : entryValues) { |
109 | 0 | usageHashInput += entryValue; |
110 | 0 | usageHashInput.push_back('\n'); |
111 | 0 | } |
112 | |
|
113 | 0 | usageHashInput.push_back('\0'); |
114 | 0 | } Unexecuted instantiation: cmCxxModuleUsageEffects.cxx:void (anonymous namespace)::AppendUsageEntries<cmRange<std::__1::__wrap_iter<BT<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const*> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, cmRange<std::__1::__wrap_iter<BT<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const*> > const&) Unexecuted instantiation: cmCxxModuleUsageEffects.cxx:void (anonymous namespace)::AppendUsageEntries<cmRange<RangeIterators::FilterIterator<std::__1::__wrap_iter<BT<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const*>, cmCxxModuleUsageEffects::cmCxxModuleUsageEffects(cmGeneratorTarget const*)::$_0> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, cmRange<RangeIterators::FilterIterator<std::__1::__wrap_iter<BT<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const*>, cmCxxModuleUsageEffects::cmCxxModuleUsageEffects(cmGeneratorTarget const*)::$_0> > const&) Unexecuted instantiation: cmCxxModuleUsageEffects.cxx:void (anonymous namespace)::AppendUsageEntries<cmRange<RangeIterators::FilterIterator<std::__1::__wrap_iter<BT<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const*>, cmCxxModuleUsageEffects::cmCxxModuleUsageEffects(cmGeneratorTarget const*)::$_1> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, cmRange<RangeIterators::FilterIterator<std::__1::__wrap_iter<BT<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > const*>, cmCxxModuleUsageEffects::cmCxxModuleUsageEffects(cmGeneratorTarget const*)::$_1> > const&) |
115 | | } |
116 | | |
117 | | cmCxxModuleUsageEffects::cmCxxModuleUsageEffects(cmGeneratorTarget const* gt) |
118 | 0 | { |
119 | 0 | auto const* tgt = gt->Target; |
120 | 0 | auto const filter = GetFlagFilter(gt); |
121 | |
|
122 | 0 | std::string usageHashInput; |
123 | 0 | if (tgt->IsImported()) { |
124 | 0 | AppendUsageEntries(usageHashInput, |
125 | 0 | tgt->GetImportedCxxModulesCompileFeaturesEntries()); |
126 | 0 | AppendUsageEntries( |
127 | 0 | usageHashInput, |
128 | 0 | tgt->GetImportedCxxModulesCompileOptionsEntries().filter( |
129 | 0 | [&](const BT<std::string>& flag) { return !filter(flag.Value); })); |
130 | 0 | } else { |
131 | 0 | AppendUsageEntries(usageHashInput, tgt->GetCompileFeaturesEntries()); |
132 | 0 | AppendUsageEntries( |
133 | 0 | usageHashInput, |
134 | 0 | tgt->GetCompileOptionsEntries().filter( |
135 | 0 | [&](const BT<std::string>& flag) { return !filter(flag.Value); })); |
136 | 0 | } |
137 | |
|
138 | 0 | cmCryptoHash hasher(cmCryptoHash::AlgoSHA3_512); |
139 | 0 | this->Hash = hasher.HashString(usageHashInput); |
140 | 0 | } |
141 | | |
142 | | std::string const& cmCxxModuleUsageEffects::GetHash() const |
143 | 0 | { |
144 | 0 | return this->Hash; |
145 | 0 | } |