scripts/populate: cleanups and misc fixes
author"Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Mar 28 23:01:19 2010 +0200 (2010-03-28)
changeset 186311cc7a5102fa
parent 1862 7fec5db8933c
child 1864 758d5137fe87
scripts/populate: cleanups and misc fixes

- it's a POSIX compliant shell script: drop bash, use /bin/sh
- fix help text
- use an absolute path for sysroot
- replace "echo" with "printf"
- replace "stat -c '%i'" with "ls -1id"
- replace "pushd / popd" with "cd / cd -"
- remove superfluous break
- bail out if required lib not found, except if forced
scripts/populate.in
     1.1 --- a/scripts/populate.in	Fri Mar 26 23:17:23 2010 +0100
     1.2 +++ b/scripts/populate.in	Sun Mar 28 23:01:19 2010 +0200
     1.3 @@ -1,4 +1,4 @@
     1.4 -#!@@CT_bash@@
     1.5 +#!/bin/sh
     1.6  # This script will populate the root directory with libs from the sysroot.
     1.7  # (C) 2007 Yann E. MORIN
     1.8  # Licensed under the GPL v2
     1.9 @@ -23,7 +23,7 @@
    1.10                        |"${grep}" -E -- '--with-sysroot='    \
    1.11                        |cut -d = -f 2-
    1.12                       )"
    1.13 -CT_SYSROOT_DIR="$(echo "${CT_CFG_SYSROOT_DIR}"                                  \
    1.14 +CT_SYSROOT_DIR="$(printf "${CT_CFG_SYSROOT_DIR}\n"                              \
    1.15                    |"${sed}" -r -e "s:^${CT_CFG_PREFIX_DIR}:${CT_PREFIX_DIR}:;"  \
    1.16                    |"${sed}" -r -e 's,/+,/,g;'                                   \
    1.17                   )"
    1.18 @@ -39,19 +39,21 @@
    1.19      $myname OPTIONS -s source_root -d destination_root
    1.20  
    1.21  DESCRIPTION
    1.22 -    $myname will 'populate' your target root file system ('src_dir') with
    1.23 -    libraries from the toolchain (eg. libc.so...), storing the result into
    1.24 -    'dst_dir'.
    1.25 +    This script will 'populate' your target root file system 'source_root'
    1.26 +    with libraries from the toolchain (eg. libc.so...), storing the result
    1.27 +    into 'dst_dir'.
    1.28  
    1.29  OPTIONS
    1.30      -s src_dir
    1.31 -        use 'src_dir' as the un-populated (source) root directory
    1.32 +        Use 'src_dir' as the un-populated (source) root directory.
    1.33  
    1.34      -d dst_dir
    1.35 -        use 'dst_dir' as the place to put the populated root directory
    1.36 +        Use 'dst_dir' as the place to put the populated root directory.
    1.37 +        See the -f and -m options, below, on the required (non-)existence
    1.38 +        of this directory.
    1.39  
    1.40      -r sysroot_dir
    1.41 -        use 'sysroot_dir' as the sysroot instead of the toolchain default
    1.42 +        Use 'sysroot_dir' as the sysroot instead of the toolchain default.
    1.43  
    1.44      -l name1[:name2[...]]
    1.45          Always add the specified shared library/ies name1, name2... from the
    1.46 @@ -71,14 +73,14 @@
    1.47          after a # is ignored until the end of the line; spaces are ignored;
    1.48          empty lines are ignored. Libraries are searched for as with -l.
    1.49  
    1.50 -    -f  force execution: if destination directory already exists, it will be
    1.51 +    -f  Force execution: if destination directory already exists, it will be
    1.52          removed first; if a specified library (above) was not found, continue.
    1.53          Note: if using -m and the destination directory already exists, it
    1.54          is *not* removed, see below.
    1.55  
    1.56      -m  Merge the source root directory with the destination root directory.
    1.57          If the latter does not exist, it is created, and -m is ignored.
    1.58 -        If the destination droot directory exists, then the content of the
    1.59 +        If the destination root directory exists, then the content of the
    1.60          source root directory is copied in there, and the result is populated
    1.61          as usual.
    1.62          It can be usefull if constructing a rootfs incrementally from many
    1.63 @@ -112,10 +114,10 @@
    1.64          h)  doHelp
    1.65              exit 0
    1.66              ;;
    1.67 -        :)  echo "$myname: '-${OPTARG}' takes exactly one argument."
    1.68 +        :)  printf "$myname: '-${OPTARG}' takes exactly one argument.\n"
    1.69              exit 1
    1.70              ;;
    1.71 -        ?)  echo "$myname: unknown option '-${OPTARG}'."
    1.72 +        ?)  printf "$myname: unknown option '-${OPTARG}'.\n"
    1.73              exit 1
    1.74              ;;
    1.75      esac
    1.76 @@ -127,11 +129,11 @@
    1.77      exit 1
    1.78  fi
    1.79  if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
    1.80 -    echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
    1.81 +    printf "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory\n"
    1.82      exit 1
    1.83  fi
    1.84  if [ ! -d "${CT_SYSROOT_DIR}" ]; then
    1.85 -    echo "$myname: '${CT_SYSROOT_DIR}': no such file or directory"
    1.86 +    printf "$myname: '${CT_SYSROOT_DIR}': no such file or directory\n"
    1.87      exit 1
    1.88  fi
    1.89  # If the dest dir does not exist, all is well
    1.90 @@ -142,19 +144,21 @@
    1.91      case "${CT_FORCE}:${CT_MERGE}" in
    1.92          *:y)    ;;
    1.93          y:)     rm -rf "${CT_ROOT_DST_DIR}";;
    1.94 -        :)      echo "$myname: '${CT_ROOT_DST_DIR}': already exists" && exit 1 ;;
    1.95 +        :)      printf "$myname: '${CT_ROOT_DST_DIR}': already exists\n"
    1.96 +                exit 1
    1.97 +                ;;
    1.98      esac
    1.99  fi
   1.100 -src_inode=$(stat -c '%i' "${CT_ROOT_SRC_DIR}/.")
   1.101 -dst_inode=$(stat -c '%i' "${CT_ROOT_DST_DIR}/." 2>/dev/null || true)
   1.102 +src_inode=$(ls -1id "${CT_ROOT_SRC_DIR}/." |awk '{ print $1 }')
   1.103 +dst_inode=$(ls -1id "${CT_ROOT_DST_DIR}/." 2>/dev/null |awk '{ print $1 }')
   1.104  if [ "${src_inode}" -eq "$((dst_inode+0))" ]; then
   1.105 -    echo "$myname: source and destination are the same!"
   1.106 +    printf "$myname: source and destination are the same!\n"
   1.107      exit 1
   1.108  fi
   1.109  
   1.110  # Check existence of the forced libraries file
   1.111  if [ -n "${CT_LIB_FILE}" -a ! \( -f "${CT_LIB_FILE}" -a -r "${CT_LIB_FILE}" \) ]; then
   1.112 -    echo "$myname: forced libraries file '${CT_LIB_FILE}' not found!"
   1.113 +    printf "$myname: forced libraries file '${CT_LIB_FILE}' not found!\n"
   1.114      exit 1
   1.115  fi
   1.116  
   1.117 @@ -164,11 +168,12 @@
   1.118  # Make all path absolute
   1.119  CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
   1.120  CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
   1.121 +CT_SYSROOT_DIR=$(cd "${CT_SYSROOT_DIR}"; pwd)
   1.122  
   1.123  # Populate the destination directory with files from the source directory
   1.124 -pushd "${CT_ROOT_SRC_DIR}" >/dev/null
   1.125 +cd "${CT_ROOT_SRC_DIR}"
   1.126  cp -a . "${CT_ROOT_DST_DIR}"
   1.127 -popd >/dev/null
   1.128 +cd - >/dev/null
   1.129  
   1.130  # A function do search for a library
   1.131  # Usage: do_add_lib libname
   1.132 @@ -196,14 +201,13 @@
   1.133              ${CT_PRINTF} "      installing as '%s/%s', mode='%s'\n" "${dir}" "${true_libname}" "${mode}"
   1.134              "${install}" -m "${mode}" "${libfile}" "${dir}/${true_libname}"
   1.135              return 0
   1.136 -            break
   1.137          fi
   1.138      done
   1.139      return 1
   1.140  }
   1.141  
   1.142  # We'll work in the copied rootfs
   1.143 -pushd "${CT_ROOT_DST_DIR}" >/dev/null
   1.144 +cd "${CT_ROOT_DST_DIR}"
   1.145  
   1.146  # First of, copy the forced libraries into the working copy
   1.147  lib_list=
   1.148 @@ -215,7 +219,7 @@
   1.149                          "${CT_LIB_FILE}"
   1.150                )
   1.151  fi
   1.152 -CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}"             \
   1.153 +CT_LIB_LIST=$(printf "${CT_LIB_LIST}:${lib_list}\n"         \
   1.154                |"${sed}" -r -e 's/^:+//; s/:+$//; s/:+/ /g;' \
   1.155               )
   1.156  if [ -n "${CT_LIB_LIST}" ]; then
   1.157 @@ -231,8 +235,8 @@
   1.158              fi
   1.159          done
   1.160          if [ ${found} -eq 0 ]; then
   1.161 -            echo "$myname: library '${libname}' not found!"
   1.162 -            [ "${CT_FORCE}" = y ] || exit 1
   1.163 +            printf "$myname: library '${libname}' not found!\n"
   1.164 +            [ "${CT_FORCE}" = "y" ] || exit 1
   1.165          fi
   1.166      done
   1.167  fi
   1.168 @@ -260,11 +264,12 @@
   1.169              if do_add_lib "${libname}"; then
   1.170                  still_needed=1
   1.171              else
   1.172 -                echo "$myname: library '${libname}' not found!"
   1.173 +                printf "$myname: library '${libname}' not found!\n"
   1.174 +                [ "${CT_FORCE}" = "y" ] || exit 1
   1.175              fi
   1.176          done
   1.177      done
   1.178  done
   1.179  
   1.180  # OK, we're done. Back off.
   1.181 -popd >/dev/null
   1.182 +cd - >/dev/null