usr variable for build process
[nestedvm.git] / Makefile
1  # 
2 # What to build
3 #
4
5 # Java sources that are part of the compiler/interpreter
6 java_sources = $(wildcard src/org/xwt/mips/*.java) $(wildcard src/org/xwt/mips/util/*.java)
7
8 # Java sources that are part of the compiler/interpreter that are
9 # generated from other sources
10 java_gen_sources = $(patsubst %,build/org/xwt/mips/%.java, UsermodeConstants)
11
12 # C sources that are part of the compiler/interpreter
13 mips_sources = crt0.c support_aux.c
14 mips_asm_sources = support.s
15
16 mips2java_root = $(shell pwd)
17 build = $(mips2java_root)/build
18 tasks = upstream/tasks
19
20 #
21 # MIPS Settings (don't change these)
22 #
23 flags = -march=mips1
24 MIPS_CC = mips-unknown-elf-gcc
25 MIPS_CXX = mips-unknown-elf-g++
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_LD = mips-unknown-elf-gcc
40 MIPS_LDFLAGS= \
41         $(flags) -L$(build)/org/xwt/mips --static \
42         -T $(mips2java_root)/src/org/xwt/mips/linker.ld -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 = \
63         $(java_sources:src/%.java=build/%.class) \
64         $(java_gen_sources:%.java=%.class)
65
66 mips_objects = $(mips_sources:%.c=build/org/xwt/mips/%.o) $(mips_asm_sources:%.s=build/org/xwt/mips/%.o)
67
68 .SECONDARY:
69
70 usr = $(mips2java_root)/upstream/install
71 PATH := $(usr)/bin:$(PATH)
72 export PATH
73
74 #
75 # General Build Stuff
76 #
77 all: $(java_classes) $(mips_objects)
78 ifdef NATIVE_MIPS2JAVA_COMPILER
79 all: build/mips2java$(EXE_EXT) $(mips_objects)
80 endif
81
82 $(tasks)/%:
83         $(MAKE) -C upstream tasks/$* usr="$(usr)" MIPS_LDFLAGS="$(MIPS_LDFLAGS)" MIPS_CFLAGS="$(flags) $(mips_optflags)"
84
85 upstream_clean_%:
86         $(MAKE) -C upstream clean_$* usr="$(usr)"
87
88 errno_h = $(usr)/mips-unknown-elf/include/sys/errno.h
89 $(errno_h): $(tasks)/build_newlib
90
91 unistd_h = $(usr)/mips-unknown-elf/include/sys/unistd.h
92 $(unistd_h): $(tasks)/build_newlib
93
94 #
95 # Interpreter/Compiler/Runtime Java Compilation
96 #
97
98 # This works around a gcj -C bug
99 ifeq ($(firstword $(JAVAC)),gcj)
100 build/org/xwt/mips/util/.Dummy.class:
101         mkdir -p `dirname $@`
102         touch $@
103 $(java_classes): build/org/xwt/mips/util/.Dummy.class
104 endif
105
106 $(java_classes): $(java_sources) $(java_gen_sources) $(bcel_jar)
107         $(JAVAC) -classpath $(classpath) -d build $(java_sources) $(java_gen_sources)
108
109 build/org/xwt/mips/UsermodeConstants.java: src/org/xwt/mips/syscalls.h $(errno_h) $(unistd_h)
110         @mkdir -p `dirname $@`
111         cat $^ | ( \
112                 echo "package org.xwt.mips;"; \
113                 echo "public interface UsermodeConstants {"; \
114                 tr '\t' ' ' | sed -n ' \
115                         s/  */ /g; \
116                         s/ *# *define \([A-Z_][A-Za-z0-9_]*\) \([0-9][0-9x]*\)/    public static final int \1 = \2;/p'; \
117                 echo "}"; \
118         ) > $@
119         
120 $(bcel_jar): upstream/tasks/extract_bcel
121         @true
122
123 # FIXME: We're cramming more than we need into the binary here
124 build/mips2java$(EXE_EXT): $(java_sources) $(java_gen_sources)
125         @mkdir -p `dirname $@`
126         $(GCJ) -s -o $@ --main=org.xwt.mips.Compiler $(java_sources) $(java_gen_sources)
127
128 #
129 # MIPS Binary compilation
130 #
131 build/%.o: src/%.c $(tasks)/full_toolchain
132         @mkdir -p `dirname $@`
133         $(MIPS_CC) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) -c -o $@ $<
134
135 build/%.o: src/%.s $(tasks)/full_toolchain
136         @mkdir -p `dirname $@`
137         $(MIPS_CC) -x assembler-with-cpp -c -o $@ $<
138
139 %.s: %.c $(tasks)/full_toolchain
140         $(MIPS_CC) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) -c -S -o $@ $<
141
142 build/%.mips: build/%.o $(mips_objects)
143         $(MIPS_LD) -o $@ $< $(MIPS_LDFLAGS) $($(notdir $*)_LDFLAGS)
144
145 build/%.mips: src/%.cc $(tasks)/full_toolchain $(mips_objects)
146         @mkdir -p `dirname $@`
147         $(MIPS_CXX) $(MIPS_CFLAGS) $($(notdir $*)_CFLAGS) $(MIPS_LDFLAGS) $($(notdir $*)_LDFLAGS) -o $@ $<
148
149 build/%.mips.stripped: build/%.mips
150         cp $< $@
151         $(MIPS_STRIP) -s $@
152
153 # MIPS Compiler generated class compilation
154 ifdef DO_JAVASOURCE
155
156 build/%.java: build/%.mips build/org/xwt/mips/JavaSourceCompiler.class 
157         $(JAVA) -cp $(classpath) org.xwt.mips.Compiler -outformat javasource $(compiler_flags) $($(notdir $*)_COMPILERFLAGS) $(subst /,.,$*) $< > build/$*.java
158
159 build/%.class: build/%.java build/org/xwt/mips/Runtime.class
160         $(JAVAC) $(JAVAC_NODEBUG_FLAGS) -classpath build -d build $<
161 else
162
163 build/%.class: build/%.mips build/org/xwt/mips/ClassFileCompiler.class
164         $(JAVA) -cp $(classpath) org.xwt.mips.Compiler -outformat class -outfile $@ $(compiler_flags) $($(notdir $*)_COMPILERFLAGS) $(subst /,.,$*) $<
165
166
167 endif
168
169 # General Java Class compilation
170 build/%.class: src/%.java
171         $(JAVAC) -classpath build -d build $<
172
173 clean:
174         rm -rf build/tests build/org/xwt/mips *.jar build/mips2java$(EXE_EXT)
175
176 #
177 # env.sh
178 #
179 env.sh: Makefile $(tasks)/full_toolchain build/org/xwt/mips/Compiler.class
180         @rm -f "$@~"
181         @echo 'PATH="$(mips2java_root)/build:$(usr)/bin:$$PATH"; export PATH' >> $@~
182         @echo 'CC=mips-unknown-elf-gcc; export CC' >> $@~
183         @echo 'CXX=mips-unknown-elf-g++; export CXX' >> $@~
184         @echo 'AS=mips-unknown-elf-as; export AS' >> $@~
185         @echo 'LD=mips-unknown-elf-ar; 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_util_classes = SeekableData SeekableByteArray SeekableFile SeekableInputStream
199 runtime_classes = Runtime Registers UsermodeConstants  $(runtime_util_classes:%=util/%)
200 unixruntime_classes = $(runtime_classes) UnixRuntime
201
202 runtime.jar: $(runtime_classes:%=build/org/xwt/mips/%.class)
203         cd build && jar cf ../$@ $(runtime_classes:%=org/xwt/mips/%*.class)
204
205 unixruntime.jar: $(unixruntime_classes:%=build/org/xwt/mips/%.class)
206         cd build && jar cf ../$@ $(unixruntime_classes:%=org/xwt/mips/%*.class)
207
208
209
210 #
211 # Tests
212 # These are simply here for convenience. They aren't required 
213 # to build or run mips2java
214 #
215
216 build/tests/Env.class: build/org/xwt/mips/Runtime.class build/org/xwt/mips/Interpreter.class
217
218 # Generic Hello Worldish test
219 test: build/tests/Test.class
220         $(JAVA) -cp build tests.Test "arg 1" "arg 2" "arg 3"
221 inttest: build/tests/Test.mips build/org/xwt/mips/Interpreter.class
222         $(JAVA) -cp build org.xwt.mips.Interpreter build/tests/Test.mips "arg 1" "arg 2" "arg 3"
223 cxxtest: build/tests/CXXTest.class
224         $(JAVA) -cp build tests.CXXTest
225
226 # CallTest
227 build/tests/CallTest.class: build/tests/Test.class
228 calltest: build/tests/CallTest.class
229         $(JAVA) -cp build tests.CallTest `date|perl -pe 's/\D+/ /g;'` `id -u`
230
231 # FDTest
232 build/tests/FDTest.class: build/tests/Test.class
233 fdtest: build/tests/FDTest.class
234         $(JAVA) -cp build tests.FDTest
235
236
237 # Simple
238 Simple_LDFLAGS = -nostdlib
239 simpletest: build/tests/Simple.class
240         $(JAVA) -cp build tests.Simple
241
242 # Paranoia
243 Paranoia_CFLAGS = "-Wno-error"
244 Paranoia_LDFLAGS = -lm
245 paranoiatest: build/tests/Paranoia.class
246         $(JAVA) -cp build tests.Paranoia
247         
248 #
249 # Freetype Stuff
250 #
251 FreeType_CFLAGS = -Iupstream/build/freetype/include
252 FreeType_LDFLAGS =  -Lupstream/build/freetype/objs -lfreetype
253
254 FreeTypeDemoHelper_CFLAGS = $(FreeType_CFLAGS)
255 FreeTypeDemoHelper_LDFLAGS = $(FreeType_LDFLAGS)
256 build/tests/FreeTypeDemoHelper.o: $(mips_objects) $(tasks)/build_freetype
257 build/tests/FreeTypeDemoHelper.mips: 
258 build/tests/FreeTypeDemo.class: build/tests/FreeTypeDemoHelper.class
259
260 FTBench_CFLAGS =  $(FreeType_CFLAGS)
261 FTBench_LDFLAGS = $(FreeType_LDFLAGS)
262 build/tests/FTBench.o: $(tasks)/build_freetype
263
264 #
265 # MSPack Stuff
266 #
267 MSPackHelper_CFLAGS = -Iupstream/build/libmspack/mspack
268 MSPackHelper_LDFLAGS = -Lupstream/build/libmspack/mspack -lmspack
269 build/tests/MSPackHelper.o: $(mips_objects) $(tasks)/build_libmspack
270 build/tests/MSPack.class: build/tests/MSPackHelper.class
271
272 MSPackBench_CFLAGS = -Iupstream/build/libmspack/mspack
273 MSPackBench_LDFLAGS = -Lupstream/build/libmspack/mspack -lmspack
274 build/tests/MSPackBench.o: $(tasks)/build_libmspack
275
276 #
277 # Echo
278 #
279 build/tests/Echo.class: build/tests/EchoHelper.class
280
281 #
282 # Libjpeg
283 #
284 DJpeg_COMPILERFLAGS = -o onepage,pagesize=8m
285 build/tests/DJpeg.mips: $(mips_objects) $(tasks)/build_libjpeg
286         @mkdir -p `dirname $@`
287         cp upstream/build/libjpeg/djpeg $@
288
289 #
290 # Busybox
291 #
292 BusyBox_COMPILERFLAGS = -o unixruntime
293 build/tests/BusyBox.mips: $(mips_object) $(tasks)/build_busybox
294         @mkdir -p `dirname $@`
295         cp upstream/build/busybox/busybox $@
296         
297 busyboxtest: build/tests/BusyBox.class
298         $(JAVA) -cp build tests.BusyBox ash
299
300 #
301 # Boehm GC
302 #
303 build/tests/GCTest.mips: $(mips_objects) $(tasks)/build_boehmgc
304         @mkdir -p `dirname $@`
305         cp upstream/build/boehmgc/gctest $@
306
307 boehmgctest: build/tests/Env.class build/tests/GCTest.class
308         $(JAVA) -cp build tests.Env GC_PRINT_STATS=1  tests.GCTest
309
310
311 #
312 # Speed tests
313 #
314
315 build/tests/SpeedTest.class: build/org/xwt/mips/Runtime.class
316
317 tmp/thebride_1280.jpg:
318         @mkdir -p tmp
319         cd tmp && wget http://www.kill-bill.com/images/wallpaper/thebride_1280.jpg
320
321 oldspeedtest: build/tests/DJpeg.class tmp/thebride_1280.jpg
322         bash -c "time $(JAVA) -cp build tests.DJpeg -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg"
323         @echo "e90f6b915aee2fc0d2eb9fc60ace6203  tmp/thebride_1280.tga" | md5sum -c && echo "MD5 is OK"
324
325 djpegspeedtest: build/tests/SpeedTest.class build/tests/DJpeg.class tmp/thebride_1280.jpg
326         @echo "Running DJpeg test..."
327         @$(JAVA) -cp build tests.SpeedTest tests.DJpeg 8 -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg
328
329 mspackspeedtest: build/tests/SpeedTest.class build/tests/MSPackBench.class
330         @if [ -e tmp/mspack/comic32.exe ]; then \
331                 echo "Running MSPackBench test..."; \
332                 cd tmp/mspack && $(JAVA) -cp ../../build tests.SpeedTest tests.MSPackBench 20 *32.exe; \
333         else \
334                 echo "Run \"make check\" to get the MS True Type fonts for the MSPackBench test"; \
335         fi
336
337 speedtest: build/tests/SpeedTest.class build/tests/DJpeg.class build/tests/FTBench.class tmp/thebride_1280.jpg build/tests/MSPackBench.class
338         @echo "Running DJpeg test..."
339         @$(JAVA) -cp build tests.SpeedTest tests.DJpeg 10 -targa -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg
340         @if [ -e tmp/mspack/Comic.TTF ]; then \
341                 echo "Running FTBench test..."; \
342                 $(JAVA) -cp build tests.SpeedTest tests.FTBench 10 tmp/mspack/Comic.TTF tmp/mspack/Comic.TTF.render; \
343         else \
344                 echo "Run \"make check\" to get Arial.TTF for the FTBench test"; \
345         fi
346         @if false && [ -e tmp/mspack/comic32.exe ]; then \
347                 echo "Running MSPackBench test..."; \
348                 cd tmp/mspack && $(JAVA) -cp ../../build tests.SpeedTest tests.MSPackBench 10 *32.exe; \
349         else \
350                 echo "Run \"make check\" to get the MS True Type fonts for the MSPackBench test"; \
351         fi
352
353 intspeed: build/tests/DJpeg.mips  build/org/xwt/mips/Interpreter.class tmp/thebride_1280.jpg
354         time $(JAVA) -cp build org.xwt.mips.Interpreter build/tests/DJpeg.mips -targa  -outfile tmp/thebride_1280.tga tmp/thebride_1280.jpg
355         @echo "e90f6b915aee2fc0d2eb9fc60ace6203  tmp/thebride_1280.tga" | md5sum -c && echo "MD5 is OK"
356
357 #
358 # Verification checks
359 #
360
361 check: $(patsubst %,build/tests/%.class, FTBench MSPackBench DJpeg GCTest) tmp/thebride_1280.jpg
362         @/bin/bash ./src/tests/check.sh running_from_make
363
364 compiletests: $(patsubst %,build/tests/%.class,FTBench MSPackBench DJpeg Test FreeTypeDemoHelper MSPackHelper EchoHelper BusyBox GCTest Fork)
365         @true
366
367
368 # IVME Paper
369 doc/nestedvm.ivme04.pdf: doc/nestedvm.ivme04.tex doc/acmconf.cls
370         cd doc; pdflatex nestedvm.ivme04.tex && ./pst2pdf && pdflatex nestedvm.ivme04.tex
371
372 pdf: doc/nestedvm.ivme04.pdf
373         open doc/nestedvm.ivme04.pdf