* more re-work
[mascara-docs.git] / i386 / junos / ucla / src / lab2 / GNUmakefile
blob2cd2670e09b35ababc4aba656054b9ec96d34aba
2 # This makefile system follows the structuring conventions
3 # recommended by Peter Miller in his excellent paper:
5 # Recursive Make Considered Harmful
6 # http://aegis.sourceforge.net/auug97.pdf
8 OBJDIR := obj
10 # Run 'make V=1' to turn on verbose commands, or 'make V=0' to turn them off.
11 ifeq ($(V),1)
12 override V =
13 endif
14 ifeq ($(V),0)
15 override V = @
16 endif
18 -include conf/lab.mk
20 -include conf/env.mk
22 LABSETUP ?= ./
24 TOP = .
26 # Cross-compiler jos toolchain
28 # This Makefile will automatically use the cross-compiler toolchain
29 # installed as 'i386-jos-elf-*', if one exists. If the host tools ('gcc',
30 # 'objdump', and so forth) compile for a 32-bit x86 ELF target, that will
31 # be detected as well. If you have the right compiler toolchain installed
32 # using a different name, set GCCPREFIX explicitly in conf/env.mk
34 # try to infer the correct GCCPREFIX
35 GCCPREFIX ?= $(shell if i386-jos-elf-objdump -i 2>&1 | grep '^elf32-i386$$' >/dev/null 2>&1; \
36 then echo 'i386-jos-elf-'; \
37 elif objdump -i 2>&1 | grep 'elf32-i386' >/dev/null 2>&1; \
38 then echo ''; \
39 else echo "***" 1>&2; \
40 echo "*** Error: Couldn't find an i386-*-elf version of GCC/binutils." 1>&2; \
41 echo "*** Is the directory with i386-jos-elf-gcc in your PATH?" 1>&2; \
42 echo "*** If your i386-*-elf toolchain is installed with a command" 1>&2; \
43 echo "*** prefix other than 'i386-jos-elf-', set your GCCPREFIX" 1>&2; \
44 echo "*** environment variable to that prefix and run 'make' again." 1>&2; \
45 echo "*** To turn off this error, run 'gmake GCCPREFIX= ...'." 1>&2; \
46 echo "***" 1>&2; exit 1; fi)
48 # try to infer the correct QEMU
49 QEMU ?= $(shell if which qemu > /dev/null; \
50 then echo qemu; exit; \
51 else \
52 qemu=/Applications/Q.app/Contents/MacOS/i386-softmmu.app/Contents/MacOS/i386-softmmu; \
53 if test -x $$qemu; then echo $$qemu; exit; fi; fi; \
54 echo "***" 1>&2; \
55 echo "*** Error: Couldn't find a working QEMU executable." 1>&2; \
56 echo "*** Is the directory containing the qemu binary in your PATH" 1>&2; \
57 echo "*** or have you tried setting the QEMU variable in conf/env.mk?" 1>&2; \
58 echo "***" 1>&2; exit 1)
61 CC := $(GCCPREFIX)gcc -pipe
62 AS := $(GCCPREFIX)as
63 AR := $(GCCPREFIX)ar
64 LD := $(GCCPREFIX)ld
65 OBJCOPY := $(GCCPREFIX)objcopy
66 OBJDUMP := $(GCCPREFIX)objdump
67 NM := $(GCCPREFIX)nm
69 # Native commands
70 NCC := gcc $(CC_VER) -pipe
71 TAR := gtar
72 PERL := perl
74 # Compiler flags
75 # -fno-builtin is required to avoid refs to undefined functions in the kernel.
76 # Only optimize to -O1 to discourage inlining, which complicates backtraces.
77 CFLAGS := $(CFLAGS) $(DEFS) $(LABDEFS) -O1 -fno-builtin -I$(TOP) -MD
78 CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32
80 # Host compiler flags
81 # Programs that run on the build host, rather than the VM, may require a
82 # different compiler, and thus different compiler flags.
83 HOST_CFLAGS := $(CFLAGS)
85 # Add -fno-stack-protector if the option exists.
86 CFLAGS += $(shell $(CC) -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
87 HOST_CFLAGS += $(shell gcc -fno-stack-protector -E -x c /dev/null >/dev/null 2>&1 && echo -fno-stack-protector)
90 # Common linker flags
91 LDFLAGS := -m elf_i386
93 # Linker flags for JOS user programs
94 ULDFLAGS := -T user/user.ld
96 # libgcc defines some necessary functions
97 GCC_LIB := $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
99 # Lists that the */Makefrag makefile fragments will add to
100 OBJDIRS :=
102 # Make sure that 'all' is the first target
103 all:
105 # Eliminate default suffix rules
106 .SUFFIXES:
108 # Delete target files if there is an error (or make is interrupted)
109 .DELETE_ON_ERROR:
111 # make it so that no intermediate .o files are ever deleted
112 .PRECIOUS: %.o $(OBJDIR)/boot/%.o $(OBJDIR)/kern/%.o \
113 $(OBJDIR)/lib/%.o $(OBJDIR)/fs/%.o $(OBJDIR)/net/%.o \
114 $(OBJDIR)/user/%.o
116 KERN_CFLAGS := $(CFLAGS) -DJOS_KERNEL -gstabs
117 USER_CFLAGS := $(CFLAGS) -DJOS_USER -gstabs
120 # C++ compiler flags
121 CXX := $(GCCPREFIX)c++ -pipe
122 KERN_CXXFLAGS := $(KERN_CFLAGS) -fno-exceptions -fno-rtti -Wno-non-virtual-dtor
123 USER_CXXFLAGS := $(USER_CFLAGS) -fno-exceptions -fno-rtti -Wno-non-virtual-dtor
125 CXXFILT := $(GCCPREFIX)c++filt
129 # Include Makefrags for subdirectories
130 include boot/Makefrag
131 include kern/Makefrag
134 # define some "unique" ports for QEMU to listen on; we need up to 4
135 BASEPORT ?= $(shell expr "(" `id -u` % 1750 ")" "*" 4 + 25000)
137 # define QEMU options
138 QEMUOPTS := -hda $(OBJDIR)/kernel.img
139 IMAGES := $(OBJDIR)/kernel.img
140 QEMUGRAPHICOPTS := -serial stdio
141 QEMUNOGRAPHICOPTS := -serial mon:stdio
143 ifdef VNC
144 QEMUGRAPHICOPTS += -k en-us -vnc localhost:$(VNC),reverse
145 endif
147 ifndef GDB
148 GDB ?= $(BASEPORT)
149 else ifeq ($(GDB),1)
150 GDB ?= $(BASEPORT)
151 endif
152 QEMUGDBOPTS = $(shell if $(QEMU) -nographic -help | grep -q '^-gdb'; \
153 then echo "-gdb tcp::$(GDB)"; else echo "-s -p $(GDB)"; fi)
155 .gdbinit: .gdbinit.tmpl
156 sed "s/localhost:1234/localhost:$(GDB)/" < $^ > $@
158 run qemu: $(IMAGES)
159 $(QEMU) $(QEMUOPTS) $(QEMUGRAPHICOPTS)
161 run-nox qemu-nox: $(IMAGES)
162 @echo "***"
163 @echo "*** Use Ctrl-a x to exit qemu"
164 @echo "***"
165 $(QEMU) -nographic $(QEMUOPTS) $(QEMUNOGRAPHICOPTS)
167 run-gdb qemu-gdb: $(IMAGES) .gdbinit
168 @echo "***"
169 @echo "*** Now run 'gdb'." 1>&2
170 @echo "***"
171 $(QEMU) $(QEMUOPTS) $(QEMUGRAPHICOPTS) -S $(QEMUGDBOPTS)
173 run-nox-gdb qemu-nox-gdb: $(IMAGES) .gdbinit
174 @echo "***"
175 @echo "*** Now run 'gdb'." 1>&2
176 @echo "***"
177 $(QEMU) -nographic $(QEMUOPTS) $(QEMUNOGRAPHICOPTS) -S $(QEMUGDBOPTS)
179 print-c++filt:
180 @if which $(CXXFILT) >/dev/null; then echo $(CXXFILT); else echo cat; fi
182 print-qemu:
183 @echo $(QEMU)
185 print-gdbport:
186 @echo $(GDB)
188 print-qemugdb:
189 @echo $(QEMUGDBOPTS)
191 # For deleting the build
192 clean:
193 rm -rf $(OBJDIR) .gdbinit jos.in kern/programs.c
195 realclean: clean
196 rm -rf lab$(LAB).tar.gz jos.out
198 distclean: realclean
199 rm -rf conf/gcc.mk
201 grade: always $(LABSETUP)grade-lab$(LAB).sh $(LABSETUP)grade-functions.sh
202 @echo $(MAKE) clean
203 @$(MAKE) clean || \
204 (echo "'make clean' failed. HINT: Do you have another running instance of JOS?" && exit 1)
205 $(MAKE) all
206 sh $(LABSETUP)grade-lab$(LAB).sh
208 tarball: realclean
209 tar cf - `find . -type f | grep -v '^\.*$$' | grep -v '/CVS/' | grep -v '/\.svn/' | grep -v '/\.git/' | grep -v 'lab[0-9].*\.tar\.gz'` | gzip > lab$(LAB)-handin.tar.gz
212 # This magic automatically generates makefile dependencies
213 # for header files included from C source files we compile,
214 # and keeps those dependencies up-to-date every time we recompile.
215 # See 'mergedep.pl' for more information.
216 $(OBJDIR)/.deps: $(foreach dir, $(OBJDIRS), $(wildcard $(OBJDIR)/$(dir)/*.d))
217 @mkdir -p $(@D)
218 @$(PERL) mergedep.pl $@ $^
220 -include $(OBJDIR)/.deps
222 always:
225 .PHONY: all always \
226 handin tarball clean realclean distclean grade \
227 qemu qemu-nox qemu-gdb qemu-nox-gdb \
228 run run-nox run-gdb run-nox-gdb