1 # Sample Makefile to build simple project.
3 # This Makefile expect all source files (.c) to be at the same level, in the
4 # $(SRC_PATH) directory.
6 # This Makefile expect all embedded library source files (.c) to be at the same level, in the
7 # $(LIBRARY_PATH) directory.
9 # It will automatically generate dependencies, compile all files, and produce a
10 # binary using the provided name linked against the library if necessary.
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.
18 # Automatic dependencies code from:
19 # http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
22 LIBRARY_NAME
:=lib
$(BINARY_NAME
)
24 LDLIBS
:=-l
$(BINARY_NAME
) -lncurses
-ltinfo
28 # ====================================
29 # DO NOT CHANGE STUFF BEYOND THIS LINE
30 # ====================================
36 WARN_FLAGS
= -Wall
-Wextra
38 UNAME
:= $(shell uname
-s
)
40 ifeq ($(BUILD_TYPE
),debug
)
41 BUILDDIR
:= .build
/debug
49 BUILDDIR
:= .build
/release
61 SHLIBLDFLAG
:= -shared
65 OBJDIR
:= $(BUILDDIR
)/objs
66 $(shell mkdir
-p
$(OBJDIR
))
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
)))
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
78 ifeq ($(findstring CYGWIN_NT
,$(UNAME
)),CYGWIN_NT
)
79 CYGWIN_DLIBRARY_NAME
:= $(subst lib
,cyg
,$(LIBRARY_NAME
))
81 # Cygwin ncurses do not contain tinfo
82 LDLIBS
:= $(filter-out -ltinfo
,$(LDLIBS
))
85 # do not archive external libraries
86 CYGWIN_LDLIBS_NOARCHIVE
:= $(filter-out -l
$(BINARY_NAME
),$(LDLIBS
))
87 CYGWIN_LIBLDFLAGS
:= -Wl
,--out-implib
=$(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(LIBRARY_NAME
)$(DLIB_SUFFIX
)$(SLIB_SUFFIX
) \
88 -Wl
,--export-all-symbols \
89 -Wl
,--enable-auto-import
92 ifeq ($(UNAME
),Darwin
)
93 SHLIBLDFLAG
=-dynamiclib
94 LDLIBS
:= $(filter-out -ltinfo
,$(LDLIBS
))
100 ifeq ($(LTO_SUPPORT
),yes
)
101 CFLAGS_LTO
= -flto
-ffat-lto-objects
102 LDFLAGS_LTO
= -fuse-linker-plugin
-flto
105 ifeq ($(GOLD_SUPPORT
),yes
)
106 LDFLAGS_GOLD
= -fuse-ld
=gold
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
)
111 CFLAGS
:= -DDEBUG
=$(DEBUG
) $(CFLAGS
) $(CFLAGS_LTO
) $(WARN_FLAGS
) $(STD_FLAG
) $(OPTI_FLAG
) $(DEBUG_FLAG
) $(INCLUDES_PATH
)
112 LIBCFLAGS
:= -fPIC
$(CFLAGS
)
113 LDFLAGS
:= $(LDFLAGS
) $(LDFLAGS_LTO
) $(LDFLAGS_GOLD
) $(STRIP_FLAG
)
114 LIBLDFLAGS
:= $(SHLIBLDFLAG
) $(LDFLAGS
)
115 STATICLIBLDFLAGS
:= -static
$(LDFLAGS
)
116 LDLIBS
:= $(LDLIBS
) -L
$(LIBRARY_PATH
)
118 # Mac OS X do not support static linking
119 ifeq ($(UNAME
),Darwin
)
120 all: $(BINARY_NAME
)$(BINARY_SUFFIX
) $(BINARY_NAME
).dynamic
$(BINARY_SUFFIX
)
122 all: $(BINARY_NAME
)$(BINARY_SUFFIX
) $(BINARY_NAME
).dynamic
$(BINARY_SUFFIX
) $(BINARY_NAME
).static
$(BINARY_SUFFIX
)
125 $(LIBRARY_PATH
)/$(SLIB_PREFIX
)$(LIBRARY_NAME
)$(SLIB_SUFFIX
): $(LIBOBJS
)
129 ifeq ($(findstring CYGWIN_NT
,$(UNAME
)),CYGWIN_NT
)
130 $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(LIBRARY_NAME
)$(DLIB_SUFFIX
)$(SLIB_SUFFIX
): $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(CYGWIN_DLIBRARY_NAME
)$(DLIB_SUFFIX
)
131 $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(CYGWIN_DLIBRARY_NAME
)$(DLIB_SUFFIX
): $(LIBOBJS
)
133 @
$(LD
) $(LIBCFLAGS
) $(LIBLDFLAGS
) $(CYGWIN_LIBLDFLAGS
) -Wl
,--whole-archive
$^
-Wl
,--no-whole-archive
$(CYGWIN_LDLIBS_NOARCHIVE
) -o
$@
135 $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(LIBRARY_NAME
)$(DLIB_SUFFIX
): $(LIBOBJS
)
137 @
$(LD
) $(LIBCFLAGS
) $(LIBLDFLAGS
) $^
$(LDLIBS
) -o
$@
140 #$(BINARY_NAME)$(BINARY_SUFFIX): $(OBJS) $(LIBOBJS)
142 # @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
144 $(BINARY_NAME
)$(BINARY_SUFFIX
): $(OBJS
) $(LIBRARY_PATH
)/$(SLIB_PREFIX
)$(LIBRARY_NAME
)$(SLIB_SUFFIX
)
146 @
$(LD
) $(CFLAGS
) $(LDFLAGS
) $^
$(LDLIBS
) -o
$@
148 $(BINARY_NAME
).static
$(BINARY_SUFFIX
): $(OBJS
) $(LIBRARY_PATH
)/$(SLIB_PREFIX
)$(LIBRARY_NAME
)$(SLIB_SUFFIX
)
150 @
$(LD
) $(CFLAGS
) $(STATICLIBLDFLAGS
) $^
$(LDLIBS
) -o
$@
152 ifeq ($(findstring CYGWIN_NT
,$(UNAME
)),CYGWIN_NT
)
153 $(BINARY_NAME
).dynamic
$(BINARY_SUFFIX
): $(OBJS
) $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(LIBRARY_NAME
)$(DLIB_SUFFIX
)$(SLIB_SUFFIX
)
155 $(BINARY_NAME
).dynamic
$(BINARY_SUFFIX
): $(OBJS
) $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(LIBRARY_NAME
)$(DLIB_SUFFIX
)
158 @
$(LD
) $(CFLAGS
) $(LDFLAGS
) $^
$(LDLIBS
) -o
$@
160 $(OBJDIR
)/%.o
: $(SRC_PATH
)/%.c
$(DEPDIR
)/%.d
161 @echo
"[C ] $(notdir $*)"
162 @
$(CC
) $(DEPFLAGS
) $(CFLAGS
) -c
$< -o
$@
165 include $(wildcard $(patsubst %,$(DEPDIR
)/%.d
,$(basename $(SRCS
))))
167 $(OBJDIR
)/%.o
: $(LIBRARY_PATH
)/%.c
$(DEPDIR
)/%.d
168 @echo
"[C ] $(notdir $*)"
169 @
$(CC
) $(DEPFLAGS
) $(LIBCFLAGS
) -c
$< -o
$@
172 include $(wildcard $(patsubst %,$(DEPDIR
)/%.d
,$(basename $(LIBSRCS
))))
176 .PRECIOUS
: $(DEPDIR
)/%.d
178 #FIXME: add an install target
183 -@
rm $(BINARY_NAME
)$(BINARY_SUFFIX
)
184 ifneq ($(UNAME
),Darwin
)
185 -@
rm $(BINARY_NAME
).static
$(BINARY_SUFFIX
)
187 -@
rm $(BINARY_NAME
).dynamic
$(BINARY_SUFFIX
)
188 -@
rm $(LIBRARY_PATH
)/$(SLIB_PREFIX
)$(LIBRARY_NAME
)$(SLIB_SUFFIX
)
189 ifeq ($(findstring CYGWIN_NT
,$(UNAME
)),CYGWIN_NT
)
190 -@
rm $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(CYGWIN_DLIBRARY_NAME
)$(DLIB_SUFFIX
)
191 -@
rm $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(LIBRARY_NAME
)$(DLIB_SUFFIX
)$(SLIB_SUFFIX
)
193 -@
rm $(LIBRARY_PATH
)/$(DLIB_PREFIX
)$(LIBRARY_NAME
)$(DLIB_SUFFIX
)
199 disassemble
: $(BINARY_NAME
)$(BINARY_SUFFIX
)
202 symbols
: $(BINARY_NAME
)$(BINARY_SUFFIX
)
203 objdump
-t
$< |
sort | less