-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
58 lines (43 loc) · 1.45 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
DEBUG ?= 0
LLD ?= 1
ASAN ?= 0
ELF := relocgen.elf
CC := clang
INC := -I relocgen/include -I lib
WARNINGS := -Wall -Wextra -Wpedantic -Wshadow -Werror=implicit-function-declaration -Wvla
CFLAGS := -std=c11
LDFLAGS :=
ifeq ($(DEBUG),0)
OPTFLAGS := -O2
CFLAGS += -Werror
else
OPTFLAGS := -O0 -g3
endif
ifneq ($(ASAN),0)
CFLAGS += -fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=undefined
endif
ifneq ($(LLD),0)
LDFLAGS += -fuse-ld=lld
endif
SRC_DIRS := $(shell find relocgen -type d)
C_FILES := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c))
H_FILES := $(foreach dir,$(INC),$(wildcard $(dir)/*.h))
O_FILES := $(foreach f,$(C_FILES:.c=.o),build/$f)
LIB_DIRS := $(shell find lib -type d)
C_LIB_FILES := $(foreach dir,$(LIB_DIRS),$(wildcard $(dir)/*.c))
O_LIB_FILES := $(foreach f,$(C_LIB_FILES:.c=.o),build/$f)
# Main targets
all: $(ELF)
clean:
$(RM) -r build $(ELF)
format:
clang-format-11 -i $(C_FILES) $(H_FILES)
.PHONY: all clean format
# create build directories
$(shell mkdir -p $(foreach dir,$(SRC_DIRS),build/$(dir)) $(foreach dir,$(LIB_DIRS),build/$(dir)))
$(ELF): $(O_FILES) $(O_LIB_FILES)
$(CC) $(INC) $(WARNINGS) $(CFLAGS) $(OPTFLAGS) $(LDFLAGS) -o $@ $^
build/%.o: %.c $(H_FILES)
$(CC) -c $(INC) $(WARNINGS) $(CFLAGS) $(OPTFLAGS) -o $@ $<
build/lib/%.o: lib/%.c
$(CC) -c $(INC) $(WARNINGS) $(CFLAGS) $(OPTFLAGS) -o $@ $<