Make tests/userprog makefile modular so we can reuse it for
[pintos-anon] / src / Makefile.userprog
1 include $(SRCDIR)/Make.config
2
3 SHELL = /bin/sh
4
5 VPATH = $(SRCDIR)
6
7 DEFINES = -DUSER
8 CPPFLAGS = -nostdinc -I$(SRCDIR) -I- -I$(SRCDIR)/lib -I$(SRCDIR)/lib/user
9
10 # Linker flags.
11 LDFLAGS = -nostdlib -static -s
12 LDLIBS = $(shell $(CC) -print-libgcc-file-name)
13
14 # Uncomment the line below to round up segment sizes to full pages for
15 # testing purposes only.
16 #LDFLAGS += -Wl,-T,fullpage.x
17
18 # C library sources linked into every test program.
19 LIB_SRC  = lib/debug.c                  # Debug code.
20 LIB_SRC += lib/random.c                 # Pseudo-random numbers.
21 LIB_SRC += lib/stdio.c                  # I/O library.
22 LIB_SRC += lib/stdlib.c                 # atoi()
23 LIB_SRC += lib/string.c                 # String functions.
24 LIB_SRC += lib/user/syscall.c           # System calls.
25 LIB_SRC += lib/user/syscall-stub.S      # System call stub.
26 LIB_SRC += lib/user/console.c           # Console code.
27
28 LIB_OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(LIB_SRC)))
29 LIB_DEP = $(patsubst %.o,%.d,$(LIB_OBJ))
30 LIB = lib/user/entry.o libc.a
31
32 PROGS_SRC = $(foreach prog,$(PROGS),$($(prog)_SRC))
33 PROGS_OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$(PROGS_SRC)))
34 PROGS_DEP = $(patsubst %.o,%.d,$(PROGS_OBJ))
35
36 all: $(PROGS)
37
38 define TEMPLATE
39 $(1)_OBJ = $(patsubst %.c,%.o,$(patsubst %.S,%.o,$($(1)_SRC)))
40 $(1): $$($(1)_OBJ) $$(LIB)
41 endef
42
43 $(foreach prog,$(PROGS),$(eval $(call TEMPLATE,$(prog))))
44
45 $(PROGS): $(LIB)
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)