Unified build system to use SOURCES for sim builds too, a single Makefile-look
made by configure and various related adjustments. This has not yet been tested
on cygwin.


git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6001 a1c6a512-1295-4272-9138-f99709370657
diff --git a/apps/Makefile b/apps/Makefile
index 75b9fba..bcdc1ef 100644
--- a/apps/Makefile
+++ b/apps/Makefile
@@ -13,11 +13,6 @@
 LDS := $(FIRMDIR)/app.lds
 ROMLDS := $(FIRMDIR)/rom.lds
 
-ifdef DEBUG
-    DEFINES := -DDEBUG
-    CFLAGS += -g
-endif
-
 ifdef ENABLEDPLUGINS
 ROCKS=rocks
 endif
@@ -26,7 +21,8 @@
 CODECS=build-codecs
 endif
 
-SRC := $(shell cat SOURCES | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) $(TARGET) $(DEFINES) -E -P -include "config.h" - )
+SRC := $(shell cat SOURCES | gcc -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) \
+$(TARGET) $(DEFINES) $(EXTRA_DEFINES) -E -P -include "config.h" - )
 DIRS = .
 
 ifdef APPEXTRA
@@ -44,9 +40,11 @@
 MAXINFILE = $(OBJDIR)/romstart.temp
 MAXOUTFILE = $(OBJDIR)/romstart
 
-ifdef DEBUG
-all: $(OBJDIR)/rockbox.elf $(CODECS) $(ROCKS)
+ifdef SIMVER
+# this is a sim build
+all: $(OBJDIR)/$(BINARY) $(CODECS) $(ROCKS)
 else
+# regular target build
 all: $(OBJDIR)/$(BINARY) $(FLASHFILE) $(CODECS) $(ROCKS) $(ARCHOSROM)
 endif
 
@@ -80,9 +78,11 @@
 	@echo "LD rombox.elf"
 	@$(CC) $(GCCOPTS) -Os -nostdlib -o $@ $(OBJS) -L$(OBJDIR) -lrockbox -lgcc -L$(FIRMDIR) -T$(LINKROM) -Wl,-Map,$(OBJDIR)/rombox.map
 
+ifndef SIMVER
+
 $(OBJDIR)/rockbox.elf : $(OBJS) $(LINKFILE) $(OBJDIR)/librockbox.a $(DEPFILE)
 	@echo "LD rockbox.elf"
-	$(CC) $(GCCOPTS) -Os -nostdlib -o $@ $(OBJS) -L$(OBJDIR) -lrockbox -lgcc -L$(FIRMDIR) -T$(LINKFILE) -Wl,-Map,$(OBJDIR)/rockbox.map
+	$(CC) $(GCCOPTS) -Os -nostdlib -o $@ $(OBJS) -L$(OBJDIR) -lrockbox -lgcc -T$(LINKFILE) -Wl,-Map,$(OBJDIR)/rockbox.map
 
 $(OBJDIR)/rockbox.bin : $(OBJDIR)/rockbox.elf
 	@echo "OBJCOPY $<"
@@ -99,6 +99,16 @@
 	@echo "Build firmware file"
 	@$(MKFIRMWARE) $< $@
 
+else
+# this is a simulator build
+$(OBJDIR)/$(BINARY) : $(OBJS) $(OBJDIR)/librockbox.a $(DEPFILE) $(OBJDIR)/libsim.a
+	@echo "LD $(BINARY)"
+	$(CC) $(GCCOPTS) -o $@ $(OBJS) $(LDOPTS) -L$(OBJDIR) -lrockbox -lsim -Wl,-Map,$(OBJDIR)/rockbox.map
+
+$(OBJDIR)/libsim.a:
+	@$(MAKE) -C $(SIMDIR)
+endif
+
 $(OBJDIR)/rockbox.ucl: $(OBJDIR)/rockbox.bin
 	@a=`uclpack -h 2>/dev/null`; \
 	if test -n "$$a"; then \
@@ -160,5 +170,8 @@
 	@$(MAKE) -C plugins clean
 	@$(MAKE) -C codecs clean
 	@rm -rf $(OBJDIR)/recorder $(OBJDIR)/player
+ifdef SIMVER
+	@$(MAKE) -C $(SIMDIR) clean
+endif
 
 -include $(DEPFILE)
diff --git a/apps/SOURCES b/apps/SOURCES
index 43cd3dd..33a6ded 100644
--- a/apps/SOURCES
+++ b/apps/SOURCES
@@ -19,7 +19,9 @@
 sleeptimer.c
 sound_menu.c
 status.c
+#ifndef SIMULATOR
 talk.c
+#endif
 tree.c
 dbtree.c
 filetree.c
diff --git a/apps/codecs/libFLAC/Makefile b/apps/codecs/libFLAC/Makefile
index 329de9c..ee4feab 100644
--- a/apps/codecs/libFLAC/Makefile
+++ b/apps/codecs/libFLAC/Makefile
@@ -14,11 +14,22 @@
 INCLUDES += -I$(APPSDIR)/$(APPEXTRA)
 endif
 
-FLACOPTS = -DFLAC__NO_ASM -DFLAC__ALIGN_MALLOC_DATA -DFLAC__INTEGER_ONLY_LIBRARY -DVERSION=\"1.1.2\" -fomit-frame-pointer -funroll-loops -finline-functions -Wall -W -Winline -DFLaC__INLINE=__inline__ --param large-function-insns=10000
+NEWGCC=$(shell expr $(GCCNUM) ">" 303)
+
+FLACOPTS = -DFLAC__NO_ASM -DFLAC__ALIGN_MALLOC_DATA			      \
+-DFLAC__INTEGER_ONLY_LIBRARY -DVERSION=\"1.1.2\" -fomit-frame-pointer	      \
+-funroll-loops -finline-functions -Wall -W -Winline -DFLaC__INLINE=__inline__
+
+ifeq ($(NEWGCC), 1)
+  # this is 3.4.X options:
+  FLACOPTS += --param large-function-insns=10000
+endif
+
 CFLAGS = $(GCCOPTS) $(FLACOPTS)\
 $(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE}
 
-SRC := $(shell cat SOURCES | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) $(TARGET) $(DEFINES) -E -P -include "config.h" - )
+SRC := $(shell cat SOURCES | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) \
+$(TARGET) $(DEFINES) -E -P -include "config.h" - )
 SOURCES = $(SRC)
 OBJS := $(SRC:%.c=$(OBJDIR)/libFLAC/%.o)
 DEPFILE = $(OBJDIR)/dep-libFLAC
diff --git a/apps/main_menu.c b/apps/main_menu.c
index 3eb1d69..c7cfb0e 100644
--- a/apps/main_menu.c
+++ b/apps/main_menu.c
@@ -274,13 +274,6 @@
 }
 
 #ifdef HAVE_RECORDING
