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