scripts/populate.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Sat Jan 31 17:38:26 2009 +0000 (2009-01-31)
changeset 1180 734db80cc9b9
parent 1097 b46a557b33c9
child 1190 15908c920549
permissions -rw-r--r--
Further handle the lib64 -> lib symlinks.
It at least helps powerpc64 to build, and should innocuous to other archs.

/trunk/scripts/crosstool-NG.sh.in | 8 6 2 0 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
     1 #!@@CT_bash@@
     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
     5 set -e
     6 
     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)"
    13 
    14 myname=$(basename "$0")
    15 
    16 # Parse the tools' paths configuration
    17 . "${CT_LIB_DIR}/paths.mk"
    18 
    19 doHelp() {
    20     cat <<_EOF_
    21 NAME
    22     $myname - populate the target root file system
    23 
    24 SYNOPSIS
    25     $myname OPTIONS -s source_root -d destination_root
    26 
    27 DESCRIPTION
    28     $myname will 'populate' your target root file system ('src_dir') with
    29     libraries from the toolchain (eg. libc.so...), storing the result into
    30     'dst_dir'.
    31 
    32 OPTIONS
    33     -s src_dir
    34         use 'src_dir' as the un-populated (source) root directory
    35 
    36     -d dst_dir
    37         use 'dst_dir' as the place to put the populated root directory
    38 
    39     -l name1[:name2[...]]
    40         Always add the specified shared library/ies name1, name2... from the
    41         toolchain (in the sys-root). Actual library names are searched as
    42         follows (where 'name' is replaced with the given name) in the
    43         sys-root directory:
    44           - libname.so
    45           - name.so
    46           - name
    47         If the file is found, then the SONAME of the library is used, and the
    48         library is copied with that name. If the library was not found, this
    49         yields an error (unless -f was given).
    50 
    51     -L file
    52         Read 'file' for a list of shared libraries to always add from the
    53         toolchain. The file should contain one library name per line; text
    54         after a # is ignored until the end of the line; spaces are ignored;
    55         empty lines are ignored. Libraries are searched for as with -l.
    56 
    57     -f  force execution: if destination directory already exists, it will be
    58         removed first; if a specified library (above) was not found, continue.
    59 
    60     -v  Be verbose
    61 
    62 _EOF_
    63 }
    64 
    65 CT_ROOT_SRC_DIR=
    66 CT_ROOT_DST_DIR=
    67 CT_LIB_LIST=
    68 CT_LIB_FILE=
    69 CT_FORCE=no
    70 CT_ECHO=true
    71 OPTIND=1
    72 while getopts ":s:d:l:L:fvh" CT_OPT; do
    73     case "${CT_OPT}" in
    74         s)  CT_ROOT_SRC_DIR="${OPTARG}";;
    75         d)  CT_ROOT_DST_DIR="${OPTARG}";;
    76         l)  CT_LIB_LIST="${CT_LIB_LIST}:${OPTARG}";;
    77         L)  CT_LIB_FILE="${OPTARG}";;
    78         f)  CT_FORCE=y;;
    79         v)  CT_ECHO=echo;;
    80         h)  doHelp
    81             exit 0
    82             ;;
    83         :)  echo "$myname: '-${OPTARG}' takes exactly one argument."
    84             exit 1
    85             ;;
    86         ?)  echo "$myname: unknown option '-${OPTARG}'."
    87             exit 1
    88             ;;
    89     esac
    90 done
    91 
    92 # Sanity checks
    93 if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
    94     doHelp
    95     exit 1
    96 fi
    97 if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
    98     echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
    99     exit 1
   100 fi
   101 if [ -d "${CT_ROOT_DST_DIR}" -a "${CT_FORCE}" != "y" ]; then
   102     echo "$myname: '${CT_ROOT_DST_DIR}': already exists"
   103     exit 1
   104 fi
   105 src_inode=$(ls -di "${CT_ROOT_SRC_DIR}")
   106 dst_inode=$(ls -di "${CT_ROOT_DST_DIR}" 2>/dev/null)
   107 if [ "${src_inode}" = "${dst_inode}" ]; then
   108     echo "$myname: source and destination are the same!"
   109     exit 1
   110 fi
   111 
   112 # Check existence of the forced libraries file
   113 if [ -n "${CT_LIB_FILE}" -a ! \( -f "${CT_LIB_FILE}" -a -r "${CT_LIB_FILE}" \) ]; then
   114     echo "$myname: forced libraries file '${CT_LIB_FILE}' not found!"
   115     exit 1
   116 fi
   117 
   118 # Get rid of potentially older destination directory
   119 rm -rf "${CT_ROOT_DST_DIR}"
   120 
   121 # Create the working copy
   122 mkdir -p "${CT_ROOT_DST_DIR}"
   123 
   124 # Make all path absolute
   125 CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
   126 CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
   127 
   128 # Populate the destination directory with files form the source directory
   129 pushd "${CT_ROOT_SRC_DIR}" >/dev/null
   130 tar cf - . |(cd "${CT_ROOT_DST_DIR}"; tar xf -)
   131 popd >/dev/null
   132 
   133 # A function do search for a library
   134 # Usage: do_add_lib libname
   135 # returns: 0 if library was found and added, !0 otherwise
   136 do_add_lib() {
   137     local libname="$1"
   138     local true_libname
   139     local dir
   140     for dir in lib usr/lib; do
   141         ${CT_ECHO} -n "    trying in '${dir}'"
   142         libfile="${CT_SYSROOT_DIR}/${dir}/${libname}"
   143         ${CT_ECHO} ": '${libfile}'"
   144         if [ -e "${libfile}" ]; then
   145             mkdir -p "${dir}"
   146             true_libname=$("${CT_READELF}" -d "${libfile}"          \
   147                            |"${grep}" "Library soname:"             \
   148                            |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;'   \
   149                           )
   150             ${CT_ECHO} "      installing as '${dir}/${true_libname}'"
   151             "${install}" -m 0644 "${libfile}" "${dir}/${true_libname}"
   152             return 0
   153             break
   154         fi
   155     done
   156     return 1
   157 }
   158 
   159 # First of, copy the forced libraries into the working copy
   160 if [ -n "${CT_LIB_FILE}" ]; then
   161     lib_list=$("${sed}" -r -e ':loop; s/#.*//;'         \
   162                            -e 's/[[:space:]]+//g;'      \
   163                            -e 's/([^:])$/\1:/;'         \
   164                            -e '/$/N; s/\n//; tloop;'    \
   165                         "${CT_LIB_FILE}"
   166               )
   167     CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}"             \
   168                   |"${sed}" -r -e 's/:+/:/g; s/^:+//; s/:+$//;' \
   169                  )
   170 fi
   171 CT_LIB_LIST=$(echo "${CT_LIB_LIST}:${lib_list}"             \
   172               |"${sed}" -r -e 's/^:+//; s/:+$//; s/:+/ /g;' \
   173              )
   174 ${CT_ECHO} "Installing forced libraries..."
   175 pushd "${CT_ROOT_DST_DIR}" >/dev/null
   176 for name in ${CT_LIB_LIST}; do
   177     [ -z "${name}" ] && continue
   178     found=0
   179     for libname in "lib${name}.so" "${name}.so" "${name}"; do
   180         ${CT_ECHO} "  searching for '${libname}'"
   181         if do_add_lib "${libname}"; then
   182             found=1
   183             break
   184         fi
   185     done
   186     if [ ${found} -eq 0 ]; then
   187         echo "$myname: library '${libname}' not found!"
   188         [ "${CT_FORCE}" = y ] || exit 1
   189     fi
   190 done
   191 popd >/dev/null
   192 
   193 # Parse the working copy for executables and libraries
   194 pushd "${CT_ROOT_DST_DIR}" >/dev/null
   195 still_needed=1
   196 while [ ${still_needed} -eq 1 ]; do
   197     ${CT_ECHO} "Looping..."
   198     still_needed=0
   199     for f in $(find . -type f -exec file {} \;                                              \
   200                |"${grep}" -E ': ELF [[:digit:]]+-bit (L|M)SB (executable|shared object),'   \
   201                |cut -d ":" -f 1                                                             \
   202               ); do
   203         ${CT_ECHO} "Scanning '${f}'"
   204         for libname in $("${CT_READELF}" -d "${f}"                              \
   205                          |"${grep}" -E '\(NEEDED\)[[:space:]]+Shared library:'  \
   206                          |"${sed}" -r -e 's,.+\[(.+)\] *$,\1,;'                 \
   207                         ); do
   208             ${CT_ECHO} "  searching for '${libname}'"
   209             if [    -e "lib/${libname}"     \
   210                  -o -e "usr/lib/${libname}" ]; then
   211                 ${CT_ECHO} "    already present"
   212                 continue
   213             fi
   214             if do_add_lib "${libname}"; then
   215                 still_needed=1
   216             else
   217                 echo "$myname: library '${libname}' not found!"
   218             fi
   219         done
   220     done
   221 done
   222 popd >/dev/null