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