scripts/populate.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sun Feb 01 22:41:16 2009 +0000 (2009-02-01)
changeset 1190 15908c920549
parent 1177 748c418d3b6a
child 1352 d7ddcb75e0f7
child 1400 ee206adb53a7
permissions -rw-r--r--
Fix populate to correctly use tools discovered via ./configure.

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