tools/populate.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Mon Oct 27 18:42:26 2008 +0000 (2008-10-27)
changeset 1001 c8ac48ba1411
parent 770 1af057f49f7e
permissions -rw-r--r--
Enhance the make fragments:
- comment the different parts
- re-order the code so that it is homogeneous amogst fragments
- eye-candy in some existing comments

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