2010-03-28 21:01:19 +00:00
|
|
|
#!/bin/sh
|
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
|
|
|
|
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@@"
|
2015-11-17 10:48:09 +00:00
|
|
|
awk="@@CT_awk@@"
|
2009-01-29 22:35:26 +00:00
|
|
|
|
2010-03-25 21:42:00 +00:00
|
|
|
# Detect where the toolchain is:
|
|
|
|
CT_PREFIX_DIR="$(cd "$(dirname "$0")/.."; pwd)"
|
|
|
|
CT_GCC="${0%-populate}-gcc"
|
|
|
|
CT_READELF="${0%-populate}-readelf"
|
|
|
|
CT_CFG_PREFIX_DIR="$("${CT_GCC}" -v 2>&1 \
|
|
|
|
|tr ' ' '\n' \
|
|
|
|
|"${grep}" -E -- '--prefix=' \
|
|
|
|
|cut -d = -f 2-
|
|
|
|
)"
|
|
|
|
CT_CFG_SYSROOT_DIR="$("${CT_GCC}" -v 2>&1 \
|
|
|
|
|tr ' ' '\n' \
|
|
|
|
|"${grep}" -E -- '--with-sysroot=' \
|
|
|
|
|cut -d = -f 2-
|
|
|
|
)"
|
2010-03-28 21:01:19 +00:00
|
|
|
CT_SYSROOT_DIR="$(printf "${CT_CFG_SYSROOT_DIR}\n" \
|
2010-03-25 21:42:00 +00:00
|
|
|
|"${sed}" -r -e "s:^${CT_CFG_PREFIX_DIR}:${CT_PREFIX_DIR}:;" \
|
|
|
|
|"${sed}" -r -e 's,/+,/,g;' \
|
|
|
|
)"
|
|
|
|
|
|
|
|
myname=$(basename "$0")
|
|
|
|
|
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
|
2010-03-28 21:01:19 +00:00
|
|
|
This script will 'populate' your target root file system 'source_root'
|
|
|
|
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
|
2010-03-28 21:01:19 +00:00
|
|
|
Use 'src_dir' as the un-populated (source) root directory.
|
2008-10-08 11:57:03 +00:00
|
|
|
|
|
|
|
-d dst_dir
|
2010-03-28 21:01:19 +00:00
|
|
|
Use 'dst_dir' as the place to put the populated root directory.
|
|
|
|
See the -f and -m options, below, on the required (non-)existence
|
|
|
|
of this directory.
|
2008-10-08 11:57:03 +00:00
|
|
|
|
2010-03-23 18:27:41 +00:00
|
|
|
-r sysroot_dir
|
2010-03-28 21:01:19 +00:00
|
|
|
Use 'sysroot_dir' as the sysroot instead of the toolchain default.
|
2010-03-23 18:27:41 +00:00
|
|
|
|
2008-10-08 11:57:03 +00:00
|
|
|
-l name1[:name2[...]]
|
|
|
|
Always add the specified shared library/ies name1, name2... from the
|
2011-01-25 19:31:16 +00:00
|
|
|
toolchain (in the sysroot). Actual library names are searched as
|
2008-10-08 11:57:03 +00:00
|
|
|
follows (where 'name' is replaced with the given name) in the
|
2011-01-25 19:31:16 +00:00
|
|
|
sysroot directory:
|
2008-10-08 11:57:03 +00:00
|
|
|
- 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.
|
|
|
|
|
2010-03-28 21:01:19 +00:00
|
|
|
-f Force execution: if destination directory already exists, it will be
|
2008-10-08 11:57:03 +00:00
|
|
|
removed first; if a specified library (above) was not found, continue.
|
2010-03-24 21:36:51 +00:00
|
|
|
Note: if using -m and the destination directory already exists, it
|
|
|
|
is *not* removed, see below.
|
|
|
|
|
|
|
|
-m Merge the source root directory with the destination root directory.
|
|
|
|
If the latter does not exist, it is created, and -m is ignored.
|
2010-03-28 21:01:19 +00:00
|
|
|
If the destination root directory exists, then the content of the
|
2010-03-24 21:36:51 +00:00
|
|
|
source root directory is copied in there, and the result is populated
|
|
|
|
as usual.
|
2011-07-17 14:53:40 +00:00
|
|
|
It can be useful if constructing a rootfs incrementally from many
|
2010-03-24 21:36:51 +00:00
|
|
|
smaller source root directories, or if your destination root directory
|
|
|
|
is an NFS export that your target mounts as / (and you don't want to
|
|
|
|
re-run exportfs -av everytime). USE WITH CARE!
|
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=
|
2010-03-24 21:36:51 +00:00
|
|
|
CT_MERGE=
|
|
|
|
CT_FORCE=
|
2009-06-11 21:47:19 +00:00
|
|
|
CT_PRINTF=:
|
2009-01-29 22:35:26 +00:00
|
|
|
OPTIND=1
|
2010-03-24 21:36:51 +00:00
|
|
|
while getopts ":s:d:r:l:L:fmvh" 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}";;
|
2010-03-23 18:27:41 +00:00
|
|
|
r) CT_SYSROOT_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;;
|
2010-03-24 21:36:51 +00:00
|
|
|
m) CT_MERGE=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
|
|
|
|
;;
|
2010-03-28 21:01:19 +00:00
|
|
|
:) printf "$myname: '-${OPTARG}' takes exactly one argument.\n"
|
2007-07-08 17:44:59 +00:00
|
|
|
exit 1
|
|
|
|
;;
|
2010-03-28 21:01:19 +00:00
|
|
|
?) printf "$myname: unknown option '-${OPTARG}'.\n"
|
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
|
2010-03-28 21:01:19 +00:00
|
|
|
printf "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory\n"
|
2007-07-08 17:44:59 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2010-03-23 18:27:41 +00:00
|
|
|
if [ ! -d "${CT_SYSROOT_DIR}" ]; then
|
2010-03-28 21:01:19 +00:00
|
|
|
printf "$myname: '${CT_SYSROOT_DIR}': no such file or directory\n"
|
2010-03-23 18:27:41 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2010-03-24 21:36:51 +00:00
|
|
|
# If the dest dir does not exist, all is well
|
|
|
|
# If merging, we accept an existing dest directory
|
|
|
|
# If forcing and not merging, we remove an exiting dest directory
|
|
|
|
# If not forcing and not merging, we do not accept an exiting dest directory
|
|
|
|
if [ -d "${CT_ROOT_DST_DIR}" ]; then
|
|
|
|
case "${CT_FORCE}:${CT_MERGE}" in
|
|
|
|
*:y) ;;
|
|
|
|
y:) rm -rf "${CT_ROOT_DST_DIR}";;
|
2010-03-28 21:01:19 +00:00
|
|
|
:) printf "$myname: '${CT_ROOT_DST_DIR}': already exists\n"
|
|
|
|
exit 1
|
|
|
|
;;
|
2010-03-24 21:36:51 +00:00
|
|
|
esac
|
2007-07-08 17:44:59 +00:00
|
|
|
fi
|
2015-11-17 10:48:09 +00:00
|
|
|
src_inode=$(ls -1id "${CT_ROOT_SRC_DIR}/." |${awk} '{ print $1 }')
|
|
|
|
dst_inode=$(ls -1id "${CT_ROOT_DST_DIR}/." 2>/dev/null |${awk} '{ print $1 }')
|
2009-06-11 21:47:19 +00:00
|
|
|
if [ "${src_inode}" -eq "$((dst_inode+0))" ]; then
|
2010-03-28 21:01:19 +00:00
|
|
|
printf "$myname: source and destination are the same!\n"
|
2007-07-08 17:44:59 +00:00
|
|
|
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
|
2010-03-28 21:01:19 +00:00
|
|
|
printf "$myname: forced libraries file '${CT_LIB_FILE}' not found!\n"
|
2008-10-08 11:57:03 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2010-03-24 21:36:51 +00:00
|
|
|
# Create the working copy, no issue if already existing
|
2007-07-08 17:44:59 +00:00
|
|
|
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)
|
2010-03-28 21:01:19 +00:00
|
|
|
CT_SYSROOT_DIR=$(cd "${CT_SYSROOT_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
|
2010-03-28 21:01:19 +00:00
|
|
|
cd "${CT_ROOT_SRC_DIR}"
|
2010-01-02 17:00:54 +00:00
|
|
|
cp -a . "${CT_ROOT_DST_DIR}"
|
2010-03-28 21:01:19 +00:00
|
|
|
cd - >/dev/null
|
2008-10-08 11:57:03 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2015-05-31 19:17:17 +00:00
|
|
|
for dir in lib usr/lib usr/lib/gconv; do
|
2010-03-28 22:15:32 +00:00
|
|
|
if [ -e "${dir}/${libname}" ]; then
|
|
|
|
${CT_PRINTF} " already present\n"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
done
|
2015-05-31 19:17:17 +00:00
|
|
|
for dir in lib usr/lib usr/lib/gconv; 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}"
|
2012-04-27 01:55:59 +00:00
|
|
|
${install} -m "${mode}" "${libfile}" "${dir}/${true_libname}"
|
2010-03-28 22:15:32 +00:00
|
|
|
do_resolve_deps "${dir}/${true_libname}"
|
2009-01-29 22:35:26 +00:00
|
|
|
return 0
|
2008-10-08 11:57:03 +00:00
|
|
|
fi
|
|
|
|
done
|
2009-01-29 22:35:26 +00:00
|
|
|
return 1
|
2008-10-08 11:57:03 +00:00
|
|
|
}
|
|
|
|
|
2010-03-28 22:15:32 +00:00
|
|
|
# A function to resolve all NEEDED entries for the given file, relative
|
|
|
|
# to the working directory (eg. dst_dir)
|
|
|
|
# Usage: do_resolve_deps some/where/some/file
|
|
|
|
# Returns: 0, meaning all dependencies were found
|
|
|
|
# If not all dependencies could be found, exists with error code 1
|
|
|
|
# (unless forced)
|
|
|
|
do_resolve_deps() {
|
|
|
|
local file="${1}"
|
|
|
|
local libname
|
|
|
|
|
|
|
|
for libname in $("${CT_READELF}" -d "${file}" \
|
|
|
|
|"${grep}" -E '\(NEEDED\)[[:space:]]+Shared library:' \
|
|
|
|
|"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;' \
|
|
|
|
); do
|
|
|
|
[ -n "${libname}" ] || continue
|
|
|
|
${CT_PRINTF} "Searching for '%s' needed by '%s'\n" "${libname}" "${file}"
|
|
|
|
if ! do_add_lib "${libname}"; then
|
|
|
|
printf "$myname: library '${libname}' not found!\n"
|
|
|
|
[ "${CT_FORCE}" = "y" ] || exit 1
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2009-06-11 21:47:19 +00:00
|
|
|
# We'll work in the copied rootfs
|
2010-03-28 21:01:19 +00:00
|
|
|
cd "${CT_ROOT_DST_DIR}"
|
2009-06-11 21:47:19 +00:00
|
|
|
|
2008-10-08 11:57:03 +00:00
|
|
|
# First of, copy the forced libraries into the working copy
|
2010-03-26 22:17:23 +00:00
|
|
|
lib_list=
|
2008-10-08 11:57:03 +00:00
|
|
|
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}"
|
|
|
|
)
|
2008-10-08 11:57:03 +00:00
|
|
|
fi
|
2010-03-28 21:01:19 +00:00
|
|
|
CT_LIB_LIST=$(printf "${CT_LIB_LIST}:${lib_list}\n" \
|
2009-01-29 22:35:26 +00:00
|
|
|
|"${sed}" -r -e 's/^:+//; s/:+$//; s/:+/ /g;' \
|
|
|
|
)
|
2009-06-11 21:47:19 +00:00
|
|
|
if [ -n "${CT_LIB_LIST}" ]; then
|
|
|
|
for name in ${CT_LIB_LIST}; do
|
|
|
|
[ -z "${name}" ] && continue
|
|
|
|
found=0
|
|
|
|
for libname in "lib${name}.so" "${name}.so" "${name}"; do
|
2010-03-28 22:15:32 +00:00
|
|
|
${CT_PRINTF} "Searching for forced library '%s'\n" "${libname}"
|
2009-06-11 21:47:19 +00:00
|
|
|
if do_add_lib "${libname}"; then
|
|
|
|
found=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ ${found} -eq 0 ]; then
|
2010-03-28 21:01:19 +00:00
|
|
|
printf "$myname: library '${libname}' not found!\n"
|
|
|
|
[ "${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
|
|
|
|
2010-03-28 22:15:32 +00:00
|
|
|
# Create a temporary place where to store... temp files.
|
|
|
|
rand="$( dd if=/dev/urandom bs=1024 count=1 2>/dev/null \
|
|
|
|
|md5sum \
|
2015-11-17 10:48:09 +00:00
|
|
|
|${awk} '{ print $1; }'
|
2010-03-28 22:15:32 +00:00
|
|
|
)"
|
|
|
|
CT_TMP_DIR="${TMPDIR:-/tmp}/populate-${rand}-${$}"
|
|
|
|
( umask 0077; mkdir "${CT_TMP_DIR}" ) || { printf "Could not create temporary directory\n"; exit 1; }
|
|
|
|
trap "rm -rf ${CT_TMP_DIR}" EXIT
|
|
|
|
|
|
|
|
# List all ELF (executables|shared objects)...
|
|
|
|
find . -type f -exec file {} \; \
|
2014-09-08 16:53:18 +00:00
|
|
|
|"${grep}" -E ': ELF [[:digit:]]+-bit (L|M)SB +(executable|shared object),' \
|
2010-03-28 22:15:32 +00:00
|
|
|
|cut -d ":" -f 1 \
|
|
|
|
>"${CT_TMP_DIR}/files.list"
|
|
|
|
|
|
|
|
# ... and use that list to find missing dependencies
|
|
|
|
while read file; do
|
|
|
|
do_resolve_deps "${file}"
|
|
|
|
done <"${CT_TMP_DIR}/files.list"
|
|
|
|
|
|
|
|
rm -rf "${CT_TMP_DIR}"
|
|
|
|
trap - EXIT
|
2009-06-11 21:47:19 +00:00
|
|
|
|
|
|
|
# OK, we're done. Back off.
|
2010-03-28 21:01:19 +00:00
|
|
|
cd - >/dev/null
|