Buildsystem: be more friendly with cygwin environment
[TD_C.git] / TP_11 / exo2 / Makefile
... / ...
CommitLineData
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
24BUILD_TYPE=debug
25#BUILD_TYPE=release
26
27# ====================================
28# DO NOT CHANGE STUFF BEYOND THIS LINE
29# ====================================
30
31all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static
32
33CC=gcc
34LD=gcc
35AR=ar
36
37WARN_FLAGS = -Wall -Wextra
38STD_FLAG = -std=c11
39UNAME := $(shell uname -o)
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 ($(UNAME),Cygwin)
58GOLD_SUPPORT = no
59endif
60
61ifeq ($(LTO_SUPPORT),yes)
62CFLAGS_LTO = -flto -ffat-lto-objects
63LDFLAGS_LTO = -fuse-linker-plugin -flto
64endif
65
66ifeq ($(GOLD_SUPPORT),yes)
67LDFLAGS_GOLD = -fuse-ld=gold
68endif
69
70# Putting header files in the source directory is not the purpose of this INCLUDES variable
71INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH)
72CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES)
73LIBCFLAGS := -fPIC $(CFLAGS)
74LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG)
75LIBLDFLAGS := -shared $(LDFLAGS)
76STATICLIBLDFLAGS := -static $(LDFLAGS)
77LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME)
78
79OBJDIR := $(BUILDDIR)/objs
80$(shell mkdir -p $(OBJDIR))
81
82SRCS=$(wildcard $(SRC_PATH)/*.c)
83LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
84OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
85LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
86
87DEPDIR := $(BUILDDIR)/deps
88$(shell mkdir -p $(DEPDIR))
89DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
90POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
91
92$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS)
93 @echo "[AR StO] $@"
94 @$(AR) rcs $@ $^
95
96$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS)
97 @echo "[LD ShO] $@"
98 @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@
99
100#$(BINARY_NAME): $(OBJS) $(LIBOBJS)
101# @echo "[LD ] $@"
102# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
103
104$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
105 @echo "[LD ] $@"
106 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
107
108$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
109 @echo "[LD ] $@"
110 @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
111
112$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so
113 @echo "[LD ] $@"
114 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
115
116$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d
117 @echo "[C ] $(notdir $*)"
118 @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
119 @$(POSTCOMPILE)
120
121include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
122
123$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d
124 @echo "[C ] $(notdir $*)"
125 @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@
126 @$(POSTCOMPILE)
127
128include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS))))
129
130$(DEPDIR)/%.d: ;
131
132.PRECIOUS: $(DEPDIR)/%.d
133
134#FIXME: add an install target
135
136clean:
137 @echo "[CLN]"
138 -@rm -r $(BUILDDIR)
139 -@rm $(BINARY_NAME)
140 -@rm $(BINARY_NAME).static
141 -@rm $(BINARY_NAME).dynamic
142 -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a
143 -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so
144
145disassemble: $(BINARY_NAME)
146 objdump -d $< | less
147
148symbols: $(BINARY_NAME)
149 objdump -t $< | sort | less