Commit | Line | Data |
---|---|---|
f12bd30b 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 | |
14 | BINARY_NAME=sqp | |
15 | #BUILD_TYPE=debug | |
16 | BUILD_TYPE=release | |
17 | ||
18 | # ==================================== | |
19 | # DO NOT CHANGE STUFF BEYOND THIS LINE | |
20 | # ==================================== | |
21 | ||
22 | all: $(BINARY_NAME) | |
23 | ||
24 | CC=gcc | |
25 | LD=gcc | |
26 | ||
27 | WARN_FLAGS = -Wall -Wextra | |
28 | STD_FLAG = -std=c11 | |
29 | ||
30 | ifeq ($(BUILD_TYPE),debug) | |
31 | BUILDDIR := .build/debug | |
32 | DEBUG_FLAG = -g | |
33 | DEBUG = 1 | |
34 | STRIP_FLAG = | |
35 | OPTI_FLAG = -O0 | |
36 | else | |
37 | BUILDDIR := .build/release | |
38 | DEBUG_FLAG = | |
39 | DEBUG = 0 | |
40 | STRIP_FLAG = -s | |
41 | OPTI_FLAG = -O3 | |
42 | endif | |
43 | ||
44 | CFLAGS := -DDEBUG=$(DEBUG) $(CFLAGS) $(WARN_FLAGS) $(STD_FLAG) $(OPTI_FLAG) $(DEBUG_FLAG) | |
45 | LDFLAGS := $(LDFLAGS) $(STRIP_FLAG) | |
46 | ||
47 | OBJDIR := $(BUILDDIR)/objs | |
48 | $(shell mkdir -p $(OBJDIR)) | |
49 | ||
50 | SRCS=$(wildcard *.c) | |
51 | OBJS=$(patsubst %.c,$(OBJDIR)/%.o,$(SRCS)) | |
52 | ||
53 | DEPDIR := $(BUILDDIR)/deps | |
54 | $(shell mkdir -p $(DEPDIR)) | |
55 | DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td | |
56 | POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d | |
57 | ||
58 | $(BINARY_NAME): $(OBJS) | |
59 | @echo "[LD ] $@" | |
60 | @$(LD) $(CFLAGS) $(LDFLAGS) $^ $(LDLIBS) -o $@ | |
61 | ||
62 | $(OBJDIR)/%.o: %.c $(DEPDIR)/%.d | |
63 | @echo "[C ] $*" | |
64 | @$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@ | |
65 | @$(POSTCOMPILE) | |
66 | ||
67 | $(DEPDIR)/%.d: ; | |
68 | ||
69 | .PRECIOUS: $(DEPDIR)/%.d | |
70 | ||
71 | include $(wildcard $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))) | |
72 | ||
73 | clean: | |
74 | @echo "[CLN]" | |
75 | -@rm -r $(BUILDDIR) | |
76 | -@rm $(BINARY_NAME) | |
77 | ||
78 | disassemble: $(BINARY_NAME) | |
79 | objdump -d $< | less | |
80 | ||
81 | symbols: $(BINARY_NAME) | |
82 | objdump -t $< | sort | less |