adaddf03082e863ecebc91f9e794d753d489bf24
[TD_C.git] / TP_11 / exo1 / Makefile
1 # Sample Makefile to build simple project.
2 #
3 # This Makefile expect all source files (.c) to be at the same level, in the
4 # current working directory.
5 #
6 # It will automatically generate dependencies, compile all files, and produce a
7 # binary using the provided name.
8 #
9 # Set BINARY_NAME to the name of the binary file to build.
10 # Set BUILD_TYPE to either debug or release
11 #
12 # Automatic dependencies code from:
13 # http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
14 BINARY_NAME=exo1
15 LIBOBJS=clist.o # object to put in the library
16 LIBRARY_NAME=libtpC
17 #LDLIBS=-L. -ltpC
18 BUILD_TYPE=debug
19
20 # ====================================
21 # DO NOT CHANGE STUFF BEYOND THIS LINE
22 # ====================================
23
24 all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).staticlocal
25
26 CC=gcc
27 LD=gcc
28 AR=ar
29
30 WARN_FLAGS = -Wall -Wextra
31 STD_FLAG = -std=c11
32
33 ifeq ($(BUILD_TYPE),debug)
34 BUILDDIR := .build/debug
35 DEBUG_FLAG = -g
36 STRIP_FLAG =
37 OPTI_FLAG = -O0
38 else
39 BUILDDIR := .build/release
40 DEBUG_FLAG =
41 STRIP_FLAG = -s
42 OPTIFLAG = -O3
43 endif
44
45 #FIXME: Add a way to better control the library build
46 CFLAGS := -fPIC $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG)
47 LDFLAGS := $(LDFLAGS) $(STRIP_FLAG)
48 LIBLDFLAGS := -shared $(LDFLAGS)
49
50 OBJDIR := $(BUILDDIR)/objs
51 $(shell mkdir -p $(OBJDIR))
52
53 SRCS=$(wildcard *.c)
54 OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
55 OBJSLIB=$(addprefix $(OBJDIR)/,$(LIBOBJS))
56
57 DEPDIR := $(BUILDDIR)/deps
58 $(shell mkdir -p $(DEPDIR))
59 DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
60 POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
61
62 $(LIBRARY_NAME).a: $(OBJSLIB)
63 @echo "[AR StO] $@"
64 @$(AR) rcs $@ $^
65
66 $(LIBRARY_NAME).so: $(OBJSLIB)
67 @echo "[LD ShO] $@"
68 @$(LD) $(CFLAGS) $(LIBLDFLAGS) $^ $(LDLIBS) -o $@
69
70 $(BINARY_NAME): $(OBJS)
71 @echo "[LD ] $@"
72 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
73
74 LOCALLDLIBS=-L. -ltpC
75 $(BINARY_NAME).staticlocal: $(OBJDIR)/$(BINARY_NAME).o $(LIBRARY_NAME).a
76 @echo "[LD ] $@"
77 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
78
79 $(BINARY_NAME).dynamic: $(OBJDIR)/$(BINARY_NAME).o $(LIBRARY_NAME).so
80 @echo "[LD ] $@"
81 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LOCALLDLIBS) $(LDLIBS) -o $@
82
83 $(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
84 @echo "[C ] $*"
85 @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
86 @$(POSTCOMPILE)
87
88 $(DEPDIR)/%.d: ;
89
90 .PRECIOUS: $(DEPDIR)/%.d
91
92 include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
93
94 clean:
95 @echo "[CLN]"
96 -@rm -r $(BUILDDIR)
97 -@rm $(BINARY_NAME)
98 -@rm $(BINARY_NAME).staticlocal
99 -@rm $(BINARY_NAME).dynamic
100 -@rm $(LIBRARY_NAME).so
101 -@rm $(LIBRARY_NAME).a
102
103 disassemble: $(BINARY_NAME)
104 objdump -d $< | less
105
106 symbols: $(BINARY_NAME)
107 objdump -t $< | sort | less