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