diff --git a/tests/golden/embld-help.sh b/tests/golden/embld-help.sh new file mode 100755 index 00000000..036a6078 --- /dev/null +++ b/tests/golden/embld-help.sh @@ -0,0 +1,33 @@ +#!/bin/sh +# `embld --help` (and -h) prints the usage on stdout and exits 0, the way +# `embsim --help` and `embmap --help` do. A bare `embld` with no inputs keeps +# printing the same usage to stderr and exits 2. Each case checks the exit +# status and which stream got the text, not only that the words appear. +set -eu +echo "TEST-MARKER embld-help" + +EMBLD=${EMBLD:-./embld} +[ -x "$EMBLD" ] || { echo "skipped: embld is not built (make embld)"; exit 0; } +out=tests/golden/out/embld-help +rm -rf "$out"; mkdir -p "$out" + +for flag in --help -h; do + rc=0 + "$EMBLD" $flag > "$out/help.out" 2> "$out/help.err" || rc=$? + [ "$rc" -eq 0 ] || { echo "FAIL: $flag exited $rc, want 0"; cat "$out/help.err"; exit 1; } + [ ! -s "$out/help.err" ] || { echo "FAIL: $flag wrote to stderr"; cat "$out/help.err"; exit 1; } + grep -q '^usage: embld ' "$out/help.out" || + { echo "FAIL: $flag printed no usage on stdout"; cat "$out/help.out"; exit 1; } + grep -q 'embld --doctor' "$out/help.out" || + { echo "FAIL: $flag usage lacks the --doctor line"; cat "$out/help.out"; exit 1; } + echo "$flag: usage on stdout, exit 0" +done + +rc=0 +"$EMBLD" > "$out/bare.out" 2> "$out/bare.err" || rc=$? +[ "$rc" -eq 2 ] || { echo "FAIL: bare embld exited $rc, want 2"; exit 1; } +[ ! -s "$out/bare.out" ] || { echo "FAIL: bare embld wrote to stdout"; exit 1; } +grep -q '^usage: embld ' "$out/bare.err" || + { echo "FAIL: bare embld printed no usage on stderr"; cat "$out/bare.err"; exit 1; } +echo "bare embld: usage on stderr, exit 2" +echo "PASS embld-help" diff --git a/tools/embld/embld.c b/tools/embld/embld.c index 32a6b218..bd582646 100644 --- a/tools/embld/embld.c +++ b/tools/embld/embld.c @@ -25,6 +25,19 @@ #include "doctor.h" #include "../../src/embx/embx.h" +/* The usage text, printed to stdout for --help and to stderr when there + * is nothing to link. */ +static void usage(FILE *f) +{ + fprintf(f, "usage: embld [-o OUT] [-e ENTRY] [-Ttext ADDR] [-Tstack ADDR]\n" + " [-T SCRIPT [-L DIR]... [--orphan-handling=place|warn|error]]\n" + " [--gc-sections [--print-gc-sections]] [-Map FILE]\n" + " [--print-memory-usage] [--cmse-implib [--out-implib=FILE]]\n" + " [--embx [--cap NAME]...] INPUT.o|INPUT.a ...\n" + " embld --doctor INPUT.o|INPUT.a ... " + "(why the link fails)\n"); +} + int main(int argc, char **argv) { const char *out = "a.out"; @@ -184,6 +197,9 @@ int main(int argc, char **argv) return 2; } else if (strcmp(argv[i], "--doctor") == 0) { doctor = 1; + } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + usage(stdout); /* asked for, so stdout and success */ + return 0; } else if (argv[i][0] == '-') { fprintf(stderr, "embld: unknown option '%s'\n", argv[i]); return 2; @@ -193,13 +209,7 @@ int main(int argc, char **argv) } } if (!ninputs) { - fprintf(stderr, "usage: embld [-o OUT] [-e ENTRY] [-Ttext ADDR] [-Tstack ADDR]\n" - " [-T SCRIPT [-L DIR]... [--orphan-handling=place|warn|error]]\n" - " [--gc-sections [--print-gc-sections]] [-Map FILE]\n" - " [--print-memory-usage] [--cmse-implib [--out-implib=FILE]]\n" - " [--embx [--cap NAME]...] INPUT.o|INPUT.a ...\n" - " embld --doctor INPUT.o|INPUT.a ... " - "(why the link fails)\n"); + usage(stderr); /* a bare invocation is a mistake */ return 2; } /* each -l, now that every -L is known: libNAME.a (or, for -l:FILE,