+# PHASE 1a: upstream code and how to fetch it
+include Makefile.upstream
+jpeg_c_sources := jdapimin.c jdapistd.c jcomapi.c jcparam.c jdmaster.c jdinput.c jdmainct.c jdcoefct.c jdpostct.c jdmarker.c
+jpeg_c_sources += jdhuff.c jdphuff.c jddctmgr.c jidctint.c jidctfst.c jidctflt.c jidctred.c jdsample.c jdcolor.c jdmerge.c
+jpeg_c_sources += jquant1.c jquant2.c jerror.c jutils.c jmemnobs.c jmemmgr.c
+
+upstream/jpeg-6b/src/%.c: download_jpeg-6b
+
+freetype_sources := ftsystem.c ftmm.c ftbbox.c ftinit.c ftdebug.c ftbase.c ftglyph.c smooth.c sfnt.c truetype.c
+upstream/freetype-2.1.4/%.c: download_freetype-2.1.4
+
+# PHASE 1b: human-written inputs
+java_sources := $(patsubst src/%.java, build/java/%.java, $(shell find src -name \*.java))
+java_sources += $(patsubst src/%.java.pp, build/java/%.java, $(shell find src -name \*.java.pp))
+java_sources += $(patsubst src/%.mips.c, build/java/%.java, $(shell find src -name \*.mips.c))
+java_sources += build/java/org/xwt/Builtin.java
+
+# PHASE 2: ready-to-compile java files (either symlinked to phase 1a sources or else generated from them)
+build/java/%.java: src/%.java ; @echo linking $@; mkdir -p $(@D); ln -s `echo $(@D) | sed 's_[^/]*\(/\|$$\)_../_g'`/$< $@
+build/cc/%.cc: src/%.c ; @echo linking $@; mkdir -p $(@D); ln -s `echo $(@D) | sed 's_[^/]*\(/\|$$\)_../_g'`/$< $@
+build/c/jpeg/%.c: upstream/jpeg-6b/src/%.c
+ @echo linking $@; mkdir -p $(@D); ln -s `echo $(@D) | sed 's_[^/]*\(/\|$$\)_../_g'`/$< $@
+build/c/freetype/%.c: upstream/freetype-2.1.4/src/base/%.c
+ @echo linking $@; mkdir -p $(@D); ln -s `echo $(@D) | sed 's_[^/]*\(/\|$$\)_../_g'`/$< $@
+build/c/freetype/%.c: upstream/freetype-2.1.4/src/%/%.c
+ @echo linking $@; mkdir -p $(@D); ln -s `echo $(@D) | sed 's_[^/]*\(/\|$$\)_../_g'`/$< $@
+
+build/java/%.java: src/%.java.pp build/class/org/xwt/util/Preprocessor.class
+ @echo "preprocessing .java.pp -> .java: $<"
+ @mkdir -p `dirname $@`
+ @rm -f $@
+ @java -cp build/class org.xwt.util.Preprocessor < $< > $@
+
+# PHASE 3: class files
+build/class/%.class: build/java/%.java .jikes
+ @echo "compiling .java -> .class: $<"
+ @mkdir -p build/class
+ @./.jikes $<
+
+compile: .compile
+.compile: $(java_sources)
+ @echo "compiling .java -> .class: src/**/*.java"
+ @mkdir -p build/class
+ @./.jikes $(java_sources)
+ @touch .compile
+
+# PHASE 4: gcj-generated headers
+java_headers := $(java_sources:build/java/%.java=build/h/%.h)
+build/h/%.h: build/class/%.class .compile
+ @echo "extracting .class -> .h: $<"
+ @mkdir -p `dirname $@`
+ @ls `echo $< | sed s/.class\$$//`*.class |\
+ sed s_build/class/__ | sed s/.class\$$//g | sed s_/_._g | (cd build/class; xargs ../../$(gcc_path)/bin/gcjh -d ../h --classpath .)
+
+# PHASE 4: object files
+build/$(platform)/%.java.o: build/java/%.java
+ @echo "compiling .java -> .o: $<"
+ @mkdir -p `dirname $@`
+ @$(gcj) -c $< -o $@
+
+build/$(platform)/org/xwt/plat/$(platform).cc.o: src/org/xwt/plat/*.cc $(java_headers)
+ @echo "compiling .cc -> .o: $<"
+ @mkdir -p `dirname $@`
+ @$(g++) -I/usr/X11R6/include/ -I$(gcc_path)/include -Ibuild/h -Iupstream/$(platform)/include -Wno-multichar -c $< -o $@
+
+build/$(platform)/jpeg-6b/%.c.o: upstream/jpeg-6b/src/%.c
+ @echo "compiling .c -> .o: $<"
+ @mkdir -p `dirname $@`
+ @$(gcc) -Ibuild/h -c $< -o $@
+
+# PHASE 5: linking
+$(target_bin).ar: $(java_object_files) build/$(platform)/org/xwt/plat/$(platform).cc.o $(jpeg_object_files)
+ @echo "archiving .o -> $(target_bin).ar"
+ @rm -f $(target_bin).ar
+ @$(ar) -c $(target_bin).ar $^
+
+$(target_bin): $(target_bin).ar
+ @echo "linking .o -> $(target_bin)"
+ @mkdir -p build/$(platform)
+ @$(gcj) --main=org.xwt.Main -o build/$(platform)/$(target_bin) $(target_bin).ar -L$(gcc_path)/lib $(platform_link)
+
+
+##############################################################################
+# Special treatment:
+#