From 033834a28e983314bd98d1169d751ac166cd7334 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 8 May 2021 17:50:09 +0900 Subject: [PATCH] Add --help --- cmdline.cc | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++- main.cc | 2 +- passes.cc | 4 +- test/help.sh | 10 +++++ 4 files changed, 135 insertions(+), 4 deletions(-) create mode 100755 test/help.sh diff --git a/cmdline.cc b/cmdline.cc index 78e36924..c363215c 100644 --- a/cmdline.cc +++ b/cmdline.cc @@ -4,6 +4,118 @@ #include #include +static const char helpmsg[] = R"(Options: + --help Report usage information + -v, --version Report version information + -(, --start-group Ignored + -), --end-group Ignored + -C DIR, --directory DIR + -E, --export-dynamic + --no-export-dynamic + -F LIBNAME, --filter LIBNAME + -I FILE, --dynamic-linker FILE + --no-dynamic-linker + -L DIR, --library-path DIR + -M, --print-map + -O NUMBER Ignored + -S, --strip-debug + -T FILE, --script FILE + -X, --discard-locals + -e SYMBOL, --entry SYMBOL + -f SHLIB, --auxiliary SHLIB + -h LIBNAME, --soname LIBNAME + -l LIBNAME + -m EMULATION Ignored + -o FILE + -s, --strip-all + -u SYMBOL, --undefined SYMBOL + --Bdynamic + --Bshareable + --Bstatic + --Bsymbolic + --Bsymbolic-functions + --Map + --allow-multiple-definition Ignored + --as-needed + --no-as-needed + --build-id [none,md5,sha1,sha256,uuid,HEXSTRING] + --no-build-id + --chroot DIR + --color-diagnostics Ignored + --compress-debug-sections [none,zlib,zlib-gabi] + --demangle + --no-demangle + --disable-new-dtags + --dynamic-list + --eh-frame-hdr + --eh-frame-hdr + --no-eh-frame-hdr + --enable-new-dtags + --exclude-libs + --execstack + --fatal-warnings Ignored + --no-fatal-warnings Ignored + --fini SYMBOL + --fork + --no-fork + --gc-sections + --no-gc-sections + --gdb-index + --hash-style [sysv,gnu,both] + --icf + --no-icf + --init SYMBOL + --no-undefined + --perf + --pie, --pic-executable + --no-pie, --no-pic-executable + --plugin Ignored + --plugin-opt Ignored + --pop-state + --preload + --print-gc-sections + --no-print-gc-sections + --print-icf-sections + --no-print-icf-sections + --push-state + --quick-exit + --no-quick-exit + --relax + --no-relax + --repro + --rpath DIR + --rpath-link DIR + --shared + --sort-common Ignored + --sort-section Ignored + --spare-dynamic-tags NUMBER + --static + --stats + --sysroot DIR + --thread-count COUNT + --threads + --no-threads + --trace + --version-script FILE + --warn-common + --no-warn-common + --whole-archive + --no-whole-archive + -z now + -z lazy + -z execstack + -z noexecstack + -z relro + -z norelro + -z defs + -z nodefs + -z nodlopen + -z nodelete + -z nocopyreloc + -z initfirst + -z interpose + -z no-undefined)"; + template static std::vector read_response_file(Context &ctx, std::string_view path) { @@ -152,7 +264,7 @@ std::string create_response_file(Context &ctx) { out << "\n"; } - for (i64 i = 0; i < ctx.cmdline_args.size(); i++) { + for (i64 i = 1; i < ctx.cmdline_args.size(); i++) { std::string_view arg = ctx.cmdline_args[i]; if (arg == "-repro" || arg == "--repro") { @@ -237,6 +349,8 @@ template void parse_nonpositional_args(Context &ctx, std::vector &remaining) { std::span args = ctx.cmdline_args; + args = args.subspan(1); + ctx.arg.thread_count = get_default_thread_count(); while (!args.empty()) { @@ -247,6 +361,13 @@ void parse_nonpositional_args(Context &ctx, exit(0); } + if (read_flag(args, "help")) { + SyncOut(ctx) << "Usage: " << ctx.cmdline_args[0] + << " [options] file...\n\n" + << helpmsg; + exit(0); + } + if (read_arg(ctx, args, arg, "o")) { ctx.arg.output = arg; } else if (read_arg(ctx, args, arg, "dynamic-linker") || diff --git a/main.cc b/main.cc index 2463ef81..803a2068 100644 --- a/main.cc +++ b/main.cc @@ -297,7 +297,7 @@ int do_main(int argc, char **argv) { Timer t_all(ctx, "all"); // Parse non-positional command line options - ctx.cmdline_args = expand_response_files(ctx, argv + 1); + ctx.cmdline_args = expand_response_files(ctx, argv); std::vector file_args; parse_nonpositional_args(ctx, file_args); diff --git a/passes.cc b/passes.cc index 9eb063bd..7b84b8ae 100644 --- a/passes.cc +++ b/passes.cc @@ -201,8 +201,8 @@ void convert_common_symbols(Context &ctx) { template static std::string get_cmdline_args(Context &ctx) { std::stringstream ss; - ss << ctx.cmdline_args[0]; - for (i64 i = 1; i < ctx.cmdline_args.size(); i++) + ss << ctx.cmdline_args[1]; + for (i64 i = 2; i < ctx.cmdline_args.size(); i++) ss << " " << ctx.cmdline_args[i]; return ss.str(); } diff --git a/test/help.sh b/test/help.sh new file mode 100755 index 00000000..e1ec6a53 --- /dev/null +++ b/test/help.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -e +cd $(dirname $0) +echo -n "Testing $(basename -s .sh $0) ... " +t=$(pwd)/tmp/$(basename -s .sh $0) +mkdir -p $t + +../mold --help | grep -q Usage + +echo OK