Add more code snippet from the course.
[TD_SE.git] / prodcons / Makefile
CommitLineData
66c9107f
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# current working directory.
5#
6# It will automatically generate dependencies, compile all files, and produce a
7# binary using the provided name.
8#
9# Set BINARY_NAME to the name of the binary file to build.
10# Set BUILD_TYPE to either debug or release
11#
12# Automatic dependencies code from:
13# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
14BINARY_NAME=prodcons
15BUILD_TYPE=debug
16#BUILD_TYPE=release
17LDLIBS=-lpthread
18
19# ====================================
20# DO NOT CHANGE STUFF BEYOND THIS LINE
21# ====================================
22
23all: $(BINARY_NAME)
24
25CC=gcc
26LD=gcc
27
28WARN_FLAGS = -Wall -Wextra
29STD_FLAG = -std=c11
30
31ifeq ($(BUILD_TYPE),debug)
32BUILDDIR := .build/debug
33DEBUG_FLAG = -g
34DEBUG = 1
35STRIP_FLAG =
36OPTI_FLAG = -O0
37else
38BUILDDIR := .build/release
39DEBUG_FLAG =
40DEBUG = 0
41STRIP_FLAG = -s
42OPTI_FLAG = -O3
43endif
44
45CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG)
46LDFLAGS := $(LDFLAGS) $(STRIP_FLAG)
47
48OBJDIR := $(BUILDDIR)/objs
49$(shell mkdir -p $(OBJDIR))
50
51SRCS=$(wildcard *.c)
52OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS))
53
54DEPDIR := $(BUILDDIR)/deps
55$(shell mkdir -p $(DEPDIR))
56DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
57POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
58
59$(BINARY_NAME): $(OBJS)
60 @echo "[LD ] $@"
61 @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@
62
63$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
64 @echo "[C ] $*"
65 @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
66 @$(POSTCOMPILE)
67
68$(DEPDIR)/%.d: ;
69
70.PRECIOUS: $(DEPDIR)/%.d
71
72include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))))
73
74clean:
75 @echo "[CLN]"
76 -@rm -r $(BUILDDIR)
77 -@rm $(BINARY_NAME)
78
79disassemble: $(BINARY_NAME)
80 objdump -d $< | less
81
82symbols: $(BINARY_NAME)
83 objdump -t $< | sort | less