scripts/populate.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Thu Mar 25 22:42:00 2010 +0100 (2010-03-25)
changeset 1861 a5c020c3d31d
parent 1860 ed94d8e12b63
child 1862 7fec5db8933c
permissions -rw-r--r--
scripts/populate: properly locate the sysroot and required tools

This fixes two problems:
- the sysroot might be in a sub-directory (think SYSROOT_DIR_PREFIX)
- it is not needed to have the target tuple to properly detect the sysroot
and the required tools

As a side effect, this script is now no longer dependent on the target
tuple, and in the future, we might be able to share it across many
toolchains (when/if we can install all of them in the same place).
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@1400
     7
# Use the tools discovered by crosstool-NG's ./configure:
yann@1190
     8
install="@@CT_install@@"
yann@1190
     9
grep="@@CT_grep@@"
yann@1190
    10
sed="@@CT_sed@@"
yann@1177
    11
yann@1861
    12
# Detect where the toolchain is:
yann@1861
    13
CT_PREFIX_DIR="$(cd "$(dirname "$0")/.."; pwd)"
yann@1861
    14
CT_GCC="${0%-populate}-gcc"
yann@1861
    15
CT_READELF="${0%-populate}-readelf"
yann@1861
    16
CT_CFG_PREFIX_DIR="$("${CT_GCC}" -v 2>&1            \
yann@1861
    17
                     |tr ' ' '\n'                   \
yann@1861
    18
                     |"${grep}" -E -- '--prefix='   \
yann@1861
    19
                     |cut -d = -f 2-
yann@1861
    20
                    )"
yann@1861
    21
CT_CFG_SYSROOT_DIR="$("${CT_GCC}" -v 2>&1                   \
yann@1861
    22
                      |tr ' ' '\n'                          \
yann@1861
    23
                      |"${grep}" -E -- '--with-sysroot='    \
yann@1861
    24
                      |cut -d = -f 2-
yann@1861
    25
                     )"
yann@1861
    26
CT_SYSROOT_DIR="$(echo "${CT_CFG_SYSROOT_DIR}"                                  \
yann@1861
    27
                  |"${sed}" -r -e "s:^${CT_CFG_PREFIX_DIR}:${CT_PREFIX_DIR}:;"  \
yann@1861
    28
                  |"${sed}" -r -e 's,/+,/,g;'                                   \
yann@1861
    29
                 )"
yann@1861
    30
yann@1861
    31
myname=$(basename "$0")
yann@1861
    32
yann@217
    33
