TP 13 exo2: Add the code structure and some basic displaying routines for the tic...
[TD_C.git] / TP_13 / exo2 / Makefile
CommitLineData
7515f897
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# $(SRC_PATH) directory.
5#
6# This Makefile expect all embedded library source files (.c) to be at the same level, in the
7# $(LIBRARY_PATH) directory.
8#
9# It will automatically generate dependencies, compile all files, and produce a
10# binary using the provided name linked against the library if necessary.
11#
12# Set BINARY_NAME to the name of the binary file to build.
13# Set LIBRARY_NAME to the name of the library file to build.
14# The default path for the library code and object is lib.
15# By default the linker will look for $(BINARY_NAME) library name.
16# Set BUILD_TYPE to either debug or release.
17#
18# Automatic dependencies code from:
19# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
20BINARY_NAME=exo2
21SRC_PATH:=src
22LIBRARY_NAME=libexo2
23LIBRARY_PATH:=lib
24LDLIBS=-lncurses -ltinfo
25BUILD_TYPE=debug
26#BUILD_TYPE=release
27
28# ====================================
29# DO NOT CHANGE STUFF BEYOND THIS LINE
30# ====================================
31
32all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static
33
34CC=gcc
35LD=gcc
36AR=ar
37
38WARN_FLAGS = -Wall -Wextra
39STD_FLAG = -std=c11
40
41ifeq ($(BUILD_TYPE),debug)
42BUILDDIR := .build/debug
43DEBUG_FLAG = -g
44STRIP_FLAG =
45OPTI_FLAG = -O0
46LTO_SUPPORT = yes
47GOLD_SUPPORT = yes
48else
49BUILDDIR := .build/release
50DEBUG_FLAG =
51STRIP_FLAG = -s
52OPTI_FLAG = -O3
53LTO_SUPPORT = yes
54GOLD_SUPPORT = yes
55endif
56
57ifeq ($(LTO_SUPPORT),yes)
58CFLAGS_LTO = -flto -ffat-lto-objects
59LDFLAGS_LTO = -fuse-linker-plugin -flto
60endif
61
62ifeq ($(GOLD_SUPPORT),yes)
63LDFLAGS_GOLD = -fuse-ld=gold
64endif
65
66# Putting header files in the source directory is not the purpose of this INCLUDES variable
67INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH)
68CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES)
69LIBCFLAGS := -fPIC $(CFLAGS)
70LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG)
71LIBLDFLAGS := -shared $(LDFLAGS)
72STATICLIBLDFLAGS := -static $(LDFLAGS)
73LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME)
74
75OBJDIR := $(BUILDDIR)/objs
76$(shell mkdir -p $(OBJDIR))
77
78SRCS=$(wildcard $(SRC_PATH)/*.c)
79LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
80OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
81LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
82
83DEPDIR := $(BUILDDIR)/deps
84$(shell mkdir -p $(DEPDIR))
85DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
86POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
87
88$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS)
89 @echo "[AR StO] $@"
90 @$(AR) rcs $@ $^
91
92$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS)
93 @echo "[LD ShO] $@"
94 @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@
95
96#$(BINARY_NAME): $(OBJS) $(LIBOBJS)
97# @echo "[LD ] $@"
98# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
99
100$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
101 @echo "[LD ] $@"
102 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
103
104$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
105 @echo "[LD ] $@"
106 @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
107
108$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so
109 @echo "[LD ] $@"
110 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
111
112$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d
113 @echo "[C ] $(notdir $*)"
114 @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
115 @$(POSTCOMPILE)
116
117include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
118
119$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d
120 @echo "[C ] $(notdir $*)"
121 @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@
122 @$(POSTCOMPILE)
123
124include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS))))
125
126$(DEPDIR)/%.d: ;
127
128.PRECIOUS: $(DEPDIR)/%.d
129
130#FIXME: add an install target
131
132clean:
133 @echo "[CLN]"
134 -@rm -r $(BUILDDIR)
135 -@rm $(BINARY_NAME)
136 -@rm $(BINARY_NAME).static
137 -@rm $(BINARY_NAME).dynamic
138 -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a
139 -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so
140
141disassemble: $(BINARY_NAME)
142 objdump -d $< | less
143
144symbols: $(BINARY_NAME)
145 objdump -t $< | sort | less