tons of stuff
[nestedvm.git] / Makefile
1 .SECONDARY:
2
3
4 # What to build
5 #
6
7 # Java sources that are part of the compiler/interpreter
8 java_sources = $(wildcard src/org/ibex/nestedvm/*.java) $(wildcard src/org/ibex/nestedvm/util/*.java)
9
10 # C sources that are part of the compiler/interpreter
11 mips_sources = crt0.c support_aux.c
12 mips_asm_sources = support.s
13
14 mips2java_root = $(shell pwd)
15 build = $(mips2java_root)/build
16 tasks = upstream/tasks
17
18 #
19 # MIPS Settings (don't change these)
20 #
21 flags = -march=mips1
22 MIPS_CC = mips-unknown-elf-gcc
23 MIPS_CXX = mips-unknown-elf-g++
24
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 -g \
28         -mmemcpy \
29         -ffunction-sections -fdata-sections \
30         -falign-functions=512 \
31         -fno-rename-registers \
32         -fno-schedule-insns \
33         -fno-delayed-branch \
34         -freduce-all-givs
35
36 MIPS_CFLAGS = $(mips_optflags) $(flags) -I. -Wall -Wno-unused -Werror
37 MIPS_LD = mips-unknown-elf-gcc
38 MIPS_LDFLAGS= \
39         $(flags) -L$(build)/org/ibex/nestedvm --static \
40         -T $(mips2java_root)/src/org/ibex/nestedvm/linker.ld -Wl,--gc-sections
41 MIPS_STRIP = mips-unknown-elf-strip
42
43 # Java compiler/VM settings
44 JAVAC = javac
45 JAVA = java
46 ifeq ($(firstword $(JAVAC)),gcj)
47         JAVAC_NODEBUG_FLAGS = -g0
48 else
49         JAVAC_NODEBUG_FLAGS = -g:none
50 endif
51
52 bcel_jar = upstream/build/bcel-5.1/bcel-5.1.jar
53 classpath = build:$(bcel_jar)
54
55 GCJ = gcj
56 EXE_EXT = 
57
58 #####
59
60 java_classes = $(java_sources:src/%.java=build/%.class)
61 mips_objects = $(mips_sources:%.c=build/org/ibex/nestedvm/%.o) $(mips_asm_sources:%.s=build/org/ibex/nestedvm/%.o)
62
63 usr = $(mips2java_root)/upstream/install
64 PATH := $(usr)/bin:$(PATH)
65 export PATH
66
67 #
68 # General Build Stuff
69 #
70 all: $(java_classes) $(mips_objects)
71 ifdef NATIVE_MIPS2JAVA_COMPILER
72 all: build/mips2java$(EXE_EXT) $(mips_objects)
73 endif
74
75 $(tasks)/%:
76         $(MAKE) -C upstream tasks/$* usr="$(usr)" MIPS_LDFLAGS="$(MIPS_LDFLAGS)" MIPS_CFLAGS="$(flags) $(mips_optflags)"
77
78 upstream_clean_%:
79         $(MAKE) -C upstream clean_$* usr="$(usr)"
80
81 #
82 # Interpreter/Compiler/Runtime Java Compilation
83 #
84
85 # This works around a gcj -C bug
86 ifeq ($(firstword $(JAVAC)),gcj)
87 build/org/ibex/nestedvm/util/.Dummy.class:
88         mkdir -p `dirname $@`
89         touch $@
90 $(java_classes): build/org/ibex/nestedvm/util/.Dummy.class
91 endif
92
93 $(java_classes): $(java_sources) $(bcel_jar)
94         $(JAVAC) -classpath $(classpath) -d build $(java_sources)
95
96 $(bcel_jar): upstream/tasks/extract_bcel
97         @true
98
99 # GCJ Stuff
100 # FIXME: We're cramming more than we need into the binary here
101 build/mips2java$(EXE_EXT): $(java_sources) $(java_gen_sources)
102         @mkdir -p `dirname $@`
103         $(GCJ) -s -o $@ --main=org.ibex.nestedvm.Compiler $(java_sources) $(java_gen_sources)
104
105 #
106 # MIPS Binary compilation
107 #
108 build/%.o: src/%.c $(tasks)/full_toolchain
109         @mkdir -p `dirname $@`
110         $(MIPS_CC) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) -c -o $@ $<
111
112 build/%.o: src/%.s $(tasks)/full_toolchain
113         @mkdir -p `dirname $@`
114         $(MIPS_CC) -x assembler-with-cpp -c -o $@ $<
115
116 %.s: %.c $(tasks)/full_toolchain
117         $(MIPS_CC) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) -c -S -o $@ $<
118
119 build/%.mips: build/%.o $(mips_objects)
120         $(MIPS_LD) -o $@ $< $(MIPS_LDFLAGS) $($(notdir $*)_LDFLAGS)
121
122 build/%.mips: src/%.cc $(tasks)/full_toolchain $(mips_objects)
123         @mkdir -p `dirname $@`
124         $(MIPS_CXX) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) $(MIPS_LDFLAGS) $($(notdir $*)_LDFLAGS) -o $@ $<
125
126 build/%.mips.stripped: build/%.mips
127         cp $< $@
128         $(MIPS_STRIP) -s $@
129
130 # MIPS Compiler generated class compilation
131 ifdef DO_JAVASOURCE
132
133 build/%.java: build/%.mips build/org/ibex/nestedvm/JavaSourceCompiler.class 
134         $(JAVA) -cp $(classpath) org.ibex.nestedvm.Compiler -outformat javasource $(compiler_flags) $($(notdir $*)_COMPILERFLAGS) $(subst /,.,$*) $< > build/$*.java
135
136 build/%.class: build/%.java build/org/ibex/nestedvm/Runtime.class
137         $(JAVAC) $(JAVAC_NODEBUG_FLAGS) -classpath build -d build $<
138 else
139
140 build/%.class: build/%.mips build/org/ibex/nestedvm/ClassFileCompiler.class
141         $(JAVA) -cp $(classpath) org.ibex.nestedvm.Compiler -outformat class -outfile $@ $(compiler_flags) $($(notdir $*)_COMPILERFLAGS) $(subst /,.,$*) $<
142
143
144 endif
145
146 # General Java Class compilation
147 build/%.class: src/%.java
148         $(JAVAC) -classpath build -d build $<
149
150 clean:
151         rm -rf build/tests build/org/ibex/nestedvm *.jar build/mips2java$(EXE_EXT)
152
153 #
154 # env.sh
155 #
156 env.sh: Makefile $(tasks)/full_toolchain build/org/ibex/nestedvm/Compiler.class
157         @rm -f "$@~"
158         @echo 'PATH="$(mips2java_root)/build:$(usr)/bin:$$PATH"; export PATH' >> $@~
159         @echo 'CC=mips-unknown-elf-gcc; export CC' >> $@~
160         @echo 'CXX=mips-unknown-elf-g++; export CXX' >> $@~
161         @echo 'AS=mips-unknown-elf-as; export AS' >> $@~
162         @echo 'AR=mips-unknown-elf-ar; export AR' >> $@~
163         @echo 'LD=mips-unknown-elf-ld; export LD' >> $@~
164         @echo 'RANLIB=mips-unknown-elf-ranlib; export RANLIB' >> $@~
165         @echo 'CFLAGS="$(mips_optflags)"; export CFLAGS' >> $@~
166         @echo 'CXXFLAGS="$(mips_optflags)"; export CXXFLAGS' >> $@~
167         @echo 'LDFLAGS="$(MIPS_LDFLAGS)"; export LDFLAGS' >> $@~
168         @echo 'CLASSPATH=$(mips2java_root)/build:$(mips2java_root)/$(bcel_jar):.; export CLASSPATH' >> $@~
169         @mv "$@~" "$@"
170         @echo "$@ created successfully"
171
172 #
173 # Runtime.jar
174 #
175
176 runtime_util_classes = SeekableData SeekableByteArray SeekableFile SeekableInputStream
177 runtime_classes = Runtime Registers UsermodeConstants  $(runtime_util_classes:%=util/%)
178 unixruntime_classes = $(runtime_classes) UnixRuntime
179
180 runtime.jar: $(runtime_classes:%=build/org/ibex/nestedvm/%.class)
181         cd build && jar cf ../$@ $(runtime_classes:%=org/ibex/nestedvm/%*.class)
182
183 unixruntime.jar: $(unixruntime_classes:%=build/org/ibex/nestedvm/%.class)
184         cd build && jar cf ../$@ $(unixruntime_classes:%=org/ibex/nestedvm/%*.class)
185
186 # This is only for Brian to use... don't mess with it
187 rebuild-constants: $(tasks)/build_newlib
188         @mkdir -p `dirname $@`
189         ( \
190                 cat \
191                         src/org/ibex/nestedvm/syscalls.h \
192                         $(usr)/mips-unknown-elf/include/sys/{errno.h,unistd.h,syslimits.h}; \
193                 $(MIPS_CC) -E -dM $(usr)/mips-unknown-elf/include/sys/fcntl.h | awk '$$2 ~ /^[OF]_/ { print; }'; \
194         ) | ( \
195                 echo "// THIS FILE IS AUTOGENERATED! DO NOT EDIT!"; \
196                 echo "// run \"make rebuild-constants\" if it needs to be updated"; \
197                 echo ""; \
198                 echo "package org.ibex.nestedvm;"; \
199                 echo "public interface UsermodeConstants {"; \
200                 tr '\t' ' ' | sed -n ' \
201                         s/  */ /g; \
202                         s/ *# *define \([A-Z_][A-Za-z0-9_]*\) \([0-9][0-9x]*\)/    public static final int \1 = \2;/p'; \
203                 echo "}"; \
204         ) > src/org/ibex/nestedvm/UsermodeConstants.java
205         
206 #
207 # Tests
208 # These are simply here for convenience. They aren't required 
209 # to build or run mips2java
210 #
211
212 build/tests/Env.class: build/org/ibex/nestedvm/Runtime.class build/org/ibex/nestedvm/Interpreter.class
213
214 # Generic Hello Worldish test
215 test: build/tests/Test.class
216         $(JAVA) -cp build tests.Test "arg 1" "arg 2" "arg 3"
217 inttest: build/tests/Test.mips build/org/ibex/nestedvm/Interpreter.class
218         $(JAVA) -cp build org.ibex.nestedvm.Interpreter build/tests/Test.mips "arg 1" "arg 2" "arg 3"
219 cxxtest: build/tests/CXXTest.class
220         $(JAVA) -cp build tests.CXXTest
221
222 # CallTest
223 build/tests/CallTest.class: build/tests/Test.class
224 calltest: build/tests/CallTest.class
225         $(JAVA) -cp build tests.CallTest `date|perl -pe 's/\D+/ /g;'` `id -u`
226
227 # FDTest
228 build/tests/FDTest.class: build/tests/Test.class
229 fdtest: build/tests/FDTest.class
230         $(JAVA) -cp build tests.FDTest
231
232
233 # Simple
234 Simple_LDFLAGS = -nostdlib
235 simpletest: build/tests/Simple.class
236         $(JAVA) -cp build tests.Simple
237
238 # Paranoia
239 Paranoia_CFLAGS = "-Wno-error"
240 Paranoia_LDFLAGS = -lm
241 paranoiatest: build/tests/Paranoia.class
242         $(JAVA) -cp build tests.Paranoia
243         
244 #
245 # Freetype Stuff
246 #
247 FreeType_CFLAGS = -Iupstream/build/freetype/include
248 FreeType_LDFLAGS =  -Lupstream/build/freetype/objs -lfreetype
249
250 FreeTypeDemoHelper_CFLAGS = $(FreeType_CFLAGS)
251 FreeTypeDemoHelper_LDFLAGS = $(FreeType_LDFLAGS)
252 build/tests/FreeTypeDemoHelper.o: $(mips_objects) $(tasks)/build_freetype
253 build/tests/FreeTypeDemoHelper.mips: 
254 build/tests/FreeTypeDemo.class: build/tests/FreeTypeDemoHelper.class
255
256 FTBench_CFLAGS =  $(FreeType_CFLAGS)
257 FTBench_LDFLAGS = $(FreeType_LDFLAGS)
258 build/tests/FTBench.o: $(tasks)/build_freetype
259
260 #
261 # MSPack Stuff
262 #
263 MSPackHelper_CFLAGS = -Iupstream/build/libmspack/mspack
264 MSPackHelper_LDFLAGS = -Lupstream/build/libmspack/mspack -lmspack
265 build/tests/MSPackHelper.o: $(mips_objects) $(tasks)/build_libmspack
266 build/tests/MSPack.class: build/tests/MSPackHelper.class
267
268 MSPackBench_CFLAGS = -Iupstream/build/libmspack/mspack
269 MSPackBench_LDFLAGS = -Lupstream/build/libmspack/mspack -lmspack
270 build/tests/MSPackBench.o: $(tasks)/build_libmspack
271
272 #
273 # Echo
274 #
275 build/tests/Echo.class: build/tests/EchoHelper.class
276
277 #
278 # Libjpeg
279 #
280 DJpeg_COMPILERFLAGS = -o onepage,pagesize=8m
281 build/tests/DJpeg.mips: $(mips_objects) $(tasks)/build_libjpeg
282         @mkdir -p `dirname $@`
283         cp upstream/build/libjpeg/djpeg $@
284
285 #
286 # Busybox
287 #
288 BusyBox_COMPILERFLAGS = -o unixruntime
289 build/tests/BusyBox.mips: $(mips_objects) $(tasks)/build_busybox
290         @mkdir -p `dirname $@`
291         cp upstream/build/busybox/busybox $@
292         
293 busyboxtest: build/tests/BusyBox.class
294         $(JAVA) -cp $(classpath) tests.BusyBox ash
295
296 #
297 # Boehm GC
298 #
299 build/tests/GCTest.mips: $(mips_objects) $(tasks)/build_boehmgc
300         @mkdir -p `dirname $@`
301         cp upstream/build/boehmgc/gctest $@
302
303 boehmgctest: build/tests/Env.class build/tests/GCTest.class
304         $(JAVA) -cp build tests.Env GC_PRINT_STATS=1  tests.GCTest
305
306
307 #
308 # Speed tests
309 #
310
311 build/tests/SpeedTest.class: build/org/ibex/nestedvm/Runtime.class
312
313 tmp/thebride_1280.jpg:
314         @mkdir -p tmp
315         cd tmp && wget http://www.kill-bill.com/images/wallpaper/thebride_1280.jpg
316
317 oldspeedtest: build/tests/DJpeg.class tmp/thebride_1280.jpg
318         bash -c "time $(JAVA) -cp build tests.DJpeg -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg"
319         @echo "e90f6b915aee2fc0d2eb9fc60ace6203  tmp/thebride_1280.tga" | md5sum -c && echo "MD5 is OK"
320
321 djpegspeedtest: build/tests/SpeedTest.class build/tests/DJpeg.class tmp/thebride_1280.jpg
322         @echo "Running DJpeg test..."
323         @$(JAVA) -cp build tests.SpeedTest tests.DJpeg 8 -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg
324
325 mspackspeedtest: build/tests/SpeedTest.class build/tests/MSPackBench.class
326         @if [ -e tmp/mspack/comic32.exe ]; then \
327                 echo "Running MSPackBench test..."; \
328                 cd tmp/mspack && $(JAVA) -cp ../../build tests.SpeedTest tests.MSPackBench 20 *32.exe; \
329         else \
330                 echo "Run \"make check\" to get the MS True Type fonts for the MSPackBench test"; \
331         fi
332
333 speedtest: build/tests/SpeedTest.class build/tests/DJpeg.class build/tests/FTBench.class tmp/thebride_1280.jpg build/tests/MSPackBench.class
334         @echo "Running DJpeg test..."
335         @$(JAVA) -cp build tests.SpeedTest tests.DJpeg 10 -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg
336         @if [ -e tmp/mspack/Comic.TTF ]; then \
337                 echo "Running FTBench test..."; \
338                 $(JAVA) -cp build tests.SpeedTest tests.FTBench 10 tmp/mspack/Comic.TTF tmp/mspack/Comic.TTF.render; \
339         else \
340                 echo "Run \"make check\" to get Arial.TTF for the FTBench test"; \
341         fi
342         @if false && [ -e tmp/mspack/comic32.exe ]; then \
343                 echo "Running MSPackBench test..."; \
344                 cd tmp/mspack && $(JAVA) -cp ../../build tests.SpeedTest tests.MSPackBench 10 *32.exe; \
345         else \
346                 echo "Run \"make check\" to get the MS True Type fonts for the MSPackBench test"; \
347         fi
348
349 intspeed: build/tests/DJpeg.mips  build/org/ibex/nestedvm/Interpreter.class tmp/thebride_1280.jpg
350         time $(JAVA) -cp build org.ibex.nestedvm.Interpreter build/tests/DJpeg.mips -targa  -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg
351         @echo "e90f6b915aee2fc0d2eb9fc60ace6203  tmp/thebride_1280.tga" | md5sum -c && echo "MD5 is OK"
352
353 #
354 # Verification checks
355 #
356
357 check: $(patsubst %,build/tests/%.class, FTBench MSPackBench DJpeg GCTest) tmp/thebride_1280.jpg
358         @/bin/bash ./src/tests/check.sh running_from_make
359
360 compiletests: $(patsubst %,build/tests/%.class,FTBench MSPackBench DJpeg Test FreeTypeDemoHelper MSPackHelper EchoHelper BusyBox GCTest Fork)
361         @true
362
363
364 # IVME Paper
365 doc/nestedvm.ivme04.pdf: doc/nestedvm.ivme04.tex doc/acmconf.cls
366         cd doc; pdflatex nestedvm.ivme04.tex && ./pst2pdf && pdflatex nestedvm.ivme04.tex
367
368 pdf: doc/nestedvm.ivme04.pdf
369         open doc/nestedvm.ivme04.pdf