Merge branch 'master' of git.piment-noir.org:Project_algorithmic_C
[Project_algorithmic_C.git] / Makefile
CommitLineData
4ddf6f1a
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:=othello
21SRC_PATH:=src
7aab9e03 22LIBRARY_NAME:=lib$(BINARY_NAME)
4ddf6f1a 23LIBRARY_PATH:=lib
7aab9e03 24LDLIBS:=-l$(BINARY_NAME) -lncurses -ltinfo
314ec686
JB
25#BUILD_TYPE:=debug
26BUILD_TYPE:=release
4ddf6f1a
JB
27
28# ====================================
29# DO NOT CHANGE STUFF BEYOND THIS LINE
30# ====================================
31
32CC=gcc
33LD=gcc
34AR=ar
35
36WARN_FLAGS = -Wall -Wextra
37STD_FLAG = -std=c11
0e770dcc 38UNAME := $(shell uname -s)
4ddf6f1a
JB
39
40ifeq ($(BUILD_TYPE),debug)
41BUILDDIR := .build/debug
42DEBUG_FLAG = -g
c8c562de 43DEBUG = 1
4ddf6f1a
JB
44STRIP_FLAG =
45OPTI_FLAG = -O0
46LTO_SUPPORT = yes
47GOLD_SUPPORT = yes
48else
49BUILDDIR := .build/release
50DEBUG_FLAG =
c8c562de 51DEBUG = 0
4ddf6f1a
JB
52STRIP_FLAG = -s
53OPTI_FLAG = -O3
54LTO_SUPPORT = yes
55GOLD_SUPPORT = yes
56endif
57
58BINARY_SUFFIX :=
59DLIB_PREFIX :=
60SDLIB_PREFIX :=
0e770dcc 61SHLIBLDFLAG := -shared
4ddf6f1a
JB
62DLIB_SUFFIX := .so
63SLIB_SUFFIX := .a
64
65OBJDIR := $(BUILDDIR)/objs
66$(shell mkdir -p $(OBJDIR))
67
68SRCS=$(wildcard $(SRC_PATH)/*.c)
69LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
70OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
71LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
72
73DEPDIR := $(BUILDDIR)/deps
74$(shell mkdir -p $(DEPDIR))
75DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
76POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
77
0e770dcc 78ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
4ddf6f1a
JB
79CYGWIN_DLIBRARY_NAME := $(subst lib,cyg,$(LIBRARY_NAME))
80GOLD_SUPPORT = no
81# Cygwin ncurses do not contain tinfo
82LDLIBS := $(filter-out -ltinfo,$(LDLIBS))
83BINARY_SUFFIX := .exe
84DLIB_SUFFIX := .dll
85# do not archive external libraries
0e770dcc 86CYGWIN_LDLIBS_NOARCHIVE := $(filter-out -l$(BINARY_NAME),$(LDLIBS))
31ec3615 87CYGWIN_LIBLDFLAGS := -Wl,--out-implib=$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX) \
4ddf6f1a
JB
88 -Wl,--export-all-symbols \
89 -Wl,--enable-auto-import
90endif
91
0e770dcc
JB
92ifeq ($(UNAME),Darwin)
93SHLIBLDFLAG=-dynamiclib
94LDLIBS := $(filter-out -ltinfo,$(LDLIBS))
95DLIB_SUFFIX=.dylib
96LTO_SUPPORT=no
97GOLD_SUPPORT=no
98endif
99
4ddf6f1a
JB
100ifeq ($(LTO_SUPPORT),yes)
101CFLAGS_LTO = -flto -ffat-lto-objects
102LDFLAGS_LTO = -fuse-linker-plugin -flto
103endif
104
105ifeq ($(GOLD_SUPPORT),yes)
106LDFLAGS_GOLD = -fuse-ld=gold
107endif
108
109# Putting header files in the source directory is not the purpose of this INCLUDES_PATH variable
110INCLUDES_PATH := $(INCLUDES_PATH) -I$(LIBRARY_PATH)
c8c562de 111CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES_PATH)
4ddf6f1a
JB
112LIBCFLAGS := -fPIC $(CFLAGS)
113LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG)
0e770dcc 114LIBLDFLAGS := $(SHLIBLDFLAG) $(LDFLAGS)
4ddf6f1a 115STATICLIBLDFLAGS := -static $(LDFLAGS)
0e770dcc 116LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH)
4ddf6f1a 117
0e770dcc
JB
118# Mac OS X do not support static linking
119ifeq ($(UNAME),Darwin)
120all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX)
121else
4ddf6f1a 122all: $(BINARY_NAME)$(BINARY_SUFFIX) $(BINARY_NAME).dynamic$(BINARY_SUFFIX) $(BINARY_NAME).static$(BINARY_SUFFIX)
0e770dcc 123endif
4ddf6f1a
JB
124
125$(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX): $(LIBOBJS)
126 @echo "[AR StO] $@"
127 @$(AR) rcs $@ $^
128
0e770dcc 129ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
31ec3615 130$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX): $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX)
4ddf6f1a
JB
131$(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
132 @echo "[LD DLL] $@"
133 @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $(CYGWIN_LIBLDFLAGS) -Wl,--whole-archive $^ -Wl,--no-whole-archive $(CYGWIN_LDLIBS_NOARCHIVE) -o $@
134else
135$(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS)
136 @echo "[LD ShO] $@"
0e770dcc 137 @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ $(LDLIBS) -o $@
4ddf6f1a
JB
138endif
139
140#$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBOBJS)
141# @echo "[LD ] $@"
142# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
143
144$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
145 @echo "[LD ] $@"
146 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
147
148$(BINARY_NAME).static$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
149 @echo "[LD ] $@"
150 @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
151
0e770dcc 152ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
31ec3615 153$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX)
4ddf6f1a
JB
154else
155$(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
156endif
157 @echo "[LD ] $@"
158 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
159
160$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d
161 @echo "[C ] $(notdir $*)"
162 @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
163 @$(POSTCOMPILE)
164
165include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
166
167$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d
168 @echo "[C ] $(notdir $*)"
169 @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@
170 @$(POSTCOMPILE)
171
172include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS))))
173
174$(DEPDIR)/%.d: ;
175
176.PRECIOUS: $(DEPDIR)/%.d
177
178#FIXME: add an install target
179
180clean:
181 @echo "[CLN]"
182 -@rm -r $(BUILDDIR)
183 -@rm $(BINARY_NAME)$(BINARY_SUFFIX)
0e770dcc 184ifneq ($(UNAME),Darwin)
4ddf6f1a 185 -@rm $(BINARY_NAME).static$(BINARY_SUFFIX)
0e770dcc 186endif
4ddf6f1a
JB
187 -@rm $(BINARY_NAME).dynamic$(BINARY_SUFFIX)
188 -@rm $(LIBRARY_PATH)/$(SLIB_PREFIX)$(LIBRARY_NAME)$(SLIB_SUFFIX)
0e770dcc 189ifeq ($(findstring CYGWIN_NT,$(UNAME)),CYGWIN_NT)
4ddf6f1a 190 -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(CYGWIN_DLIBRARY_NAME)$(DLIB_SUFFIX)
31ec3615 191 -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)$(SLIB_SUFFIX)
4ddf6f1a
JB
192else
193 -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX)
194endif
195
21b9239c
JB
196run: $(BINARY_NAME)
197 ./$(BINARY_NAME)
198
4ddf6f1a
JB
199disassemble: $(BINARY_NAME)$(BINARY_SUFFIX)
200 objdump -d $< | less
201
202symbols: $(BINARY_NAME)$(BINARY_SUFFIX)
203 objdump -t $< | sort | less