doHelp() {
yann@217
    34
    cat <<_EOF_
yann@910
    35
NAME
yann@910
    36
    $myname - populate the target root file system
yann@217
    37
yann@910
    38
SYNOPSIS
yann@910
    39
    $myname OPTIONS -s source_root -d destination_root
yann@217
    40
yann@910
    41
DESCRIPTION
yann@910
    42
    $myname will 'populate' your target root file system ('src_dir') with
yann@910
    43
    libraries from the toolchain (eg. libc.so...), storing the result into
yann@910
    44
    'dst_dir'.
yann@217
    45
yann@910
    46
OPTIONS
yann@910
    47
    -s src_dir
yann@910
    48
        use 'src_dir' as the un-populated (source) root directory
yann@910
    49
yann@910
    50
    -d dst_dir
yann@910
    51
        use 'dst_dir' as the place to put the populated root directory
yann@910
    52
ncase@1859
    53
    -r sysroot_dir
ncase@1859
    54
        use 'sysroot_dir' as the sysroot instead of the toolchain default
ncase@1859
    55
yann@910
    56
    -l name1[:name2[...]]
yann@910
    57
        Always add the specified shared library/ies name1, name2... from the
yann@910
    58
        toolchain (in the sys-root). Actual library names are searched as
yann@910
    59
        follows (where 'name' is replaced with the given name) in the
yann@910
    60
        sys-root directory:
yann@910
    61
          - libname.so
yann@910
    62
          - name.so
yann@910
    63
          - name
yann@910
    64
        If the file is found, then the SONAME of the library is used, and the
yann@910
    65
        library is copied with that name. If the library was not found, this
yann@910
    66
        yields an error (unless -f was given).
yann@910
    67
yann@910
    68
    -L file
yann@910
    69
        Read 'file' for a list of shared libraries to always add from the
yann@910
    70
        toolchain. The file should contain one library name per line; text
yann@910
    71
        after a # is ignored until the end of the line; spaces are ignored;
yann@910
    72
        empty lines are ignored. Libraries are searched for as with -l.
yann@910
    73
yann@910
    74
    -f  force execution: if destination directory already exists, it will be
yann@910
    75
        removed first; if a specified library (above) was not found, continue.
yann@1860
    76
        Note: if using -m and the destination directory already exists, it
yann@1860
    77
        is *not* removed, see below.
yann@1860
    78
yann@1860
    79
    -m  Merge the source root directory with the destination root directory.
yann@1860
    80
        If the latter does not exist, it is created, and -m is ignored.
yann@1860
    81
        If the destination droot directory exists, then the content of the
yann@1860
    82
        source root directory is copied in there, and the result is populated
yann@1860
    83
        as usual.
yann@1860
    84
        It can be usefull if constructing a rootfs incrementally from many
yann@1860
    85
        smaller source root directories, or if your destination root directory
yann@1860
    86
        is an NFS export that your target mounts as / (and you don't want to
yann@1860
    87
        re-run exportfs -av everytime). USE WITH CARE!
yann@258
    88
yann@1352
    89
    -v  Be verbose. By default, populate is absolutely silent.
yann@910
    90
yann@217
    91
_EOF_
yann@217
    92
}
yann@217
    93
yann@217
    94
CT_ROOT_SRC_DIR=
yann@217
    95
CT_ROOT_DST_DIR=
yann@910
    96
CT_LIB_LIST=
yann@910
    97
CT_LIB_FILE=
yann@1860
    98
CT_MERGE=
yann@1860
    99
CT_FORCE=
yann@1352
   100
CT_PRINTF=:
yann@1177
   101
OPTIND=1
yann@1860
   102
while getopts ":s:d:r:l:L:fmvh" CT_OPT; do
yann@217
   103
    case "${CT_OPT}" in
yann@217
   104
        s)  CT_ROOT_SRC_DIR="${OPTARG}";;
yann@217
   105
        d)  CT_ROOT_DST_DIR="${OPTARG}";;
ncase@1859
   106
        r)  CT_SYSROOT_DIR="${OPTARG}";;
yann@910
   107
        l)  CT_LIB_LIST="${CT_LIB_LIST}:${OPTARG}";;
yann@910
   108
        L)  CT_LIB_FILE="${OPTARG}";;
yann@259
   109
        f)  CT_FORCE=y;;
yann@1860
   110
        m)  CT_MERGE=y;;
yann@1352
   111
        v)  CT_PRINTF=printf;;
yann@217
   112
        h)  doHelp
yann@217
   113
            exit 0
yann@217
   114
            ;;
yann@523
   115
        :)  echo "$myname: '-${OPTARG}' takes exactly one argument."
yann@217
   116
            exit 1
yann@217
   117
            ;;
yann@523
   118
        ?)  echo "$myname: unknown option '-${OPTARG}'."
yann@217
   119
            exit 1
yann@217
   120
            ;;
yann@217
   121
    esac
yann@217
   122
done
yann@217
   123
yann@217
   124
# Sanity checks
yann@217
   125
if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
yann@217
   126
    doHelp
yann@217
   127
    exit 1
yann@217
   128
fi
yann@217
   129
if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
yann@523
   130
    echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
yann@217
   131
    exit 1
yann@217
   132
fi
ncase@1859
   133
if [ ! -d "${CT_SYSROOT_DIR}" ]; then
ncase@1859
   134
    echo "$myname: '${CT_SYSROOT_DIR}': no such file or directory"
ncase@1859
   135
    exit 1
ncase@1859
   136
fi
yann@1860
   137
# If the dest dir does not exist, all is well
yann@1860
   138
