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
24 LDLIBS
=-lncurses
-ltinfo
28 # ====================================
29 # DO NOT CHANGE STUFF BEYOND THIS LINE
30 # ====================================
32 all: $(BINARY_NAME
) $(BINARY_NAME
).dynamic
$(BINARY_NAME
).static
38 WARN_FLAGS
= -Wall
-Wextra
40 UNAME
:= $(shell uname
-o
)
42 ifeq ($(BUILD_TYPE
),debug
)
43 BUILDDIR
:= .build
/debug
50 BUILDDIR
:= .build
/release
58 ifeq ($(UNAME
),Cygwin
)
62 ifeq ($(LTO_SUPPORT
),yes
)
63 CFLAGS_LTO
= -flto
-ffat-lto-objects
64 LDFLAGS_LTO
= -fuse-linker-plugin
-flto
67 ifeq ($(GOLD_SUPPORT
),yes
)
68 LDFLAGS_GOLD
= -fuse-ld
=gold
71 # Putting header files in the source directory is not the purpose of this INCLUDES variable
72 INCLUDES
:= $(INCLUDES
) -I
$(LIBRARY_PATH
)
73 CFLAGS
:= $(CFLAGS
) $(CFLAGS_LTO
) $(WARN_FLAGS
) $(STD_FLAG
) $(OPTI_FLAG
) $(DEBUG_FLAG
) $(INCLUDES
)
74 LIBCFLAGS
:= -fPIC
$(CFLAGS
)
75 LDFLAGS
:= $(LDFLAGS
) $(LDFLAGS_LTO
) $(LDFLAGS_GOLD
) $(STRIP_FLAG
)
76 LIBLDFLAGS
:= -shared
$(LDFLAGS
)
77 STATICLIBLDFLAGS
:= -static
$(LDFLAGS
)
78 LDLIBS
:= $(LDLIBS
) -L
$(LIBRARY_PATH
) -l
$(BINARY_NAME
)
80 OBJDIR
:= $(BUILDDIR
)/objs
81 $(shell mkdir
-p
$(OBJDIR
))
83 SRCS
=$(wildcard $(SRC_PATH
)/*.c
)
84 LIBSRCS
=$(wildcard $(LIBRARY_PATH
)/*.c
)
85 OBJS
=$(patsubst %.c
,$(OBJDIR
)/%.o
,$(notdir $(SRCS
)))
86 LIBOBJS
=$(patsubst %.c
,$(OBJDIR
)/%.o
,$(notdir $(LIBSRCS
)))
88 DEPDIR
:= $(BUILDDIR
)/deps
89 $(shell mkdir
-p
$(DEPDIR
))
90 DEPFLAGS
= -MT
$@
-MMD
-MP
-MF
$(DEPDIR
)/$(notdir $*).Td
91 POSTCOMPILE
= mv
-f
$(DEPDIR
)/$(notdir $*).Td
$(DEPDIR
)/$(notdir $*).d
93 $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
: $(LIBOBJS
)
97 $(LIBRARY_PATH
)/$(LIBRARY_NAME
).so
: $(LIBOBJS
)
99 @
$(LD
) $(LIBCFLAGS
) $(LIBLDFLAGS
) $^
-o
$@
101 #$(BINARY_NAME): $(OBJS) $(LIBOBJS)
103 # @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
105 $(BINARY_NAME
): $(OBJS
) $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
107 @
$(LD
) $(CFLAGS
) $(LDFLAGS
) $^
$(LDLIBS
) -o
$@
109 $(BINARY_NAME
).static
: $(OBJS
) $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
111 @
$(LD
) $(CFLAGS
) $(STATICLIBLDFLAGS
) $^
$(LDLIBS
) -o
$@
113 $(BINARY_NAME
).dynamic
: $(OBJS
) $(LIBRARY_PATH
)/$(LIBRARY_NAME
).so
115 @
$(LD
) $(CFLAGS
) $(LDFLAGS
) $^
$(LDLIBS
) -o
$@
117 $(OBJDIR
)/%.o
: $(SRC_PATH
)/%.c
$(DEPDIR
)/%.d
118 @echo
"[C ] $(notdir $*)"
119 @
$(CC
) $(DEPFLAGS
) $(CFLAGS
) -c
$< -o
$@
122 include $(wildcard $(patsubst %,$(DEPDIR
)/%.d
,$(basename $(SRCS
))))
124 $(OBJDIR
)/%.o
: $(LIBRARY_PATH
)/%.c
$(DEPDIR
)/%.d
125 @echo
"[C ] $(notdir $*)"
126 @
$(CC
) $(DEPFLAGS
) $(LIBCFLAGS
) -c
$< -o
$@
129 include $(wildcard $(patsubst %,$(DEPDIR
)/%.d
,$(basename $(LIBSRCS
))))
133 .PRECIOUS
: $(DEPDIR
)/%.d
135 #FIXME: add an install target
141 -@
rm $(BINARY_NAME
).static
142 -@
rm $(BINARY_NAME
).dynamic
143 -@
rm $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
144 -@
rm $(LIBRARY_PATH
)/$(LIBRARY_NAME
).so
146 disassemble
: $(BINARY_NAME
)
149 symbols
: $(BINARY_NAME
)
150 objdump
-t
$< |
sort | less