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