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
10 # Run 'make V=1' to turn on verbose commands, or 'make V=0' to turn them off.
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; \
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
; \
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
; \
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
65 OBJCOPY
:= $(GCCPREFIX
)objcopy
66 OBJDUMP
:= $(GCCPREFIX
)objdump
70 NCC
:= gcc
$(CC_VER
) -pipe
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
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
)
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
102 # Make sure that 'all' is the first target
105 # Eliminate default suffix rules
108 # Delete target files if there is an error (or make is interrupted)
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 \
116 KERN_CFLAGS
:= $(CFLAGS
) -DJOS_KERNEL
-gstabs
117 USER_CFLAGS
:= $(CFLAGS
) -DJOS_USER
-gstabs
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
144 QEMUGRAPHICOPTS
+= -k en-us
-vnc localhost
:$(VNC
),reverse
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)/" < $^
> $@
159 $(QEMU
) $(QEMUOPTS
) $(QEMUGRAPHICOPTS
)
161 run-nox qemu-nox
: $(IMAGES
)
163 @echo
"*** Use Ctrl-a x to exit qemu"
165 $(QEMU
) -nographic
$(QEMUOPTS
) $(QEMUNOGRAPHICOPTS
)
167 run-gdb qemu-gdb
: $(IMAGES
) .gdbinit
169 @echo
"*** Now run 'gdb'." 1>&2
171 $(QEMU
) $(QEMUOPTS
) $(QEMUGRAPHICOPTS
) -S
$(QEMUGDBOPTS
)
173 run-nox-gdb qemu-nox-gdb
: $(IMAGES
) .gdbinit
175 @echo
"*** Now run 'gdb'." 1>&2
177 $(QEMU
) -nographic
$(QEMUOPTS
) $(QEMUNOGRAPHICOPTS
) -S
$(QEMUGDBOPTS
)
180 @if which
$(CXXFILT
) >/dev
/null
; then echo
$(CXXFILT
); else echo cat
; fi
191 # For deleting the build
193 rm -rf
$(OBJDIR
) .gdbinit jos.in kern
/programs.c
196 rm -rf lab
$(LAB
).
tar.gz jos.out
201 grade
: always
$(LABSETUP
)grade-lab
$(LAB
).sh
$(LABSETUP
)grade-functions.sh
204 (echo
"'make clean' failed. HINT: Do you have another running instance of JOS?" && exit
1)
206 sh
$(LABSETUP
)grade-lab
$(LAB
).sh
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
))
218 @
$(PERL
) mergedep.pl
$@
$^
220 -include $(OBJDIR
)/.deps
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