-#ifdef SIMULATOR
-bool recording_screen(void)
-{
-    /* a dummy */
-    return false;
-}
-#endif
 
 static bool recording_settings(void)
 {
diff --git a/apps/plugin.c b/apps/plugin.c
index 93717cc..10daeaf 100644
--- a/apps/plugin.c
+++ b/apps/plugin.c
@@ -187,9 +187,7 @@
     strncasecmp,
     memset,
     memcpy,
-#ifndef SIMULATOR
     _ctype_,
-#endif
     atoi,
 
     /* sound */
diff --git a/apps/plugin.h b/apps/plugin.h
index 590d02a..96a9aab 100644
--- a/apps/plugin.h
+++ b/apps/plugin.h
@@ -211,9 +211,7 @@
     int (*strncasecmp)(const char *s1, const char *s2, size_t n);
     void* (*memset)(void *dst, int c, size_t length);
     void* (*memcpy)(void *out, const void *in, size_t n);
-#ifndef SIMULATOR
     const char *_ctype_;
-#endif
     int (*atoi)(const char *str);
 
     /* sound */
diff --git a/apps/plugins/Makefile b/apps/plugins/Makefile
index 0c90bde..6af3932 100644
--- a/apps/plugins/Makefile
+++ b/apps/plugins/Makefile
@@ -24,16 +24,19 @@
 LINKFILE := $(OBJDIR)/pluginlink.lds
 DEPFILE = $(OBJDIR)/dep-plugins
 
-SRC := $(shell cat SOURCES | $(CC) -DMEMORYSIZE=$(MEMORYSIZE)	\
- $(INCLUDES) $(TARGET) $(DEFINES) -E -P -include "config.h" - )
+SRC := $(shell cat SOURCES | gcc -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) \
+ $(TARGET) $(DEFINES) $(EXTRA_DEFINES) -E -P -include "config.h" - )
 ROCKS := $(SRC:%.c=$(OBJDIR)/%.rock)
 SOURCES = $(SRC)
 ELFS := $(SRC:%.c=$(OBJDIR)/%.elf)
 OBJS := $(SRC:%.c=$(OBJDIR)/%.o)
+# as created by the cross-compiler for win32:
+DEFS := $(SRC:%.c=$(OBJDIR)/%.def)
 DIRS = .
 
 all: $(OBJDIR)/libplugin.a $(ROCKS) $(DEPFILE)
 
+ifndef SIMVER
 $(OBJDIR)/%.elf: $(OBJDIR)/%.o $(LINKFILE) $(OBJDIR)/libplugin.a
 	@echo "LD $@"
 	@$(CC) $(GCCOPTS) -O -nostdlib -o $@ $< -L$(OBJDIR) $(CODECLIBS) -lplugin -lgcc -T$(LINKFILE) -Wl,-Map,$(OBJDIR)/$*.map
@@ -41,6 +44,37 @@
 $(OBJDIR)/%.rock : $(OBJDIR)/%.elf
 	@echo "OBJCOPY $<"
 	@$(OC) -O binary $< $@
+else
+
+ifeq ($(SIMVER), x11)
+###################################################
+# This is the X11 simulator version
+
+$(OBJDIR)/%.rock : $(OBJDIR)/%.o $(APPSDIR)/plugin.h
+	@echo "LD $@"
+	@$(CC) $(APPCFLAGS) -DPLUGIN -shared $< -L$(OBJDIR) -lplugin -o $@
+ifeq ($(UNAME),CYGWIN)
+# 'x' must be kept or you'll have "Win32 error 5"
+#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
+#         #define ERROR_ACCESS_DENIED 5L
+else
+	@chmod -x $@
+endif
+
+else # end of x11-simulator
+###################################################
+# This is the win32 simulator version
+DLLTOOLFLAGS = --export-all
+DLLWRAPFLAGS = -s --entry _DllMain@12 --target=i386-mingw32 -mno-cygwin
+
+$(OBJDIR)/%.rock : $(OBJDIR)/%.o $(APPSDIR)/plugin.h
+	@echo "DLL $@"
+	@$(DLLTOOL) $(DLLTOOLFLAGS) -z $(OBJDIR)/$*.def $<
+	@$(DLLWRAP) $(DLLWRAPFLAGS) --def $(OBJDIR)/$*.def $< $(OBJDIR)/libplugin.a -o $@
+	@chmod -x $@
+endif # end of win32-simulator
+
+endif # end of simulator section
 
 include $(TOOLSDIR)/make.inc
 
@@ -56,7 +90,8 @@
 
 clean:
 	@echo "cleaning plugins"
-	@rm -f $(ROCKS) $(LINKFILE) $(OBJDIR)/*.rock $(DEPFILE) $(ELFS) $(OBJS)
+	@rm -f $(ROCKS) $(LINKFILE) $(OBJDIR)/*.rock $(DEPFILE) $(ELFS) \
+	$(OBJS) $(DEFS)
 	@$(MAKE) -C lib clean
 
 -include $(DEPFILE)
diff --git a/apps/plugins/lib/Makefile b/apps/plugins/lib/Makefile
index b9daf73..b492bb5 100644
--- a/apps/plugins/lib/Makefile
+++ b/apps/plugins/lib/Makefile
@@ -18,9 +18,9 @@
 endif
 
 CFLAGS = $(GCCOPTS) \
-$(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE}
+$(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE} -DPLUGIN
 
-SRC := $(shell cat SOURCES | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) $(TARGET) $(DEFINES) -E -P -include "config.h" - )
+SRC := $(shell cat SOURCES | gcc -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) $(TARGET) $(DEFINES) $(EXTRA_DEFINES) -E -P -include "config.h" - )
 SOURCES = $(SRC)
 OBJS := $(SRC:%.c=$(OBJDIR)/%.o)
 DEPFILE = $(OBJDIR)/dep-pluginlib
@@ -31,8 +31,9 @@
 all: $(OUTPUT)
 
 $(OUTPUT): $(OBJS)
-	@echo "AR $@"
+	@echo "AR+RANLIB $@"
 	@$(AR) ruv $@ $+ >/dev/null 2>&1
+	@$(RANLIB) $@
 
 include $(TOOLSDIR)/make.inc
 
diff --git a/firmware/Makefile b/firmware/Makefile
index 966d9e0..618a770 100644
--- a/firmware/Makefile
+++ b/firmware/Makefile
@@ -11,7 +11,7 @@
 
 CFLAGS = $(GCCOPTS) $(INCLUDES) $(TARGET) $(EXTRA_DEFINES) -DMEM=${MEMORYSIZE}
 
-SRC := $(shell cat SOURCES | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) $(TARGET) $(DEFINES) -E -P -include "config.h" - )
+SRC := $(shell cat SOURCES | gcc -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) $(TARGET) $(DEFINES) $(EXTRA_DEFINES) -E -P -include "config.h" - )
 SOURCES = $(SRC)
 OBJS2 := $(patsubst %.c, $(OBJDIR)/%.o, $(SRC)) $(OBJDIR)/sysfont.o
 OBJS = $(patsubst %.S, $(OBJDIR)/%.o, $(OBJS2))
@@ -29,6 +29,7 @@
 $(OUTPUT): $(OBJS) $(DEPFILE)
 	@echo "AR $@"
 	@$(AR) ruv $@ $(OBJS) >/dev/null 2>&1
+	@$(RANLIB) $@
 
 include $(TOOLSDIR)/make.inc
 
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 535b221..a5e95e7 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -1,11 +1,15 @@
+#ifndef SIMULATOR
 backlight.c
+#endif
 buffer.c
 common/atoi.c
 common/ctype.c
+#ifndef SIMULATOR
 common/dir.c
+common/file.c
+#endif
 common/disk.c
 common/errno.c
-common/file.c
 common/memcmp.c
 common/qsort.c
 common/random.c
@@ -16,7 +20,7 @@
 common/strchr.c
 common/strcmp.c
 common/strcpy.c
-#if CONFIG_CPU == SH7034
+#if (CONFIG_CPU == SH7034) && !defined(SIMULATOR)
 common/strlen_a.S
 #else
 common/strlen.c
@@ -33,7 +37,20 @@
 common/memcpy.c
 common/memset.c
 #endif
-debug.c
+#ifdef HAVE_LCD_CHARCELLS
+drivers/lcd-player-charset.c
+drivers/lcd-player.c
+#endif
+#ifdef HAVE_LCD_BITMAP
+#if CONFIG_LCD == LCD_S1D15E06
+drivers/lcd-h100.c
+#else
+drivers/lcd-recorder.c
+#endif
+#endif
+drivers/power.c
+drivers/led.c
+#ifndef SIMULATOR
 drivers/adc.c
 #ifdef HAVE_MMC
 drivers/ata_mmc.c
@@ -54,42 +71,35 @@
 #endif
 #endif
 drivers/i2c.c
-#ifdef HAVE_LCD_CHARCELLS
-drivers/lcd-player-charset.c
-drivers/lcd-player.c
-#endif
-#ifdef HAVE_LCD_BITMAP
-#if CONFIG_LCD == LCD_S1D15E06
-drivers/lcd-h100.c
-#else
-drivers/lcd-recorder.c
-#endif
-#endif
-drivers/led.c
 #if CONFIG_HWCODEC != MASNONE
 drivers/mas.c
 #endif
-drivers/power.c
 #ifdef HAVE_RTC
 drivers/rtc.c
 #endif
 drivers/serial.c
+#endif /* !SIMULATOR */
 #ifdef HAVE_LCD_BITMAP
 font.c
 #endif
 hwcompat.c
 id3.c
