Rewrote command line flag parsing in SkSLMain

The previous command line parsing was extremely limited and did not
allow us to easily add additional flags. This generalizes the parse so
that we are not dependent on fixed numbers of arguments in fixed
positions.

In the future we may be able to use CommandLineFlags, but right now it
doesn't completely support our needs.

Change-Id: I55bdc993e9ea71a85678d11b991dfc69e151b014
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/496098
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
Ethan Nicholas 2022-01-18 13:47:01 -05:00 committed by SkCQ
parent 9f231c3309
commit ecc319f52e

View File

@ -8,6 +8,7 @@
#define SK_OPTS_NS skslc_standalone
#include "include/core/SkGraphics.h"
#include "include/core/SkStream.h"
#include "include/private/SkTOptional.h"
#include "src/core/SkCpu.h"
#include "src/core/SkOpts.h"
#include "src/opts/SkChecksum_opts.h"
@ -256,30 +257,50 @@ static void show_usage() {
"--nosettings: ignore /*#pragma settings*/ comments\n");
}
static bool set_flag(skstd::optional<bool>* flag, const char* name, bool value) {
if (flag->has_value()) {
printf("%s flag was specified multiple times\n", name);
return false;
}
*flag = value;
return true;
}
/**
* Handle a single input.
*/
ResultCode processCommand(std::vector<SkSL::String>& args) {
bool honorSettings = true;
if (args.size() == 4) {
// Handle four-argument case: `skslc in.sksl out.glsl --settings`
const SkSL::String& settingsArg = args[3];
if (settingsArg == "--settings") {
honorSettings = true;
} else if (settingsArg == "--nosettings") {
honorSettings = false;
ResultCode processCommand(const std::vector<SkSL::String>& args) {
skstd::optional<bool> honorSettings;
std::vector<SkSL::String> paths;
for (size_t i = 1; i < args.size(); ++i) {
const SkSL::String& arg = args[i];
if (arg == "--settings") {
if (!set_flag(&honorSettings, "settings", true)) {
return ResultCode::kInputError;
}
} else if (arg == "--nosettings") {
if (!set_flag(&honorSettings, "settings", false)) {
return ResultCode::kInputError;
}
} else if (!arg.starts_with("--")) {
paths.push_back(arg);
} else {
printf("unrecognized flag: %s\n\n", settingsArg.c_str());
show_usage();
return ResultCode::kInputError;
}
} else if (args.size() != 3) {
}
if (paths.size() != 2) {
show_usage();
return ResultCode::kInputError;
}
if (!honorSettings.has_value()) {
honorSettings = true;
}
const SkSL::String& inputPath = paths[0];
const SkSL::String& outputPath = paths[1];
SkSL::ProgramKind kind;
const SkSL::String& inputPath = args[1];
if (inputPath.ends_with(".vert")) {
kind = SkSL::ProgramKind::kVertex;
} else if (inputPath.ends_with(".frag") || inputPath.ends_with(".sksl")) {
@ -308,7 +329,7 @@ ResultCode processCommand(std::vector<SkSL::String>& args) {
auto standaloneCaps = SkSL::ShaderCapsFactory::Standalone();
const SkSL::ShaderCaps* caps = standaloneCaps.get();
std::unique_ptr<SkSL::SkVMDebugTrace> debugTrace;
if (honorSettings) {
if (*honorSettings) {
if (!detect_shader_settings(text, &settings, &caps, &debugTrace)) {
return ResultCode::kInputError;
}
@ -321,8 +342,6 @@ ResultCode processCommand(std::vector<SkSL::String>& args) {
settings.fRTFlipSet = 0;
settings.fRTFlipBinding = 0;
const SkSL::String& outputPath = args[2];
auto emitCompileError = [&](SkSL::FileOutputStream& out, const char* errorText) {
// Overwrite the compiler output, if any, with an error message.
out.close();