TP_13 exo2: More explicit name to the includes path variable
[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
7515f897
JB
32CC=gcc
33LD=gcc
34AR=ar
35
36WARN_FLAGS = -Wall -Wextra
37STD_FLAG = -std=c11
5d640630 38UNAME := $(shell uname -o)
7515f897
JB
39
40ifeq ($(BUILD_TYPE),debug)
41BUILDDIR := .build/debug
42DEBUG_FLAG = -g
43STRIP_FLAG =
44OPTI_FLAG = -O0
45LTO_SUPPORT = yes
46GOLD_SUPPORT = yes
47else
48BUILDDIR := .build/release
49DEBUG_FLAG =
50STRIP_FLAG = -s
51OPTI_FLAG = -O3
52LTO_SUPPORT = yes
53GOLD_SUPPORT = yes
54endif
55
adb9472d
JB
56BINARY_SUFFIX :=
57DLIB_PREFIX :=
58SDLIB_PREFIX :=
59DLIB_SUFFIX := .so
60SLIB_SUFFIX := .a
61
62OBJDIR := $(BUILDDIR)/objs
63$(shell mkdir -p $(OBJDIR))
64
65SRCS=$(wildcard $(SRC_PATH)/*.c)
66LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
67OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
68LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
69
70DEPDIR := $(BUILDDIR)/deps
71$(shell mkdir -p $(DEPDIR))
72DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
73POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
74
5d640630
JB
75ifeq ($(UNAME),Cygwin)
76GOLD_SUPPORT = no
adb9472d
JB
77# Cygwin ncurses do not contain tinfo
78LDLIBS := $(filter-out -ltinfo,$(LDLIBS))
79BINARY_SUFFIX := .exe
80DLIB_SUFFIX := .dll
81CYGWIN_LDLIBS_NOARCHIVE = -lncurses
40b6c889 82CYGWIN_LIBLDFLAGS := -Wl,--out-implib=$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a \
adb9472d
JB
83 -Wl,--export-all-symbols \
84 -Wl,--enable-auto-import
5d640630
JB
85endif
86
7515f897
JB
87ifeq ($(LTO_SUPPORT),yes)
88CFLAGS_LTO = -flto -ffat-lto-objects
89LDFLAGS_LTO = -fuse-linker-plugin -flto
90endif
91
92ifeq ($(GOLD_SUPPORT),yes)
93LDFLAGS_GOLD = -fuse-ld=gold
94endif
95
40b6c889
JB
96# Putting header files in the source directory is not the purpose of this INCLUDES_PATH variable
97INCLUDES_PATH := $(INCLUDES_PATH) -I$(LIBRARY_PATH)
98CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES_PATH)
7515f897
JB
99LIBCFLAGS := -fPIC $(CFLAGS)
100LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG)
101LIBLDFLAGS := -shared $(LDFLAGS)
102STATICLIBLDFLAGS := -static $(LDFLAGS)
103LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME)
104
adb9472d 105all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX) $(BINARY_NAME).static$(BINARY_SUFFIX)
7515f897 106
adb9472d 107$(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX): $(LIBOBJS)
7515f897
JB
108 @echo "[AR StO] $@"
109 @$(AR) rcs $@ $^
110
adb9472d
JB
111ifeq ($(UNAME),Cygwin)
112$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a: $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
113$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
114 @echo "[LD DLL] $@"
115 @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $(CYGWIN_LIBLDFLAGS) -Wl,--whole-archive $^ -Wl,--no-whole-archive $(CYGWIN_LDLIBS_NOARCHIVE) -o $@
116else
117$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
7515f897
JB
118 @echo "[LD ShO] $@"
119 @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@
adb9472d 120endif
7515f897 121
adb9472d 122#$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBOBJS)
7515f897
JB
123# @echo "[LD ] $@"
124# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
125
adb9472d 126$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
7515f897
JB
127 @echo "[LD ] $@"
128 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
129
adb9472d 130$(BINARY_NAME).static$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
7515f897
JB
131 @echo "[LD ] $@"
132 @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
133
adb9472d
JB
134ifeq ($(UNAME),Cygwin)
135$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a
136else
137$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
138endif
7515f897
JB
139 @echo "[LD ] $@"
140 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
141
142$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d
143 @echo "[C ] $(notdir $*)"
144 @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
145 @$(POSTCOMPILE)
146
147include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
148
149$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d
150 @echo "[C ] $(notdir $*)"
151 @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@
152 @$(POSTCOMPILE)
153
154include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS))))
155
156$(DEPDIR)/%.d: ;
157
158.PRECIOUS: $(DEPDIR)/%.d
159
160#FIXME: add an install target
161
162clean:
163 @echo "[CLN]"
164 -@rm -r $(BUILDDIR)
adb9472d
JB
165 -@rm $(BINARY_NAME)$(BINARY_SUFFIX)
166 -@rm $(BINARY_NAME).static$(BINARY_SUFFIX)
167 -@rm $(BINARY_NAME).dynamic$(BINARY_SUFFIX)
168 -@rm $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
169 -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
170ifeq ($(UNAME),Cygwin)
171 -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a
172endif
7515f897 173
adb9472d 174disassemble: $(BINARY_NAME)$(BINARY_SUFFIX)
7515f897
JB
175 objdump -d $< | less
176
adb9472d 177symbols: $(BINARY_NAME)$(BINARY_SUFFIX)
7515f897 178 objdump -t $< | sort | less