/src/CMake/Source/cmFileSetMetadata.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 "cmFileSetMetadata.h" |
4 | | |
5 | | #include <map> |
6 | | #include <string> |
7 | | #include <utility> |
8 | | |
9 | | #include <cmext/algorithm> |
10 | | #include <cmext/string_view> |
11 | | |
12 | | #include "cmsys/RegularExpression.hxx" |
13 | | |
14 | | #include "cmMakefile.h" |
15 | | #include "cmMessageType.h" |
16 | | #include "cmStringAlgorithms.h" |
17 | | #include "cmSystemTools.h" |
18 | | |
19 | | namespace cm { |
20 | | namespace FileSetMetadata { |
21 | | cm::string_view VisibilityToName(Visibility vis) |
22 | 0 | { |
23 | 0 | switch (vis) { |
24 | 0 | case Visibility::Interface: |
25 | 0 | return "INTERFACE"_s; |
26 | 0 | case Visibility::Public: |
27 | 0 | return "PUBLIC"_s; |
28 | 0 | case Visibility::Private: |
29 | 0 | return "PRIVATE"_s; |
30 | 0 | } |
31 | 0 | return ""_s; |
32 | 0 | } |
33 | | |
34 | | Visibility VisibilityFromName(cm::string_view name, cmMakefile* mf) |
35 | 0 | { |
36 | 0 | if (name == "INTERFACE"_s) { |
37 | 0 | return Visibility::Interface; |
38 | 0 | } |
39 | 0 | if (name == "PUBLIC"_s) { |
40 | 0 | return Visibility::Public; |
41 | 0 | } |
42 | 0 | if (name == "PRIVATE"_s) { |
43 | 0 | return Visibility::Private; |
44 | 0 | } |
45 | 0 | auto msg = cmStrCat("File set visibility \"", name, "\" is not valid."); |
46 | 0 | if (mf) { |
47 | 0 | mf->IssueMessage(MessageType::FATAL_ERROR, msg); |
48 | 0 | } else { |
49 | 0 | cmSystemTools::Error(msg); |
50 | 0 | } |
51 | 0 | return Visibility::Private; |
52 | 0 | } |
53 | | |
54 | | bool VisibilityIsForSelf(Visibility vis) |
55 | 0 | { |
56 | 0 | switch (vis) { |
57 | 0 | case Visibility::Interface: |
58 | 0 | return false; |
59 | 0 | case Visibility::Public: |
60 | 0 | case Visibility::Private: |
61 | 0 | return true; |
62 | 0 | } |
63 | 0 | return false; |
64 | 0 | } |
65 | | |
66 | | bool VisibilityIsForInterface(Visibility vis) |
67 | 0 | { |
68 | 0 | switch (vis) { |
69 | 0 | case Visibility::Interface: |
70 | 0 | case Visibility::Public: |
71 | 0 | return true; |
72 | 0 | case Visibility::Private: |
73 | 0 | return false; |
74 | 0 | } |
75 | 0 | return false; |
76 | 0 | } |
77 | | |
78 | | cm::string_view const HEADERS = "HEADERS"_s; |
79 | | cm::string_view const SOURCES = "SOURCES"_s; |
80 | | cm::string_view const CXX_MODULES = "CXX_MODULES"_s; |
81 | | |
82 | | namespace { |
83 | | std::map<cm::string_view, FileSetDescriptor> const FileSetDescriptors{ |
84 | | { cm::FileSetMetadata::HEADERS, |
85 | | { cm::FileSetMetadata::HEADERS, |
86 | | cm::FileSetMetadata::FileSetLookup::Target, |
87 | | { DependencyMode ::Includables }, |
88 | | DependencyMode ::Includables, |
89 | | FrameworkCompatible::No } }, |
90 | | { cm::FileSetMetadata::SOURCES, |
91 | | { cm::FileSetMetadata::SOURCES, |
92 | | cm::FileSetMetadata::FileSetLookup::Dependencies, |
93 | | { DependencyMode ::IndependentFiles, DependencyMode ::Includables }, |
94 | | DependencyMode ::Includables, |
95 | | FrameworkCompatible::Yes } }, |
96 | | { cm::FileSetMetadata::CXX_MODULES, |
97 | | { cm::FileSetMetadata::CXX_MODULES, |
98 | | cm::FileSetMetadata::FileSetLookup::Target, |
99 | | { DependencyMode ::IndependentFiles }, |
100 | | DependencyMode ::IndependentFiles, |
101 | | FrameworkCompatible::No } }, |
102 | | }; |
103 | | |
104 | | std::vector<cm::string_view> KnownTypes{ HEADERS, SOURCES, CXX_MODULES }; |
105 | | |
106 | | cmsys::RegularExpression const ValidNameRegex("^[a-z0-9][a-zA-Z0-9_]*$"); |
107 | | } |
108 | | |
109 | | cm::optional<FileSetDescriptor> GetFileSetDescriptor(cm::string_view type) |
110 | 0 | { |
111 | 0 | auto it = FileSetDescriptors.find(type); |
112 | 0 | if (it != FileSetDescriptors.end()) { |
113 | 0 | return it->second; |
114 | 0 | } |
115 | 0 | return cm::nullopt; |
116 | 0 | } |
117 | | |
118 | | DependencyMode GetDependencyMode(cm::string_view type) |
119 | 0 | { |
120 | 0 | auto descriptor = GetFileSetDescriptor(type); |
121 | 0 | if (descriptor) { |
122 | 0 | return descriptor->DefaultDependency; |
123 | 0 | } |
124 | 0 | return DependencyMode::Includables; |
125 | 0 | } |
126 | | DependencyMode GetDependencyMode(cm::string_view type, |
127 | | DependencyMode requestedMode) |
128 | 0 | { |
129 | 0 | auto descriptor = GetFileSetDescriptor(type); |
130 | 0 | if (descriptor) { |
131 | | // Select the requested mode or the next-weakest mode that is supported by |
132 | | // the file set type |
133 | 0 | auto mode = descriptor->SupportedDependencies.lower_bound(requestedMode); |
134 | 0 | return mode == descriptor->SupportedDependencies.end() |
135 | 0 | ? descriptor->DefaultDependency |
136 | 0 | : *mode; |
137 | 0 | } |
138 | 0 | return DependencyMode::Includables; |
139 | 0 | } |
140 | | |
141 | | bool IsFrameworkSupported(cm::string_view type) |
142 | 0 | { |
143 | 0 | auto descriptor = GetFileSetDescriptor(type); |
144 | 0 | if (descriptor) { |
145 | 0 | return descriptor->FrameworkSupported == FrameworkCompatible::Yes; |
146 | 0 | } |
147 | 0 | return false; |
148 | 0 | } |
149 | | |
150 | | std::vector<cm::string_view> const& GetKnownTypes() |
151 | 0 | { |
152 | 0 | return KnownTypes; |
153 | 0 | } |
154 | | |
155 | | bool IsKnownType(cm::string_view type) |
156 | 0 | { |
157 | 0 | return cm::contains(GetKnownTypes(), type); |
158 | 0 | } |
159 | | |
160 | | bool IsValidName(cm::string_view name) |
161 | 0 | { |
162 | 0 | cmsys::RegularExpressionMatch match; |
163 | 0 | return ValidNameRegex.find(name.data(), match); |
164 | 0 | } |
165 | | } |
166 | | } |