-
Notifications
You must be signed in to change notification settings - Fork 1
/
Makefile
143 lines (115 loc) · 3.49 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
TARGET = libmpool.so
include config.mk
# compilation options
DEBUG ?= 0
MEMCHECK ?= 0
ASAN ?= 0
TSAN ?= 0 # requires gcc >= 7 (gcc.gnu.org/bugzilla/show_bug.cgi?id=67308)
PREFIX ?= /usr
# use all available procs by default
NPROC ?= $(shell grep -c "processor" /proc/cpuinfo)
MAKEFLAGS += -j$(NPROC)
# env
CC ?= gcc
makefile_full_path := $(abspath $(lastword $(MAKEFILE_LIST)))
TOPDIR := $(patsubst %/,%,$(dir $(makefile_full_path)))
ifeq ($(CC),clang)
CFLAGS_WARN = -Weverything
CFLAGS_WARN += -Wno-padded
else
CFLAGS_WARN = -Wall -Wextra -pedantic -Wshadow -Wstrict-prototypes -Wmissing-prototypes
CFLAGS_WARN += -Wno-padded
endif
ifeq ($(DEBUG),1)
CFLAGS_DEBUG += -O0 -g
else
CFLAGS_DEBUG += -O3 -DNDEBUG
endif
ifeq ($(ASAN).$(TSAN), 1.1)
$(error "cannot enable both address and thread sanitizers")
endif
ifeq ($(ASAN).$(MEMCHECK), 1.1)
$(error "should not enable both address sanitizer and memcheck")
endif
ifeq ($(MEMCHECK), 1)
CFLAGS_MEMCHECK = -DMEMCHECK
endif
ifeq ($(ASAN), 1)
CFLAGS_ASAN = -fsanitize=address
LDFLAGS_ASAN = -lasan
endif
ifeq ($(TSAN), 1)
CFLAGS_TSAN = -fsanitize=thread
LDFLAGS_TSAN = -ltsan
endif
CFLAGS_ALL := $(CFLAGS_WARN) $(CFLAGS_DEBUG) $(CFLAGS_MEMCHECK) $(CFLAGS_ASAN) $(CFLAGS_TSAN)
LDFLAGS_ALL := $(LDFLAGS_ASAN) $(LDFLAGS_TSAN)
CPPFLAGS := -pipe -std=gnu11 -I$(TOPDIR)/src/ $(CPPFLAGS_CONFIG) $(CPPFLAGS)
CFLAGS := $(CFLAGS_ALL) $(CFLAGS)
LDFLAGS := $(LDFLAGS_ALL) $(LDFLAGS)
# source definitions
HEADERS = \
src/common.h \
src/mpool.h
SOURCES = \
src/mpool.c
OBJECTS = $(SOURCES:.c=.o)
$(OBJECTS): $(HEADERS)
.INTERMEDIATE: $(OBJECTS)
%.o: %.c
$(CC) $(CPPFLAGS) -shared -fPIC $(CFLAGS) -c -o $@ $<
$(TARGET): $(OBJECTS)
$(CC) -shared -fPIC $(CFLAGS) $(LDFLAGS) -o $(TARGET) $^
.PHONY: clean
clean: test_clean
-@rm -vf $(OBJECTS)
-@rm -vf $(TARGET)
# syntax check using clang static analyzer
.PHONY: syntax
syntax:
$(foreach file, $(SOURCES), \
clang -Weverything $(CPPFLAGS) $(CFLAGS) -fsyntax-only $(file);)
.PHONY: fixstyle
fixstyle: devtools/uncrustify.cfg
uncrustify -c $^ -l C --replace $(SOURCES) $(HEADERS)
.PHONY: install
install:
@mkdir -p $(PREFIX)/include $(PREFIX)/lib
@cp -vf $(TARGET) $(PREFIX)/lib
@cp -vf $(HEADERS) $(PREFIX)/include
.PHONY: uninstall
uninstall:
-@rm -vf $(PREFIX)/lib/$(TARGET)
-@$(foreach header, $(notdir $(HEADERS)), rm -vf $(PREFIX)/include/$(header);)
include test/test.mk
.PHONY: test
test: $(ALL_TESTS) $(TEST_MPOOL_OVERLOAD) $(TEST_SYSTEM_ALLOCS)
$(foreach test_sample, $(ALL_TESTS), \
LD_LIBRARY_PATH=$(TOPDIR) $(TOPDIR)/$(test_sample) || exit 1;)
.PHONY: check
check: test syntax
.PHONY: help
help:
@echo "# Targets:"
@echo "build - build application"
@echo "clean - clean"
@echo "syntax - run static analyzer"
@echo "test - run tests"
@echo "check - run static checks and functional tests"
@echo "install - install to $(PREFIX)"
@echo "fixstyle - fix coding style"
@echo
@echo "# Config:"
@echo "LOG2_CPU_CACHELINE_SIZE = $(CONFIG_LOG2_CPU_CACHELINE_SIZE)"
@echo "LOG2_CPU_PAGE_SIZE = $(CONFIG_LOG2_CPU_PAGE_SIZE)"
@echo
@echo "# Environment:"
@echo "CC = $(CC)"
@echo "PREFIX = $(PREFIX)"
@echo "DEBUG = $(DEBUG)"
@echo "MEMCHECK = $(MEMCHECK)"
@echo "ASAN = $(ASAN)"
@echo "TSAN = $(TSAN)"
.PHONY: all
all: $(TARGET)
.DEFAULT_GOAL := all