TP11: Rename the static binary target to express that it's not
[TD_C.git] / TP_11 / exo2 / Makefile
CommitLineData
a5a96953
JB
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
14BINARY_NAME=exo2
15LIBOBJS=clist.o ui.o# object to put in the library
16LIBRARY_NAME=libtpC
17#LDLIBS=-L. -ltpC
18BUILD_TYPE=debug
19
20# ====================================
21# DO NOT CHANGE STUFF BEYOND THIS LINE
22# ====================================
23
f1294ffe 24all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).staticlocal
a5a96953
JB
25
26CC=gcc
27LD=gcc
28AR=ar
29
30WARN_FLAGS = -Wall -Wextra
31STD_FLAG = -std=c11
32
33ifeq ($(BUILD_TYPE),debug)
34BUILDDIR := .build/debug
35DEBUG_FLAG = -g
36STRIP_FLAG =
37OPTI_FLAG = -O0
38else
39BUILDDIR := .build/release
40DEBUG_FLAG =
41STRIP_FLAG = -s
42OPTIFLAG = -O3
43endif
44
45#FIXME: Add a way to better control the library build
46CFLAGS := -fPIC $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG)
47LDFLAGS := $(LDFLAGS) $(STRIP_FLAG)
48LIBLDFLAGS := -shared $(LDFLAGS)
49
50OBJDIR := $(BUILDDIR)/objs
51$(shell mkdir -p $(OBJDIR))
52
53SRCS=$(wildcard *.c)
54OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
55OBJSLIB=$(addprefix $(OBJDIR)/,$(LIBOBJS))
56
57DEPDIR := $(BUILDDIR)/deps
58$(shell mkdir -p $(DEPDIR))
59DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
60POSTCOMPILE = 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
74LOCALLDLIBS=-L. -ltpC
f1294ffe 75$(BINARY_NAME).staticlocal: $(OBJDIR)/$(BINARY_NAME).o $(LIBRARY_NAME).a
a5a96953
JB
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
92include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
93
94clean:
95 @echo "[CLN]"
96 -@rm -r $(BUILDDIR)
97 -@rm $(BINARY_NAME)
f1294ffe 98 -@rm $(BINARY_NAME).staticlocal
a5a96953
JB
99 -@rm $(BINARY_NAME).dynamic
100 -@rm $(LIBRARY_NAME).so
101 -@rm $(LIBRARY_NAME).a
102
103disassemble: $(BINARY_NAME)
104 objdump -d $< | less
105
106symbols: $(BINARY_NAME)
107 objdump -t $< | sort | less