+# auto directory creation with %/%: %?
+
+target_Darwin := powerpc-apple-darwin
+target_Win32 := i686-pc-mingw32
+target_Solaris := sparc-sun-solaris2.7
+target_Linux := i686-pc-linux-gnu
+target := $(target_$(platform))
+
+all: JVM Linux Win32 Darwin Solaris
+
+clean: ; rm -rf build
+dist-clean:
+ rm -rf .jikes .configure* .install* build .compile .build*
+ find upstream -name config.cache -exec rm {} \;
+ test -e upstream/mips && make -C upstream/mips clean
+
+libwing_Linux := -Lupstream/install/i686-pc-linux-gnu/lib/
+libwing_Linux += upstream/install/i686-pc-linux-gnu/lib/libWINGs.a
+libwing_Linux += upstream/install/i686-pc-linux-gnu/lib/libwraster.a
+
+libwing_Solaris := -Lupstream/install/sparc-sun-solaris2.7/lib/
+libwing_Solaris += upstream/install/sparc-sun-solaris2.7/lib/libWINGs.a
+libwing_Solaris += upstream/install/sparc-sun-solaris2.7/lib/libwraster.a
+
+JVM: build/JVM/ibex.jar
+Linux: ; make gcj platform=Linux link_flags="$(libwing_Linux) -lXpm -lX11 -lXext"
+Solaris: ; make gcj platform=Solaris link_flags="$(libwing_Solaris) -lXpm -lX11 -lXext -lpthread"
+Win32: ; make gcj platform=Win32 link_flags="-Wl,--subsystem,windows -lcomdlg32"
+Darwin: ; make gcj platform=Darwin link_flags="$(darwin_linker_flags)"
+
+darwin_libdir := upstream/install/powerpc-apple-darwin/lib
+darwin_linker_flags := -Wl,-dylib_file,/usr/lib/libSystem.B.dylib:$(darwin_libdir)/libSystem.B.dylib
+darwin_linker_flags += -Wl,-dylib_file,/usr/lib/system/libmathCommon.A.dylib:$(darwin_libdir)/libmathCommon.A.dylib
+darwin_ogl_libdir := /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries
+darwin_linker_flags += -Wl,-dylib_file,$(darwin_ogl_libdir)/libGL.dylib:$(darwin_libdir)/libGL.dylib
+darwin_linker_flags += -Wl,-dylib_file,$(darwin_ogl_libdir):$(darwin_libdir)/libGLU.dylib
+darwin_linker_flags += -lSystem.B -lmathCommon.A -lGL -lGLU
+
+# stupid broke-ass darwin linker...
+darwin_linker_flags += build/Darwin/org/ibex/plat/OpenGL.java.o build/Darwin/org/ibex/plat/POSIX.java.o
+
+target_bin_extension_$(platform) := $(shell echo $(platform) | tr A-Z a-z)
+target_bin_extension_Win32 := exe
+target_bin_extension_JVM := jar
+target_bin_extension := $(target_bin_extension_$(platform))
+target_bin := ibex.$(target_bin_extension)
+
+jikes_flags := -nowarn
+gcc_optimizations := -O2
+#gcc_optimizations := -O9 -ffast-math -fomit-frame-pointer -foptimize-sibling-calls
+#gcc_optimizations += -finline-functions -funroll-loops -ffunction-sections -fdata-sections
+
+gcc_flags := -nostdinc $(gcc_optimizations) -Ibuild/h -Iupstream/jpeg-6b/src -Iupstream/jpeg-6b/build-$(target)
+gcc_flags += -Iupstream/install/lib/gcc-lib/$(target)/3.3/include -Iupstream/install/$(target)/include
+gcj := upstream/install/bin/$(target)-gcj $(gcc_flags) -fCLASSPATH=build/java
+gcjh := $(shell pwd)/upstream/install/bin/$(shell test -e upstream/install/bin/$(target)-gcjh && echo $(target)-)gcjh
+g++ := upstream/install/bin/$(target)-gcj $(gcc_flags) -Iupstream/install/include -Wno-multichar
+gcc := upstream/install/bin/$(target)-gcc $(gcc_flags)
+jar := $(shell ((type fastjar &>/dev/null) && echo fastjar) || echo jar)
+
+gcj: .install_gcc-3.3_$(target)
+ make compile
+ make build/$(platform)/$(target_bin) link_flags="$(link_flags)" platform=$(platform)
+
+include Makefile.upstream
+
+java_sources := $(patsubst src/%.java, build/java/%.java, $(shell find src -name '*.java' | grep -v Preprocessor | grep -v plat/AWT | grep -v plat/Java2))
+java_sources += build/java/org/xwt/mips/Errno.java
+java_sources += build/java/org/xwt/mips/Unistd.java
+java_sources += build/java/org/xwt/mips/Syscalls.java
+java_sources += build/java/org/xwt/mips/Registers.java
+java_sources += build/java/org/xwt/mips/Runtime.java
+java_sources += build/java/org/xwt/mips/UnixRuntime.java
+java_sources += build/java/org/xwt/mips/util/SeekableByteArray.java
+java_sources += build/java/org/xwt/mips/util/SeekableData.java
+java_sources += build/java/org/xwt/mips/util/SeekableFile.java
+java_sources += build/java/org/xwt/mips/util/SeekableInputStream.java
+
+build/java/org/xwt/mips/util/%:
+ @test -e .install_mips2java || make .install_mips2java
+ @echo linking $@
+ @mkdir -p $(@D)
+ @cd $(@D); ln -sf ../../../../../../upstream/mips/org/xwt/mips/util/$*
+
+build/java/org/xwt/mips/%:
+ @test -e .install_mips2java || make .install_mips2java
+ @echo linking $@
+ @mkdir -p $(@D)
+ @test -e upstream/mips/build/org/xwt/mips/$* && (cd $(@D); \
+ ln -sf ../../../../../upstream/mips/build/org/xwt/mips/$*); true
+ @test -e upstream/mips/org/xwt/mips/$* && (cd $(@D); \
+ ln -sf ../../../../../upstream/mips/org/xwt/mips/$*); true
+
+
+### Java Class Files ##############################################################################
+
+build/class/org/ibex/translators/MIPSApps.class: build/mips/mipsapps.mips .jikes
+ mkdir -p build/java/org/ibex/translators
+ @echo -e "\n\033[1mtranslating .mips -> .java: $<\033[0m"
+ (echo -e 'package org.ibex.translators;\n'; \
+ java -Xint -cp upstream/mips/build org.xwt.mips.Compiler MIPSApps build/mips/mipsapps.mips) \
+ | sed 's,//.*$$,,;'| tr -d '\n' > build/java/org/ibex/translators/MIPSApps.java
+ @echo -e "\n\033[1mcompiling .java -> .class: $<\033[0m"
+ ./.jikes -g:none build/java/org/ibex/translators/MIPSApps.java
+
+build/java/org/ibex/%.java: src/org/ibex/%.java
+ make build/class/org/ibex/util/Preprocessor.class
+ @echo -e "\n\033[1mpreprocessing .java -> .java: $<\033[0m"
+ mkdir -p `dirname $@`; java -cp build/class org.ibex.util.Preprocessor < $< > $@
+
+build/java/%.java: src/%.java ; @echo linking $@; mkdir -p $(@D); ln -fs `echo $(@D)/ | sed 's_[^/]*//*_../_g'`/$< $@
+build/cc/%.cc: src/%.c ; @echo linking $@; mkdir -p $(@D); ln -fs `echo $(@D)/ | sed 's_[^/]*//*_../_g'`/$< $@
+build/res/%: src/% ; @echo linking $@; mkdir -p $(@D); ln -fs `echo $(@D)/ | sed 's_[^/]*//*_../_g'`/$< $@
+
+gnu_regexp_jfiles := $(patsubst src/%.java, build/java/%.java, $(shell find src/gnu/regexp -name \*.java))
+gnu_regexp := $(patsubst src/%.java, build/class/%.class, $(shell find src/gnu/regexp -name \*.java))
+
+build/class/gnu/regexp/%.class: build/java/gnu/regexp/%.java $(gnu_regexp_jfiles) .jikes
+ @echo -e "\n\033[1mcompiling .java -> .class: $<\033[0m"
+ mkdir -p build/class/org/ibex/util/
+ ./.jikes $<
+
+build/class/org/ibex/util/Preprocessor.class: src/org/ibex/util/Preprocessor.java $(gnu_regexp) .jikes
+ @echo -e "\n\033[1mcompiling .java -> .class: $<\033[0m"
+ mkdir -p build/class/org/ibex/util/
+ ./.jikes src/org/ibex/util/Vec.java
+ ./.jikes $<
+
+build/class/%.class: build/java/%.java $(java_sources) .jikes
+ @echo -e "\n\033[1mcompiling .java -> .class: $<\033[0m"
+ mkdir -p $(@D)
+ ./.jikes $<
+
+oldcompile:; CLASSPATH=$$CLASSPATH:lib/libgcj-minimal.jar:upstream/mips/build javac -target 1.1 -source 1.2 -d build/class `find build/java/ -name \*.java`
+
+compile: .jikes .install_mips2java
+ @make -s $(java_sources:build/java/%.java=build/class/%.class)
+.compile: compile
+
+#.compile: $(java_sources) .jikes .install_mips2java
+# @echo -e "\n\033[1mcompiling .java -> .class: src/**/*.java\033[0m"
+# @rm -f $(java_sources:build/java/%.java=build/class/%.class)
+# mkdir -p build/class
+# cd build/java; for A in `find . -name \*.java`; do mkdir -p ../class/`dirname $$A`; done
+# @./.jikes -Xlint:deprecation $(java_sources)
+# touch .compile
+
+build/JVM/ibex.jar: .compile build/res/builtin.jar build/class/org/ibex/translators/MIPSApps.class
+ @echo -e "\n\033[1marchiving .class -> .jar: build/JVM/ibex.jar\033[0m"
+ mkdir -p build/JVM
+ echo -e "Manifest-Version: 1.0\nMain-Class: org.ibex.Main\n" > build/JVM/.manifest
+ cd build/class/org/ibex; ln -sf ../../../res/builtin.jar
+ cd build/class; $(jar) cfm ../JVM/ibex.jar ../JVM/.manifest \
+ `find . \! -type d` \
+ $(patsubst %,../../upstream/mips/build/org/xwt/mips/%*.class, Runtime Registers Syscalls Errno)
+
+
+### GCJH Headers ##############################################################################
+
+java_headers := $(java_sources:build/java/%.java=build/h/%.h)
+build/h/edu/stanford/ejalbert/BrowserLauncher.h:; mkdir -p $(@D); touch $@
+build/h/%.h: build/class/%.class .compile
+ @echo -e "\n\033[1mextracting .class -> .h: $<\033[0m"
+ mkdir -p `dirname $@`
+ ls `echo $< | sed s/.class\$$//`*.class |\
+ sed s_build/class/__ | sed s/.class\$$//g | sed s_/_._g | (cd build/class; xargs $(gcjh) -d ../h --classpath .)
+
+
+### Native Code ##############################################################################
+
+# a hack since we've disabled gcj's awt implementation
+build/$(platform)/org/ibex/plat/Java2.java.o: ; touch .empty.c; mkdir -p $(@D); $(gcc) -c .empty.c -o $@;
+build/$(platform)/org/ibex/plat/AWT.java.o: ; touch .empty.c; mkdir -p $(@D); $(gcc) -c .empty.c -o $@;
+
+build/$(platform)/%.java.o: build/class/%.class
+ @echo -e "\n\033[1mcompiling .java -> .o: $<\033[0m"
+ mkdir -p `dirname $@`
+ $(gcj) -fCLASSPATH=build/pruned -c $< -o $@
+
+# FIXME detect subclasses of X11
+build/$(platform)/org/ibex/plat/Linux.cc.o: .install_WindowMaker-0.80.2_$(target)
+build/$(platform)/org/ibex/plat/Solaris.cc.o: .install_WindowMaker-0.80.2_$(target)
+
+build/$(platform)/org/ibex/plat/$(platform).cc.o: src/org/ibex/plat/$(platform).cc src/org/ibex/plat/*.cc .configure_jpeg-6b_$(target)
+ @make $(java_headers)
+ @echo -e "\n\033[1mcompiling .cc -> .o: $<\033[0m"
+ mkdir -p `dirname $@`
+ $(g++) -c $< -o $@
+
+nonplat_java_sources := $(filter-out build/java/org/ibex/plat/$(platform).java,$(java_sources))
+build/$(platform)/$(platform).ar: $(nonplat_java_sources:build/java/%.java=build/$(platform)/%.java.o)
+ @echo -e "\n\033[1marchiving .o -> .a\033[0m"
+ mkdir -p build/$(platform)
+ rm -f $@
+ upstream/install/$(target)/bin/ar rc $@ $^
+ upstream/install/$(target)/bin/ranlib $@
+
+upstream/jpeg-6b/build-$(target)/libjpeg.a: .install_jpeg-6b_$(target)
+
+# note: binaries appear in a different order in the dependency line vs the link line
+build/$(platform)/$(target_bin): upstream/jpeg-6b/build-$(target)/libjpeg.a build/$(platform)/builtin.o build/$(platform)/org/ibex/plat/$(platform).cc.o
+ @echo -e "\n\033[1mlinking .o -> $(target_bin)\033[0m"
+ find upstream/gcc-3.3/build-i686-pc-linux-gnu/i686-pc-linux-gnu/libjava/ \
+ -name nat\*.o -or \( -name \*.o -not -name '*[A-Z]*' \) -exec nm {} \; |\
+ grep _ZN | c++filt --format java | grep " U " | sed 's_\.class\$$_.class_' | sed 's_ * U __' | sed 's_(.*__' \
+ > .natcalls
+ make $(java_sources)
+ rm -rf build/pruned; mkdir -p build/pruned
+ javac -classpath lib/bcel-5.1.jar:build/class -d build/class src/org/ibex/util/BytecodePruner.java
+ rm -f build/$(platform)/ibex.jar
+ cd build/class; \
+ fastjar cf ../$(platform)/ibex.jar \
+ `find . -name \*.class | grep -v ibex/plat/` \
+ org/ibex/plat/$(platform)*.class \
+ org/ibex/plat/X11*.class \
+ org/ibex/plat/GCJ*.class \
+ org/ibex/plat/POSIX*.class
+ java -cp lib/bcel-5.1.jar:build/class org.ibex.util.BytecodePruner \
+ build/$(platform)/ibex.jar:upstream/install/share/java/libgcj-3.3.jar \
+ -o build/pruned \
+ `cat .natcalls`
+
+ rm -rf build/pruned/org/ibex/js/*; cp build/java/org/ibex/js/* build/pruned/org/ibex/js/
+
+ rm build/pruned/java/lang/System.class
+ cp upstream/gcc-3.3/src/libjava/java/lang/System.java build/pruned/java/lang/
+
+ rm build/pruned/gnu/gcj/runtime/FirstThread*.class
+ cp upstream/gcc-3.3/src/libjava/gnu/gcj/runtime/FirstThread.java build/pruned/gnu/gcj/runtime/
+
+ rm build/pruned/org/ibex/plat/GCJ*.class
+ cp build/java/org/ibex/plat/GCJ.java build/pruned/org/ibex/plat/
+
+ cp upstream/gcc-3.3/build-$(target)/$(target)/libjava/java/lang/Object.class build/pruned/java/lang/
+
+ find build/pruned/gnu/java/locale/ -name 'LocaleInformation_*' -not -name 'LocaleInformation_en.class' -not -name 'LocaleInformation_en_US.class' -exec rm {} \;
+
+# cd build/pruned; fastjar cvf ../../tmp.jar .
+# java -jar lib/jarg.jar -verbose -verboseufm -normlv -normsf -normsy -normin \
+# -normex -nornc -nornf -nornm -nobco tmp.jar
+# cd build/pruned; fastjar xvf ../../tmp.jar
+
+# try oneshot compile/link
+# --redefine-sym to merge errorthrowers? or do this in C++?
+# --rename-section to try to merge Utf8 sections?
+
+ cd build/pruned; \
+ for A in `find . -name \*.class -or -name \*.java`; do \
+ echo compiling $$A....; \
+ ../../upstream/install/bin/$(target)-gcj \
+ -fdata-sections -ffunction-sections -w -c -Os \
+ -fCLASSPATH=../../build/$(platform)/ibex.jar $$A && \
+ (mkdir -p ../../build/$(platform)/`dirname $$A`; mv *.o ../../build/$(platform)/`dirname $$A`); \
+ done
+ rm -f build/$(platform)/ibex.a
+ cd upstream/gcc-3.3/build-$(target)/$(target)/libjava; \
+ $(shell pwd)/upstream/install/$(target)/bin/ar cq \
+ $(shell pwd)/build/$(platform)/ibex.a \
+ boehm.o defineclass.o exception.o \
+ posix-threads.o posix.o prims.o resolve.o \
+ `find . -name nat\*.o | grep -v JIS` \
+ `find . -name \*.o -not -name '*[A-Z]*'` \
+ `find $(shell pwd)/build/$(platform) -name \*.o`
+ PATH=upstream/install/bin:$$PATH upstream/install/bin/$(target)-gcj \
+ --main=org.ibex.plat.$(platform) \
+ -Lupstream/install/$(target)/lib \
+ -Lupstream/install/lib \
+ -ffunction-sections -fdata-sections -Os -w -fCLASSPATH=build/$(platform)/ibex.jar \
+ build/$(platform)/ibex.a \
+ upstream/jpeg-6b/build-$(target)/libjpeg.a \
+ $(link_flags) \
+ upstream/gcc-3.3/build-$(target)/$(target)/boehm-gc/.libs/libgcjgc.a \
+ -lz -ldl -lffi \
+ -Wl,--noinhibit-exec,--gc-sections \
+ -o $@
+
+# linker
+# --no-whole-archive,--relax,-O,2