initial checkin
[org.ibex.nanogoat.git] / upstream / mips / Makefile
1
2 # What to build
3 #
4
5 # Java sources that are part of the compiler/interpreter
6 java_sources = $(wildcard org/xwt/mips/*.java) $(wildcard org/xwt/mips/util/*.java)
7
8 # Java sources that are part of the compiler/interpreter that are
9 # generated from other sources
10 java_gen_sources = $(patsubst %,build/org/xwt/mips/%.java, Errno Syscalls Unistd)
11
12 # C sources that are part of the compiler/interpreter
13 mips_sources = crt0.c support_aux.c
14 mips_asm_sources = support.s
15
16 mips2java_root = $(shell pwd)
17 build = $(mips2java_root)/build
18 tasks = upstream/tasks
19
20 #
21 # MIPS Settings (don't change these)
22 #
23 flags = -march=mips1
24 MIPS_CC = mips-unknown-elf-gcc
25 # Be VERY careful about changing any of these as they can break binary 
26 # compatibility and create hard to find bugs
27 mips_optflags = -O3 \
28         -fno-rename-registers -freduce-all-givs \
29         -fno-peephole -fno-peephole2 -fmove-all-movables -fno-schedule-insns \
30         -fno-delayed-branch \
31         -mmemcpy \
32         -ffunction-sections -fdata-sections
33
34 MIPS_CFLAGS = $(mips_optflags) $(flags) -I. -Wall -Wno-unused -Werror
35 MIPS_LD = mips-unknown-elf-gcc
36 MIPS_LDFLAGS= $(flags) -L$(build)/org/xwt/mips --static -T $(mips2java_root)/org/xwt/mips/linker.ld -Wl,--gc-sections
37 MIPS_STRIP = mips-unknown-elf-strip
38
39 # Java compiler/VM settings
40 JAVAC = javac
41 JAVA = java
42 ifeq ($(firstword $(JAVAC)),gcj)
43         JAVAC_NODEBUG_FLAGS = -g0
44 else
45         JAVAC_NODEBUG_FLAGS = -g:none
46 endif
47
48 GCJ = gcj
49 EXE_EXT = 
50
51 #####
52
53 java_classes = \
54         $(java_sources:%.java=build/%.class) \
55         $(java_gen_sources:%.java=%.class)
56
57 mips_objects = $(mips_sources:%.c=build/org/xwt/mips/%.o) $(mips_asm_sources:%.s=build/org/xwt/mips/%.o)
58
59 .SECONDARY:
60
61 PATH := $(mips2java_root)/upstream/install/bin:$(PATH)
62 export PATH
63
64 #
65 # General Build Stuff
66 #
67 all: $(java_classes) $(mips_objects)
68 ifdef NATIVE_MIPS2JAVA_COMPILER
69 all: build/mips2java$(EXE_EXT)
70 endif
71
72 $(tasks)/%:
73         $(MAKE) -C upstream tasks/$* MIPS_LDFLAGS="$(MIPS_LDFLAGS)" MIPS_CFLAGS="$(flags) $(mips_optflags)"
74
75 upstream_clean_%:
76         $(MAKE) -C upstream clean_$*
77
78 errno_h = upstream/install/mips-unknown-elf/include/sys/errno.h
79 $(errno_h): $(tasks)/build_newlib
80
81 unistd_h = upstream/install/mips-unknown-elf/include/sys/unistd.h
82 $(unistd_h): $(tasks)/build_newlib
83
84 #
85 # Interpreter/Compiler/Runtime Java Compilation
86 #
87
88 # This works around a gcj -C bug
89 ifeq ($(firstword $(JAVAC)),gcj)
90 build/org/xwt/mips/util/.Dummy.class:
91         mkdir -p `dirname $@`
92         touch $@
93 $(java_classes): build/org/xwt/mips/util/.Dummy.class
94 endif
95
96 $(java_classes): $(java_sources) $(java_gen_sources)
97         $(JAVAC) -classpath build -d build $(java_sources) $(java_gen_sources)
98
99 build/org/xwt/mips/Syscalls.java: org/xwt/mips/syscalls.h
100         @mkdir -p `dirname $@`
101         ./scripts/htojava.sh org.xwt.mips.Syscalls < $<  > $@
102
103 build/org/xwt/mips/Errno.java: $(errno_h)
104         @mkdir -p `dirname $@`
105         ./scripts/htojava.sh org.xwt.mips.Errno < $<  > $@
106
107 build/org/xwt/mips/Unistd.java: $(unistd_h)
108         @mkdir -p `dirname $@`
109         ./scripts/htojava.sh org.xwt.mips.Unistd < $<  > $@
110
111 build/mips2java$(EXE_EXT): $(java_sources) $(java_gen_sources)
112         @mkdir -p `dirname $@`
113         $(GCJ) -s -o $@ --main=org.xwt.mips.Compiler $(java_sources) $(java_gen_sources)
114
115 #
116 # MIPS Binary compilation
117 #
118 build/%.o: %.c $(tasks)/full_toolchain
119         @mkdir -p `dirname $@`
120         $(MIPS_CC) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) -c -o $@ $<
121
122 build/%.o: %.s $(tasks)/full_toolchain
123         @mkdir -p `dirname $@`
124         $(MIPS_CC) -x assembler-with-cpp -c -o $@ $<
125
126 %.s: %.c $(tasks)/full_toolchain
127         $(MIPS_CC) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) -c -S -o $@ $<
128
129 build/%.mips: build/%.o $(mips_objects)
130         $(MIPS_LD) -o $@ $< $(MIPS_LDFLAGS) $($(notdir $*)_LDFLAGS)
131
132 build/%.mips.stripped: build/%.mips
133         cp $< $@
134         $(MIPS_STRIP) -s $@
135
136 # MIPS Compiler generated class compilation
137 ifdef NATIVE_MIPS2JAVA_COMPILER
138 build/%.java: build/%.mips build/mips2java$(EXE_EXT)
139         build/mips2java$(EXE_EXT) $(compiler_flags) $($(notdir $*)_COMPILERFLAGS) $(subst /,.,$*) $< > build/$*.java
140 else
141 build/%.java: build/%.mips build/org/xwt/mips/Compiler.class
142         $(JAVA) -cp build org.xwt.mips.Compiler $(compiler_flags) $($(notdir $*)_COMPILERFLAGS) $(subst /,.,$*) $< > build/$*.java
143 endif
144
145 build/%.class: build/%.java build/org/xwt/mips/Runtime.class
146         $(JAVAC) $(JAVAC_NODEBUG_FLAGS) -classpath build -d build $<
147
148 # General Java Class compilation
149 build/%.class: %.java
150         $(JAVAC) -classpath build -d build $<
151
152 clean:
153         rm -rf build/tests build/org/xwt/mips *.jar build/mips2java$(EXE_EXT)
154
155 #
156 # env.sh
157 #
158 env.sh: Makefile $(tasks)/full_toolchain build/org/xwt/mips/Compiler.class
159         @rm -f "$@~"
160         @echo 'PATH="$(mips2java_root)/build:$(mips2java_root)/upstream/install/bin:$$PATH"; export PATH' >> $@~
161         @echo 'CC=mips-unknown-elf-gcc; export CC' >> $@~
162         @echo 'CXX=mips-unknown-elf-g++; export CXX' >> $@~
163         @echo 'AS=mips-unknown-elf-as; export AS' >> $@~
164         @echo 'LD=mips-unknown-elf-ar; export LD' >> $@~
165         @echo 'RANLIB=mips-unknown-elf-ranlib; export RANLIB' >> $@~
166         @echo 'CFLAGS="$(mips_optflags)"; export CFLAGS' >> $@~
167         @echo 'CXXFLAGS="$(mips_optflags)"; export CXXFLAGS' >> $@~
168         @echo 'LDFLAGS="$(MIPS_LDFLAGS)"; export LDFLAGS' >> $@~
169         @echo 'CLASSPATH=$(mips2java_root)/build:.; export CLASSPATH' >> $@~
170         @mv "$@~" "$@"
171         @echo "$@ created successfully"
172
173 #
174 # Runtime.jar
175 #
176
177 runtime_util_classes = SeekableData SeekableByteArray SeekableFile SeekableInputStream
178 runtime_classes = Runtime Registers Syscalls Errno Unistd $(runtime_util_classes:%=util/%)
179 unixruntime_classes = $(runtime_classes) UnixRuntime
180
181 runtime.jar: $(runtime_classes:%=build/org/xwt/mips/%.class)
182         cd build && jar cf ../$@ $(runtime_classes:%=org/xwt/mips/%*.class)
183
184 unixruntime.jar: $(unixruntime_classes:%=build/org/xwt/mips/%.class)
185         cd build && jar cf ../$@ $(unixruntime_classes:%=org/xwt/mips/%*.class)
186
187
188
189 #
190 # Tests
191 # These are simply here for convenience. They aren't required 
192 # to build or run mips2java
193 #
194
195 build/tests/Env.class: build/org/xwt/mips/Runtime.class build/org/xwt/mips/Interpreter.class
196
197 # Generic Hello Worldish test
198 test: build/tests/Test.class
199         $(JAVA) -cp build tests.Test "arg 1" "arg 2" "arg 3"
200 inttest: build/tests/Test.mips build/org/xwt/mips/Interpreter.class
201         $(JAVA) -cp build org.xwt.mips.Interpreter build/tests/Test.mips "arg 1" "arg 2" "arg 3"
202
203
204 # CallTest
205 build/tests/CallTest.class: build/tests/Test.class
206 calltest: build/tests/CallTest.class
207         $(JAVA) -cp build tests.CallTest `date|perl -pe 's/\D+/ /g;'` `id -u`
208
209 # FDTest
210 build/tests/FDTest.class: build/tests/Test.class
211 fdtest: build/tests/FDTest.class
212         $(JAVA) -cp build tests.FDTest
213
214
215 #
216 # Freetype Stuff
217 #
218 FreeType_CFLAGS = -Iupstream/build/freetype/include
219 FreeType_LDFLAGS =  -Lupstream/build/freetype/objs -lfreetype
220
221 FreeTypeDemoHelper_CFLAGS = $(FreeType_CFLAGS)
222 FreeTypeDemoHelper_LDFLAGS = $(FreeType_LDFLAGS)
223 build/tests/FreeTypeDemoHelper.o: $(mips_objects) $(tasks)/build_freetype
224 build/tests/FreeTypeDemoHelper.mips: 
225 build/tests/FreeTypeDemo.class: build/tests/FreeTypeDemoHelper.class
226
227 FTBench_CFLAGS =  $(FreeType_CFLAGS)
228 FTBench_LDFLAGS = $(FreeType_LDFLAGS)
229 build/tests/FTBench.o: $(tasks)/build_freetype
230
231 #
232 # MSPack Stuff
233 #
234 MSPackHelper_CFLAGS = -Iupstream/build/libmspack/mspack
235 MSPackHelper_LDFLAGS = -Lupstream/build/libmspack/mspack -lmspack
236 build/tests/MSPackHelper.o: $(mips_objects) $(tasks)/build_libmspack
237 build/tests/MSPack.class: build/tests/MSPackHelper.class
238
239 MSPackBench_CFLAGS = -Iupstream/build/libmspack/mspack
240 MSPackBench_LDFLAGS = -Lupstream/build/libmspack/mspack -lmspack
241 build/tests/MSPackBench.o: $(tasks)/build_libmspack
242
243 #
244 # Echo
245 #
246 build/tests/Echo.class: build/tests/EchoHelper.class
247
248 #
249 # Libjpeg
250 #
251 DJpeg_COMPILERFLAGS = -o onepage,pagesize=8m
252 build/tests/DJpeg.mips: $(mips_objects) $(tasks)/build_libjpeg
253         @mkdir -p `dirname $@`
254         cp upstream/build/libjpeg/djpeg $@
255
256 #
257 # Busybox
258 #
259 BusyBox_COMPILERFLAGS = -o unixruntime
260 build/tests/BusyBox.mips: $(mips_object) $(tasks)/build_busybox
261         @mkdir -p `dirname $@`
262         cp upstream/build/busybox/busybox $@
263         
264 busyboxtest: build/tests/BusyBox.class
265         $(JAVA) -cp build tests.BusyBox ash
266
267 #
268 # Boehm GC
269 #
270 build/tests/GCTest.mips: $(mips_objects) $(tasks)/build_boehmgc
271         @mkdir -p `dirname $@`
272         cp upstream/build/boehmgc/gctest $@
273
274 boehmgctest: build/tests/Env.class build/tests/GCTest.class
275         $(JAVA) -cp build tests.Env GC_PRINT_STATS=1  tests.GCTest
276
277
278 #
279 # Speed tests
280 #
281
282 build/tests/SpeedTest.class: build/org/xwt/mips/Runtime.class
283
284 tmp/thebride_1280.jpg:
285         @mkdir -p tmp
286         cd tmp && wget http://www.kill-bill.com/images/wallpaper/thebride_1280.jpg
287
288 oldspeedtest: build/tests/DJpeg.class tmp/thebride_1280.jpg
289         bash -c "time $(JAVA) -cp build tests.DJpeg -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg"
290         @echo "e90f6b915aee2fc0d2eb9fc60ace6203  tmp/thebride_1280.tga" | md5sum -c && echo "MD5 is OK"
291
292 speedtest: build/tests/SpeedTest.class build/tests/DJpeg.class tmp/thebride_1280.jpg
293         $(JAVA) -cp build tests.SpeedTest tests.DJpeg 4 -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg
294
295 intspeed: build/tests/DJpeg.mips  build/org/xwt/mips/Interpreter.class tmp/thebride_1280.jpg
296         time $(JAVA) -cp build org.xwt.mips.Interpreter build/tests/DJpeg.mips -targa < tmp/thebride_1280.jpg > tmp/thebride_1280.tga
297         @echo "e90f6b915aee2fc0d2eb9fc60ace6203  tmp/thebride_1280.tga" | md5sum -c && echo "MD5 is OK"
298
299 #
300 # Verification checks
301 #
302
303 check: $(patsubst %,build/tests/%.class, FTBench MSPackBench DJpeg GCTest BusyBox) tmp/thebride_1280.jpg
304         @./tests/check.sh running_from_make
305
306 compiletests: $(patsubst %,build/tests/%.class,FTBench MSPackBench DJpeg Test FreeTypeDemoHelper MSPackHelper EchoHelper BusyBox GCTest Fork)
307         @true
308