[Fuego] [PATCH] Optimize the code of assert_has_program.

Wang Mingyu <[email protected]>
Newsgroups dev.linux.lists.fuego
Message-ID <[email protected]>
It is faster to check the prgoram or file on the target.

Usage:
before:
    assert_has_program AAA
    assert_has_program BBB

after:
    assert_has_program "AAA BBB"
    assert_has_program "AAA BBB" SEARCH_PATH

Note: SEARCH_PATH is optional.

Signed-off-by: Wang Mingyu <[email protected]>
---
 scripts/functions.sh  | 65 +++++++++++++++++++++++++++++++++++++++----
 scripts/need_check.sh | 11 ++------
 2 files changed, 61 insertions(+), 15 deletions(-)

diff --git a/scripts/functions.sh b/scripts/functions.sh
index 0fa80b8..3aea0c8 100755
--- a/scripts/functions.sh
+++ b/scripts/functions.sh
@@ -1010,6 +1010,57 @@ function is_on_target_path {
     is_on_target $1 $2 $TARGET_PATH
 }
 
+# check for a program or file on the target, and set a variable if it's present
+# $1 - file, dir or program on target
+# $2 is the path of target to search the program/file
+# $3 - tmpfile to save the detect result
+function set_cmd_str {
+   tmpfile=$3
+   cmd_str="touch $tmpfile
+         # split $1 on whitespace, without file globbing
+         for prg in \$(echo $1 | tr \" \" \"\\n\") ; do
+             # split search path on colon
+             for d in \$(echo $2 | tr \":\" \"\\n\") ; do
+                 # execute a command on the target to detect \$d/\$prg
+                 if [ -z \"\$(cat $tmpfile)\" -a -e \"\$d/\$prg\" ]
+                 then
+                     echo \"\$d/\$prg\" >$tmpfile ;break;
+                 fi
+             done
+             if [ ! -s $tmpfile ]
+             then
+                 find / -name \"\$prg\" | head -n 1 >$tmpfile
+             fi
+             if [ ! -s $tmpfile ]
+             then
+                 echo \"\$prg\" >$tmpfile ; break;
+             else
+                 > $tmpfile
+             fi
+         done"
+}
+
+# check for a program or file on the target, and set a variable if it's present
+# $1 - file, dir or program on target
+# $2 - variable to set during the build
+# $3 is the path of target to search the program/file (optional, default: $PATH)
+function assert_on_target {
+    local TARGET_PATH=${3}
+    if [ -z "$TARGET_PATH" ]; then
+       TARGET_PATH=$(cmd "echo \$PATH")
+    fi
+
+    tmpfile=$(mktemp /tmp/found_loc.XXXXXX)
+    set_cmd_str "$1" $TARGET_PATH $tmpfile
+    cmd "$cmd_str"
+    get $tmpfile $tmpfile
+    if [ -s $tmpfile ] ; then
+        export $2=$(cat $tmpfile)
+    fi
+    cmd "rm $tmpfile"
+    rm -f $tmpfile # -f for tests running on the host
+}
+
 # check for a library on the SDK, and set a variable if it's present
 # $1 - the file (library) we want to search for in the SDK
 # $2 - variable to set the location (if the file is found)
@@ -1036,13 +1087,15 @@ function is_on_sdk {
 
 # check for a program or file on the target, and send message if the program or file is missing
 # $1 has the program that is required on the target board
-# this has the side effect of defining PROGRAM_$1 (uppercased)
-# with the value as the directory where $1 is found on the board.
+# $2 is the path of target to search the program/file (optional, default: $PATH)
 function assert_has_program {
-   upName=${1^^}
-   progVar=PROGRAM_${upName//[-,.]/_}
-   is_on_target_path $1 ${progVar}
-   assert_define ${progVar} "Missing '$1' program on target board"
+   assert_on_target "$1" prgName $2
+   if [ -n "$prgName" ]
+   then
+       upName=${prgName^^}
+       progVar=PROGRAM_${upName//[-,.]/_}
+       assert_define ${progVar} "Missing '$prgName' program on target board"
+   fi
 }
 
 # check for a module on the target, and abort if it is missing
diff --git a/scripts/need_check.sh b/scripts/need_check.sh
index c9bbb75..9bb0098 100755
--- a/scripts/need_check.sh
+++ b/scripts/need_check.sh
@@ -319,18 +319,11 @@ function check_root {
 # check if those specified commands exist on board
 # $1 has single string with a list of command entries to check for
 function check_program {
-  # split $1 on whitespace, without file globbing
-  set -f
-  arg_array=($1)
-  set +f
-
-  for prg in "${arg_array[@]}" ; do
-    is_on_target_path $prg prg_path
-    if [ -z "$prg_path" ] ; then
+    assert_on_target "$1" prg
+    if [ -n "$prg" ] ; then
       echo -e "\n\nABORTED: Expected command \"$prg\" on the target, but it's not there!"
       return 1
     fi
-  done
 
   # return OK if all necessary commands exist on the target
   return 0
-- 
2.17.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.