scripts/populate.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Mon Apr 20 21:10:03 2009 +0000 (2009-04-20)
changeset 1299 3448ac3f1a5d
parent 1177 748c418d3b6a
child 1352 d7ddcb75e0f7
child 1400 ee206adb53a7
permissions -rw-r--r--
There's no longer any reason to require GNU awk:
- the only part that required it (socks proxy settings) is gone,
- all remaining awk scripts are POSIXly correct (or should be).

-------- diffstat follows --------
/trunk/configure | 5 2 3 0 ++---
/trunk/Makefile.in | 2 0 2 0 --
/trunk/scripts/build/kernel/linux.sh | 2 1 1 0 +-
/trunk/scripts/build/internals.sh | 1 0 1 0 -
/trunk/scripts/build/mpfr.sh | 2 1 1 0 +-
/trunk/scripts/functions | 4 2 2 0 ++--
/trunk/scripts/saveSample.sh.in | 4 2 2 0 ++--
7 files changed, 8 insertions(+), 12 deletions(-)
     1 #!@@CT_bash@@
     2 # This script will populate the root directory with libs from the sysroot.
     3 # (C) 2007 Yann E. MORIN
     4 # Licensed under the GPL v2
     5 set -e
     6 
     7 # Detect where the toolchain is:
     8 CT_PREFIX_DIR="$(cd "$(dirname "$0")/.."; pwd)"
     9 CT_BIN_DIR="${CT_PREFIX_DIR}/bin"
    10 CT_READELF="${CT_BIN_DIR}/@@CT_TARGET@@-readelf"
    11 CT_LIB_DIR="${CT_PREFIX_DIR}/lib"
    12 CT_SYSROOT_DIR="$(cd "${CT_BIN_DIR}/../@@CT_TARGET@@/sys-root"; pwd)"
    13 
    14 myname=$(basename "$0")
    15 
    16 # Use the tols discovered by crosstool-NG's ./configure:
    17 install="@@CT_install@@"
    18 grep="@@CT_grep@@"
    19 sed="@@CT_sed@@"
    20 
    21 doHelp() {
    22     cat <<_EOF_
    23 NAME
    24     $myname - populate the target root file system
    25 
    26 SYNOPSIS
    27     $myname OPTIONS -s source_root -d destination_root
    28 
    29 DESCRIPTION
    30     $myname will 'populate' your target root file system ('src_dir') with
    31     libraries from the toolchain (eg. libc.so...), storing the result into
    32     'dst_dir'.
    33 
    34 OPTIONS
    35     -s src_dir
    36         use 'src_dir' as the un-populated (source) root directory
    37 
    38     -d dst_dir
    39         use 'dst_dir' as the place to put the populated root directory
    40 
    41     -l name1[:name2[...]]
    42         Always add the specified shared library/ies name1, name2... from the
    43         toolchain (in the sys-root). Actual library names are searched as
    44         follows (where 'name' is replaced with the given name) in the
    45         sys-root directory:
    46           - libname.so
    47           - name.so
    48           - name
    49         If the file is found, then the SONAME of the library is used, and the
    50         library is copied with that name. If the library was not found, this
    51         yields an error (unless -f was given).
    52 
    53     -L file
    54         Read 'file' for a list of shared libraries to always add from the
    55         toolchain. The file should contain one library name per line; text
    56         after a # is ignored until the end of the line; spaces are ignored;
    57         empty lines are ignored. Libraries are searched for as with -l.
    58 
    59     -f  force execution: if destination directory already exists, it will be
    60         removed first; if a specified library (above) was not found, continue.
    61 
    62     -v  Be verbose
    63 
    64 _EOF_
    65 }
    66 
    67 CT_ROOT_SRC_DIR=
    68 CT_ROOT_DST_DIR=
    69 CT_LIB_LIST=
    70 CT_LIB_FILE=
    71 CT_FORCE=no
    72 CT_ECHO=true
    73 OPTIND=1
    74 while getopts ":s:d:l:L:fvh" CT_OPT; do
    75     case "${CT_OPT}" in
    76         s)  CT_ROOT_SRC_DIR="${OPTARG}";;
    77         d)  CT_ROOT_DST_DIR="${OPTARG}";;
    78         l)  CT_LIB_LIST="${CT_LIB_LIST}:${OPTARG}";;
    79         L)  CT_LIB_FILE="${OPTARG}";;
    80         f)  CT_FORCE=y;;
    81         v)  CT_ECHO=echo;;
    82         h)  doHelp
    83             exit 0
    84             ;;
    85         :)  echo "$myname: '-${OPTARG}' takes exactly one argument."
    86             exit 1
    87             ;;
    88         ?)  echo "$myname: unknown option '-${OPTARG}'."
    89             exit 1
    90             ;;
    91     esac
    92 done
    93 
    94 # Sanity checks
    95 if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
    96     doHelp
    97     exit 1
    98 fi
    99 if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
   100     echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
   101     exit 1
   102 fi
   103 if [ -d "${CT_ROOT_DST_DIR}" -a "${CT_FORCE}" != "y" ]; then
   104     echo "$myname: '${CT_ROOT_DST_DIR}': already exists"
   105     exit 1
   106 fi
   107 src_inode=$(ls -di "${CT_ROOT_SRC_DIR}")
   108 dst_inode=$(ls -di "${CT_ROOT_DST_DIR}" 2>/dev/null)
   109 if [ "${src_inode}" = "${dst_inode}" ]; then
   110     echo "$myname: source and destination are the same!"
   111     exit 1
   112 fi
   113 
   114 # Check existence of the forced libraries file
   115 if [ -n "${CT_LIB_FILE}" -a ! \( -f "${CT_LIB_FILE}" -a -r "${CT_LIB_FILE}" \) ]; then
   116     echo "$myname: forced libraries file '${CT_LIB_FILE}' not found!"
   117     exit 1
   118 fi
   119 
   120 # Get rid of potentially older destination directory
   121 rm -rf "${CT_ROOT_DST_DIR}"
   122 
   123 # Create the working copy
   124 mkdir -p "${CT_ROOT_DST_DIR}"
   125 
   126 # Make all path absolute
   127 CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
   128 CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
   129 
   130 # Populate the destination directory with files form the source directory
   131 pushd "${CT_ROOT_SRC_DIR}" >/dev/null
   132 tar cf - . |(cd "${CT_ROOT_DST_DIR}"; tar xf -)
   133 popd >/dev/null
   134 
   135 # A function do search for a library
   136 # Usage: do_add_lib libname
   137 # returns: 0 if library was found and added, !0 otherwise
   138 do_add_lib() {
   139     local libname="$1"
   140     local true_libname
   141     local dir
   142     for dir in lib usr/lib; do
   143         ${CT_ECHO} -n "    trying in '${dir}'"
   144         libfile="${CT_SYSROOT_DIR}/${dir}/${libname}"
   145         ${CT_ECHO} ": '${libfile}'"
   146         if [ -e "${libfile}" ]; then
   147             mkdir -p "${dir}"
   148             true_libname=$("${CT_READELF}" -d "${libfile}"          \
   149                            |"${grep}" "Library soname:"             \
   150                            |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;'   \
   151                           )
   152             ${CT_ECHO} "      installing as '${dir}/${true_libname}'"
   153             "${install}" -m 0644 "${libfile}" "${dir}/${true_libname}"
   154             return 0
   155             break
   156         fi
   157     done
   158     return 1
   159 }
   160 
   161 # First of, copy the forced libraries into the working copy
   162 if [ -n "${CT_LIB_FILE}" ]; then
   163     lib_list=$("${sed}" -r -e ':loop; s/#.*//;'         \
   164                            -e 's/[[:space:]]+//g;'      \
   165                            -e 's/([^:])$/\1:/;'         \
   166                            -e '/$/N; s/\n//; tloop;'    \
   167                         "${CT_LIB_FILE}"
   168               )
   169     CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}"             \
   170                   |"${sed}" -r -e 's/:+/:/g; s/^:+//; s/:+$//;' \
   171                  )
   172 fi
   173 CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}"             \
   174               |"${sed}" -r -e 's/^:+//; s/:+$//; s/:+/ /g;' \
   175              )
   176 ${CT_ECHO} "Installing forced libraries..."
   177 pushd "${CT_ROOT_DST_DIR}" >/dev/null
   178 for name in ${CT_LIB_LIST}; do
   179     [ -z "${name}" ] && continue
   180     found=0
   181     for libname in "lib${name}.so" "${name}.so" "${name}"; do
   182         ${CT_ECHO} "  searching for '${libname}'"
   183         if do_add_lib "${libname}"; then
   184             found=1
   185             break
   186         fi
   187     done
   188     if [ ${found} -eq 0 ]; then
   189         echo "$myname: library '${libname}' not found!"
   190         [ "${CT_FORCE}" = y ] || exit 1
   191     fi
   192 done
   193 popd >/dev/null
   194 
   195 # Parse the working copy for executables and libraries
   196 pushd "${CT_ROOT_DST_DIR}" >/dev/null
   197 still_needed=1
   198 while [ ${still_needed} -eq 1 ]; do
   199     ${CT_ECHO} "Looping..."
   200     still_needed=0
   201     for f in $(find . -type f -exec file {} \;                                              \
   202                |"${grep}" -E ': ELF [[:digit:]]+-bit (L|M)SB (executable|shared object),'   \
   203                |cut -d ":" -f 1                                                             \
   204               ); do
   205         ${CT_ECHO} "Scanning '${f}'"
   206         for libname in $("${CT_READELF}" -d "${f}"                              \
   207                          |"${grep}" -E '\(NEEDED\)[[:space:]]+Shared library:'  \
   208                          |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;'                 \
   209                         ); do
   210             ${CT_ECHO} "  searching for '${libname}'"
   211             if [    -e "lib/${libname}"     \
   212                  -o -e "usr/lib/${libname}" ]; then
   213                 ${CT_ECHO} "    already present"
   214                 continue
   215             fi
   216             if do_add_lib "${libname}"; then
   217                 still_needed=1
   218             else
   219                 echo "$myname: library '${libname}' not found!"
   220             fi
   221         done
   222     done
   223 done
   224 popd >/dev/null