# If merging, we accept an existing dest directory
yann@1860
   139
# If forcing and not merging, we remove an exiting dest directory
yann@1860
   140
# If not forcing and not merging, we do not accept an exiting dest directory
yann@1860
   141
if [ -d "${CT_ROOT_DST_DIR}" ]; then
yann@1860
   142
    case "${CT_FORCE}:${CT_MERGE}" in
yann@1860
   143
        *:y)    ;;
yann@1860
   144
        y:)     rm -rf "${CT_ROOT_DST_DIR}";;
yann@1860
   145
        :)      echo "$myname: '${CT_ROOT_DST_DIR}': already exists" && exit 1 ;;
yann@1860
   146
    esac
yann@217
   147
fi
yann@1352
   148
src_inode=$(stat -c '%i' "${CT_ROOT_SRC_DIR}/.")
yann@1352
   149
dst_inode=$(stat -c '%i' "${CT_ROOT_DST_DIR}/." 2>/dev/null || true)
yann@1352
   150
if [ "${src_inode}" -eq "$((dst_inode+0))" ]; then
yann@217
   151
    echo "$myname: source and destination are the same!"
yann@217
   152
    exit 1
yann@217
   153
fi
yann@217
   154
yann@910
   155
# Check existence of the forced libraries file
yann@910
   156
if [ -n "${CT_LIB_FILE}" -a ! \( -f "${CT_LIB_FILE}" -a -r "${CT_LIB_FILE}" \) ]; then
yann@910
   157
    echo "$myname: forced libraries file '${CT_LIB_FILE}' not found!"
yann@910
   158
    exit 1
yann@910
   159
fi
yann@910
   160
yann@1860
   161
# Create the working copy, no issue if already existing
yann@217
   162
mkdir -p "${CT_ROOT_DST_DIR}"
yann@217
   163
yann@217
   164
# Make all path absolute
yann@253
   165
CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
yann@253
   166
CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
yann@217
   167
yann@1678
   168
# Populate the destination directory with files from the source directory
yann@910
   169
pushd "${CT_ROOT_SRC_DIR}" >/dev/null
yann@1678
   170
cp -a . "${CT_ROOT_DST_DIR}"
yann@910
   171
popd >/dev/null
yann@910
   172
yann@910
   173
# A function do search for a library
yann@910
   174
# Usage: do_add_lib libname
yann@910
   175
# returns: 0 if library was found and added, !0 otherwise
yann@910
   176
do_add_lib() {
yann@910
   177
    local libname="$1"
yann@910
   178
    local true_libname
yann@1177
   179
    local dir
yann@1352
   180
    local mode
yann@1352
   181
yann@1177
   182
    for dir in lib usr/lib; do
yann@1352
   183
        ${CT_PRINTF} "    trying in '%s'" "${dir}"
yann@1177
   184
        libfile="${CT_SYSROOT_DIR}/${dir}/${libname}"
yann@1352
   185
        ${CT_PRINTF} ": '%s'\n" "${libfile}"
yann@910
   186
        if [ -e "${libfile}" ]; then
yann@1177
   187
            mkdir -p "${dir}"
yann@1177
   188
            true_libname=$("${CT_READELF}" -d "${libfile}"          \
yann@1177
   189
                           |"${grep}" "Library soname:"             \
yann@1177
   190
                           |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;'   \
yann@1177
   191
                          )
yann@1352
   192
            case "${libfile}" in
yann@1352
   193
                */ld*)  mode=0755;;
yann@1352
   194
                *)      mode=0644;;
yann@1352
   195
            esac
yann@1352
   196
            ${CT_PRINTF} "      installing as '%s/%s', mode='%s'\n" "${dir}" "${true_libname}" "${mode}"
yann@1352
   197
            "${install}" -m "${mode}" "${libfile}" "${dir}/${true_libname}"
yann@1177
   198
            return 0
yann@910
   199
            break
yann@910
   200
        fi
yann@910
   201
    done
yann@1177
   202
    return 1
yann@910
   203
}
yann@910
   204
yann@1352
   205
