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