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