Commit | Line | Data |
---|---|---|
a0a33bfa 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=exam2 | |
15 | BUILD_TYPE=debug | |
16 | ||
17 | # ==================================== | |
18 | # DO NOT CHANGE STUFF BEYOND THIS LINE | |
19 | # ==================================== | |
20 | ||
21 | all: $(BINARY_NAME) | |
22 | ||
23 | CC=gcc | |
24 | LD=gcc | |
25 | ||
26 | WARN_FLAGS = -Wall -Wextra | |
27 | STD_FLAG = -std=c11 | |
28 | ||
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 | OPTI_FLAG = -O3 | |
39 | endif | |
40 | ||
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)))) | |
69 | ||
70 | clean: | |
71 | @echo "[CLN]" | |
72 | -@rm -r $(BUILDDIR) | |
73 | -@rm $(BINARY_NAME) | |
74 | ||
75 | disassemble: $(BINARY_NAME) | |
76 | objdump -d $< | less | |
77 | ||
78 | symbols: $(BINARY_NAME) | |
79 | objdump -t $< | sort | less |