tools/populate.in
author "Yann E. MORIN" <yann.morin.1998@anciens.enib.fr>
Tue Aug 05 13:27:09 2008 +0000 (2008-08-05)
changeset 760 80098c869f5e
parent 688 b8d87af44232
child 770 1af057f49f7e
permissions -rw-r--r--
Mark "ADA", "ObjC", "ObjC++" and "other languages" as being EXPERIMENTAL.

/trunk/config/cc.in | 12 8 4 0 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
     1 #!/bin/bash
     2 
     3 # This script will populate the root directory with libs from the sysroot.
     4 # (C) 2007 Yann E. MORIN
     5 # Licensed under the GPL v2
     6 
     7 # Detect where the toolchain is:
     8 BIN_DIR="$(cd "$(dirname "$(readlink -fn "$0")")"; pwd)"
     9 CT_READELF="${BIN_DIR}/@@CT_TARGET@@-readelf"
    10 CT_SYSROOT_DIR="${BIN_DIR}/../@@CT_TARGET@@/sys-root"
    11 
    12 myname=$(basename "$0")
    13 
    14 doHelp() {
    15     cat <<_EOF_
    16 $myname [-f] [-v] -s source_root -d destination_root
    17 
    18     -s dir
    19         use 'dir' as the un-populated (source) root directory
    20 
    21     -d dir
    22         use 'dir' as the place to put the populated root directory
    23 
    24     -f  force execution: if destination directory already exists,
    25         it will be removed first.
    26 
    27     -v  Be verbose
    28 _EOF_
    29 }
    30 
    31 CT_ROOT_SRC_DIR=
    32 CT_ROOT_DST_DIR=
    33 CT_FORCE=no
    34 CT_ECHO=true
    35 while getopts ":s:d:fvh" CT_OPT; do
    36     case "${CT_OPT}" in
    37         s)  CT_ROOT_SRC_DIR="${OPTARG}";;
    38         d)  CT_ROOT_DST_DIR="${OPTARG}";;
    39         f)  CT_FORCE=y;;
    40         v)  CT_ECHO=echo;;
    41         h)  doHelp
    42             exit 0
    43             ;;
    44         :)  echo "$myname: '-${OPTARG}' takes exactly one argument."
    45             exit 1
    46             ;;
    47         ?)  echo "$myname: unknown option '-${OPTARG}'."
    48             exit 1
    49             ;;
    50     esac
    51 done
    52 
    53 # Sanity checks
    54 if [ -z "${CT_ROOT_SRC_DIR}" -o -z "${CT_ROOT_DST_DIR}" ]; then
    55     doHelp
    56     exit 1
    57 fi
    58 if [ ! -d "${CT_ROOT_SRC_DIR}" ]; then
    59     echo "$myname: '${CT_ROOT_SRC_DIR}': no such file or directory"
    60     exit 1
    61 fi
    62 if [ -d "${CT_ROOT_DST_DIR}" -a "${CT_FORCE}" != "y" ]; then
    63     echo "$myname: '${CT_ROOT_DST_DIR}': already exists"
    64     exit 1
    65 fi
    66 src_inode=$(ls -di "${CT_ROOT_SRC_DIR}")
    67 dst_inode=$(ls -di "${CT_ROOT_DST_DIR}" 2>/dev/null)
    68 if [ "${src_inode}" = "${dst_inode}" ]; then
    69     echo "$myname: source and destination are the same!"
    70     exit 1
    71 fi
    72 
    73 # Get rid of potentially older destination directory
    74 if [ -d "${CT_ROOT_DST_DIR}" ]; then
    75     mv "${CT_ROOT_DST_DIR}" "${CT_ROOT_DST_DIR}.$$"
    76     setsid nohup rm -rf "${CT_ROOT_DST_DIR}.$$" >/dev/null 2>&1 &
    77 fi
    78 
    79 # Create the working copy
    80 mkdir -p "${CT_ROOT_DST_DIR}"
    81 
    82 # Make all path absolute
    83 CT_ROOT_SRC_DIR=$(cd "${CT_ROOT_SRC_DIR}"; pwd)
    84 CT_ROOT_DST_DIR=$(cd "${CT_ROOT_DST_DIR}"; pwd)
    85 
    86 cd "${CT_ROOT_SRC_DIR}"
    87 tar cf - . |(cd "${CT_ROOT_DST_DIR}"; tar xf -)
    88 
    89 # Parse the working copy for executables and libraries
    90 cd "${CT_ROOT_DST_DIR}"
    91 still_needed=1
    92 while [ ${still_needed} -eq 1 ]; do
    93     ${CT_ECHO} "Looping..."
    94     still_needed=0
    95     for f in $(find . -type f -exec file {} \; |egrep ': ELF [[:digit:]]+-bit .SB (executable|shared object),' |cut -d ":" -f 1); do
    96         ${CT_ECHO} "Scanning '${f}'"
    97         for libname in $("${CT_READELF}" -d "${f}" |egrep '(NEEDED)' |sed -r -e 's,.+\[(.+)\] *$,\1,;'); do
    98             ${CT_ECHO} "  searching for '${libname}'"
    99             if [    -e "lib/${libname}"     \
   100                  -o -e "usr/lib/${libname}" ]; then
   101                 ${CT_ECHO} "    already present"
   102                 continue
   103             fi
   104             for dir in . usr; do
   105                 ${CT_ECHO} -n "    trying in '${dir}'"
   106                 libfile="${CT_SYSROOT_DIR}/${dir}/lib/${libname}"
   107                 ${CT_ECHO} ": '${libfile}'"
   108                 if [ -e "${libfile}" ]; then
   109                     mkdir -p "${dir}/lib"
   110                     ${CT_ECHO} "      installing '${dir}/lib/${libname}'"
   111                     cp "${libfile}" "${dir}/lib/${libname}"
   112                     still_needed=1
   113                     break
   114                 fi
   115             done
   116         done
   117     done
   118 done