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