-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
83 lines (55 loc) · 1.44 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Variables for compilers and tools
# (only set if not already set)
GCC ?= /usr/bin/gcc
MKDIR ?= /bin/mkdir
CCFLAGS := -c
LDFLAGS :=
# Variables for input and output files and directories
INC_DIR ?= ./include
SRC_DIR ?= ./src
OBJ_DIR ?= ./obj
BIN_DIR ?= ./bin
PROG ?= snaek
# Debug/Release default configuration
DEBUG ?= 1
# Debug/Release configurations
ifeq ($(DEBUG),1)
OBJ_DIR := $(OBJ_DIR)/debug
BIN_DIR := $(BIN_DIR)/debug
CCFLAGS += -O0 -g -DDEBUG
else
OBJ_DIR := $(OBJ_DIR)/release
BIN_DIR := $(BIN_DIR)/release
CCFLAGS += -O3
endif
LIBRARIES :=
LIB_PATHS :=
INCLUDES :=
INCLUDES += $(INC_DIR)
# Inclusion of external headers and libraries
# Add headers and libraries to compiler/linker flags
CCFLAGS += $(foreach inc,$(INCLUDES),-I$(inc))
LDFLAGS += $(foreach path,$(LIB_PATHS),-L$(path))
LDFLAGS += $(foreach lib,$(LIBRARIES),-l$(lib))
# List all source files, corresponding object files, and their directories
CSRC := $(shell find $(SRC_DIR) -name \*.c)
SRC := $(CSRC)
COBJ := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.c.o,$(CSRC))
OBJ := $(COBJ)
DIRS := $(sort $(dir $(OBJ)) $(dir $(BIN_DIR)/$(PROG)))
# Targets
.PHONY: build clean
default: $(BIN_DIR)/$(PROG)
$(BIN_DIR)/$(PROG): $(OBJ)
$(GCC) $(LDFLAGS) $^ -o $@
$(OBJ): | $(DIRS)
$(OBJ_DIR)/%.c.o: $(SRC_DIR)/%.c
$(GCC) $(CCFLAGS) $^ -o $@
$(DIRS):
@$(MKDIR) -p $@ 2> /dev/null; true
echo:
@echo $(SRC)
@echo $(OBJ)
@echo $(DIRS)
clean:
@rm $(OBJ) $(PROG) 2> /dev/null; true