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