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
41 ifeq ($(BUILD_TYPE
),debug
)
42 BUILDDIR
:= .build
/debug
49 BUILDDIR
:= .build
/release
57 ifeq ($(LTO_SUPPORT
),yes
)
58 CFLAGS_LTO
= -flto
-ffat-lto-objects
59 LDFLAGS_LTO
= -fuse-linker-plugin
-flto
62 ifeq ($(GOLD_SUPPORT
),yes
)
63 LDFLAGS_GOLD
= -fuse-ld
=gold
66 # Putting header files in the source directory is not the purpose of this INCLUDES variable
67 INCLUDES
:= $(INCLUDES
) -I
$(LIBRARY_PATH
)
68 CFLAGS
:= $(CFLAGS
) $(CFLAGS_LTO
) $(WARN_FLAGS
) $(STD_FLAG
) $(OPTI_FLAG
) $(DEBUG_FLAG
) $(INCLUDES
)
69 LIBCFLAGS
:= -fPIC
$(CFLAGS
)
70 LDFLAGS
:= $(LDFLAGS
) $(LDFLAGS_LTO
) $(LDFLAGS_GOLD
) $(STRIP_FLAG
)
71 LIBLDFLAGS
:= -shared
$(LDFLAGS
)
72 STATICLIBLDFLAGS
:= -static
$(LDFLAGS
)
73 LDLIBS
:= $(LDLIBS
) -L
$(LIBRARY_PATH
) -l
$(BINARY_NAME
)
75 OBJDIR
:= $(BUILDDIR
)/objs
76 $(shell mkdir
-p
$(OBJDIR
))
78 SRCS
=$(wildcard $(SRC_PATH
)/*.c
)
79 LIBSRCS
=$(wildcard $(LIBRARY_PATH
)/*.c
)
80 OBJS
=$(patsubst %.c
,$(OBJDIR
)/%.o
,$(notdir $(SRCS
)))
81 LIBOBJS
=$(patsubst %.c
,$(OBJDIR
)/%.o
,$(notdir $(LIBSRCS
)))
83 DEPDIR
:= $(BUILDDIR
)/deps
84 $(shell mkdir
-p
$(DEPDIR
))
85 DEPFLAGS
= -MT
$@
-MMD
-MP
-MF
$(DEPDIR
)/$(notdir $*).Td
86 POSTCOMPILE
= mv
-f
$(DEPDIR
)/$(notdir $*).Td
$(DEPDIR
)/$(notdir $*).d
88 $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
: $(LIBOBJS
)
92 $(LIBRARY_PATH
)/$(LIBRARY_NAME
).so
: $(LIBOBJS
)
94 @
$(LD
) $(LIBCFLAGS
) $(LIBLDFLAGS
) $^
-o
$@
96 #$(BINARY_NAME): $(OBJS) $(LIBOBJS)
98 # @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
100 $(BINARY_NAME
): $(OBJS
) $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
102 @
$(LD
) $(CFLAGS
) $(LDFLAGS
) $^
$(LDLIBS
) -o
$@
104 $(BINARY_NAME
).static
: $(OBJS
) $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
106 @
$(LD
) $(CFLAGS
) $(STATICLIBLDFLAGS
) $^
$(LDLIBS
) -o
$@
108 $(BINARY_NAME
).dynamic
: $(OBJS
) $(LIBRARY_PATH
)/$(LIBRARY_NAME
).so
110 @
$(LD
) $(CFLAGS
) $(LDFLAGS
) $^
$(LDLIBS
) -o
$@
112 $(OBJDIR
)/%.o
: $(SRC_PATH
)/%.c
$(DEPDIR
)/%.d
113 @echo
"[C ] $(notdir $*)"
114 @
$(CC
) $(DEPFLAGS
) $(CFLAGS
) -c
$< -o
$@
117 include $(wildcard $(patsubst %,$(DEPDIR
)/%.d
,$(basename $(SRCS
))))
119 $(OBJDIR
)/%.o
: $(LIBRARY_PATH
)/%.c
$(DEPDIR
)/%.d
120 @echo
"[C ] $(notdir $*)"
121 @
$(CC
) $(DEPFLAGS
) $(LIBCFLAGS
) -c
$< -o
$@
124 include $(wildcard $(patsubst %,$(DEPDIR
)/%.d
,$(basename $(LIBSRCS
))))
128 .PRECIOUS
: $(DEPDIR
)/%.d
130 #FIXME: add an install target
136 -@
rm $(BINARY_NAME
).static
137 -@
rm $(BINARY_NAME
).dynamic
138 -@
rm $(LIBRARY_PATH
)/$(LIBRARY_NAME
).a
139 -@
rm $(LIBRARY_PATH
)/$(LIBRARY_NAME
).so
141 disassemble
: $(BINARY_NAME
)
144 symbols
: $(BINARY_NAME
)
145 objdump
-t
$< |
sort | less