Commit | Line | Data |
---|---|---|
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 | |
20 | BINARY_NAME=exo2 | |
21 | SRC_PATH:=src | |
22 | LIBRARY_NAME=libexo2 | |
23 | LIBRARY_PATH:=lib | |
24 | LDLIBS=-lncurses -ltinfo | |
25 | BUILD_TYPE=debug | |
26 | #BUILD_TYPE=release | |
27 | ||
28 | # ==================================== | |
29 | # DO NOT CHANGE STUFF BEYOND THIS LINE | |
30 | # ==================================== | |
31 | ||
7515f897 JB |
32 | CC=gcc |
33 | LD=gcc | |
34 | AR=ar | |
35 | ||
36 | WARN_FLAGS = -Wall -Wextra | |
37 | STD_FLAG = -std=c11 | |
5d640630 | 38 | UNAME := $(shell uname -o) |
7515f897 JB |
39 | |
40 | ifeq ($(BUILD_TYPE),debug) | |
41 | BUILDDIR := .build/debug | |
42 | DEBUG_FLAG = -g | |
43 | STRIP_FLAG = | |
44 | OPTI_FLAG = -O0 | |
45 | LTO_SUPPORT = yes | |
46 | GOLD_SUPPORT = yes | |
47 | else | |
48 | BUILDDIR := .build/release | |
49 | DEBUG_FLAG = | |
50 | STRIP_FLAG = -s | |
51 | OPTI_FLAG = -O3 | |
52 | LTO_SUPPORT = yes | |
53 | GOLD_SUPPORT = yes | |
54 | endif | |
55 | ||
adb9472d JB |
56 | BINARY_SUFFIX := |
57 | DLIB_PREFIX := | |
58 | SDLIB_PREFIX := | |
59 | DLIB_SUFFIX := .so | |
60 | SLIB_SUFFIX := .a | |
61 | ||
62 | OBJDIR := $(BUILDDIR)/objs | |
63 | $(shell mkdir -p $(OBJDIR)) | |
64 | ||
65 | SRCS=$(wildcard $(SRC_PATH)/*.c) | |
66 | LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c) | |
67 | OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS))) | |
68 | LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS))) | |
69 | ||
70 | DEPDIR := $(BUILDDIR)/deps | |
71 | $(shell mkdir -p $(DEPDIR)) | |
72 | DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td | |
73 | POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d | |
74 | ||
5d640630 JB |
75 | ifeq ($(UNAME),Cygwin) |
76 | GOLD_SUPPORT = no | |
adb9472d JB |
77 | # Cygwin ncurses do not contain tinfo |
78 | LDLIBS := $(filter-out -ltinfo,$(LDLIBS)) | |
79 | BINARY_SUFFIX := .exe | |
80 | DLIB_SUFFIX := .dll | |
81 | CYGWIN_LDLIBS_NOARCHIVE = -lncurses | |
40b6c889 | 82 | CYGWIN_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 |
85 | endif |
86 | ||
7515f897 JB |
87 | ifeq ($(LTO_SUPPORT),yes) |
88 | CFLAGS_LTO = -flto -ffat-lto-objects | |
89 | LDFLAGS_LTO = -fuse-linker-plugin -flto | |
90 | endif | |
91 | ||
92 | ifeq ($(GOLD_SUPPORT),yes) | |
93 | LDFLAGS_GOLD = -fuse-ld=gold | |
94 | endif | |
95 | ||
40b6c889 JB |
96 | # Putting header files in the source directory is not the purpose of this INCLUDES_PATH variable |
97 | INCLUDES_PATH := $(INCLUDES_PATH) -I$(LIBRARY_PATH) | |
98 | CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES_PATH) | |
7515f897 JB |
99 | LIBCFLAGS := -fPIC $(CFLAGS) |
100 | LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG) | |
101 | LIBLDFLAGS := -shared $(LDFLAGS) | |
102 | STATICLIBLDFLAGS := -static $(LDFLAGS) | |
103 | LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME) | |
104 | ||
adb9472d | 105 | all: $(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 |
111 | ifeq ($(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 $@ | |
116 | else | |
117 | $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX): $(LIBOBJS) | |
7515f897 JB |
118 | @echo "[LD ShO] $@" |
119 | @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@ | |
adb9472d | 120 | endif |
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 |
134 | ifeq ($(UNAME),Cygwin) |
135 | $(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a | |
136 | else | |
137 | $(BINARY_NAME).dynamic$(BINARY_SUFFIX): $(OBJS) $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX) | |
138 | endif | |
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 | ||
147 | include $(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 | ||
154 | include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS)))) | |
155 | ||
156 | $(DEPDIR)/%.d: ; | |
157 | ||
158 | .PRECIOUS: $(DEPDIR)/%.d | |
159 | ||
160 | #FIXME: add an install target | |
161 | ||
162 | clean: | |
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) | |
170 | ifeq ($(UNAME),Cygwin) | |
171 | -@rm $(LIBRARY_PATH)/$(DLIB_PREFIX)$(LIBRARY_NAME)$(DLIB_SUFFIX).a | |
172 | endif | |
7515f897 | 173 | |
adb9472d | 174 | disassemble: $(BINARY_NAME)$(BINARY_SUFFIX) |
7515f897 JB |
175 | objdump -d $< | less |
176 | ||
adb9472d | 177 | symbols: $(BINARY_NAME)$(BINARY_SUFFIX) |
7515f897 | 178 | objdump -t $< | sort | less |