+#ifndef SIMULATOR
 kernel.c
+rolo.c
+thread.c
+crt0.S
+#endif
 mp3_playback.c
 mp3data.c
 mpeg.c
+#ifndef WIN32 /* the win32 sim has its own versin of these: */
 panic.c
+debug.c
+#endif
 powermgmt.c
-rolo.c
 system.c
-thread.c
 usb.c
-crt0.S
 #if CONFIG_CPU == SH7034
 bitswap.S
 descramble.S
diff --git a/firmware/common/timefuncs.c b/firmware/common/timefuncs.c
index 1e95733..1a256cc 100644
--- a/firmware/common/timefuncs.c
+++ b/firmware/common/timefuncs.c
@@ -17,6 +17,7 @@
  *
  ****************************************************************************/
 
+#include <stdio.h> /* get NULL */
 #include "config.h"
 
 #include "rtc.h"
diff --git a/firmware/debug.c b/firmware/debug.c
index 5cc032b..0989e80 100644
--- a/firmware/debug.c
+++ b/firmware/debug.c
@@ -222,6 +222,7 @@
 void debug_init(void)
 {
 }
+extern void *stderr;
 
 void debugf(const char *fmt, ...)
 {
diff --git a/firmware/include/dir.h b/firmware/include/dir.h
index 8dcbb8e..12ec724 100644
--- a/firmware/include/dir.h
+++ b/firmware/include/dir.h
@@ -22,8 +22,6 @@
 #include <stdbool.h>
 #include <file.h>
 
-#ifndef DIRENT_DEFINED
-
 #define ATTR_READ_ONLY   0x01
 #define ATTR_HIDDEN      0x02
 #define ATTR_SYSTEM      0x04
@@ -32,6 +30,8 @@
 #define ATTR_ARCHIVE     0x20
 #define ATTR_VOLUME      0x40 /* this is a volume, not a real directory */
 
+#ifndef DIRENT_DEFINED
+
 struct dirent {
     unsigned char d_name[MAX_PATH];
     int attribute;
diff --git a/firmware/include/stdio.h b/firmware/include/stdio.h
index b98f01c..968bd59 100644
--- a/firmware/include/stdio.h
+++ b/firmware/include/stdio.h
@@ -36,4 +36,13 @@
 int snprintf (char *buf, size_t size, const char *fmt, ...);
 int vsnprintf (char *buf, int size, const char *fmt, __VALIST ap);
 
+#ifdef SIMULATOR
+typedef void FILE;
+int vfprintf(FILE *stream, const char *format, __VALIST ap);
+#ifdef WIN32
+#define FILENAME_MAX 260 /* ugly hard-coded value of a limit that is set
+                            in file.h */
+#endif
+#endif
+
 #endif /* _STDIO_H_ */
diff --git a/firmware/include/time.h b/firmware/include/time.h
index 5213468..267face 100644
--- a/firmware/include/time.h
+++ b/firmware/include/time.h
@@ -20,5 +20,16 @@
   int	tm_isdst;
 };
 
+#if defined(SIMULATOR) && !defined(_TIME_T_DEFINED)
+/* for non-win32 simulators */
+typedef long time_t;
+
+/* this define below is used by the mingw headers to prevent duplicate
+   typedefs */
+#define _TIME_T_DEFINED
+time_t time(time_t *t);
+
+#endif
+
 #endif /* _TIME_H_ */
 
diff --git a/firmware/mpeg.c b/firmware/mpeg.c
index e9ddb18..8673da9 100644
--- a/firmware/mpeg.c
+++ b/firmware/mpeg.c
@@ -2485,9 +2485,12 @@
         return 0;
 }
 
-#elif CONFIG_HWCODEC == MASNONE
+#elif (CONFIG_HWCODEC == MASNONE) || defined(SIMULATOR)
 
-/* dummies coming up */
+/* dummies coming up
+
+NOTE: when we implment these functions for real for software coded targets,
+these dummies shall remain for the simulator */
 
 void bitswap(unsigned char *data, int length)
 {
diff --git a/tools/buildzip.pl b/tools/buildzip.pl
index 2179298..b44ed91 100755
--- a/tools/buildzip.pl
+++ b/tools/buildzip.pl
@@ -166,10 +166,10 @@
 
 my $target = $ARGV[0];
 
-my $exe = "";
+my $exe = $ARGV[1];
 
-if($target !~ /sim/i) {
-    # not a simulator
+if(!$exe) {
+    # not specified, guess!
     if($target =~ /(recorder|ondio)/i) {
         $exe = "ajbrec.ajz";
     }
diff --git a/tools/configure b/tools/configure
index ac00771..364a3d7 100755
--- a/tools/configure
+++ b/tools/configure
@@ -19,32 +19,83 @@
     echo $response
 }
 
+prefixtools () {
+ prefix="$1"
+ CC=${prefix}gcc
+ WINDRES=${prefix}windres
+ DLLTOOL=${prefix}dlltool
+ DLLWRAP=${prefix}dllwrap
+ RANLIB=${prefix}ranlib
+ LD=${prefix}ld
+ AR=${prefix}ar
+ AS=${prefix}as
+ OC=${prefix}objcopy
+}
+
+crosswincc () {
+ # naive approach to selecting a mingw cross-compiler on linux/*nix
+ echo "Enabling win32 crosscompiling"
+
+ prefixtools i386-mingw32msvc-
+
+ LDOPTS="-lgdi32 -luser32 -mwindows"
+ # add cross-compiler option(s)
+ GCCOPTS="$GCCOPTS -mno-cygwin"
+}
+
+simcc () {
+
+ # default tool setup for native building
+ prefixtools ""
+
+ GCCOPTS='-W -Wall -g -fno-builtin -I$(ROOTDIR)/uisimulator/common -I$(ROOTDIR)/uisimulator/$(SIMVER)'
+
+ output="rockboxui" # use this as default output binary name
+
+ case $uname in
+   CYGWIN*)
+   echo "Cygwin host detected"
+   LDOPTS='-L/usr/X11R6/lib -lSM -lICE -lXt -lX11 -lXmu -lSM -lICE -lX11 -lpthread'
+   output="rockboxui.exe" # use this as output binary name
+   ;;
+
+   Linux)
+   echo "Linux host detected"
+   LDOPTS='-L/usr/X11R6/lib -lX11 -lm -lXt -lXmu -lnsl -ldl -lpthread'
+   if [ "$simver" = "win32" ]; then
+     crosswincc # setup cross-compiler
+   fi
+   ;;
+
+   FreeBSD)
+   echo "FreeBSD host detected"
+   LDOPTS='-L/usr/X11R6/lib -lX11 -lm -lXt -lXmu -dl -lpthread'
+   if [ "$simver" = "win32" ]; then
+     crosswincc # setup cross-compiler
+   fi
+   ;;
+
+   *)
+   echo "Unsupported system: $uname, fix configure and retry"
+   exit
+   ;;
+ esac
+}
+
 shcc () {
- CC=sh-elf-gcc
- LD=sh-elf-ld
- AR=sh-elf-ar
- AS=sh-elf-as
- OC=sh-elf-objcopy
+ prefixtools sh-elf-
  GCCOPTS="$CCOPTS -m1"
  GCCOPTIMIZE="-fomit-frame-pointer -fschedule-insns"
 }
 
 calmrisccc () {
- CC=calmrisc16-unknown-elf-gcc
- LD=calmrisc16-unknown-elf-ld
- AR=calmrisc16-unknown-elf-ar
- AS=calmrisc16-unknown-elf-as
- OC=calmrisc16-unknown-elf-objcopy
+ prefixtools calmrisc16-unknown-elf-
  GCCOPTS="-Wl\,--no-check-sections $CCOPTS"
  GCCOPTIMIZE="-fomit-frame-pointer"
 }
 
 coldfirecc () {
- CC=m68k-elf-gcc
- LD=m68k-elf-ld
- AR=m68k-elf-ar
- AS=m68k-elf-as
- OC=m68k-elf-objcopy
+ prefixtools m68k-elf-
  GCCOPTS="$CCOPTS -g -m5200 -Wa\,-m5249 -malign-int -mstrict-align"
  GCCOPTIMIZE="-fomit-frame-pointer"
 }
