mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Use colored/styled text output for diagnostics and usage info (#1775)
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "itertools.hpp"
|
||||
#include "platform.hpp"
|
||||
#include "script.hpp" // Generated from script.y
|
||||
#include "style.hpp"
|
||||
#include "usage.hpp"
|
||||
#include "util.hpp" // UpperMap, printChar
|
||||
#include "verbosity.hpp"
|
||||
@@ -37,6 +38,9 @@ static char const *linkerScriptName = nullptr; // -l
|
||||
// Short options
|
||||
static char const *optstring = "dhl:m:Mn:O:o:p:S:tVvW:wx";
|
||||
|
||||
// Variables for the long-only options
|
||||
static int longOpt; // `--color`
|
||||
|
||||
// Equivalent long options
|
||||
// Please keep in the same order as short opts.
|
||||
// Also, make sure long opts don't create ambiguity:
|
||||
@@ -45,42 +49,44 @@ static char const *optstring = "dhl:m:Mn:O:o:p:S:tVvW:wx";
|
||||
// This is because long opt matching, even to a single char, is prioritized
|
||||
// over short opt matching.
|
||||
static option const longopts[] = {
|
||||
{"dmg", no_argument, nullptr, 'd'},
|
||||
{"help", no_argument, nullptr, 'h'},
|
||||
{"linkerscript", required_argument, nullptr, 'l'},
|
||||
{"map", required_argument, nullptr, 'm'},
|
||||
{"no-sym-in-map", no_argument, nullptr, 'M'},
|
||||
{"sym", required_argument, nullptr, 'n'},
|
||||
{"overlay", required_argument, nullptr, 'O'},
|
||||
{"output", required_argument, nullptr, 'o'},
|
||||
{"pad", required_argument, nullptr, 'p'},
|
||||
{"scramble", required_argument, nullptr, 'S'},
|
||||
{"tiny", no_argument, nullptr, 't'},
|
||||
{"version", no_argument, nullptr, 'V'},
|
||||
{"verbose", no_argument, nullptr, 'v'},
|
||||
{"warning", required_argument, nullptr, 'W'},
|
||||
{"wramx", no_argument, nullptr, 'w'},
|
||||
{"nopad", no_argument, nullptr, 'x'},
|
||||
{nullptr, no_argument, nullptr, 0 }
|
||||
{"dmg", no_argument, nullptr, 'd'},
|
||||
{"help", no_argument, nullptr, 'h'},
|
||||
{"linkerscript", required_argument, nullptr, 'l'},
|
||||
{"map", required_argument, nullptr, 'm'},
|
||||
{"no-sym-in-map", no_argument, nullptr, 'M'},
|
||||
{"sym", required_argument, nullptr, 'n'},
|
||||
{"overlay", required_argument, nullptr, 'O'},
|
||||
{"output", required_argument, nullptr, 'o'},
|
||||
{"pad", required_argument, nullptr, 'p'},
|
||||
{"scramble", required_argument, nullptr, 'S'},
|
||||
{"tiny", no_argument, nullptr, 't'},
|
||||
{"version", no_argument, nullptr, 'V'},
|
||||
{"verbose", no_argument, nullptr, 'v'},
|
||||
{"warning", required_argument, nullptr, 'W'},
|
||||
{"wramx", no_argument, nullptr, 'w'},
|
||||
{"nopad", no_argument, nullptr, 'x'},
|
||||
{"color", required_argument, &longOpt, 'c'},
|
||||
{nullptr, no_argument, nullptr, 0 },
|
||||
};
|
||||
|
||||
// clang-format off: long string literal
|
||||
static Usage usage(
|
||||
"Usage: rgblink [-dhMtVvwx] [-l script] [-m map_file] [-n sym_file]\n"
|
||||
" [-O overlay_file] [-o out_file] [-p pad_value]\n"
|
||||
" [-S spec] <file> ...\n"
|
||||
"Useful options:\n"
|
||||
" -l, --linkerscript <path> set the input linker script\n"
|
||||
" -m, --map <path> set the output map file\n"
|
||||
" -n, --sym <path> set the output symbol list file\n"
|
||||
" -o, --output <path> set the output file\n"
|
||||
" -p, --pad <value> set the value to pad between sections with\n"
|
||||
" -x, --nopad disable padding of output binary\n"
|
||||
" -V, --version print RGBLINK version and exit\n"
|
||||
" -W, --warning <warning> enable or disable warnings\n"
|
||||
"\n"
|
||||
"For help, use `man rgblink' or go to https://rgbds.gbdev.io/docs/\n"
|
||||
);
|
||||
// clang-format off: nested initializers
|
||||
static Usage usage = {
|
||||
.name = "rgblink",
|
||||
.flags = {
|
||||
"[-dhMtVvwx]", "[-l script]", "[-m map_file]", "[-n sym_file]", "[-O overlay_file]",
|
||||
"[-o out_file]", "[-p pad_value]", "[-S spec]", "<file> ...",
|
||||
},
|
||||
.options = {
|
||||
{{"-l", "--linkerscript <path>"}, {"set the input linker script"}},
|
||||
{{"-m", "--map <path>"}, {"set the output map file"}},
|
||||
{{"-n", "--sym <path>"}, {"set the output symbol list file"}},
|
||||
{{"-o", "--output <path>"}, {"set the output file"}},
|
||||
{{"-p", "--pad <value>"}, {"set the value to pad between sections with"}},
|
||||
{{"-x", "--nopad"}, {"disable padding of output binary"}},
|
||||
{{"-V", "--version"}, {"print RGBLINK version and exit"}},
|
||||
{{"-W", "--warning <warning>"}, {"enable or disable warnings"}},
|
||||
},
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
// LCOV_EXCL_START
|
||||
@@ -174,21 +180,29 @@ std::string const &FileStackNode::dump(uint32_t curLineNo) const {
|
||||
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
|
||||
assume(parent); // REPT nodes use their parent's name
|
||||
std::string const &lastName = parent->dump(lineNo);
|
||||
style_Set(stderr, STYLE_CYAN, false);
|
||||
fputs(" -> ", stderr);
|
||||
style_Set(stderr, STYLE_CYAN, true);
|
||||
fputs(lastName.c_str(), stderr);
|
||||
for (uint32_t iter : iters()) {
|
||||
fprintf(stderr, "::REPT~%" PRIu32, iter);
|
||||
}
|
||||
style_Set(stderr, STYLE_CYAN, false);
|
||||
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
||||
style_Reset(stderr);
|
||||
return lastName;
|
||||
} else {
|
||||
if (parent) {
|
||||
parent->dump(lineNo);
|
||||
style_Set(stderr, STYLE_CYAN, false);
|
||||
fputs(" -> ", stderr);
|
||||
}
|
||||
std::string const &nodeName = name();
|
||||
style_Set(stderr, STYLE_CYAN, true);
|
||||
fputs(nodeName.c_str(), stderr);
|
||||
style_Set(stderr, STYLE_CYAN, false);
|
||||
fprintf(stderr, "(%" PRIu32 ")", curLineNo);
|
||||
style_Reset(stderr);
|
||||
return nodeName;
|
||||
}
|
||||
}
|
||||
@@ -388,6 +402,17 @@ int main(int argc, char *argv[]) {
|
||||
// implies tiny mode
|
||||
options.is32kMode = true;
|
||||
break;
|
||||
case 0: // Long-only options
|
||||
if (longOpt == 'c') {
|
||||
if (!strcasecmp(musl_optarg, "always")) {
|
||||
style_Enable(true);
|
||||
} else if (!strcasecmp(musl_optarg, "never")) {
|
||||
style_Enable(false);
|
||||
} else if (strcasecmp(musl_optarg, "auto")) {
|
||||
fatal("Invalid argument for option '--color'");
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
usage.printAndExit(1); // LCOV_EXCL_LINE
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "style.hpp"
|
||||
|
||||
#include "link/main.hpp"
|
||||
|
||||
// clang-format off: nested initializers
|
||||
@@ -33,24 +35,29 @@ static void printDiag(
|
||||
char const *fmt,
|
||||
va_list args,
|
||||
char const *type,
|
||||
StyleColor color,
|
||||
char const *flagfmt,
|
||||
char const *flag
|
||||
) {
|
||||
style_Set(stderr, color, true);
|
||||
fprintf(stderr, "%s: ", type);
|
||||
if (src) {
|
||||
src->dump(lineNo);
|
||||
fputs(": ", stderr);
|
||||
}
|
||||
if (flagfmt) {
|
||||
style_Set(stderr, color, true);
|
||||
fprintf(stderr, flagfmt, flag);
|
||||
fputs("\n ", stderr);
|
||||
}
|
||||
style_Reset(stderr);
|
||||
vfprintf(stderr, fmt, args);
|
||||
putc('\n', stderr);
|
||||
}
|
||||
|
||||
[[noreturn]]
|
||||
static void abortLinking(char const *verb) {
|
||||
style_Set(stderr, STYLE_RED, true);
|
||||
fprintf(
|
||||
stderr,
|
||||
"Linking %s with %" PRIu64 " error%s\n",
|
||||
@@ -58,27 +65,28 @@ static void abortLinking(char const *verb) {
|
||||
warnings.nbErrors,
|
||||
warnings.nbErrors == 1 ? "" : "s"
|
||||
);
|
||||
style_Reset(stderr);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void warning(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printDiag(src, lineNo, fmt, args, "warning", nullptr, 0);
|
||||
printDiag(src, lineNo, fmt, args, "warning", STYLE_YELLOW, nullptr, nullptr);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void warning(char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printDiag(nullptr, 0, fmt, args, "warning", nullptr, 0);
|
||||
printDiag(nullptr, 0, fmt, args, "warning", STYLE_YELLOW, nullptr, nullptr);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
void error(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printDiag(src, lineNo, fmt, args, "error", nullptr, 0);
|
||||
printDiag(src, lineNo, fmt, args, "error", STYLE_RED, nullptr, nullptr);
|
||||
va_end(args);
|
||||
|
||||
warnings.incrementErrors();
|
||||
@@ -87,7 +95,7 @@ void error(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) {
|
||||
void error(char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printDiag(nullptr, 0, fmt, args, "error", nullptr, 0);
|
||||
printDiag(nullptr, 0, fmt, args, "error", STYLE_RED, nullptr, nullptr);
|
||||
va_end(args);
|
||||
|
||||
warnings.incrementErrors();
|
||||
@@ -95,7 +103,9 @@ void error(char const *fmt, ...) {
|
||||
|
||||
void errorNoDump(char const *fmt, ...) {
|
||||
va_list args;
|
||||
style_Set(stderr, STYLE_RED, true);
|
||||
fputs("error: ", stderr);
|
||||
style_Reset(stderr);
|
||||
va_start(args, fmt);
|
||||
vfprintf(stderr, fmt, args);
|
||||
va_end(args);
|
||||
@@ -104,7 +114,13 @@ void errorNoDump(char const *fmt, ...) {
|
||||
}
|
||||
|
||||
void scriptError(char const *name, uint32_t lineNo, char const *fmt, va_list args) {
|
||||
fprintf(stderr, "error: %s(%" PRIu32 "): ", name, lineNo);
|
||||
style_Set(stderr, STYLE_RED, true);
|
||||
fputs("error: ", stderr);
|
||||
style_Set(stderr, STYLE_CYAN, true);
|
||||
fputs(name, stderr);
|
||||
style_Set(stderr, STYLE_CYAN, false);
|
||||
fprintf(stderr, "(%" PRIu32 "): ", lineNo);
|
||||
style_Reset(stderr);
|
||||
vfprintf(stderr, fmt, args);
|
||||
putc('\n', stderr);
|
||||
|
||||
@@ -115,7 +131,7 @@ void scriptError(char const *name, uint32_t lineNo, char const *fmt, va_list arg
|
||||
void fatal(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printDiag(src, lineNo, fmt, args, "FATAL", nullptr, 0);
|
||||
printDiag(src, lineNo, fmt, args, "FATAL", STYLE_RED, nullptr, nullptr);
|
||||
va_end(args);
|
||||
|
||||
warnings.incrementErrors();
|
||||
@@ -126,7 +142,7 @@ void fatal(FileStackNode const *src, uint32_t lineNo, char const *fmt, ...) {
|
||||
void fatal(char const *fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
printDiag(nullptr, 0, fmt, args, "FATAL", nullptr, 0);
|
||||
printDiag(nullptr, 0, fmt, args, "FATAL", STYLE_RED, nullptr, nullptr);
|
||||
va_end(args);
|
||||
|
||||
warnings.incrementErrors();
|
||||
@@ -150,11 +166,11 @@ void warning(FileStackNode const *src, uint32_t lineNo, WarningID id, char const
|
||||
break;
|
||||
|
||||
case WarningBehavior::ENABLED:
|
||||
printDiag(src, lineNo, fmt, args, "warning", "[-W%s]", flag);
|
||||
printDiag(src, lineNo, fmt, args, "warning", STYLE_RED, "[-W%s]", flag);
|
||||
break;
|
||||
|
||||
case WarningBehavior::ERROR:
|
||||
printDiag(src, lineNo, fmt, args, "error", "[-Werror=%s]", flag);
|
||||
printDiag(src, lineNo, fmt, args, "error", STYLE_YELLOW, "[-Werror=%s]", flag);
|
||||
|
||||
warnings.incrementErrors();
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user