# HG changeset patch # User Rob Landley # Date 1256555369 18000 # Node ID 4bfe2b34dd9fd3731287c34a8f9d1361b8046ba8 # Parent b3d3ca365d4e0f29f83e19c90c0d3bd01e3b6852 Largeish refactoring/simplification of run-emulator.sh and associated code. Now automatically sets up the distcc trick if the appropriate $ARCH-cc toolchain is in the $PATH (or the current directory). diff -r b3d3ca365d4e -r 4bfe2b34dd9f run-from-build.sh --- a/run-from-build.sh Mon Oct 26 06:07:05 2009 -0500 +++ b/run-from-build.sh Mon Oct 26 06:09:29 2009 -0500 @@ -1,33 +1,7 @@ #!/bin/bash -# Read the configuration files for this target. - -source sources/include.sh || exit 1 -read_arch_dir "$1" - -# Fail if this target hasn't been built yet. - -SYSDIR="${BUILD}/system-image-$ARCH_NAME" -if [ ! -f "$SYSDIR/run-emulator.sh" ] -then - [ -z "$FAIL_QUIET" ] && echo "No $SYSDIR/run-emulator.sh" >&2 - exit 1 -fi -cd "$SYSDIR" || exit 1 +# Grab cross compiler (for distcc) and run development environment. -# Should we create a 2 gigabyte /dev/hdb image to provide the emulator with -# some writable scratch space? (If one already exists, fsck it.) This -# image (if it exists) will be mounted on /home by the emulated system's -# init script. - -[ -z "$SKIP_HOME" ] && [ -z "$MAKE_HDB" ] && MAKE_HDB="--make-hdb 2048" -[ -f "image-${ARCH}.ext2" ] && fsck.ext2 -y "image-${ARCH}.ext2" = PATH_MAX - 2) { +@@ -168,5 +172,6 @@ + new_path--; + /* Make sure it's null terminated. */ + *new_path = '\0'; ++ if (allocated) got_path = strdup(got_path); + return got_path; + } +diff -ur uClibc/include/stdlib.h uClibc2/include/stdlib.h +--- uClibc/include/stdlib.h 2008-09-11 11:17:43.000000000 -0500 ++++ uClibc2/include/stdlib.h 2009-10-26 03:43:36.000000000 -0500 +@@ -637,7 +637,7 @@ + name in RESOLVED. */ + /* we choose to handle __resolved==NULL as crash :) */ + extern char *realpath (__const char *__restrict __name, +- char *__restrict __resolved) __THROW __wur __nonnull((2)); ++ char *__restrict __resolved) __wur; + #endif + + diff -r b3d3ca365d4e -r 4bfe2b34dd9f sources/toys/ccwrap.c --- a/sources/toys/ccwrap.c Mon Oct 26 06:07:05 2009 -0500 +++ b/sources/toys/ccwrap.c Mon Oct 26 06:09:29 2009 -0500 @@ -70,7 +70,7 @@ char *cwd = getcwd(NULL, 0); if (index(filename, '/') && is_file(filename, has_exe)) - return strdup(filename); + return realpath(filename, NULL); for (;;) { char *str, *next = path ? index(path, ':') : NULL; @@ -90,8 +90,11 @@ } // If it's not a directory, return it. - if (is_file(str, has_exe)) return str; - else free(str); + if (is_file(str, has_exe)) { + char *s = realpath(str, NULL); + free(str); + return s; + } else free(str); if (!next) break; path += len; diff -r b3d3ca365d4e -r 4bfe2b34dd9f sources/toys/run-emulator.sh --- a/sources/toys/run-emulator.sh Mon Oct 26 06:07:05 2009 -0500 +++ b/sources/toys/run-emulator.sh Mon Oct 26 06:09:29 2009 -0500 @@ -1,56 +1,45 @@ #!/bin/bash -# Parse command line arguments +# The following environment variables affect the behavior of this script: + +# HDB - Image file to use for -hdb on /home (none if blank) +# HDBMEGS - Number of megabytes to create +# HDC - Image file to use for -hdc on /mnt (none of blank) +# QEMU_MEMORY - number of megabytes of memory for qemu (defaults to 128) -while [ ! -z "$1" ] -do - if [ "$1" == "--make-hdb" ] - then - shift - HDBMEGS="$1" - elif [ "$1" == "--with-hdb" ] +# Also, to use the distcc accelerator you need to have distccd and $ARCH-cc +# in the $PATH. + +[ ! -z "$QEMU_MEMORY" ] && QEMU_EXTRA="-m $QEMU_MEMORY $QEMU_EXTRA" + +# Should we set up an ext3 image as a second virtual hard drive for /home? + +if [ ! -z "$HDB" ] +then + if [ ! -e "$HDB" ] then - shift - HDB="$1" - elif [ "$1" == "--with-hdc" ] - then - shift - HDC="$1" - elif [ "$1" == "--with-distcc" ] - then - DCC="$(which distccd)" - if [ -z "$DCC" ] - then - echo 'No distccd in $PATH' >&2 - exit 1 - fi - shift - DISTCC_PATH="$1" - elif [ "$1" == "--memory" ] - then - shift - QEMU_EXTRA="-m $1 $QEMU_EXTRA" - else - ( - echo "unknown argument $1" - echo 'Usage: run-emulator.sh [OPTIONS]' - echo ' --make-hdb $MEGS - create a sparse image (if none exists) to mount on /home' - echo ' --with-hdb $FILE - Use an image file name other than hdb.img' - echo ' --with-distcc $DISTCC_PATH - set up distcc accelerator.' - echo ' Argument is path to cross compiler.' - echo ' --memory $MEGS - Tell emulator to use this many megabytes of memory.' - echo ' Default is 128 megs for 32 bit targets, 256 megs for 64 bit.' - ) >&2 - exit 1 + # If we don't already have an hdb image, should we set up a sparse file and + # format it ext3? + + if [ ! -z "$HDBMEGS" ] + then + dd if=/dev/zero of="$HDB" bs=1024 seek=$[$HDBMEGS*1024-1] count=1 && + mke2fs -b 1024 -F "$HDB" -i 4096 && + tune2fs -j -c 0 -i 0 "$HDB" + + [ $? -ne 0 ] && exit 1 + fi fi + WITH_HDB="-hdb $HDB" +fi - shift -done +[ ! -z "$HDC" ] && [ -e "$HDC" ] && WITH_HDC="-hdc $HDC" -if [ ! -z "$DISTCC_PATH" ] -then +# Start of distcc setup +unique_port() +{ # Try to find a unique port number for each running instance of the program. # To reduce the chance of the port already being in use by another program, @@ -67,57 +56,78 @@ START=$[$(awk '{print $2}' /proc/sys/net/ipv4/ip_local_port_range)] RANGE=$[65535-$START] fi - PORT=$[($$%$RANGE)+$START] + echo $[($$%$RANGE)+$START] +} + +# The ARCH gets substituted in here by system-image.sh + +ARCH= + +# If the cross compiler isn't in the $PATH, look for it in the current +# directory and the user's home directory. + +DISTCC_PATH="$(which $ARCH-cc 2>/dev/null | sed 's@\(.*\)/.*@\1@')" + +if [ -z "$DISTCC_PATH" ] +then + [ ! -f "cross-compiler-$ARCH/bin/$ARCH-cc" ] && + DISTCC_PATH="$(pwd)/cross-compiler-$ARCH/bin" + + [ ! -f "$HOME/cross-compiler-$ARCH/bin/$ARCH-cc" ] && + DISTCC_PATH="$HOME/cross-compiler-$ARCH/bin" +fi + +if [ -z "$(which distccd)" ] +then + echo 'No distccd in $PATH, acceleration disabled.' +elif [ -z "$DISTCC_PATH" ] +then + echo "No $ARCH-cc in "'$PATH'", acceleration disabled." +else + + # Populate a directory full of symlinks to the cross compiler using the + # unprefixed names distccd will try to use. + + mkdir -p "distcc_links" && + for i in $(cd "$DISTCC_PATH"; ls $ARCH-* | sed "s/^$ARCH-//" ) + do + ln -sf "$DISTCC_PATH/$ARCH-$i" "distcc_links/$i" + done + if [ -e "$DISTCC_PATH/$ARCH-rawgcc" ] + then + for i in cc gcc g++ c++ + do + ln -sf "$DISTCC_PATH/$ARCH-rawgcc" distcc_links/$i + done + fi # Run the distcc daemon on the host system with $PATH restricted to the # cross compiler binaries. - # Note that we tell it --no-detach and background it oursleves so jobs -p can + # Note that we tell it --no-detach and background it ourselves so jobs -p can # find it later to kill it after the emulator exits. + PORT=$(unique_port) CPUS=$[$(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)*2] - PATH="$(readlink -f "$DISTCC_PATH"/tools/bin)" "$DCC" --listen 127.0.0.1 \ - --no-detach --log-file distccd.log --verbose --jobs ${CPUS} --daemon \ - -a 127.0.0.1 -p $PORT & - # Cleanup afterwards: Kill child processes we started (I.E. distccd). + PATH="$(pwd)/distcc_links" "$(which distccd)" --no-detach --daemon \ + --listen 127.0.0.1 -a 127.0.0.1 -p $PORT --jobs $CPUS \ + --log-stderr --verbose 2>distccd.log & + + # Clean up afterwards: Kill child processes we started (I.E. distccd). trap "kill $(jobs -p)" EXIT # When background processes die, they should do so silently. disown $(jobs -p) - # Prepare some environment variables for run-qemu.sh + # Let the QEMU launch know we're using distcc. DISTCC_PATH_PREFIX=/usr/distcc: KERNEL_EXTRA="DISTCC_HOSTS=10.0.2.2:$PORT/$CPUS CPUS=$CPUS $KERNEL_EXTRA" fi -# Should we set up an ext3 image as a second virtual hard drive for /home? - -# Default to image "hdb.img" -[ -z "$HDB" ] && HDB="hdb.img" - -if [ ! -e "$HDB" ] -then - - # If we don't already have an hdb image, should we set up a sparse file and - # format it ext3? - - if [ ! -z "$HDBMEGS" ] - then - dd if=/dev/zero of="$HDB" bs=1024 seek=$[$HDBMEGS*1024-1] count=1 && - mke2fs -b 1024 -F "$HDB" -i 4096 && - tune2fs -j -c 0 -i 0 "$HDB" - - [ $? -ne 0 ] && exit 1 - fi -fi - -[ -e "$HDB" ] && WITH_HDB="-hdb $HDB" -[ -e "$HDC" ] && WITH_HDC="-hdc $HDC" - # Kill our child processes on exit. trap "pkill -P$$" EXIT -# The actual emulator invocation command gets appended here +# The actual emulator invocation command gets appended here by system-image.sh diff -r b3d3ca365d4e -r 4bfe2b34dd9f system-image.sh --- a/system-image.sh Mon Oct 26 06:07:05 2009 -0500 +++ b/system-image.sh Mon Oct 26 06:09:29 2009 -0500 @@ -189,7 +189,10 @@ # filesystem, kernel, and base kernel command line arguments in case you want # to use an emulator other than qemu, but put the default case in qemu_defaults -cp "$SOURCES/toys/run-emulator.sh" "$STAGE_DIR/run-emulator.sh" && +cp "$SOURCES/toys/dev-environment.sh" "$STAGE_DIR" && +sed -e 's/^ARCH=.*/ARCH='"$ARCH"/ "$SOURCES/toys/run-emulator.sh" > \ + "$STAGE_DIR/run-emulator.sh" && +chmod +x "$STAGE_DIR/run-emulator.sh" && emulator_command "$IMAGE" zImage-$ARCH >> "$STAGE_DIR/run-emulator.sh" [ $? -ne 0 ] && dienow