Always use a custom linker script, to avoid oddities in the default.
[pintos-anon] / src / Makefile.userprog
1 # -*- makefile -*-
2
3 include $(SRCDIR)/Make.config
4
5 SHELL = /bin/sh
6
7 VPATH = $(SRCDIR)
8
9 DEFINES = -DPINTOS -DUSER
10 CPPFLAGS = -nostdinc -I$(SRCDIR) -I- -I$(SRCDIR)/lib -I$(SRCDIR)/lib/user -I.
11
12 # Linker flags.
13 LDFLAGS = -nostdlib -static -Wl,-T,$(LDSCRIPT)
14 LDLIBS = $(shell $(CC) -print-libgcc-file-name)
15 LDSCRIPT = $(SRCDIR)/lib/user/normal.lds
16 # Uncomment the following line to round up section sizes
17 # to full pages (for debugging only).
18 #LDSCRIPT = $(SRCDIR)/lib/user/fullpage.lds
19
20 # C library sources linked into every test program.
21 LIB_SRC  = lib/debug.c                  # Debug code.
22 LIB_SRC += lib/random.c                 # Pseudo-random numbers.
23 LIB_SRC += lib/stdio.c                  # I/O library.
24 LIB_SRC += lib/stdlib.c                 # atoi()
25 LIB_SRC += lib/string.c                 # String functions.
26 LIB_SRC += lib/user/syscall.c           # System calls.
27 LIB_SRC += lib/user/console.c           # Console code.
28
29 LIB_OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(LIB_SRC)))
30 LIB_DEP = $(patsubst %.o,%.d,$(LIB_OBJ))
31 LIB = lib/user/entry.o libc.a
32
33 PROGS_SRC = $(foreach prog,$(PROGS),$($(subst -,_,$(prog))_SRC))
34 PROGS_OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(PROGS_SRC)))
35 PROGS_DEP = $(patsubst %.o,%.d,$(PROGS_OBJ))
36
37 all: $(PROGS)
38
39 define TEMPLATE
40 $(2)_OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$($(2)_SRC)))
41 $(1): $$($(2)_OBJ) $$(LIB) $$(LDSCRIPT)
42         $$(CC) $$(LDFLAGS) $$($(2)_OBJ) $$(LIB) $$(LDLIBS) -o $$@
43 endef
44
45 $(foreach prog,$(PROGS),$(eval $(call TEMPLATE,$(prog),$(subst -,_,$(prog)))))
46
47 libc.a: $(LIB_OBJ)
48         rm -f $@
49         ar r $@ $^
50         ranlib $@
51
52 clean::
53         rm -f $(PROGS) $(PROGS_OBJ) $(PROGS_DEP)
54         rm -f $(LIB_DEP) $(LIB_OBJ) lib/user/entry.[do] libc.a 
55
56 .PHONY: all clean
57
58 -include $(LIB_DEP) $(PROGS_DEP)