Cleanups.
[TD_C.git] / Makefile
... / ...
CommitLineData
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
20BINARY_NAME=exo1
21SRC_PATH:=src
22LIBRARY_NAME=libexo1
23LIBRARY_PATH:=lib
24BUILD_TYPE=debug
25#BUILD_TYPE=release
26
27# ====================================
28# DO NOT CHANGE STUFF BEYOND THIS LINE
29# ====================================
30
31all: $(BINARY_NAME) $(BINARY_NAME).dynamic $(BINARY_NAME).static
32
33CC=gcc
34LD=gcc
35AR=ar
36
37WARN_FLAGS = -Wall -Wextra
38STD_FLAG = -std=c11
39
40ifeq ($(BUILD_TYPE),debug)
41BUILDDIR := .build/debug
42DEBUG_FLAG = -g
43STRIP_FLAG =
44OPTI_FLAG = -O0
45LTO_SUPPORT = yes
46GOLD_SUPPORT = yes
47else
48BUILDDIR := .build/release
49DEBUG_FLAG =
50STRIP_FLAG = -s
51OPTI_FLAG = -O3
52LTO_SUPPORT = yes
53GOLD_SUPPORT = yes
54endif
55
56ifeq ($(LTO_SUPPORT),yes)
57CFLAGS_LTO = -flto -ffat-lto-objects
58LDFLAGS_LTO = -fuse-linker-plugin -flto
59endif
60
61ifeq ($(GOLD_SUPPORT),yes)
62LDFLAGS_GOLD = -fuse-ld=gold
63endif
64
65# Putting header files in the source directory is not the purpose of this INCLUDES variable
66INCLUDES := $(INCLUDES) -I$(LIBRARY_PATH)
67CFLAGS := $(CFLAGS) $(CFLAGS_LTO) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) $(INCLUDES)
68LIBCFLAGS := -fPIC $(CFLAGS)
69LDFLAGS := $(LDFLAGS) $(LDFLAGS_LTO) $(LDFLAGS_GOLD) $(STRIP_FLAG)
70LIBLDFLAGS := -shared $(LDFLAGS)
71STATICLIBLDFLAGS := -static $(LDFLAGS)
72LDLIBS := $(LDLIBS) -L$(LIBRARY_PATH) -l$(BINARY_NAME)
73
74OBJDIR := $(BUILDDIR)/objs
75$(shell mkdir -p $(OBJDIR))
76
77SRCS=$(wildcard $(SRC_PATH)/*.c)
78LIBSRCS=$(wildcard $(LIBRARY_PATH)/*.c)
79OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(SRCS)))
80LIBOBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(notdir $(LIBSRCS)))
81
82DEPDIR := $(BUILDDIR)/deps
83$(shell mkdir -p $(DEPDIR))
84DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$(notdir $*).Td
85POSTCOMPILE = mv -f $(DEPDIR)/$(notdir $*).Td $(DEPDIR)/$(notdir $*).d
86
87$(LIBRARY_PATH)/$(LIBRARY_NAME).a: $(LIBOBJS)
88 @echo "[AR StO] $@"
89 @$(AR) rcs $@ $^
90
91$(LIBRARY_PATH)/$(LIBRARY_NAME).so: $(LIBOBJS)
92 @echo "[LD ShO] $@"
93 @$(LD) $(LIBCFLAGS) $(LIBLDFLAGS) $^ -o $@
94
95#$(BINARY_NAME): $(OBJS) $(LIBOBJS)
96# @echo "[LD ] $@"
97# @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
98
99$(BINARY_NAME): $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
100 @echo "[LD ] $@"
101 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
102
103$(BINARY_NAME).static: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).a
104 @echo "[LD ] $@"
105 @$(LD) $(CFLAGS) $(STATICLIBLDFLAGS) $^ $(LDLIBS) -o $@
106
107$(BINARY_NAME).dynamic: $(OBJS) $(LIBRARY_PATH)/$(LIBRARY_NAME).so
108 @echo "[LD ] $@"
109 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
110
111$(OBJDIR)/%.o: $(SRC_PATH)/%.c $(DEPDIR)/%.d
112 @echo "[C ] $(notdir $*)"
113 @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
114 @$(POSTCOMPILE)
115
116include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
117
118$(OBJDIR)/%.o: $(LIBRARY_PATH)/%.c $(DEPDIR)/%.d
119 @echo "[C ] $(notdir $*)"
120 @$(CC) $(DEPFLAGS) $(LIBCFLAGS) -c $< -o $@
121 @$(POSTCOMPILE)
122
123include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(LIBSRCS))))
124
125$(DEPDIR)/%.d: ;
126
127.PRECIOUS: $(DEPDIR)/%.d
128
129#FIXME: add an install target
130
131clean:
132 @echo "[CLN]"
133 -@rm -r $(BUILDDIR)
134 -@rm $(BINARY_NAME)
135 -@rm $(BINARY_NAME).static
136 -@rm $(BINARY_NAME).dynamic
137 -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).a
138 -@rm $(LIBRARY_PATH)/$(LIBRARY_NAME).so
139
140disassemble: $(BINARY_NAME)
141 objdump -d $< | less
142
143symbols: $(BINARY_NAME)
144 objdump -t $< | sort | less