From 63ec42f72f61a59eb1bbeab2f81ce5bc65945b47 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Tue, 14 Aug 2018 00:27:55 +0100 Subject: [PATCH] common: factor out common opts to all scripts --- build | 31 ++++++------------------------- common | 38 ++++++++++++++++++++++++++++++++++++++ gem5-bench-cache | 10 +++++----- gem5-shell | 10 ++-------- gem5-stat | 8 ++++---- getvar | 28 ++-------------------------- qemu-trace2txt | 6 +++--- run | 28 ++-------------------------- rungdb | 16 ++-------------- rungdb-user | 19 +++++++------------ rungdbserver | 9 +++------ runtc | 8 ++++---- trace-boot | 9 +++------ trace2line | 9 +++------ 14 files changed, 84 insertions(+), 145 deletions(-) diff --git a/build b/build index dce0f45..69c78d2 100755 --- a/build +++ b/build @@ -18,11 +18,8 @@ kernel_config_fragments= post_script_args= qemu_sdl='--enable-sdl --with-sdlabi=2.0' v=0 -while getopts 'a:B:b:C:c:fGgj:hIiK:kL:lM:p:Q:qSs:t:v' OPT; do +while getopts "B:b:C:c:fGj:hIiK:klp:qSs:v${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; B) echo "$OPTARG" >> "$br2_cli_file" ;; @@ -38,11 +35,6 @@ while getopts 'a:B:b:C:c:fGgj:hIiK:kL:lM:p:Q:qSs:t:v' OPT; do f) configure=false ;; - g) - extra_make_args="${extra_make_args} gem5-reconfigure \\ -" - common_gem5=true - ;; h) echo "https://github.com/cirosantilli/linux-kernel-module-cheat#build" 2>&1 exit @@ -71,23 +63,14 @@ BR2_TARGET_ROOTFS_INITRAMFS=n extra_make_args="${extra_make_args} kernel_module-reconfigure \\ " ;; - L) - common_linux_variant="$OPTARG" - ;; l) linux_reconfigure=true extra_make_args="${extra_make_args} linux-reconfigure \\ " ;; - M) - common_gem5_variant="$OPTARG" - ;; p) post_script_args="$OPTARG" ;; - Q) - common_qemu_variant="$OPTARG" - ;; q) extra_make_args="${extra_make_args} host-qemu-reconfigure \\ " @@ -95,21 +78,19 @@ BR2_TARGET_ROOTFS_INITRAMFS=n S) qemu_sdl= ;; - s) - common_suffix="$OPTARG" - ;; - t) - common_gem5_build_type="$OPTARG" - ;; v) v=1 ;; ?) - exit 2 + common_getopts_case "$OPT" ;; esac done shift $(($OPTIND - 1)) +if "$common_gem5"; then + extra_make_args="${extra_make_args} gem5-reconfigure \\ +" +fi extra_make_args="${extra_make_args} $@" common_setup config_file="${common_buildroot_out_dir}/.config" diff --git a/common b/common index 06476ae..c8d1d62 100644 --- a/common +++ b/common @@ -24,6 +24,44 @@ common_bench_cmd() ( printf "exit_status $?\n" >> "$results_file" ) + +# Handle options common across multiple scripts. +common_getopts_case() { + case "$1" in + a) + common_arch="$OPTARG" + ;; + g) + common_gem5=true + ;; + L) + common_linux_variant="$OPTARG" + ;; + M) + common_gem5_variant="$OPTARG" + ;; + N) + common_gem5_worktree="$OPTARG" + ;; + n) + common_run_id="$OPTARG" + ;; + Q) + common_qemu_variant="$OPTARG" + ;; + s) + common_suffix="$OPTARG" + ;; + t) + common_gem5_build_type="$OPTARG" + ;; + ?) + exit 2 + ;; + esac +} +common_getopts_flags='a:gL:M:N:n:Q:s:t:' + # Setup several variables and do other initialization common to most scripts. # Typically done after getting inputs from the command line arguments. common_setup() { diff --git a/gem5-bench-cache b/gem5-bench-cache index e7e42c7..b9414cd 100755 --- a/gem5-bench-cache +++ b/gem5-bench-cache @@ -2,20 +2,20 @@ . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" common_gem5=true generate_checkpoints=true -while getopts a:C OPT; do +while getopts "C${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; C) generate_checkpoints=false ;; + ?) + common_getopts_case "$OPT" + ;; esac done shift "$(($OPTIND - 1))" +common_setup # Vars -common_setup cmd="./run -a ${common_arch} -g" cache_small='--caches --l2cache --l1d_size=1024 --l1i_size=1024 --l2_size=1024 --l3_size=1024 ' cache_large='--caches --l2cache --l1d_size=1024kB --l1i_size=1024kB --l2_size=1024kB --l3_size=1024kB' diff --git a/gem5-shell b/gem5-shell index fce386c..2a94a5d 100755 --- a/gem5-shell +++ b/gem5-shell @@ -1,16 +1,10 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" common_gem5=true -while getopts M:n: OPT; do +while getopts "${common_getopts_flags}" OPT; do case "$OPT" in - M) - common_gem5_variant="$OPTARG" - ;; - n) - common_run_id="$OPTARG" - ;; ?) - exit 2 + common_getopts_case "$OPT" ;; esac done diff --git a/gem5-stat b/gem5-stat index 59106d0..6ed64f3 100755 --- a/gem5-stat +++ b/gem5-stat @@ -1,11 +1,8 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" common_gem5=true -while getopts a:hs: OPT; do +while getopts "h${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; h) printf "\ usage: $0 [-a arch] [stat=system.cpu.numCycles] @@ -13,6 +10,9 @@ Get the value for a gem5 stat from the stats.txt file. " 1>&2 exit ;; + ?) + common_getopts_case "$OPT" + ;; esac done shift "$(($OPTIND - 1))" diff --git a/getvar b/getvar index 5d64d3c..cad9bb5 100755 --- a/getvar +++ b/getvar @@ -1,37 +1,13 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" -while getopts ha:gL:M:N:n:Q:t: OPT; do +while getopts "h${common_getopts_flags}" OPT; do case "$OPT" in h) echo "https://github.com/cirosantilli/linux-kernel-module-cheat#getvar" 2>&1 exit ;; - a) - common_arch="$OPTARG" - ;; - g) - common_gem5=true - ;; - L) - common_linux_variant="$OPTARG" - ;; - M) - common_gem5_variant="$OPTARG" - ;; - N) - common_gem5_worktree="$OPTARG" - ;; - n) - common_run_id="$OPTARG" - ;; - Q) - common_qemu_variant="$OPTARG" - ;; - t) - common_gem5_build_type="$OPTARG" - ;; ?) - exit 2 + common_getopts_case "$OPT" ;; esac done diff --git a/qemu-trace2txt b/qemu-trace2txt index 5c691f9..2a23e8e 100755 --- a/qemu-trace2txt +++ b/qemu-trace2txt @@ -1,9 +1,9 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" -while getopts a: OPT; do +while getopts "${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" + ?) + common_getopts_case "$OPT" ;; esac done diff --git a/run b/run index 9b34426..f0abedd 100755 --- a/run +++ b/run @@ -34,11 +34,8 @@ trace_enabled=false # just to prevent QEMU from emitting a warning that '' is not valid. trace_type=pr_manager_run vnc= -while getopts a:c:DdE:e:F:f:G:ghIiKkL:l:M:m:N:n:PQ:RrT:t:U:uVX:x OPT; do +while getopts "c:DdE:e:F:f:G:hIiKkl:m:PRrT:U:uVX:x${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; c) cpus="$OPTARG" ;; @@ -67,9 +64,6 @@ while getopts a:c:DdE:e:F:f:G:ghIiKkL:l:M:m:N:n:PQ:RrT:t:U:uVX:x OPT; do gem5opts="$OPTARG \\ " ;; - g) - common_gem5=true - ;; h) cat build-usage.adoc 1>&2 exit @@ -93,30 +87,15 @@ while getopts a:c:DdE:e:F:f:G:ghIiKkL:l:M:m:N:n:PQ:RrT:t:U:uVX:x OPT; do #extra_append="$extra_append kgdbwait kgdboc=kbd" kgdb=true ;; - L) - common_linux_variant="$OPTARG" - ;; l) gem5_restore_last_checkpoint="${OPTARG}" ;; - M) - common_gem5_variant="$OPTARG" - ;; m) memory="$OPTARG" ;; - N) - common_gem5_worktree="$OPTARG" - ;; - n) - common_run_id="$OPTARG" - ;; P) prebuilt=true ;; - Q) - common_qemu_variant="$OPTARG" - ;; R) rr=replay ;; @@ -127,9 +106,6 @@ while getopts a:c:DdE:e:F:f:G:ghIiKkL:l:M:m:N:n:PQ:RrT:t:U:uVX:x OPT; do trace_enabled=true trace_type="$OPTARG" ;; - t) - common_gem5_build_type="$OPTARG" - ;; U) tmux_args="$OPTARG" ;; @@ -147,7 +123,7 @@ while getopts a:c:DdE:e:F:f:G:ghIiKkL:l:M:m:N:n:PQ:RrT:t:U:uVX:x OPT; do " ;; ?) - exit 2 + common_getopts_case "$OPT" ;; esac done diff --git a/rungdb b/rungdb index aa9c85a..4f36a36 100755 --- a/rungdb +++ b/rungdb @@ -6,14 +6,11 @@ lx_symbols="-ex 'lx-symbols ../kernel_module-1.0/' \\ " kgdb=false docontinue=true -while getopts A:a:b:CgkL:n:X OPT; do +while getopts "A:a:b:CgkL:n:X${common_getopts_flags}" OPT; do case "$OPT" in A) after="$OPTARG" ;; - a) - common_arch="$OPTARG" - ;; b) before="$OPTARG" ;; @@ -21,23 +18,14 @@ while getopts A:a:b:CgkL:n:X OPT; do # No Continue. docontinue=false ;; - g) - common_gem5=true - ;; k) kgdb=true ;; - L) - common_linux_variant="$OPTARG" - ;; - n) - common_run_id="$OPTARG" - ;; X) lx_symbols= ;; ?) - exit 2 + common_getopts_case "$OPT" ;; esac done diff --git a/rungdb-user b/rungdb-user index d74fef2..a4b2371 100755 --- a/rungdb-user +++ b/rungdb-user @@ -1,22 +1,14 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" -usage="$0 []" gem5_opt= -while getopts a:gh OPT; do +while getopts "h${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; - g) - common_gem5=true - gem5_opt=-g - ;; h) - echo "$usage" - exit 0 + echo "$0 []" + exit ;; ?) - exit 2 + common_getopts_case "$OPT" ;; esac done @@ -30,6 +22,9 @@ else brk= fi common_setup +if "$common_gem5"; then + gem5_opt=-g +fi executable="${common_build_dir}/${executable_rel}" addr="$("${common_root_dir}/runtc" readelf -h "$executable" | awk '/Entry/{ print $NF }' )" ex="-ex \"add-symbol-file $executable $addr\"" diff --git a/rungdbserver b/rungdbserver index c0bd853..00b5a4f 100755 --- a/rungdbserver +++ b/rungdbserver @@ -1,12 +1,9 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" -while getopts a:g OPT; do +while getopts "${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; - g) - common_gem5=true + ?) + common_getopts_case "$OPT" ;; esac done diff --git a/runtc b/runtc index f739772..43d2781 100755 --- a/runtc +++ b/runtc @@ -1,14 +1,14 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" -while getopts a:gh OPT; do +while getopts "h${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; h) echo "https://github.com/cirosantilli/linux-kernel-module-cheat#runtc" 2>&1 exit ;; + ?) + common_getopts_case "$OPT" + ;; esac done shift "$(($OPTIND - 1))" diff --git a/trace-boot b/trace-boot index a7e327c..e3ae276 100755 --- a/trace-boot +++ b/trace-boot @@ -1,12 +1,9 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" -while getopts a:g OPT; do +while getopts "${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; - g) - common_gem5=true + ?) + common_getopts_case "$OPT" ;; esac done diff --git a/trace2line b/trace2line index da1542a..f4b4404 100755 --- a/trace2line +++ b/trace2line @@ -1,12 +1,9 @@ #!/usr/bin/env bash . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common" -while getopts a:g OPT; do +while getopts "${common_getopts_flags}" OPT; do case "$OPT" in - a) - common_arch="$OPTARG" - ;; - g) - common_gem5=true + ?) + common_getopts_case "$OPT" ;; esac done