# We'll work in the copied rootfs
yann@1352
   206
pushd "${CT_ROOT_DST_DIR}" >/dev/null
yann@1352
   207
yann@910
   208
# First of, copy the forced libraries into the working copy
yann@910
   209
if [ -n "${CT_LIB_FILE}" ]; then
yann@1177
   210
    lib_list=$("${sed}" -r -e ':loop; s/#.*//;'         \
yann@1177
   211
                           -e 's/[[:space:]]+//g;'      \
yann@1177
   212
                           -e 's/([^:])$/\1:/;'         \
yann@1177
   213
                           -e '/$/N; s/\n//; tloop;'    \
yann@1177
   214
                        "${CT_LIB_FILE}"
yann@1177
   215
              )
yann@1177
   216
    CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}"             \
yann@1177
   217
                  |"${sed}" -r -e 's/:+/:/g; s/^:+//; s/:+$//;' \
yann@1177
   218
                 )
yann@910
   219
fi
yann@1177
   220
CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}"             \
yann@1177
   221
              |"${sed}" -r -e 's/^:+//; s/:+$//; s/:+/ /g;' \
yann@1177
   222
             )
yann@1352
   223
if [ -n "${CT_LIB_LIST}" ]; then
yann@1352
   224
    ${CT_PRINTF} "Installing forced libraries...\n"
yann@1352
   225
    for name in ${CT_LIB_LIST}; do
yann@1352
   226
        [ -z "${name}" ] && continue
yann@1352
   227
        found=0
yann@1352
   228
        for libname in "lib${name}.so" "${name}.so" "${name}"; do
yann@1352
   229
            ${CT_PRINTF} "  searching for '%s'\n" "${libname}"
yann@1352
   230
            if do_add_lib "${libname}"; then
yann@1352
   231
                found=1
yann@1352
   232
                break
yann@1352
   233
            fi
yann@1352
   234
        done
yann@1352
   235
        if [ ${found} -eq 0 ]; then
yann@1352
   236
            echo "$myname: library '${libname}' not found!"
yann@1352
   237
            [ "${CT_FORCE}" = y ] || exit 1
yann@910
   238
        fi
yann@910
   239
    done
yann@1352
   240
fi
yann@217
   241
yann@217
   242
# Parse the working copy for executables and libraries
yann@217
   243
still_needed=1
yann@217
   244
while [ ${still_needed} -eq 1 ]; do
yann@1352
   245
    ${CT_PRINTF} "Looping...\n"
yann@217
   246
    still_needed=0
yann@1177
   247
    for f in $(find . -type f -exec file {} \;                                              \
yann@1177
   248
               |"${grep}" -E ': ELF [[:digit:]]+-bit (L|M)SB (executable|shared object),'   \
yann@1177
   249
               |cut -d ":" -f 1                                                             \
yann@1177
   250
              ); do
yann@1352
   251
        ${CT_PRINTF} "Scanning '%s'\n" "${f}"
yann@1177
   252
        for libname in $("${CT_READELF}" -d "${f}"                              \
yann@1177
   253
                         |"${grep}" -E '\(NEEDED\)[[:space:]]+Shared library:'  \
yann@1177
   254
                         |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;'                 \
yann@1177
   255
                        ); do
yann@1352
   256
            ${CT_PRINTF} "  searching for '%s'\n" "${libname}"
yann@259
   257
            if [    -e "lib/${libname}"     \
yann@259
   258
                 -o -e "usr/lib/${libname}" ]; then
yann@1352
   259
                ${CT_PRINTF} "    already present\n"
yann@217
   260
                continue
yann@217
   261
            fi
yann@910
   262
            if do_add_lib "${libname}"; then
yann@910
   263
                still_needed=1
yann@910
   264
            else
yann@910
   265
                echo "$myname: library '${libname}' not found!"
yann@910
   266
            fi
yann@217
   267
        done
yann@217
   268
    done
yann@217
   269
done
yann@1352
   270
yann@1352
   271
# OK, we're done. Back off.
yann@910
   272
popd >/dev/null