Skip to content

Commit

Permalink
tools/mksymtab.sh: Using getopts to parse parameters
Browse files Browse the repository at this point in the history
Use the "-a" option to specify additional lists

Examples
  - The basic.txt
    $ cat basic.txt
    basic_func0
    basic_func1
    basic_func2

  - The additional.txt
    $ cat additional.txt
    additional_func0
    additional_func1
    additional_func2

  1. Get symbols from directory "EMPTY_DIR" and additional list basic.txt
    ./tools/mksymtab.sh ./EMPTY_DIR -a basic.txt
    #if defined(CONFIG_EXECFUNCS_HAVE_SYMTAB)
    const struct symtab_s CONFIG_EXECFUNCS_SYMTAB_ARRAY[] =
    #elif defined(CONFIG_NSH_SYMTAB)
    const struct symtab_s CONFIG_NSH_SYMTAB_ARRAYNAME[] =
    #else
    const struct symtab_s dummy_symtab[] =
    #endif
    {
      {"basic_func0", &basic_func0},
      {"basic_func1", &basic_func1},
      {"basic_func2", &basic_func2},
    };

  2. Get symbols from directory "EMPTY_DIR" and two additional lists basic.txt, additional.txt
    ./tools/mksymtab.sh ./EMPTY_DIR -a basic.txt -a additional.txt
    #if defined(CONFIG_EXECFUNCS_HAVE_SYMTAB)
    const struct symtab_s CONFIG_EXECFUNCS_SYMTAB_ARRAY[] =
    #elif defined(CONFIG_NSH_SYMTAB)
    const struct symtab_s CONFIG_NSH_SYMTAB_ARRAYNAME[] =
    #else
    const struct symtab_s dummy_symtab[] =
    #endif
    {
      {"additional_func0", &additional_func0},
      {"additional_func1", &additional_func1},
      {"additional_func2", &additional_func2},
      {"basic_func0", &basic_func0},
      {"basic_func1", &basic_func1},
      {"basic_func2", &basic_func2},
    };

  3. Set prefix and get symbols from directory "EMPTY_DIR" and two additional lists basic.txt, additional.txt
    ./tools/mksymtab.sh ./EMPTY_DIR PREFIX_TEST  -a basic.txt -a additional.txt
    const struct symtab_s PREFIX_TEST_exports[] =
    {
      {"additional_func0", &additional_func0},
      {"additional_func1", &additional_func1},
      {"additional_func2", &additional_func2},
      {"basic_func0", &basic_func0},
      {"basic_func1", &basic_func1},
      {"basic_func2", &basic_func2},
    };

  4. Error: Missing <imagedirpath>
    $ ./tools/mksymtab.sh
    ERROR: Missing <imagedirpath>

    Usage: ./tools/mksymtab.sh <imagedirpath> [symtabprefix] [-a additionalsymbolspath]

UNSUPPORTED usage examples
  # `getopt` supports these, but the usage in GNU and macOS is incompatible.
  - ./tools/mksymtab.sh ./EMPTY_DIR -a basic.txt PREFIX_TEST -a additional.txt
  - ./tools/mksymtab.sh -a basic.txt ./EMPTY_DIR PREFIX_TEST -a additional.txt

References
  BASH(1)   -- getopts
  GETOPT(1) -- getopt

Signed-off-by: wangjianyu3 <wangjianyu3@xiaomi.com>
  • Loading branch information
JianyuWang0623 authored and xiaoxiang781216 committed Oct 7, 2024
1 parent d407670 commit 266049b
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions tools/mksymtab.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,45 @@

export LC_ALL=C

usage="Usage: $0 <imagedirpath> [symtabprefix [additionalsymbolspath]]"
usage() {
if [ $# -ne 0 ]; then
echo "ERROR: $@"
fi
echo -e "\nUsage: $0 <imagedirpath> [symtabprefix] [-a additionalsymbolspath]"
exit 1
}

# Check for the required directory path

dir=$1
if [ -z "$dir" ]; then
echo "ERROR: Missing <imagedirpath>"
echo ""
echo $usage
exit 1
usage "Missing <imagedirpath>"
fi

# Get the symbol table prefix

prefix=$2
if [ "x${2:0:1}" != "x-" ]; then
prefix=$2
OPTIND=3
else
OPTIND=2
fi

# Parse remaining arguments

while getopts a: opt; do
case $opt in
a)
addlist="${addlist[@]} $OPTARG"
;;
\?)
usage
esac
done

if [ $OPTIND != $(($# + 1)) ]; then
usage "Arguments remaining: \"${@:$OPTIND}\""
fi

# Extract all of the undefined symbols from the ELF files and create a
# list of sorted, unique undefined variable names.
Expand All @@ -59,20 +83,16 @@ if [ -z "$varlist" ]; then
fi
fi

if [ $# -gt 2 ]; then
shift 2
for add_sym in $@; do
if [ -f $add_sym ]; then
varlist="${varlist}\n$(cat $add_sym | grep -v "^,.*")"
elif [ -d $add_sym ]; then
varlist="${varlist}\n$(find $add_sym -type f | xargs cat | grep -v "^,.*")"
else
echo $usage
exit 1
fi
varlist=$(echo -e "${varlist}" | sort -u)
done
fi
for addsym in ${addlist[@]}; do
if [ -f $addsym ]; then
varlist="${varlist}\n$(cat $addsym | grep -v "^,.*")"
elif [ -d $addsym ]; then
varlist="${varlist}\n$(find $addsym -type f | xargs cat | grep -v "^,.*")"
else
usage
fi
varlist=$(echo -e "${varlist}" | sort -u)
done

# Now output the symbol table as a structure in a C source file. All
# undefined symbols are declared as void* types. If the toolchain does
Expand Down

0 comments on commit 266049b

Please sign in to comment.