tools/populate.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Mon Jul 28 21:32:33 2008 +0000 (2008-07-28)
changeset 747 d3e603e7c17c
parent 523 010f6f4e4dd6
child 755 c2212f59e1cf
permissions -rw-r--r--
Fourth step at renaming patches: renumber all patches with a 10-step.
yann@217
     1
#!/bin/bash
yann@217
     2
yann@217
     3
# This script will populate the root directory with libs from the sysroot.
yann@217
     4
# (C) 2007 Yann E. MORIN
yann@217
     5
# Licensed under the GPL v2
yann@217
     6
yann@217
     7
CT_READELF="@@CT_READELF@@"
yann@217
     8
CT_SYSROOT_DIR="@@CT_SYSROOT_DIR@@"
yann@217
     9
yann@217
    10
myname=$(basename "$0")
yann@217
    11
yann@217
    12
doHelp() {
yann@217
    13
    cat <<_EOF_
yann@253
    14
$myname [-f] [-v] -s source_root -d destination_root
yann@217
    15
yann@217
    16
    -s dir
yann@217
    17
        use 'dir' as the un-populated (source) root directory
yann@217
    18
yann@217
    19
    -d dir
yann@217
    20
        use 'dir' as the place to put the populated root directory
yann@217
    21
yann@258
    22
    -f  force execution: if destination directory already exists,
yann@258
    23
        it will be removed first.
yann@258
    24
yann@217
    25
    -v  Be verbose
yann@217
    26
_EOF_
yann@217
    27
}
yann@217
    28
yann@217
    29
CT_ROOT_SRC_DIR=
yann@217
    30
CT_ROOT_DST_DIR=
yann@259
    31
CT_FORCE=no
yann@217
    32
CT_ECHO=true
yann@259
    33
while getopts ":s:d:fvh" CT_OPT; do
yann@217
    34
    case "${CT_OPT}" in
yann@217
    35
        s)  CT_ROOT_SRC_DIR="${OPTARG}";;
yann@217
    36
        d)  CT_ROOT_DST_DIR="${OPTARG}";;
yann@259
    37
        f)  CT_FORCE=y;;
yann@217
    38
        v)  CT_ECHO=echo;;
yann@217
    39
        h)  doHelp
yann@217
    40
            exit 0
yann@217
    41
            ;;
yann@523
    42
        :)  echo "$myname: '-${OPTARG}' takes exactly one argument."
yann@217
    43
            exit 1
yann@217
    44
            ;;
yann@523
    45
        ?)  echo "$myname: unknown option '-${OPTARG}'."
yann@217
    46
            exit 1
yann@217
    47
            ;;
yann@217
    48
    esac
yann@217
    49
done
yann@217
    50
yann@217
    51
# Sanity checks
yann@217
    52
if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
yann@217
    53
    doHelp
yann@217
    54
    exit 1
yann@217
    55
fi
yann@217
    56
if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
yann@523
    57
    echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
yann@217
    58
    exit 1
yann@217
    59
fi
yann@259
    60
if [ -d "${CT_ROOT_DST_DIR}" -a "${CT_FORCE}" != "y" ]; then
yann@523
    61
    echo "$myname: '${CT_ROOT_DST_DIR}': already exists"
yann@217
    62
    exit 1
yann@217
    63
fi
yann@217
    64
src_inode=$(ls -di "${CT_ROOT_SRC_DIR}")
yann@217
    65
dst_inode=$(ls -di "${CT_ROOT_DST_DIR}" 2>/dev/null)
yann@217
    66
if [ "${src_inode}" = "${dst_inode}" ]; then
yann@217
    67
    echo "$myname: source and destination are the same!"
yann@217
    68
    exit 1
yann@217
    69
fi
yann@217
    70
yann@217
    71
# Get rid of potentially older destination directory
yann@217
    72
if [ -d "${CT_ROOT_DST_DIR}" ]; then
yann@217
    73
    mv "${CT_ROOT_DST_DIR}" "${CT_ROOT_DST_DIR}.$$"
yann@320
    74
    setsid nohup rm -rf "${CT_ROOT_DST_DIR}.$$" >/dev/null 2>&1 &
yann@217
    75
fi
yann@217
    76
yann@217
    77
# Create the working copy
yann@217
    78
mkdir -p "${CT_ROOT_DST_DIR}"
yann@217
    79
yann@217
    80
# Make all path absolute
yann@253
    81
CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
yann@253
    82
CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
yann@217
    83
yann@217
    84
cd "${CT_ROOT_SRC_DIR}"
yann@217
    85
tar cf - . |(cd "${CT_ROOT_DST_DIR}"; tar xf -)
yann@217
    86
yann@217
    87
# Parse the working copy for executables and libraries
yann@217
    88
cd "${CT_ROOT_DST_DIR}"
yann@217
    89
still_needed=1
yann@217
    90
while [ ${still_needed} -eq 1 ]; do
yann@217
    91
    ${CT_ECHO} "Looping..."
yann@217
    92
    still_needed=0
yann@217
    93
    for f in $(find . -type f -exec file {} \; |egrep ': ELF [[:digit:]]+-bit .SB (executable|shared object),' |cut -d ":" -f 1); do
yann@523
    94
        ${CT_ECHO} "Scanning '${f}'"
yann@217
    95
        for libname in $("${CT_READELF}" -d "${f}" |egrep '(NEEDED)' |sed -r -e 's,.+\[(.+)\] *$,\1,;'); do
yann@523
    96
            ${CT_ECHO} "  searching for '${libname}'"
yann@259
    97
            if [    -e "lib/${libname}"     \
yann@259
    98
                 -o -e "usr/lib/${libname}" ]; then
yann@217
    99
                ${CT_ECHO} "    already present"
yann@217
   100
                continue
yann@217
   101
            fi
yann@688
   102
            for dir in . usr; do
yann@523
   103
                ${CT_ECHO} -n "    trying in '${dir}'"
yann@217
   104
                libfile="${CT_SYSROOT_DIR}/${dir}/lib/${libname}"
yann@523
   105
                ${CT_ECHO} ": '${libfile}'"
yann@217
   106
                if [ -e "${libfile}" ]; then
yann@259
   107
                    mkdir -p "${dir}/lib"
yann@688
   108
                    ${CT_ECHO} "      installing '${dir}/lib/${libname}'"
yann@688
   109
                    cp "${libfile}" "${dir}/lib/${libname}"
yann@217
   110
                    still_needed=1
yann@217
   111
                    break
yann@217
   112
                fi
yann@217
   113
            done
yann@217
   114
        done
yann@217
   115
    done
yann@217
   116
done