@@ -86,6 +137,13 @@
   case $option in
    [Ww])
      simver="win32"
+
+     WINDRES=windres
+     DLLTOOL=dlltool
+     DLLWRAP=dllwrap
+
+     # make sure the code knows this is for win32
+     extradefines="$extradefines -DWIN32"
      ;;
    *)
      simver="x11"
@@ -95,77 +153,6 @@
 fi
 }
 
-
-simul () {
-
-sed > Makefile \
- -e "s,@ROOTDIR@,${rootdir},g" \
- -e "s,@ARCHOS@,${archos},g" \
- -e "s,@DEBUG@,${debug},g" \
- -e "s,@KEYPAD@,${keypad},g" \
- -e "s,@PWD@,${pwd},g" \
- -e "s,@LANGUAGE@,${language},g" \
- -e "s,@TARGET@,${target},g" \
- -e "s,@PLUGINS@,${plugins},g" \
- -e "s,@CODECS@,${codecs},g" \
- -e "s,@SIMVER@,${simver},g" \
- -e "s,@MEMORY@,${memory},g" \
-<<EOF 
-## Automaticly generated. http://rockbox.haxx.se
-
-export ARCHOS=@ARCHOS@
-export ROOTDIR=@ROOTDIR@
-export FIRMDIR=\$(ROOTDIR)/firmware
-export APPSDIR=\$(ROOTDIR)/apps
-export TOOLSDIR=\$(ROOTDIR)/tools
-export DOCSDIR=\$(ROOTDIR)/docs
-export APPSDIR=@ROOTDIR@/apps
-export SIMDIR=\$(ROOTDIR)/uisimulator/@SIMVER@
-export DEBUG=@DEBUG@
-export KEYPAD=@KEYPAD@
-export OBJDIR=@PWD@
-export SIMVER=@SIMVER@
-export TARGET=@TARGET@
-export LANGUAGE=@LANGUAGE@
-export VERSION=\$(shell date +%y%m%d-%H%M)
-export ENABLEDPLUGINS=@PLUGINS@
-export SOFTWARECODECS=@CODECS@
-export MEMORYSIZE=@MEMORY@
-
-.PHONY: 
-
-all: sim
-
-sim:
-	\$(MAKE) -C \$(SIMDIR)
-
-clean:
-	\$(MAKE) -C \$(SIMDIR) clean
-	rm -rf rockbox.zip
-
-tags:
-	@rm -f TAGS
-	make -C \$(SIMDIR) tags
-
-zip:
-	\$(TOOLSDIR)/buildzip.pl -r "\$(ROOTDIR)" sim\$(TARGET)
-
-install:
-	@echo "installing a full setup in your archos dir"
-	@(make zip && cd archos && unzip -oq ../rockbox.zip)
-EOF
-
-  echo "Created Makefile"
-
-  if [ -d "archos" ]; then
-    echo "sub directory archos already present"
-  else
-    mkdir archos
-    echo "created an archos subdirectory for simulating the hard disk"
-  fi
-
-}
-
 picklang() {
     # figure out which languages that are around
     for file in $rootdir/apps/lang/*.lang; do
@@ -238,7 +225,7 @@
 else
 
 echo "This script will setup your Rockbox build environment."
-echo "Further docs here: http://rockbox.haxx.se/docs/"
+echo "Further docs here: http://www.rockbox.org/"
 echo ""
 
 fi
@@ -471,6 +458,7 @@
     [Ss])
       debug="1"
       simulator="yes"
+      extradefines="-DSIMULATOR"
       echo "Simulator build selected"
       whichsim
       ;;
@@ -504,10 +492,43 @@
     echo "Language set to $language"
 fi
 
+uname=`uname`
+
 if [ "yes" = "$simulator" ]; then
-    # we have already dealt with the simulator Makefile separately
-    simul
-    exit
+  # setup compiler and things for simulator
+  simcc
+
+  if [ -d "archos" ]; then
+    echo "sub directory archos already present"
+  else
+    mkdir archos
+    echo "created an archos subdirectory for simulating the hard disk"
+  fi
+fi
+
+# Now, figure out version number of the (gcc) compiler we are about to use
+gccver=`$CC -dumpversion`;
+
+if [ -z "$gccver" ]; then
+  echo "WARNING: The compiler you must use ($CC) is not in your path!"
+  echo "WARNING: this may cause your build to fail since we cannot do the"
+  echo "WARNING: checks we want now."
+else
+
+  # gccver should now be "3.3.5", "3.4.3", "2.95.3-6" and similar, but don't
+  # DEPEND on it
+
+ num1=`echo $gccver | cut -d . -f1`
+ num2=`echo $gccver | cut -d . -f2`
+ gccnum=`(expr $num1 "*" 100 + $num2) 2>/dev/null`
+
+ # This makes:
+ # 3.3.X  => 303
+ # 3.4.X  => 304
+ # 2.95.3 => 295
+
+ echo "Using $CC $gccver ($gccnum)"
+
 fi
 
 sed > Makefile \
@@ -523,6 +544,10 @@
  -e "s,@AR@,${AR},g" \
  -e "s,@AS@,${AS},g" \
  -e "s,@OC@,${OC},g" \
+ -e "s,@WINDRES@,${WINDRES},g" \
+ -e "s,@DLLTOOL@,${DLLTOOL},g" \
+ -e "s,@DLLWRAP@,${DLLWRAP},g" \
+ -e "s,@RANLIB@,${RANLIB},g" \
  -e "s,@TOOL@,${tool},g" \
  -e "s,@OUTPUT@,${output},g" \
  -e "s,@APPEXTRA@,${appextra},g" \
@@ -531,11 +556,15 @@
  -e "s,@PLUGINS@,${plugins},g" \
  -e "s,@CODECS@,${codecs},g" \
  -e "s,@GCCOPTS@,${GCCOPTS},g" \
+ -e "s,@LDOPTS@,${LDOPTS},g" \
  -e "s,@LOADADDRESS@,${loadaddress},g" \
  -e "s,@EXTRADEF@,${extradefines},g" \
  -e "s,@APPSDIR@,${appsdir},g" \
+ -e "s,@SIMVER@,${simver},g" \
+ -e "s,@GCCVER@,${gccver},g" \
+ -e "s,@GCCNUM@,${gccnum},g" \
 <<EOF 
-## Automaticly generated. http://rockbox.haxx.se
+## Automaticly generated. http://www.rockbox.org/
 
 export ROOTDIR=@ROOTDIR@
 export FIRMDIR=\$(ROOTDIR)/firmware
@@ -562,8 +591,17 @@
 export AR=@AR@
 export AS=@AS@
 export OC=@OC@
+export WINDRES=@WINDRES@
+export DLLTOOL=@DLLTOOL@
+export DLLWRAP=@DLLWRAP@
+export RANLIB=@RANLIB@
 export GCCOPTS=@GCCOPTS@
 export LOADADDRESS=@LOADADDRESS@
+export SIMVER=@SIMVER@
+export SIMDIR=\$(ROOTDIR)/uisimulator/\$(SIMVER)
+export LDOPTS=@LDOPTS@
+export GCCVER=@GCCVER@
+export GCCNUM=@GCCNUM@
 
 .PHONY: all clean tags zip
 
@@ -583,8 +621,19 @@
 	\$(MAKE) -C \$(APPSDIR)/plugins tags
 	\$(MAKE) -C \$(APPSDIR)/plugins/lib tags
 
-zip:
-	\$(TOOLSDIR)/buildzip.pl -r "\$(ROOTDIR)" \$(TARGET)
+zip: all
+	\$(TOOLSDIR)/buildzip.pl -r "\$(ROOTDIR)" \$(TARGET) \$(BINARY)
 EOF
 
+if [ "yes" = "$simulator" ]; then
+
+ cat >> Makefile <<EOF
+
+install:
+	@echo "installing a full setup in your archos dir"
+	@(make zip && cd archos && unzip -oq ../rockbox.zip)
+EOF
+
+fi
+
 echo "Created Makefile"
diff --git a/uisimulator/FILES b/uisimulator/FILES
index 5735d13..66ebcfe 100644
--- a/uisimulator/FILES
+++ b/uisimulator/FILES
@@ -14,3 +14,6 @@
 x11/*.[ch]
 x11/Makefile
 x11/archos/rockbox112.bmp
+common/SOURCES
+win32/SOURCES
+x11/SOURCES
diff --git a/uisimulator/common/Makefile b/uisimulator/common/Makefile
new file mode 100644
index 0000000..b044440
--- /dev/null
+++ b/uisimulator/common/Makefile
@@ -0,0 +1,56 @@
+############################################################################
+#             __________               __   ___.                  
+#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___  
+#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /  
+#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <   
+#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
+#                     \/            \/     \/    \/            \/ 
+# $Id$
+#
+# Copyright (C) 2002 by Daniel Stenberg <daniel@haxx.se>
+#
+# All files in this archive are subject to the GNU General Public License.
+# See the file COPYING in the source tree root for full license agreement.
+#
+# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+# KIND, either express or implied.
+#
+############################################################################
+
+DEPFILE = $(OBJDIR)/dep-commonsim
+
+RM = rm -f
+DEBUG = -g
+
+# Use this for simulator-only files
+INCLUDES = -I. -I$(OBJDIR) -I$(FIRMDIR)/export -I$(APPSDIR)
+
+SRC := $(shell cat SOURCES | gcc -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) \
+ $(TARGET) $(DEFINES) $(EXTRA_DEFINES) -E -P -include "config.h" - )
+OBJS := $(SRC:%.c=$(OBJDIR)/%.o)
+
+DEFINES := -DHAVE_CONFIG_H -DGETTIMEOFDAY_TWO_ARGS -DSIMULATOR \
+$(TARGET) -DAPPSVERSION=\"$(VERSION)\" -DMEM=${MEMORYSIZE} $(EXTRA_DEFINES) 
+
+SOURCES = $(SRC)
+
+DIRS = .
+
+CFLAGS = $(DEBUG) $(DEFINES) $(INCLUDES) $(GCCOPTS)
+
+OUTFILE = $(OBJDIR)/libsim.a
+
+all: $(OUTFILE)
+
+include $(TOOLSDIR)/make.inc
+
+clean:
+	@echo "cleaning commonsim"
+	@$(RM) $(OBJS) *~ core $(DEPFILE)
+
+$(OUTFILE): $(OBJS)
+	@echo "AR+RANLIB $@"
+	@$(AR) ruv $@ $(OBJS) >/dev/null 2>&1
+	@$(RANLIB) $@
+
+-include $(DEPFILE)
diff --git a/uisimulator/common/SOURCES b/uisimulator/common/SOURCES
new file mode 100644
index 0000000..d12a78d
--- /dev/null
+++ b/uisimulator/common/SOURCES
@@ -0,0 +1,8 @@
+fmradio.c
+font-player.c
+io.c
+lcd-common.c
+lcd-playersim.c
+mpegplay.c
+sim_icons.c
+stubs.c
diff --git a/uisimulator/win32/Makefile b/uisimulator/win32/Makefile
index e7d118c..4878d85 100644
--- a/uisimulator/win32/Makefile
+++ b/uisimulator/win32/Makefile
@@ -17,145 +17,52 @@
 #
 ############################################################################
 
-APPDIR = ../../apps
-RECDIR = $(APPDIR)/recorder
-PLAYDIR = $(APPDIR)/player
-PLUGINDIR = $(APPDIR)/plugins
 SIMCOMMON = ../common
 
-ISONDIO := $(findstring ONDIO, $(TARGET))
-ISIRIVER := $(findstring IRIVER, $(TARGET))
-ISPLAYER := $(findstring PLAYER, $(TARGET))
-ifeq ($(ISPLAYER), PLAYER)
-     MACHINEDIR = $(PLAYDIR)
-else
-     MACHINEDIR = $(RECDIR)
-endif
-
-PREVAPPDIR= ..
-FIRMWAREDIR = ../../firmware
-
-# build some sources from these dirs
-DRIVERS = $(FIRMWAREDIR)/drivers
-COMMON = $(FIRMWAREDIR)/common
-
-# include here:
-EXPORT = $(FIRMWAREDIR)/export
-TOOLSDIR = ../../tools
-DOCSDIR = ../../docs
+DEPFILE = $(OBJDIR)/dep-simwin
 
 RM = rm -f
 DEBUG = -g
 
-ifndef OBJDIR
-no_configure:
-	@echo "Don't run make here. Run the tools/configure script from your own build"
-	@echo "directory, then run make there."
-	@echo
-	@echo "More help on how to build rockbox can be found here:"
-	@echo "http://rockbox.haxx.se/docs/how_to_compile.html"
-endif
+INCLUDES = -I. -I$(SIMCOMMON) -I$(OBJDIR) -I$(FIRMDIR)/export -I$(APPSDIR)
 
 DEFINES = -DHAVE_CONFIG_H -DGETTIMEOFDAY_TWO_ARGS -DSIMULATOR \
 $(TARGET) -DAPPSVERSION=\"$(VERSION)\" -DMEM=${MEMORYSIZE} $(EXTRA_DEFINES)
 
 LDFLAGS =  -lgdi32 -luser32
 
-# Use this for simulator-only files
-INCLUDES = -I. -I$(EXPORT) -I$(APPDIR) -I$(MACHINEDIR) -I$(SIMCOMMON) -I$(OBJDIR) -I$(PLUGINDIR)/lib
+SRC := $(shell cat SOURCES | gcc -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) \
+ $(TARGET) $(DEFINES) $(EXTRA_DEFINES) -E -P -include "config.h" - )
+OBJS := $(SRC:%.c=$(OBJDIR)/%.o)
 
-# The true Rockbox Applications should use this include path:
-APPINCLUDES =  $(INCLUDES)
-
-DEFINES += -DWIN32
+SOURCES = $(SRC)
+DIRS = .
 
 CFLAGS = $(DEBUG) $(DEFINES) $(INCLUDES) -W -Wall -mno-cygwin
 
-APPCFLAGS = $(DEBUG) $(DEFINES) $(APPINCLUDES) -W -Wall -mno-cygwin
 DLLTOOLFLAGS = --export-all
 DLLWRAPFLAGS = -s --entry _DllMain@12 --target=i386-mingw32 -mno-cygwin
 
-UNAME := $(shell uname|sed -e "s/CYGWIN.*/CYGWIN/")
-EXEFILE := $(OBJDIR)/rockboxui.exe
+OUTFILE = $(OBJDIR)/libsim.a
 
-ifeq ($(UNAME),CYGWIN)
-  CC = gcc
-  WINDRES = windres
-  DLLTOOL = dlltool
-  DLLWRAP = dllwrap
-  RANLIB = ranlib
-  DEFINES += -DNOCYGWIN
-  LDFLAGS += -mno-cygwin
-else
-  CC = i386-mingw32msvc-gcc
-  WINDRES = i386-mingw32msvc-windres
-  DLLTOOL = i386-mingw32msvc-dlltool
-  DLLWRAP = i386-mingw32msvc-dllwrap
-  RANLIB = i386-mingw32msvc-ranlib
-  CFLAGS += -mwindows
-  LDFLAGS += -mwindows
-  APPCFLAGS += -mwindows
-endif
+all: $(OUTFILE)
+	@echo "MAKE in common sim"
+	$(MAKE) -C $(SIMCOMMON)
 
-ifeq ($(ISPLAYER),PLAYER)
-	LCDSRSC = lcd-playersim.c lcd-player.c lcd-player-charset.c font-player.c
-else
-ifeq ($(ISIRIVER),IRIVER)
-	LCDSRSC = lcd-h100.c sysfont.c font.c
-else
-	LCDSRSC = lcd-recorder.c sysfont.c font.c
-endif
-endif
-COMMONSRCS = io.c stubs.c lcd-common.c sim_icons.c fmradio.c
+include $(TOOLSDIR)/make.inc
 
-FIRMSRCS = $(LCDSRSC) id3.c mp3data.c usb.c mpeg.c mp3_playback.c \
-	powermgmt.c power.c sprintf.c buffer.c strtok.c random.c \
-	timefuncs.c strcasestr.c
-
-APPS = main.c tree.c menu.c credits.c main_menu.c icons.c language.c \
-	playlist.c wps.c wps-display.c settings.c status.c \
-	screens.c sleeptimer.c keyboard.c onplay.c\
-	misc.c plugin.c playlist_viewer.c bookmark.c filetypes.c \
-	settings_menu.c sound_menu.c playlist_menu.c filetree.c dbtree.c
-
-ifneq ($(ISPLAYER),PLAYER)
-   APPS += peakmeter.c bmp.c widgets.c radio.c
-endif
-
-SRCS = button.c lcd-win32.c panic-win32.c thread-win32.c \
-	debug-win32.c kernel.c string-win32.c uisw32.c \
-	$(APPS) $(FIRMSRCS) $(COMMONSRCS)
-
-OBJS := $(OBJDIR)/lang.o $(SRCS:%.c=$(OBJDIR)/%.o) $(OBJDIR)/uisw32-res.o
-
-ifdef ENABLEDPLUGINS
-  ROCKS := $(patsubst $(PLUGINDIR)/%.c,$(OBJDIR)/%.rock,$(wildcard $(PLUGINDIR)/*.c))
-  PLUGINLIBOBJS := $(patsubst $(PLUGINDIR)/lib/%.c,$(OBJDIR)/%.o,$(wildcard $(PLUGINDIR)/lib/*.c))
-endif
-
-all: $(TOOLSDIR)/convbdf $(EXEFILE) $(ROCKS)
-
-$(ROCKS): $(OBJDIR)/libplugin.a
-
-$(TOOLSDIR)/convbdf:
-	$(MAKE) -C $(TOOLSDIR)
-
-$(EXEFILE): $(OBJDIR)/lang.o $(OBJS)
-	@echo LD $@
-	@$(CC) $(OBJS) -o $(EXEFILE) $(LDFLAGS)
+$(OUTFILE): $(OBJS)
+	@echo "AR+RANLIB $@"
+	@$(AR) ruv $@ $(OBJS) >/dev/null 2>&1
+	@$(RANLIB) $@
 
 clean:
-	$(RM) $(OBJS) *~ core $(EXEFILE) $(CLIENTS) $(OBJDIR)/uisw32-res.o \
-	$(OBJDIR)/lang.[cho] $(OBJDIR)/build.lang $(OBJDIR)/*.o \
-	$(OBJDIR)/*.rock $(OBJDIR)/*.def $(OBJDIR)/sysfont.c \
-	$(OBJDIR)/credits.raw $(OBJDIR)/libplugin.a
-	$(RM) -f $(OBJDIR)/UI256.bmp
-	$(RM) -r $(DEPS)
+	@echo "cleaning simwin"
+	@$(RM) $(OBJS) *~ core $(OUTFILE) $(OBJDIR)/uisw32-res.o \
+	$(OBJDIR)/UI256.bmp $(DEPFILE)
+	@$(MAKE) -C $(SIMCOMMON) clean
 
 ################## Specific dependencies ##################
-$(OBJDIR)/credits.raw: $(DOCSDIR)/CREDITS
-	perl $(APPDIR)/credits.pl < $< > $@
-
 $(OBJDIR)/UI256.bmp: UI-$(ARCHOS).bmp
 	@echo UI
 	@cp $< $@
@@ -163,83 +70,4 @@
 $(OBJDIR)/uisw32-res.o: uisw32.rc $(OBJDIR)/UI256.bmp
 	$(WINDRES) -I$(OBJDIR) -i $< -o $@
 
-$(OBJDIR)/credits.o: $(APPDIR)/credits.c $(APPDIR)/credits.h $(OBJDIR)/credits.raw
-
-$(OBJDIR)/button.o: button.c
-$(OBJDIR)/plugin.o: $(APPDIR)/plugin.c plugin-win32.h
-$(OBJDIR)/build.lang: $(APPDIR)/lang/$(LANGUAGE).lang
-	@echo UPLANG
-	@perl $(TOOLSDIR)/uplang $(APPDIR)/lang/english.lang $< > $@
-
-$(OBJDIR)/lang.o: $(OBJDIR)/build.lang
-	@echo GENLANG
-	@perl -s $(TOOLSDIR)/genlang -p=$(OBJDIR)/lang $<
-	@$(CC) $(CFLAGS) -c $(OBJDIR)/lang.c -o $@
-
-$(OBJDIR)/sysfont.o: $(FIRMWAREDIR)/fonts/clR6x8.bdf
-	@echo CC $<
-	@$(TOOLSDIR)/convbdf -c -o $(OBJDIR)/sysfont.c $<
-	@$(CC) $(APPCFLAGS) -c $(OBJDIR)/sysfont.c -o $@
-
-################## Plugins ##################
-$(OBJDIR)/libplugin.a:  $(PLUGINLIBOBJS)
-	@echo AR $@
-	@$(AR) ru $@ $+
-	@$(RANLIB) $@
-
-$(OBJDIR)/%.o: $(PLUGINDIR)/lib/%.c
-	@echo CC $<
-	@$(CC) $(CFLAGS) -DPLUGIN -c $< -o $@
-
-$(OBJDIR)/%.po : $(PLUGINDIR)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -DPLUGIN -c $< -o $@
-
-$(OBJDIR)/%.rock : $(OBJDIR)/%.po $(OBJDIR)/libplugin.a
-	@echo DLL $@
-	@$(DLLTOOL) $(DLLTOOLFLAGS) -z $(OBJDIR)/$*.def $<
-	@$(DLLWRAP) $(DLLWRAPFLAGS) --def $(OBJDIR)/$*.def $< $(OBJDIR)/libplugin.a -o $@
-	@chmod -x $@
-
-################## Patterns for building objects ##################
-$(OBJDIR)/%.o: %.c
-	@echo CC $<
-	@$(CC) $(CFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(SIMCOMMON)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(MACHINEDIR)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(DRIVERS)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(FIRMWAREDIR)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(COMMON)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(APPDIR)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-################## Auto-dependencies ##################
-DEPS:=$(OBJDIR)/.deps
-
-$(DEPS)/%.d: %.c
-		@$(SHELL) -c 'if [ ! -d $(DEPS) ]; then \
-			echo Creating the dependency directory: $(DEPS); \
-			mkdir -p $(DEPS); fi'
-		@echo "Updating Dependencies for $<"
-		@$(SHELL) -ec '$(CC) -MM $(CFLAGS) $< \
-	|sed '\''s|\($*\)\.o[ :]*|$(OBJDIR)/\1.o $(<:%.c=%.d) : |g'\'' > $@; \
-	[ -s $@ ] || rm -f $@'
-
--include $(SRCS:%.c=$(DEPS)/%.d)
+-include $(DEPFILE)
diff --git a/uisimulator/win32/SOURCES b/uisimulator/win32/SOURCES
new file mode 100644
index 0000000..6a52c5e
--- /dev/null
+++ b/uisimulator/win32/SOURCES
@@ -0,0 +1,10 @@
+button.c
+debug-win32.c
+dir-win32.c
+kernel.c
+lcd-win32.c
+mpeg-win32.c
+panic-win32.c
+string-win32.c
+thread-win32.c
+uisw32.c
diff --git a/uisimulator/x11/Makefile b/uisimulator/x11/Makefile
index 7b86ee2..68815fe 100644
--- a/uisimulator/x11/Makefile
+++ b/uisimulator/x11/Makefile
@@ -17,252 +17,44 @@
 #
 ############################################################################
 
-RECDIR = $(APPSDIR)/recorder
-PLAYDIR = $(APPSDIR)/player
-PLUGINDIR = $(APPSDIR)/plugins
 SIMCOMMON = ../common
 
-ISONDIO := $(findstring ONDIO, $(TARGET))
-ISIRIVER := $(findstring IRIVER, $(TARGET))
-ISPLAYER := $(findstring PLAYER, $(TARGET))
-ifeq ($(ISPLAYER), PLAYER)
-     MACHINEDIR = $(PLAYDIR)
-else
-     MACHINEDIR = $(RECDIR)
-endif
-
-PREVAPPSDIR= ..
-
-# build some sources from these dirs
-DRIVERS = $(FIRMDIR)/drivers
-COMMON = $(FIRMDIR)/common
-
-# include here:
-EXPORT = $(FIRMDIR)/export
+DEPFILE = $(OBJDIR)/dep-sim
 
 RM = rm -f
 DEBUG = -g
 
-ifndef OBJDIR
-no_configure:
-	@echo "Don't run make here. Run the tools/configure script from your own build"
-	@echo "directory, then run make there."
-	@echo
-	@echo "More help on how to build rockbox can be found here:"
-	@echo "http://rockbox.haxx.se/docs/how_to_compile.html"
-endif
+# Use this for simulator-only files
+INCLUDES = -I. -I$(SIMCOMMON) -I$(OBJDIR) -I$(FIRMDIR)/export -I$(APPSDIR)
+
+SRC := $(shell cat SOURCES | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) $(INCLUDES) \
+ $(TARGET) $(DEFINES) $(EXTRA_DEFINES) -E -P -include "config.h" - )
+OBJS := $(SRC:%.c=$(OBJDIR)/%.o)
 
 DEFINES := -DHAVE_CONFIG_H -DGETTIMEOFDAY_TWO_ARGS -DSIMULATOR \
 $(TARGET) -DAPPSVERSION=\"$(VERSION)\" -DMEM=${MEMORYSIZE} $(EXTRA_DEFINES) 
 
-# Use this for simulator-only files
-INCLUDES = -I. -I$(EXPORT) -I$(APPSDIR) -I$(MACHINEDIR) -I$(SIMCOMMON) -I$(OBJDIR) -I$(PLUGINDIR)/lib
+SOURCES = $(SRC)
 
-# The true Rockbox Applications should use this include path:
-APPINCLUDES =  $(INCLUDES)
+DIRS = .
 
-SRCDIRS = . $(DRIVERS) $(FIRMDIR)/export $(APPSDIR) $(MACHINEDIR)
+CFLAGS = $(DEBUG) $(DEFINES) $(INCLUDES) $(GCCOPTS)
 
-# The true Rockbox Applications should use this include path:
-APPINCLUDES = $(INCLUDES)
+OUTFILE = $(OBJDIR)/libsim.a
 
-LIBS = -lpthread
+all: $(OUTFILE)
+	@echo "MAKE in common sim"
+	$(MAKE) -C $(SIMCOMMON)
 
-CFLAGS = $(DEBUG) $(DEFINES) $(INCLUDES) -W -Wall
-
-APPCFLAGS = $(DEBUG) $(DEFINES) $(APPINCLUDES) -W -Wall
-
-UNAME := $(shell uname|sed -e "s/CYGWIN.*/CYGWIN/")
-EXEFILE = $(OBJDIR)/rockboxui
-ifeq ($(UNAME),Linux)
-  LDFLAGS = -lX11 -lm -lXt -lXmu -lnsl -ldl
-  INCLUDES += -I/usr/X11R6/include
-  LIBDIRS = -L/usr/X11R6/lib
-  DEFINES += -DHAVE_OSS
-else
-ifeq ($(UNAME),CYGWIN)
-  LDFLAGS = -lSM -lICE -lXt -lX11 -lXmu -lSM -lICE -lX11
-  INCLUDES += -I/usr/X11R6/include
-  LIBDIRS = -L/usr/X11R6/lib
-  DEFINES += -DHAVE_OSS -D_SSIZE_T_
-  EXEFILE = $(OBJDIR)/rockboxui.exe
-else
-ifeq ($(UNAME),FreeBSD)
-  LIBS = -pthread
-  LDFLAGS = -lX11 -lm -lXt -lXmu
-  INCLUDES += -I/usr/X11R6/include
-  LIBDIRS = -L/usr/X11R6/lib
-  DEFINES += -DHAVE_OSS
-else
-  LDFLAGS = -lX11 -lm -lXt -lXmu -lnsl -ldl
-  DEFINES += -DBIG_ENDIAN
-  LIBDIRS = 
-endif
-endif
-endif
-
-ifeq ($(HAVE_MPEG_PLAY),1)
-	SOUNDSRC = mpegplay.c oss_sound.c
-	LDFLAGS += $(SOUND_LDFLAGS)
-	CFLAGS += $(SOUND_CFLAGS) -DHAVE_MPEG_PLAY
-else
-	SOUNDSRC = 
-endif
-
-ifeq ($(ISPLAYER), PLAYER)
-	LCDSRSC = lcd-playersim.c lcd-player.c lcd-player-charset.c font-player.c
-else
-ifeq ($(ISIRIVER),IRIVER)
-	LCDSRSC = lcd-h100.c sysfont.c font.c
-else
-	LCDSRSC = lcd-recorder.c sysfont.c font.c
-endif
-endif
-COMMONSRCS = io.c stubs.c lcd-common.c sim_icons.c fmradio.c
-
-FIRMSRCS = $(LCDSRSC) id3.c mp3data.c usb.c mpeg.c mp3_playback.c \
-	powermgmt.c power.c sprintf.c buffer.c strtok.c random.c \
-	timefuncs.c panic.c debug.c strcasestr.c
-
-APPS = main.c tree.c menu.c credits.c main_menu.c icons.c language.c \
-	playlist.c wps.c wps-display.c settings.c status.c \
-	screens.c sleeptimer.c keyboard.c onplay.c\
-	misc.c plugin.c playlist_viewer.c bookmark.c filetypes.c \
-	settings_menu.c sound_menu.c playlist_menu.c filetree.c dbtree.c
-
-ifneq ($(ISPLAYER), PLAYER)
-   APPS += peakmeter.c bmp.c widgets.c radio.c
-endif
-
-SRCS = screenhack.c uibasic.c resources.c visual.c lcd-x11.c \
-       button-x11.c thread.c $(APPS) $(MENUS) $(FIRMSRCS) \
-       $(COMMONSRCS) $(SOUNDSRC)
-
-OBJS := $(OBJDIR)/lang.o $(SRCS:%.c=$(OBJDIR)/%.o)
-
-ifdef ENABLEDPLUGINS
-  ROCKS := $(patsubst $(PLUGINDIR)/%.c,$(OBJDIR)/%.rock,$(wildcard $(PLUGINDIR)/*.c))
-  PLUGINLIBOBJS := $(patsubst $(PLUGINDIR)/lib/%.c,$(OBJDIR)/%.o,$(wildcard $(PLUGINDIR)/lib/*.c))
-endif
-
-all: $(TOOLSDIR)/convbdf $(EXEFILE) $(ROCKS)
-
-$(ROCKS): $(OBJDIR)/libplugin.a
-
-$(TOOLSDIR)/convbdf:
-	$(MAKE) -C $(TOOLSDIR)
+include $(TOOLSDIR)/make.inc
 
 clean:
-	$(RM) $(OBJS) *~ core $(EXEFILE) $(CLIENTS) $(OBJDIR)/lang.[cho] \
-	$(OBJDIR)/build.lang $(OBJDIR)/*.o $(OBJDIR)/sysfont.c \
-	$(ROCKS) $(OBJDIR)/credits.raw
-	$(RM) -r $(DEPS)
-	$(RM) -f $(OBJDIR)/libplugin.a $(OBJDIR)/rockbox.zip
+	@echo "cleaning sim"
+	@$(RM) $(OBJS) *~ core $(OUTFILE) $(DEPFILE)
+	@$(MAKE) -C $(SIMCOMMON) clean
 
-################## Specific dependencies ##################
-$(OBJDIR)/credits.raw: $(DOCSDIR)/CREDITS
-	perl $(APPSDIR)/credits.pl < $< > $@
+$(OUTFILE): $(OBJS)
+	@echo "AR $@"
+	@$(AR) ruv $@ $(OBJS) >/dev/null 2>&1
 
-$(OBJDIR)/uisw32-res.o: uisw32.rc
-	$(WINDRES) -i $< -o $@
-
-$(OBJDIR)/credits.o: $(APPSDIR)/credits.c $(APPSDIR)/credits.h $(OBJDIR)/credits.raw
-
-$(OBJDIR)/thread.o: ./thread.c
-$(OBJDIR)/plugin.o: $(APPSDIR)/plugin.c
-$(OBJDIR)/build.lang: $(APPSDIR)/lang/$(LANGUAGE).lang
-	@echo "UPLANG"
-	@perl $(TOOLSDIR)/uplang $(APPSDIR)/lang/english.lang $< > $@
-
-$(OBJDIR)/lang.o: $(OBJDIR)/build.lang
-	@echo GENLANG
-	@perl -s $(TOOLSDIR)/genlang -p=$(OBJDIR)/lang $<
-	@echo "CC lang.c"
-	$(CC) $(CFLAGS) -c $(OBJDIR)/lang.c -o $@
-
-$(OBJDIR)/sysfont.o: $(FIRMDIR)/fonts/clR6x8.bdf
-	@echo CC $<
-	@$(TOOLSDIR)/convbdf -c -o $(OBJDIR)/sysfont.c $<
-	@$(CC) $(APPCFLAGS) -c $(OBJDIR)/sysfont.c -o $@
-
-################## Plugins ##################
-$(OBJDIR)/libplugin.a:  $(PLUGINLIBOBJS)
-	@echo AR $<
-	@$(AR) ru $@ $+
-
-$(OBJDIR)/%.o: $(PLUGINDIR)/lib/%.c
-	@echo CC $<
-	@$(CC) $(CFLAGS) -DPLUGIN -c $< -o $@
-
-$(OBJDIR)/%.rock: $(APPSDIR)/plugins/%.c $(APPSDIR)/plugin.h
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -DPLUGIN -shared $< -L$(OBJDIR) -lplugin -o $@
-ifeq ($(UNAME),CYGWIN)
-# 'x' must be kept or you'll have "Win32 error 5" 
-#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
-#         #define ERROR_ACCESS_DENIED 5L
-else
-	@chmod -x $@
-endif
-
-################## Patterns for building objects ##################
-$(OBJDIR)/%.o: ../x11/%.c
-	@echo CC $<
-	@$(CC) $(CFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(SIMCOMMON)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(MACHINEDIR)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(DRIVERS)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(FIRMDIR)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(COMMON)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-$(OBJDIR)/%.o: $(APPSDIR)/%.c
-	@echo CC $<
-	@$(CC) $(APPCFLAGS) -c $< -o $@
-
-
-################## Auto-dependencies ##################
-DEPS:=$(OBJDIR)/.deps
-
-$(DEPS)/%.d: %.c
-		@$(SHELL) -c 'if [ ! -d $(DEPS) ]; then \
-			echo Creating the dependency directory: $(DEPS); \
-			mkdir -p $(DEPS); fi'
-		@$(SHELL) -ec '$(CC) -MM $(CFLAGS) $< \
-	|sed '\''s|\($*\)\.o[ :]*|$(OBJDIR)/\1.o $(<:%.c=%.d) : |g'\'' > $@; \
-	[ -s $@ ] || rm -f $@'
-
--include $(SRCS:%.c=$(DEPS)/%.d)
-
-# these ones are simulator-specific
-
-$(OBJDIR)/%.o: %.c
-	$(CC) $(CFLAGS) -c $< -o $@
-
-ifeq ($(UNAME),CYGWIN)
-$(EXEFILE): $(OBJS)
-	@echo LD $@
-	@$(CC) -g -o $(EXEFILE) $(OBJS) $(LIBDIRS) $(LDFLAGS) $(LIBS)
-else
-$(EXEFILE): $(OBJS)
-	@echo LD $@
-	@$(CC) -g -o $(EXEFILE) $(LIBDIRS) $(LDFLAGS) $(OBJS) $(LIBS)
-endif
-
-tags:
-	@$(SHELL) -c 'for d in $(SRCDIRS); do { etags -o $(OBJDIR)/TAGS -a $$d/*.[ch]; }; done'
-
+-include $(DEPFILE)
diff --git a/uisimulator/x11/SOURCES b/uisimulator/x11/SOURCES
new file mode 100644
index 0000000..ea60b4e
--- /dev/null
+++ b/uisimulator/x11/SOURCES
@@ -0,0 +1,10 @@
+button-x11.c
+lcd-x11.c
+#if 0 /* if sound is enabled */
+oss_sound.c
+#endif
+resources.c
+screenhack.c
+thread.c
+uibasic.c
+visual.c