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