forked from Yamakuzure/xfs_undelete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
215 lines (184 loc) · 6.46 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
.PHONY: all clean help
VERSION := 0.0.1
AUTHORS := sed
# ------------------------------------
# Address and leak sanitizers can be
# combined.
# ------------------------------------
SANITIZE_ADDRESS ?= NO
SANITIZE_LEAK ?= NO
# ------------------------------------
# For the sanitizers to work, we need
# to enable debugging
# ------------------------------------
DEBUG ?= NO
ifeq "$(SANITIZE_ADDRESS)" "YES"
DEBUG := YES
endif
ifeq "$(SANITIZE_LEAK)" "YES"
DEBUG := YES
endif
# ------------------------------------
# The sources and includes are in...
# ------------------------------------
PROJECT_DIR := $(shell pwd -P)
DEPDIR := dep
OBJDIR := obj
INCLUDE :=
# -----------------------------------------------------------------------------
# Tools to use
# -----------------------------------------------------------------------------
CC := $(shell which cc)
LD := gcc
MKDIR ?= $(shell which mkdir) -p
RM := $(shell which rm) -f
SED ?= $(shell which sed)
# ------------------------------------
# Target name
# ------------------------------------
TARGET := $(shell basename $(PROJECT_DIR))
# Append '_d' on debug builds
ifeq (YES,$(DEBUG))
TARGET := ${TARGET}_d
endif
# -----------------------------------------------------------------------------
# Store already set flags and append later
# -----------------------------------------------------------------------------
caller_CPPFLAGS := $(CPPFLAGS)
caller_CFLAGS := $(CFLAGS)
caller_LDFLAGS := $(LDFLAGS)
CPPFLAGS :=
CFLAGS :=
LDFLAGS :=
# -----------------------------------------------------------------------------
# Default flags
# -----------------------------------------------------------------------------
GCC_STACKPROT := -fstack-protector-strong
GCC_CSTD := c11
COMMON_FLAGS := -Wall -Wextra -Wpedantic -Wno-format
# -----------------------------------------------------------------------------
# Determine proper pedantic switch, and if and how stack protector works
# -----------------------------------------------------------------------------
GCCVERSGTEQ82 := 0
ifeq (,$(findstring clang,$(CC)))
GCCVERSGTEQ82 := $(shell expr `$(CC) -dumpversion | cut -f1,2 -d. | tr -d '.'` \>= 82)
endif
ifeq "$(GCCVERSGTEQ82)" "1"
GCC_CSTD := c18
else
GCC_CSTD := c11
endif
# -----------------------------------------------------------------------------
# Debug Mode settings
# -----------------------------------------------------------------------------
HAS_DEBUG_FLAG := NO
OBJ_SUB_DIR := release
ifneq (,$(findstring -g,$(CFLAGS)))
ifneq (,$(findstring -ggdb,$(CFLAGS)))
HAS_DEBUG_FLAG := YES
endif
DEBUG := YES
endif
ifeq (YES,$(DEBUG))
ifeq (NO,$(HAS_DEBUG_FLAG))
COMMON_FLAGS := -ggdb ${COMMON_FLAGS} -O0
endif
CPPFLAGS := ${CPPFLAGS} -DPWX_DEBUG
COMMON_FLAGS := -march=core2 -mtune=generic ${COMMON_FLAGS} ${GCC_STACKPROT}
HAVE_SANITIZER := NO
# address sanitizer activition
ifeq (YES,$(SANITIZE_ADDRESS))
COMMON_FLAGS := ${COMMON_FLAGS} -fsanitize=address
LDFLAGS := ${LDFLAGS} -fsanitize=address
HAVE_SANITIZER := YES
endif
# Leak detector activation
ifeq (YES,$(SANITIZE_LEAK))
COMMON_FLAGS := ${COMMON_FLAGS} -fsanitize=leak
LDFLAGS := ${LDFLAGS} -fsanitize=leak
HAVE_SANITIZER := YES
endif
ifeq (NO,$(HAVE_SANITIZER))
# If no other sanitizer was set, we check for the thread sanitizer
ifeq (YES,$(SANITIZE_THREAD))
COMMON_FLAGS := ${COMMON_FLAGS} -fsanitize=thread
LDFLAGS := ${LDFLAGS} -fsanitize=thread
endif
endif
# Put objects in debug folder
OBJ_SUB_DIR := debug
else
COMMON_FLAGS := -march=native ${COMMON_FLAGS} -O2 -Wno-unused-parameter
endif
OBJDIR := ${OBJDIR}/${OBJ_SUB_DIR}
DEPDIR := ${DEPDIR}/${OBJ_SUB_DIR}
# -----------------------------------------------------------------------------
# Source files and directories
# -----------------------------------------------------------------------------
SOURCES := $(wildcard $(PROJECT_DIR)/src/*.c)
DEPENDS := $(addprefix $(DEPDIR)/,$(subst $(PROJECT_DIR)/src/, ,$(SOURCES:.c=.d)))
MODULES := $(addprefix $(OBJDIR)/,$(subst $(PROJECT_DIR)/src/, ,$(SOURCES:.c=.o)))
# -----------------------------------------------------------------------------
# Flags for compiler and linker
# -----------------------------------------------------------------------------
DEFINES := -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE -DNO_GLOG -D_GNU_SOURCE
CPPFLAGS := -fPIC ${CPPFLAGS} $(DEFINES) $(INCLUDE)
CFLAGS := $(COMMON_FLAGS) -std=$(GCC_CSTD) -pthread
LDFLAGS := -fPIE ${LDFLAGS} -lpthread
# -----------------------------------------------------------------------------
# Eventually event caller flags for overrides
# -----------------------------------------------------------------------------
CPPFLAGS := ${CPPFLAGS} $(caller_CPPFLAGS)
CFLAGS := ${CFLAGS} $(caller_CFLAGS)
LDFLAGS := ${LDFLAGS} $(caller_LDFLAGS)
# ------------------------------------
# Default target
# ------------------------------------
all: $(TARGET)
# ------------------------------------
# Compile C modules
# ------------------------------------
$(MODULES): $(OBJDIR)/%.o: src/%.c Makefile
@echo "[*] Compiling $@" ; \
$(MKDIR) $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ -c $<
# ------------------------------------
# help target to print settings
# ------------------------------------
help:
@echo "$(TARGET) Version $(VERSION) settings:"
@echo " PROJECT_DIR: $(PROJECT_DIR)"
@echo " CC : $(CC)"
@echo " LD : $(LD)"
@echo " RM : $(RM)"
@echo " SED : $(SED)"
@echo " CPPFLAGS : $(CPPFLAGS)"
@echo " CFLAGS : $(CFLAGS)"
@echo " LDFLAGS : $(LDFLAGS)"
@echo " DEPENDS : $(DEPENDS)"
@echo " MODULES : $(MODULES)"
# ------------------------------------
# Regular targets
# ------------------------------------
$(TARGET): $(MODULES)
@echo "[*] Linking $@"
$(LD) -o $@ $(MODULES) $(LDFLAGS)
clean:
@echo "[*] Performing clean..."
$(RM) $(MODULES) $(TARGET)
# ------------------------------------
# Create dependencies
# ------------------------------------
$(DEPDIR)/%.d: src/%.c
@set -e; $(RM) $@; \
$(MKDIR) $(dir $@); \
$(CC) -MM $(CPPFLAGS) $(DEFINES) $(CFLAGS) $< > $@.$$$$; \
$(SED) -e 's,\($(notdir $*)\)\.o[ :]*,$(OBJDIR)/$(dir $*)\1.o $@ : ,g' \
-e 's,$(PROJECT_DIR)/,,g' < $@.$$$$ > $@; \
$(RM) $@.$$$$
# ------------------------------------
# Include all dependency files
# ------------------------------------
ifeq (,$(findstring clean,$(MAKECMDGOALS)))
-include $(DEPENDS)
endif