2 # This script will populate the root directory with libs from the sysroot.
3 # (C) 2007 Yann E. MORIN
4 # Licensed under the GPL v2
7 # Detect where the toolchain is:
8 CT_PREFIX_DIR="$(cd "$(dirname "$0")/.."; pwd)"
9 CT_BIN_DIR="${CT_PREFIX_DIR}/bin"
10 CT_READELF="${CT_BIN_DIR}/@@CT_TARGET@@-readelf"
11 CT_LIB_DIR="${CT_PREFIX_DIR}/lib"
12 CT_SYSROOT_DIR="$(cd "${CT_BIN_DIR}/../@@CT_TARGET@@/sys-root"; pwd)"
14 myname=$(basename "$0")
16 # Use the tools discovered by crosstool-NG's ./configure:
17 install="@@CT_install@@"
24 $myname - populate the target root file system
27 $myname OPTIONS -s source_root -d destination_root
30 $myname will 'populate' your target root file system ('src_dir') with
31 libraries from the toolchain (eg. libc.so...), storing the result into
36 use 'src_dir' as the un-populated (source) root directory
39 use 'dst_dir' as the place to put the populated root directory
42 Always add the specified shared library/ies name1, name2... from the
43 toolchain (in the sys-root). Actual library names are searched as
44 follows (where 'name' is replaced with the given name) in the
49 If the file is found, then the SONAME of the library is used, and the
50 library is copied with that name. If the library was not found, this
51 yields an error (unless -f was given).
54 Read 'file' for a list of shared libraries to always add from the
55 toolchain. The file should contain one library name per line; text
56 after a # is ignored until the end of the line; spaces are ignored;
57 empty lines are ignored. Libraries are searched for as with -l.
59 -f force execution: if destination directory already exists, it will be
60 removed first; if a specified library (above) was not found, continue.
62 -v Be verbose. By default, populate is absolutely silent.
74 while getopts ":s:d:l:L:fvh" CT_OPT; do
76 s) CT_ROOT_SRC_DIR="${OPTARG}";;
77 d) CT_ROOT_DST_DIR="${OPTARG}";;
78 l) CT_LIB_LIST="${CT_LIB_LIST}:${OPTARG}";;
79 L) CT_LIB_FILE="${OPTARG}";;
85 :) echo "$myname: '-${OPTARG}' takes exactly one argument."
88 ?) echo "$myname: unknown option '-${OPTARG}'."
95 if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
99 if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
100 echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
103 if [ -d "${CT_ROOT_DST_DIR}" -a "${CT_FORCE}" != "y" ]; then
104 echo "$myname: '${CT_ROOT_DST_DIR}': already exists"
107 src_inode=$(stat -c '%i' "${CT_ROOT_SRC_DIR}/.")
108 dst_inode=$(stat -c '%i' "${CT_ROOT_DST_DIR}/." 2>/dev/null || true)
109 if [ "${src_inode}" -eq "$((dst_inode+0))" ]; then
110 echo "$myname: source and destination are the same!"
114 # Check existence of the forced libraries file
115 if [ -n "${CT_LIB_FILE}" -a ! \( -f "${CT_LIB_FILE}" -a -r "${CT_LIB_FILE}" \) ]; then
116 echo "$myname: forced libraries file '${CT_LIB_FILE}' not found!"
120 # Get rid of potentially older destination directory
121 rm -rf "${CT_ROOT_DST_DIR}"
123 # Create the working copy
124 mkdir -p "${CT_ROOT_DST_DIR}"
126 # Make all path absolute
127 CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
128 CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
130 # Populate the destination directory with files from the source directory
131 pushd "${CT_ROOT_SRC_DIR}" >/dev/null
132 cp -a . "${CT_ROOT_DST_DIR}"
135 # A function do search for a library
136 # Usage: do_add_lib libname
137 # returns: 0 if library was found and added, !0 otherwise
144 for dir in lib usr/lib; do
145 ${CT_PRINTF} " trying in '%s'" "${dir}"
146 libfile="${CT_SYSROOT_DIR}/${dir}/${libname}"
147 ${CT_PRINTF} ": '%s'\n" "${libfile}"
148 if [ -e "${libfile}" ]; then
150 true_libname=$("${CT_READELF}" -d "${libfile}" \
151 |"${grep}" "Library soname:" \
152 |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;' \
158 ${CT_PRINTF} " installing as '%s/%s', mode='%s'\n" "${dir}" "${true_libname}" "${mode}"
159 "${install}" -m "${mode}" "${libfile}" "${dir}/${true_libname}"
167 # We'll work in the copied rootfs
168 pushd "${CT_ROOT_DST_DIR}" >/dev/null
170 # First of, copy the forced libraries into the working copy
171 if [ -n "${CT_LIB_FILE}" ]; then
172 lib_list=$("${sed}" -r -e ':loop; s/#.*//;' \
173 -e 's/[[:space:]]+//g;' \
174 -e 's/([^:])$/\1:/;' \
175 -e '/$/N; s/\n//; tloop;' \
178 CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}" \
179 |"${sed}" -r -e 's/:+/:/g; s/^:+//; s/:+$//;' \
182 CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}" \
183 |"${sed}" -r -e 's/^:+//; s/:+$//; s/:+/ /g;' \
185 if [ -n "${CT_LIB_LIST}" ]; then
186 ${CT_PRINTF} "Installing forced libraries...\n"
187 for name in ${CT_LIB_LIST}; do
188 [ -z "${name}" ] && continue
190 for libname in "lib${name}.so" "${name}.so" "${name}"; do
191 ${CT_PRINTF} " searching for '%s'\n" "${libname}"
192 if do_add_lib "${libname}"; then
197 if [ ${found} -eq 0 ]; then
198 echo "$myname: library '${libname}' not found!"
199 [ "${CT_FORCE}" = y ] || exit 1
204 # Parse the working copy for executables and libraries
206 while [ ${still_needed} -eq 1 ]; do
207 ${CT_PRINTF} "Looping...\n"
209 for f in $(find . -type f -exec file {} \; \
210 |"${grep}" -E ': ELF [[:digit:]]+-bit (L|M)SB (executable|shared object),' \
213 ${CT_PRINTF} "Scanning '%s'\n" "${f}"
214 for libname in $("${CT_READELF}" -d "${f}" \
215 |"${grep}" -E '\(NEEDED\)[[:space:]]+Shared library:' \
216 |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;' \
218 ${CT_PRINTF} " searching for '%s'\n" "${libname}"
219 if [ -e "lib/${libname}" \
220 -o -e "usr/lib/${libname}" ]; then
221 ${CT_PRINTF} " already present\n"
224 if do_add_lib "${libname}"; then
227 echo "$myname: library '${libname}' not found!"
233 # OK, we're done. Back off.