changeset 863:4bfe2b34dd9f

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).
author Rob Landley <rob@landley.net>
date Mon, 26 Oct 2009 06:09:29 -0500
parents b3d3ca365d4e
children 5660a438421b
files run-from-build.sh sources/native/sbin/init.sh sources/patches/uClibc-realpath.patch sources/toys/ccwrap.c sources/toys/run-emulator.sh system-image.sh
diffstat 6 files changed, 155 insertions(+), 112 deletions(-) [+]
line wrap: on
line diff
--- 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" </dev/null
-
-# Run the emulator, using the distccd we built (if necessary) to dial out
-# to the cross-compiler.  If emulator is killed, take down distccd processes
-# as well.
-
-trap "killtree $$" EXIT
-
-./run-emulator.sh $MAKE_HDB --memory 256 --with-distcc \
-	"${BUILD}/cross-compiler-${ARCH}"
+export PATH="$(pwd)/build/host:$(pwd)/build/cross-compiler-$1/bin:$PATH" &&
+cd build/system-image-"$1" &&
+./dev-environment.sh
--- a/sources/native/sbin/init.sh	Mon Oct 26 06:07:05 2009 -0500
+++ b/sources/native/sbin/init.sh	Mon Oct 26 06:09:29 2009 -0500
@@ -27,6 +27,8 @@
   # If we have no RTC, try rdate instead:
   [ `date +%s` -lt 1000 ] && rdate 10.0.2.2 # or time-b.nist.gov
 
+  mount -t tmpfs /tmp /tmp
+
   # If there's a /dev/hdb or /dev/sdb, mount it on home
 
   [ -b /dev/hdb ] && HOMEDEV=/dev/hdb
@@ -34,7 +36,10 @@
   if [ ! -z "$HOMEDEV" ]
   then
     mount -o noatime $HOMEDEV /home
+  else
+    mount -t tmpfs /home /home
   fi
+  cd /home
 
   [ -b /dev/hdc ] && MNTDEV=/dev/hdc
   [ -b /dev/sdc ] && MNTDEV=/dev/sdc
@@ -43,11 +48,15 @@
     mount -o ro $MNTDEV /mnt
   fi
 
-  mount -t tmpfs /tmp /tmp
-
   CONSOLE="$(dmesg |
     sed -n '/^Kernel command line:/s@.* console=\(/dev/\)*\([^ ]*\).*@\2@p')"
 
+  if [ -z "$DISTCC_HOSTS" ]
+  then
+    echo "Not using distcc."
+  else
+    echo "Distcc acceleration enabled."
+  fi
   echo Type exit when done.
 
   HANDOFF=/bin/ash
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sources/patches/uClibc-realpath.patch	Mon Oct 26 06:09:29 2009 -0500
@@ -0,0 +1,44 @@
+Add cheesy malloc() support to realpath().  Still limited to PATH_MAX, but eh.
+
+diff -ur uClibc/libc/stdlib/realpath.c uClibc.new/libc/stdlib/realpath.c
+--- uClibc/libc/stdlib/realpath.c	2008-06-04 09:02:56.000000000 -0500
++++ uClibc.new/libc/stdlib/realpath.c	2009-10-25 13:17:42.000000000 -0500
+@@ -55,7 +55,7 @@
+ 	char *max_path;
+ 	char *new_path;
+ 	size_t path_len;
+-	int readlinks = 0;
++	int readlinks = 0, allocated = 0;
+ #ifdef S_IFLNK
+ 	int link_len;
+ #endif
+@@ -68,6 +68,10 @@
+ 		__set_errno(ENOENT);
+ 		return NULL;
+ 	}
++	if (!got_path) {
++		got_path = alloca(PATH_MAX);
++		allocated ++;
++	}
+ 	/* Make a copy of the source path since we may need to modify it. */
+ 	path_len = strlen(path);
+ 	if (path_len >= 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
+ 
+ 
--- 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;
--- 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
 
--- 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