Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/golden/embld-help.sh
Original file line number Diff line number Diff line change
@@ -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"
24 changes: 17 additions & 7 deletions tools/embld/embld.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
Loading