Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
switches.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <algorithm>
8#include <cctype>
9#include <filesystem>
10#include <map>
11
12#include "flutter/fml/file.h"
13#include "fml/command_line.h"
16
17namespace impeller {
18namespace compiler {
19
20static const std::map<std::string, TargetPlatform> kKnownPlatforms = {
21 {"metal-desktop", TargetPlatform::kMetalDesktop},
22 {"metal-ios", TargetPlatform::kMetalIOS},
23 {"vulkan", TargetPlatform::kVulkan},
24 {"opengl-es", TargetPlatform::kOpenGLES},
25 {"opengl-desktop", TargetPlatform::kOpenGLDesktop},
26};
27
28static const std::map<std::string, TargetPlatform> kKnownRuntimeStages = {
29 {"sksl", TargetPlatform::kSkSL},
30 {"runtime-stage-metal", TargetPlatform::kRuntimeStageMetal},
31 {"runtime-stage-gles", TargetPlatform::kRuntimeStageGLES},
32 {"runtime-stage-gles3", TargetPlatform::kRuntimeStageGLES3},
33 {"runtime-stage-vulkan", TargetPlatform::kRuntimeStageVulkan},
34};
35
36static const std::map<std::string, SourceType> kKnownSourceTypes = {
40};
41
42void Switches::PrintHelp(std::ostream& stream) {
43 // clang-format off
44 const std::string optional_prefix = "[optional] ";
45 const std::string optional_multiple_prefix = "[optional,multiple] ";
46 // clang-format on
47
48 stream << std::endl;
49 stream << "ImpellerC is an offline shader processor and reflection engine."
50 << std::endl;
51 stream << "---------------------------------------------------------------"
52 << std::endl;
53 stream << "Expected invocation is:" << std::endl << std::endl;
54 stream << "./impellerc <One platform or multiple runtime stages> "
55 "--input=<source_file> --sl=<sl_output_file> <optional arguments>"
56 << std::endl
57 << std::endl;
58
59 stream << "Valid platforms are:" << std::endl << std::endl;
60 stream << "One of [";
61 for (const auto& platform : kKnownPlatforms) {
62 stream << " --" << platform.first;
63 }
64 stream << " ]" << std::endl << std::endl;
65
66 stream << "Valid runtime stages are:" << std::endl << std::endl;
67 stream << "At least one of [";
68 for (const auto& platform : kKnownRuntimeStages) {
69 stream << " --" << platform.first;
70 }
71 stream << " ]" << std::endl << std::endl;
72
73 stream << "Optional arguments:" << std::endl << std::endl;
74 stream << optional_prefix
75 << "--spirv=<spirv_output_file> (ignored for --shader-bundle)"
76 << std::endl;
77 stream << optional_prefix << "--input-type={";
78 for (const auto& source_type : kKnownSourceTypes) {
79 stream << source_type.first << ", ";
80 }
81 stream << "}" << std::endl;
82 stream << optional_prefix << "--source-language=glsl|hlsl (default: glsl)"
83 << std::endl;
84 stream << optional_prefix
85 << "--entry-point=<entry_point_name> (default: main; "
86 "ignored for glsl)"
87 << std::endl;
88 stream << optional_prefix
89 << "--entry-point-prefix=<entry_point_prefix> (default: empty)"
90 << std::endl;
91 stream << optional_prefix
92 << "--iplr (causes --sl file to be emitted in "
93 "iplr format)"
94 << std::endl;
95 stream << optional_prefix
96 << "--shader-bundle=<bundle_spec> (causes --sl "
97 "file to be "
98 "emitted in Flutter GPU's shader bundle format)"
99 << std::endl;
100 stream << optional_prefix << "--reflection-json=<reflection_json_file>"
101 << std::endl;
102 stream << optional_prefix << "--reflection-header=<reflection_header_file>"
103 << std::endl;
104 stream << optional_prefix << "--reflection-cc=<reflection_cc_file>"
105 << std::endl;
106 stream << optional_multiple_prefix << "--include=<include_directory>"
107 << std::endl;
108 stream << optional_multiple_prefix << "--define=<define>" << std::endl;
109 stream << optional_prefix << "--depfile=<depfile_path>" << std::endl;
110 stream << optional_prefix << "--gles-language-version=<number>" << std::endl;
111 stream << optional_prefix << "--json" << std::endl;
112 stream << optional_prefix
113 << "--use-half-textures (force openGL semantics when "
114 "targeting metal)"
115 << std::endl;
116 stream << optional_prefix << "--require-framebuffer-fetch" << std::endl;
117}
118
119Switches::Switches() = default;
120
121Switches::~Switches() = default;
122
124 const fml::CommandLine& command_line) {
126 for (const auto& platform : kKnownPlatforms) {
127 if (command_line.HasOption(platform.first)) {
128 // If the platform has already been determined, the caller may have
129 // specified multiple platforms. This is an error and only one must be
130 // selected.
133 }
134 target = platform.second;
135 // Keep going to detect duplicates.
136 }
137 }
138 return target;
139}
140
141static std::vector<TargetPlatform> RuntimeStagesFromCommandLine(
142 const fml::CommandLine& command_line) {
143 std::vector<TargetPlatform> stages;
144 for (const auto& platform : kKnownRuntimeStages) {
145 if (command_line.HasOption(platform.first)) {
146 stages.push_back(platform.second);
147 }
148 }
149 return stages;
150}
151
153 const fml::CommandLine& command_line) {
154 auto source_type_option =
155 command_line.GetOptionValueWithDefault("input-type", "");
156 auto source_type_search = kKnownSourceTypes.find(source_type_option);
157 if (source_type_search == kKnownSourceTypes.end()) {
159 }
160 return source_type_search->second;
161}
162
163// Get the value of a command line option as a filesystem path. The option
164// value string must be encoded in UTF-8.
165static std::filesystem::path GetOptionAsPath(
166 const fml::CommandLine& command_line,
167 const char* arg) {
168 std::string value = command_line.GetOptionValueWithDefault(arg, "");
169 return std::filesystem::path(std::u8string(value.begin(), value.end()));
170}
171
173 : working_directory(std::make_shared<fml::UniqueFD>(fml::OpenDirectory(
174 Utf8FromPath(std::filesystem::current_path()).c_str(),
175 false, // create if necessary,
176 fml::FilePermission::kRead))),
177 source_file_name(GetOptionAsPath(command_line, "input")),
178 input_type(SourceTypeFromCommandLine(command_line)),
179 sl_file_name(GetOptionAsPath(command_line, "sl")),
180 iplr(command_line.HasOption("iplr")),
181 shader_bundle(
182 command_line.GetOptionValueWithDefault("shader-bundle", "")),
183 spirv_file_name(GetOptionAsPath(command_line, "spirv")),
184 reflection_json_name(GetOptionAsPath(command_line, "reflection-json")),
185 reflection_header_name(
186 GetOptionAsPath(command_line, "reflection-header")),
187 reflection_cc_name(GetOptionAsPath(command_line, "reflection-cc")),
188 depfile_path(GetOptionAsPath(command_line, "depfile")),
189 json_format(command_line.HasOption("json")),
190 gles_language_version(
191 stoi(command_line.GetOptionValueWithDefault("gles-language-version",
192 "0"))),
193 metal_version(
194 command_line.GetOptionValueWithDefault("metal-version", "1.2")),
195 entry_point(
196 command_line.GetOptionValueWithDefault("entry-point", "main")),
197 entry_point_prefix(
198 command_line.GetOptionValueWithDefault("entry-point-prefix", "")),
199 use_half_textures(command_line.HasOption("use-half-textures")),
200 require_framebuffer_fetch(
201 command_line.HasOption("require-framebuffer-fetch")),
202 target_platform_(TargetPlatformFromCommandLine(command_line)),
203 runtime_stages_(RuntimeStagesFromCommandLine(command_line)) {
204 auto language = ToLowerCase(
205 command_line.GetOptionValueWithDefault("source-language", "glsl"));
206
208
209 if (!working_directory || !working_directory->is_valid()) {
210 return;
211 }
212
213 for (const auto& include_dir_path : command_line.GetOptionValues("include")) {
214 if (!include_dir_path.data()) {
215 continue;
216 }
217
218 // fml::OpenDirectoryReadOnly for Windows doesn't handle relative paths
219 // beginning with `../` well, so we build an absolute path.
220
221 // Get the current working directory as a utf8 encoded string.
222 // Note that the `include_dir_path` is already utf8 encoded, and so we
223 // mustn't attempt to double-convert it to utf8 lest multi-byte characters
224 // will become mangled.
225 std::filesystem::path include_dir_absolute;
226 if (std::filesystem::path(include_dir_path).is_absolute()) {
227 include_dir_absolute = std::filesystem::path(include_dir_path);
228 } else {
229 auto cwd = Utf8FromPath(std::filesystem::current_path());
230 include_dir_absolute = std::filesystem::absolute(
231 std::filesystem::path(cwd) / include_dir_path);
232 }
233
234 auto dir = std::make_shared<fml::UniqueFD>(fml::OpenDirectoryReadOnly(
235 *working_directory, include_dir_absolute.string().c_str()));
236 if (!dir || !dir->is_valid()) {
237 continue;
238 }
239
240 IncludeDir dir_entry;
241 dir_entry.name = include_dir_path;
242 dir_entry.dir = std::move(dir);
243
244 include_directories.emplace_back(std::move(dir_entry));
245 }
246
247 for (const auto& define : command_line.GetOptionValues("define")) {
248 defines.emplace_back(define);
249 }
250}
251
252bool Switches::AreValid(std::ostream& explain) const {
253 // When producing a shader bundle, all flags related to single shader inputs
254 // and outputs such as `--input` and `--spirv-file-name` are ignored. Instead,
255 // input files are read from the shader bundle spec and a single flatbuffer
256 // containing all compiled shaders and reflection state is output to `--sl`.
257 const bool shader_bundle_mode = !shader_bundle.empty();
258
259 bool valid = true;
260 if (target_platform_ == TargetPlatform::kUnknown && runtime_stages_.empty() &&
261 !shader_bundle_mode) {
262 explain << "Either a target platform was not specified, or no runtime "
263 "stages were specified."
264 << std::endl;
265 valid = false;
266 }
267
268 if (source_language == SourceLanguage::kUnknown && !shader_bundle_mode) {
269 explain << "Invalid source language type." << std::endl;
270 valid = false;
271 }
272
273 if (!working_directory || !working_directory->is_valid()) {
274 explain << "Could not open the working directory: \""
275 << Utf8FromPath(std::filesystem::current_path()).c_str() << "\""
276 << std::endl;
277 valid = false;
278 }
279
280 if (source_file_name.empty() && !shader_bundle_mode) {
281 explain << "Input file name was empty." << std::endl;
282 valid = false;
283 }
284
285 if (sl_file_name.empty()) {
286 explain << "Target shading language file name was empty." << std::endl;
287 valid = false;
288 }
289
290 if (spirv_file_name.empty() && !shader_bundle_mode) {
291 explain << "Spirv file name was empty." << std::endl;
292 valid = false;
293 }
294
295 if (iplr && shader_bundle_mode) {
296 explain << "--iplr and --shader-bundle flag cannot be specified at the "
297 "same time"
298 << std::endl;
299 valid = false;
300 }
301
302 return valid;
303}
304
305std::vector<TargetPlatform> Switches::PlatformsToCompile() const {
306 if (target_platform_ == TargetPlatform::kUnknown) {
307 return runtime_stages_;
308 }
309 return {target_platform_};
310}
311
313 if (target_platform_ == TargetPlatform::kUnknown &&
314 !runtime_stages_.empty()) {
315 return runtime_stages_.front();
316 }
317 return target_platform_;
318}
319
321 std::optional<TargetPlatform> target_platform) const {
322 SourceOptions options;
323 options.target_platform =
324 target_platform.value_or(SelectDefaultTargetPlatform());
328 } else {
329 options.type = input_type;
330 }
332 options.file_name = source_file_name;
334 options.defines = defines;
335 options.entry_point_name =
339 options.json_format = json_format;
344 return options;
345}
346
347} // namespace compiler
348} // namespace impeller
std::vector< std::string_view > GetOptionValues(std::string_view name) const
std::string GetOptionValueWithDefault(std::string_view name, std::string_view default_value) const
bool HasOption(std::string_view name, size_t *index=nullptr) const
std::vector< TargetPlatform > PlatformsToCompile() const
A vector containing at least one valid platform.
Definition switches.cc:305
std::string entry_point_prefix
Definition switches.h:45
std::filesystem::path sl_file_name
Definition switches.h:31
SourceLanguage source_language
Definition switches.h:41
std::vector< std::string > defines
Definition switches.h:39
std::shared_ptr< fml::UniqueFD > working_directory
Definition switches.h:24
SourceOptions CreateSourceOptions(std::optional< TargetPlatform > target_platform=std::nullopt) const
Definition switches.cc:320
bool AreValid(std::ostream &explain) const
Definition switches.cc:252
static void PrintHelp(std::ostream &stream)
Definition switches.cc:42
std::filesystem::path spirv_file_name
Definition switches.h:34
std::vector< IncludeDir > include_directories
Definition switches.h:25
std::filesystem::path source_file_name
Definition switches.h:26
TargetPlatform SelectDefaultTargetPlatform() const
Definition switches.cc:312
int32_t value
uint32_t * target
fml::UniqueFD OpenDirectoryReadOnly(const fml::UniqueFD &base_directory, const char *path)
Definition file.cc:97
std::string ToLowerCase(std::string_view string)
Definition utilities.cc:61
static std::vector< TargetPlatform > RuntimeStagesFromCommandLine(const fml::CommandLine &command_line)
Definition switches.cc:141
static const std::map< std::string, TargetPlatform > kKnownPlatforms
Definition switches.cc:20
SourceType SourceTypeFromFileName(const std::filesystem::path &file_name)
Definition types.cc:17
static std::filesystem::path GetOptionAsPath(const fml::CommandLine &command_line, const char *arg)
Definition switches.cc:165
std::string EntryPointFunctionNameFromSourceName(const std::filesystem::path &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition types.cc:101
static const std::map< std::string, TargetPlatform > kKnownRuntimeStages
Definition switches.cc:28
std::string Utf8FromPath(const std::filesystem::path &path)
Converts a native format path to a utf8 string.
Definition utilities.cc:30
static TargetPlatform TargetPlatformFromCommandLine(const fml::CommandLine &command_line)
Definition switches.cc:123
SourceLanguage ToSourceLanguage(const std::string &source_language)
Definition types.cc:52
static const std::map< std::string, SourceType > kKnownSourceTypes
Definition switches.cc:36
static SourceType SourceTypeFromCommandLine(const fml::CommandLine &command_line)
Definition switches.cc:152
Definition ref_ptr.h:261
std::shared_ptr< fml::UniqueFD > dir
Definition include_dir.h:17
bool use_half_textures
Whether half-precision textures should be supported, requiring opengl semantics. Only used on metal t...
std::vector< IncludeDir > include_dirs
std::filesystem::path file_name
bool require_framebuffer_fetch
Whether the GLSL framebuffer fetch extension will be required.
std::shared_ptr< fml::UniqueFD > working_directory
std::vector< std::string > defines