2009-01-29 22:35:26 +00:00
|
|
|
#!@@CT_bash@@
|
2007-07-08 17:44:59 +00:00
|
|
|
# This script will populate the root directory with libs from the sysroot.
|
|
|
|
# (C) 2007 Yann E. MORIN
|
|
|
|
# Licensed under the GPL v2
|
2009-01-29 22:35:26 +00:00
|
|
|
set -e
|
2007-07-08 17:44:59 +00:00
|
|
|
|
2008-08-04 08:44:54 +00:00
|
|
|
# Detect where the toolchain is:
|
2009-01-29 22:35:26 +00:00
|
|
|
CT_PREFIX_DIR="$(cd "$(dirname "$0")/.."; pwd)"
|
|
|
|
CT_BIN_DIR="${CT_PREFIX_DIR}/bin"
|
|
|
|
CT_READELF="${CT_BIN_DIR}/@@CT_TARGET@@-readelf"
|
|
|
|
CT_LIB_DIR="${CT_PREFIX_DIR}/lib"
|
|
|
|
CT_SYSROOT_DIR="$(cd "${CT_BIN_DIR}/../@@CT_TARGET@@/sys-root"; pwd)"
|
2007-07-08 17:44:59 +00:00
|
|
|
|
|
|
|
myname=$(basename "$0")
|
|
|
|
|
2009-06-11 21:47:19 +00:00
|
|
|
# Use the tools discovered by crosstool-NG's ./configure:
|
2009-02-01 22:41:16 +00:00
|
|
|
install="@@CT_install@@"
|
|
|
|
grep="@@CT_grep@@"
|
|
|
|
sed="@@CT_sed@@"
|
2009-01-29 22:35:26 +00:00
|
|
|
|
2007-07-08 17:44:59 +00:00
|
|
|
doHelp() {
|
|
|
|
cat <<_EOF_
|
2008-10-08 11:57:03 +00:00
|
|
|
NAME
|
|
|
|
$myname - populate the target root file system
|
2007-07-08 17:44:59 +00:00
|
|
|
|
2008-10-08 11:57:03 +00:00
|
|
|
SYNOPSIS
|
|
|
|
$myname OPTIONS -s source_root -d destination_root
|
2007-07-08 17:44:59 +00:00
|
|
|
|
2008-10-08 11:57:03 +00:00
|
|
|
DESCRIPTION
|
|
|
|
$myname will 'populate' your target root file system ('src_dir') with
|
|
|
|
libraries from the toolchain (eg. libc.so...), storing the result into
|
|
|
|
'dst_dir'.
|
2007-07-08 17:44:59 +00:00
|
|
|
|
2008-10-08 11:57:03 +00:00
|
|
|
OPTIONS
|
|
|
|
-s src_dir
|
|
|
|
use 'src_dir' as the un-populated (source) root directory
|
|
|
|
|
|
|
|
-d dst_dir
|
|
|
|
use 'dst_dir' as the place to put the populated root directory
|
|
|
|
|
|
|
|
-l name1[:name2[...]]
|
|
|
|
Always add the specified shared library/ies name1, name2... from the
|
|
|
|
toolchain (in the sys-root). Actual library names are searched as
|
|
|
|
follows (where 'name' is replaced with the given name) in the
|
|
|
|
sys-root directory:
|
|
|
|
- libname.so
|
|
|
|
- name.so
|
|
|
|
- name
|
|
|
|
If the file is found, then the SONAME of the library is used, and the
|
|
|
|
library is copied with that name. If the library was not found, this
|
|
|
|
yields an error (unless -f was given).
|
|
|
|
|
|
|
|
-L file
|
|
|
|
Read 'file' for a list of shared libraries to always add from the
|
|
|
|
toolchain. The file should contain one library name per line; text
|
|
|
|
after a # is ignored until the end of the line; spaces are ignored;
|
|
|
|
empty lines are ignored. Libraries are searched for as with -l.
|
|
|
|
|
|
|
|
-f force execution: if destination directory already exists, it will be
|
|
|
|
removed first; if a specified library (above) was not found, continue.
|
2007-07-14 20:43:51 +00:00
|
|
|
|
2009-06-11 21:47:19 +00:00
|
|
|
-v Be verbose. By default, populate is absolutely silent.
|
2008-10-08 11:57:03 +00:00
|
|
|
|
2007-07-08 17:44:59 +00:00
|
|
|
_EOF_
|
|
|
|
}
|
|
|
|
|
|
|
|
CT_ROOT_SRC_DIR=
|
|
|
|
CT_ROOT_DST_DIR=
|
2008-10-08 11:57:03 +00:00
|
|
|
CT_LIB_LIST=
|
|
|
|
CT_LIB_FILE=
|
2007-07-14 21:21:55 +00:00
|
|
|
CT_FORCE=no
|
2009-06-11 21:47:19 +00:00
|
|
|
CT_PRINTF=:
|
2009-01-29 22:35:26 +00:00
|
|
|
OPTIND=1
|
2008-10-08 11:57:03 +00:00
|
|
|
while getopts ":s:d:l:L:fvh" CT_OPT; do
|
2007-07-08 17:44:59 +00:00
|
|
|
case "${CT_OPT}" in
|
|
|
|
s) CT_ROOT_SRC_DIR="${OPTARG}";;
|
|
|
|
d) CT_ROOT_DST_DIR="${OPTARG}";;
|
2008-10-08 11:57:03 +00:00
|
|
|
l) CT_LIB_LIST="${CT_LIB_LIST}:${OPTARG}";;
|
|
|
|
L) CT_LIB_FILE="${OPTARG}";;
|
2007-07-14 21:21:55 +00:00
|
|
|
f) CT_FORCE=y;;
|
2009-06-11 21:47:19 +00:00
|
|
|
v) CT_PRINTF=printf;;
|
2007-07-08 17:44:59 +00:00
|
|
|
h) doHelp
|
|
|
|
exit 0
|
|
|
|
;;
|
2008-05-20 21:32:39 +00:00
|
|
|
:) echo "$myname: '-${OPTARG}' takes exactly one argument."
|
2007-07-08 17:44:59 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
2008-05-20 21:32:39 +00:00
|
|
|
?) echo "$myname: unknown option '-${OPTARG}'."
|
2007-07-08 17:44:59 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Sanity checks
|
|
|
|
if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
|
|
|
|
doHelp
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
|
2008-05-20 21:32:39 +00:00
|
|
|
echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
|
2007-07-08 17:44:59 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2007-07-14 21:21:55 +00:00
|
|
|
if [ -d "${CT_ROOT_DST_DIR}" -a "${CT_FORCE}" != "y" ]; then
|
2008-05-20 21:32:39 +00:00
|
|
|
echo "$myname: '${CT_ROOT_DST_DIR}': already exists"
|
2007-07-08 17:44:59 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2009-06-11 21:47:19 +00:00
|
|
|
src_inode=$(stat -c '%i' "${CT_ROOT_SRC_DIR}/.")
|
|
|
|
dst_inode=$(stat -c '%i' "${CT_ROOT_DST_DIR}/." 2>/dev/null || true)
|
|
|
|
if [ "${src_inode}" -eq "$((dst_inode+0))" ]; then
|
2007-07-08 17:44:59 +00:00
|
|
|
echo "$myname: source and destination are the same!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2008-10-08 11:57:03 +00:00
|
|
|
# Check existence of the forced libraries file
|
|
|
|
if [ -n "${CT_LIB_FILE}" -a ! \( -f "${CT_LIB_FILE}" -a -r "${CT_LIB_FILE}" \) ]; then
|
|
|
|
echo "$myname: forced libraries file '${CT_LIB_FILE}' not found!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2007-07-08 17:44:59 +00:00
|
|
|
# Get rid of potentially older destination directory
|
2009-01-29 22:35:26 +00:00
|
|
|
rm -rf "${CT_ROOT_DST_DIR}"
|
2007-07-08 17:44:59 +00:00
|
|
|
|
|
|
|
# Create the working copy
|
|
|
|
mkdir -p "${CT_ROOT_DST_DIR}"
|
|
|
|
|
|
|
|
# Make all path absolute
|
2007-07-14 13:09:17 +00:00
|
|
|
CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
|
|
|
|
CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
|
2007-07-08 17:44:59 +00:00
|
|
|
|
2010-01-02 17:00:54 +00:00
|
|
|
# Populate the destination directory with files from the source directory
|
2008-10-08 11:57:03 +00:00
|
|
|
pushd "${CT_ROOT_SRC_DIR}" >/dev/null
|
2010-01-02 17:00:54 +00:00
|
|
|
cp -a . "${CT_ROOT_DST_DIR}"
|
2008-10-08 11:57:03 +00:00
|
|
|
popd >/dev/null
|
|
|
|
|
|
|
|
# A function do search for a library
|
|
|
|
# Usage: do_add_lib libname
|
|
|
|
# returns: 0 if library was found and added, !0 otherwise
|
|
|
|
do_add_lib() {
|
|
|
|
local libname="$1"
|
|
|
|
local true_libname
|
2009-01-29 22:35:26 +00:00
|
|
|
local dir
|
2009-06-11 21:47:19 +00:00
|
|
|
local mode
|
|
|
|
|
2009-01-29 22:35:26 +00:00
|
|
|
for dir in lib usr/lib; do
|
2009-06-11 21:47:19 +00:00
|
|
|
${CT_PRINTF} " trying in '%s'" "${dir}"
|
2009-01-29 22:35:26 +00:00
|
|
|
libfile="${CT_SYSROOT_DIR}/${dir}/${libname}"
|
2009-06-11 21:47:19 +00:00
|
|
|
${CT_PRINTF} ": '%s'\n" "${libfile}"
|
2008-10-08 11:57:03 +00:00
|
|
|
if [ -e "${libfile}" ]; then
|
2009-01-29 22:35:26 +00:00
|
|
|
mkdir -p "${dir}"
|
|
|
|
true_libname=$("${CT_READELF}" -d "${libfile}" \
|
|
|
|
|"${grep}" "Library soname:" \
|
|
|
|
|"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;' \
|
|
|
|
)
|
2009-06-11 21:47:19 +00:00
|
|
|
case "${libfile}" in
|
|
|
|
*/ld*) mode=0755;;
|
|
|
|
*) mode=0644;;
|
|
|
|
esac
|
|
|
|
${CT_PRINTF} " installing as '%s/%s', mode='%s'\n" "${dir}" "${true_libname}" "${mode}"
|
|
|
|
"${install}" -m "${mode}" "${libfile}" "${dir}/${true_libname}"
|
2009-01-29 22:35:26 +00:00
|
|
|
return 0
|
2008-10-08 11:57:03 +00:00
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2009-01-29 22:35:26 +00:00
|
|
|
return 1
|
2008-10-08 11:57:03 +00:00
|
|
|
}
|
|
|
|
|
2009-06-11 21:47:19 +00:00
|
|
|
# We'll work in the copied rootfs
|
|
|
|
pushd "${CT_ROOT_DST_DIR}" >/dev/null
|
|
|
|
|
2008-10-08 11:57:03 +00:00
|
|
|
# First of, copy the forced libraries into the working copy
|
|
|
|
if [ -n "${CT_LIB_FILE}" ]; then
|
2009-01-29 22:35:26 +00:00
|
|
|
lib_list=$("${sed}" -r -e ':loop; s/#.*//;' \
|
|
|
|
-e 's/[[:space:]]+//g;' \
|
|
|
|
-e 's/([^:])$/\1:/;' \
|
|
|
|
-e '/$/N; s/\n//; tloop;' \
|
|
|
|
"${CT_LIB_FILE}"
|
|
|
|
)
|
|
|
|
CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}" \
|
|
|
|
|"${sed}" -r -e 's/:+/:/g; s/^:+//; s/:+$//;' \
|
|
|
|
)
|
2008-10-08 11:57:03 +00:00
|
|
|
fi
|
2009-01-29 22:35:26 +00:00
|
|
|
CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}" \
|
|
|
|
|"${sed}" -r -e 's/^:+//; s/:+$//; s/:+/ /g;' \
|
|
|
|
)
|
2009-06-11 21:47:19 +00:00
|
|
|
if [ -n "${CT_LIB_LIST}" ]; then
|
|
|
|
${CT_PRINTF} "Installing forced libraries...\n"
|
|
|
|
for name in ${CT_LIB_LIST}; do
|
|
|
|
[ -z "${name}" ] && continue
|
|
|
|
found=0
|
|
|
|
for libname in "lib${name}.so" "${name}.so" "${name}"; do
|
|
|
|
${CT_PRINTF} " searching for '%s'\n" "${libname}"
|
|
|
|
if do_add_lib "${libname}"; then
|
|
|
|
found=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ ${found} -eq 0 ]; then
|
|
|
|
echo "$myname: library '${libname}' not found!"
|
|
|
|
[ "${CT_FORCE}" = y ] || exit 1
|
2008-10-08 11:57:03 +00:00
|
|
|
fi
|
|
|
|
done
|
2009-06-11 21:47:19 +00:00
|
|
|
fi
|
2007-07-08 17:44:59 +00:00
|
|
|
|
|
|
|
# Parse the working copy for executables and libraries
|
|
|
|
still_needed=1
|
|
|
|
while [ ${still_needed} -eq 1 ]; do
|
2009-06-11 21:47:19 +00:00
|
|
|
${CT_PRINTF} "Looping...\n"
|
2007-07-08 17:44:59 +00:00
|
|
|
still_needed=0
|
2009-01-29 22:35:26 +00:00
|
|
|
for f in $(find . -type f -exec file {} \; \
|
|
|
|
|"${grep}" -E ': ELF [[:digit:]]+-bit (L|M)SB (executable|shared object),' \
|
|
|
|
|cut -d ":" -f 1 \
|
|
|
|
); do
|
2009-06-11 21:47:19 +00:00
|
|
|
${CT_PRINTF} "Scanning '%s'\n" "${f}"
|
2009-01-29 22:35:26 +00:00
|
|
|
for libname in $("${CT_READELF}" -d "${f}" \
|
|
|
|
|"${grep}" -E '\(NEEDED\)[[:space:]]+Shared library:' \
|
|
|
|
|"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;' \
|
|
|
|
); do
|
2009-06-11 21:47:19 +00:00
|
|
|
${CT_PRINTF} " searching for '%s'\n" "${libname}"
|
2007-07-14 21:21:55 +00:00
|
|
|
if [ -e "lib/${libname}" \
|
|
|
|
-o -e "usr/lib/${libname}" ]; then
|
2009-06-11 21:47:19 +00:00
|
|
|
${CT_PRINTF} " already present\n"
|
2007-07-08 17:44:59 +00:00
|
|
|
continue
|
|
|
|
fi
|
2008-10-08 11:57:03 +00:00
|
|
|
if do_add_lib "${libname}"; then
|
|
|
|
still_needed=1
|
|
|
|
else
|
|
|
|
echo "$myname: library '${libname}' not found!"
|
|
|
|
fi
|
2007-07-08 17:44:59 +00:00
|
|
|
done
|
|
|
|
done
|
|
|
|
done
|
2009-06-11 21:47:19 +00:00
|
|
|
|
|
|
|
# OK, we're done. Back off.
|
2008-10-08 11:57:03 +00:00
|
|
|
popd >/dev/null
|