1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/scripts/populate.in Sun Jan 25 19:26:40 2009 +0000
1.3 @@ -0,0 +1,200 @@
1.4 +#!/bin/bash
1.5 +
1.6 +# This script will populate the root directory with libs from the sysroot.
1.7 +# (C) 2007 Yann E. MORIN
1.8 +# Licensed under the GPL v2
1.9 +
1.10 +# Detect where the toolchain is:
1.11 +BIN_DIR="$(cd "$(dirname "$0")"; pwd)"
1.12 +CT_READELF="${BIN_DIR}/@@CT_TARGET@@-readelf"
1.13 +CT_SYSROOT_DIR="${BIN_DIR}/../@@CT_TARGET@@/sys-root"
1.14 +
1.15 +myname=$(basename "$0")
1.16 +
1.17 +doHelp() {
1.18 + cat <<_EOF_
1.19 +NAME
1.20 + $myname - populate the target root file system
1.21 +
1.22 +SYNOPSIS
1.23 + $myname OPTIONS -s source_root -d destination_root
1.24 +
1.25 +DESCRIPTION
1.26 + $myname will 'populate' your target root file system ('src_dir') with
1.27 + libraries from the toolchain (eg. libc.so...), storing the result into
1.28 + 'dst_dir'.
1.29 +
1.30 +OPTIONS
1.31 + -s src_dir
1.32 + use 'src_dir' as the un-populated (source) root directory
1.33 +
1.34 + -d dst_dir
1.35 + use 'dst_dir' as the place to put the populated root directory
1.36 +
1.37 + -l name1[:name2[...]]
1.38 + Always add the specified shared library/ies name1, name2... from the
1.39 + toolchain (in the sys-root). Actual library names are searched as
1.40 + follows (where 'name' is replaced with the given name) in the
1.41 + sys-root directory:
1.42 + - libname.so
1.43 + - name.so
1.44 + - name
1.45 + If the file is found, then the SONAME of the library is used, and the
1.46 + library is copied with that name. If the library was not found, this
1.47 + yields an error (unless -f was given).
1.48 +
1.49 + -L file
1.50 + Read 'file' for a list of shared libraries to always add from the
1.51 + toolchain. The file should contain one library name per line; text
1.52 + after a # is ignored until the end of the line; spaces are ignored;
1.53 + empty lines are ignored. Libraries are searched for as with -l.
1.54 +
1.55 + -f force execution: if destination directory already exists, it will be
1.56 + removed first; if a specified library (above) was not found, continue.
1.57 +
1.58 + -v Be verbose
1.59 +
1.60 +_EOF_
1.61 +}
1.62 +
1.63 +CT_ROOT_SRC_DIR=
1.64 +CT_ROOT_DST_DIR=
1.65 +CT_LIB_LIST=
1.66 +CT_LIB_FILE=
1.67 +CT_FORCE=no
1.68 +CT_ECHO=true
1.69 +while getopts ":s:d:l:L:fvh" CT_OPT; do
1.70 + case "${CT_OPT}" in
1.71 + s) CT_ROOT_SRC_DIR="${OPTARG}";;
1.72 + d) CT_ROOT_DST_DIR="${OPTARG}";;
1.73 + l) CT_LIB_LIST="${CT_LIB_LIST}:${OPTARG}";;
1.74 + L) CT_LIB_FILE="${OPTARG}";;
1.75 + f) CT_FORCE=y;;
1.76 + v) CT_ECHO=echo;;
1.77 + h) doHelp
1.78 + exit 0
1.79 + ;;
1.80 + :) echo "$myname: '-${OPTARG}' takes exactly one argument."
1.81 + exit 1
1.82 + ;;
1.83 + ?) echo "$myname: unknown option '-${OPTARG}'."
1.84 + exit 1
1.85 + ;;
1.86 + esac
1.87 +done
1.88 +
1.89 +# Sanity checks
1.90 +if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
1.91 + doHelp
1.92 + exit 1
1.93 +fi
1.94 +if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
1.95 + echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
1.96 + exit 1
1.97 +fi
1.98 +if [ -d "${CT_ROOT_DST_DIR}" -a "${CT_FORCE}" != "y" ]; then
1.99 + echo "$myname: '${CT_ROOT_DST_DIR}': already exists"
1.100 + exit 1
1.101 +fi
1.102 +src_inode=$(ls -di "${CT_ROOT_SRC_DIR}")
1.103 +dst_inode=$(ls -di "${CT_ROOT_DST_DIR}" 2>/dev/null)
1.104 +if [ "${src_inode}" = "${dst_inode}" ]; then
1.105 + echo "$myname: source and destination are the same!"
1.106 + exit 1
1.107 +fi
1.108 +
1.109 +# Check existence of the forced libraries file
1.110 +if [ -n "${CT_LIB_FILE}" -a ! \( -f "${CT_LIB_FILE}" -a -r "${CT_LIB_FILE}" \) ]; then
1.111 + echo "$myname: forced libraries file '${CT_LIB_FILE}' not found!"
1.112 + exit 1
1.113 +fi
1.114 +
1.115 +# Get rid of potentially older destination directory
1.116 +if [ -d "${CT_ROOT_DST_DIR}" ]; then
1.117 + mv "${CT_ROOT_DST_DIR}" "${CT_ROOT_DST_DIR}.$$"
1.118 + setsid nohup rm -rf "${CT_ROOT_DST_DIR}.$$" >/dev/null 2>&1 &
1.119 +fi
1.120 +
1.121 +# Create the working copy
1.122 +mkdir -p "${CT_ROOT_DST_DIR}"
1.123 +
1.124 +# Make all path absolute
1.125 +CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
1.126 +CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
1.127 +
1.128 +pushd "${CT_ROOT_SRC_DIR}" >/dev/null
1.129 +tar cf - . |(cd "${CT_ROOT_DST_DIR}"; tar xf -)
1.130 +popd >/dev/null
1.131 +
1.132 +# A function do search for a library
1.133 +# Usage: do_add_lib libname
1.134 +# returns: 0 if library was found and added, !0 otherwise
1.135 +do_add_lib() {
1.136 + local libname="$1"
1.137 + local ret=1
1.138 + local true_libname
1.139 + for dir in . usr; do
1.140 + ${CT_ECHO} -n " trying in '${dir}'"
1.141 + libfile="${CT_SYSROOT_DIR}/${dir}/lib/${libname}"
1.142 + ${CT_ECHO} ": '${libfile}'"
1.143 + if [ -e "${libfile}" ]; then
1.144 + mkdir -p "${dir}/lib"
1.145 + true_libname=$("${CT_READELF}" -d "${libfile}" |egrep "SONAME" |sed -r -e 's,.+\[(.+)\] *$,\1,;')
1.146 + ${CT_ECHO} " installing as '${dir}/lib/${true_libname}'"
1.147 + cat "${libfile}" >"${dir}/lib/${true_libname}"
1.148 + ret=0
1.149 + break
1.150 + fi
1.151 + done
1.152 + return ${ret}
1.153 +}
1.154 +
1.155 +# First of, copy the forced libraries into the working copy
1.156 +if [ -n "${CT_LIB_FILE}" ]; then
1.157 + lib_list=$(sed -r -e ':loop; s/#.*//; s/[[:space:]]+//g; s/([^:])$/\1:/; /$/N; s/\n//; tloop;' "${CT_LIB_FILE}")
1.158 + CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}" |sed -r -e 's/:+/:/g; s/^:+//; s/:+$//;')
1.159 +fi
1.160 +CT_LIB_LIST="${CT_LIB_LIST//:/ }"
1.161 +${CT_ECHO} "Installing forced libraries..."
1.162 +pushd "${CT_ROOT_DST_DIR}" >/dev/null
1.163 +for name in ${CT_LIB_LIST}; do
1.164 + [ -z "${name}" ] && continue
1.165 + found=0
1.166 + for libname in "lib${name}.so" "${name}.so" "${name}"; do
1.167 + ${CT_ECHO} " searching for '${libname}'"
1.168 + if do_add_lib "${libname}"; then
1.169 + found=1
1.170 + break
1.171 + fi
1.172 + done
1.173 + if [ ${found} -eq 0 ]; then
1.174 + echo "$myname: library '${libname}' not found!"
1.175 + [ "${CT_FORCE}" = y ] || exit 1
1.176 + fi
1.177 +done
1.178 +popd >/dev/null
1.179 +
1.180 +# Parse the working copy for executables and libraries
1.181 +pushd "${CT_ROOT_DST_DIR}" >/dev/null
1.182 +still_needed=1
1.183 +while [ ${still_needed} -eq 1 ]; do
1.184 + ${CT_ECHO} "Looping..."
1.185 + still_needed=0
1.186 + for f in $(find . -type f -exec file {} \; |egrep ': ELF [[:digit:]]+-bit .SB (executable|shared object),' |cut -d ":" -f 1); do
1.187 + ${CT_ECHO} "Scanning '${f}'"
1.188 + for libname in $("${CT_READELF}" -d "${f}" |egrep '(NEEDED)' |sed -r -e 's,.+\[(.+)\] *$,\1,;'); do
1.189 + ${CT_ECHO} " searching for '${libname}'"
1.190 + if [ -e "lib/${libname}" \
1.191 + -o -e "usr/lib/${libname}" ]; then
1.192 + ${CT_ECHO} " already present"
1.193 + continue
1.194 + fi
1.195 + if do_add_lib "${libname}"; then
1.196 + still_needed=1
1.197 + else
1.198 + echo "$myname: library '${libname}' not found!"
1.199 + fi
1.200 + done
1.201 + done
1.202 +done
1.203 +popd >/dev/null