-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMakefile
242 lines (187 loc) ยท 8.38 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -----------------------------------------------------------------------------
#
# C+Python Project Makefile
# =========================
#
# Updated: 2016-11-15
# Author: FFunction <ffctn.com>
#
# -----------------------------------------------------------------------------#
# NOTE: To do profiling, use operf & opreport
# === CONFIGURATION ===========================================================
PROJECT :=parsing
PYMODULE :=lib$(PROJECT)
FEATURES :=pcre fortify gc
ALL_FEATURES :=pcre memcheck debug trace fortify gc assert
# === FEATURES ================================================================
LIBS=
ifneq (,$(findstring pcre,$(FEATURES)))
LIBS +=libpcre
endif
ifneq (,$(findstring python2,$(FEATURES)))
LIBS +=python2
CFLAGS+=-DWITH_PYTHON
PYTHON=python2
endif
ifneq (,$(findstring python3,$(FEATURES)))
LIBS +=python3
CFLAGS+=-DWITH_PYTHON
PYTHON=python3
endif
ifeq (,$(findstring assert,$(FEATURES)))
CFLAGS+=-DNDEBUG
endif
ifneq (,$(findstring debug,$(FEATURES)))
CFLAGS+=-Og
else
CFLAGS+=-O3
endif
ifneq (,$(findstring fortify,$(FEATURES)))
CFLAGS+= -U_FORTIFY_SOURCE -fstack-protector-all
endif
# === PATHS ===================================================================
BUILD =.build
DIST =dist
SOURCES =src
TESTS =test
# === TOOLS ===================================================================
PYTHON ?=python3.7
# === SOURCES =================================================================
SOURCES_C =$(wildcard $(SOURCES)/c/*.c)
SOURCES_H =$(wildcard $(SOURCES)/h/*.h)
SOURCES_PY =$(wildcard $(SOURCES)/py/*.py) $(wildcard $(SOURCES)/py/*/*.py)
TESTS_C =$(wildcard $(TESTS)/*.c)
TESTS_PY =$(wildcard $(TESTS)/*.py)
# === BUILD FILES =============================================================
BUILD_SOURCES_O =$(SOURCES_C:$(SOURCES)/c/%.c=$(BUILD)/%.o)
BUILD_TESTS_O =$(TESTS_C:$(TESTS)/%.c=$(BUILD)/%.o)
BUILD_O =$(BUILD_SOURCES_O) $(BUILD_TESTS_O)
BUILD_PY_SO =$(SOURCES)/python/lib$(PROJECT)/lib$(PROJECT).so \
$(SOURCES)/python/lib$(PROJECT)/_lib$(PROJECT).so \
BUILD_PY_FFI =$(SOURCES)/python/lib$(PROJECT)/_lib$(PROJECT).ffi \
$(SOURCES)/python/lib$(PROJECT)/_lib$(PROJECT).c
BUILD_ALL =$(BUILD_O) $(BUILD_PY_SO) $(BUILD_PY_FFI)
# === DIST FILES ==============================================================
DIST_TESTS = $(TESTS_C:$(TESTS)/%.c=$(DIST)/%)
DIST_BIN = $(DIST_TESTS)
DIST_SO = $(DIST)/lib$(PROJECT).so $(DIST)/lib$(PROJECT).so.$(VERSION)
DIST_ALL = $(DIST_BIN) $(DIST_SO)
PRODUCTS = $(DIST_ALL)
# === COMPILER FILES ==========================================================
CC ?= gcc
CFEATURES:=$(shell echo $(FEATURES:%=-DWITH_%) | tr a-z A-Z)
CFLAGS +=$(shell pkg-config --cflags $(LIBS))
CFLAGS +=-I$(SOURCES)/h -Wall -fPIC $(CFEATURES) -g #-DMEMCHECK_ENABLED -pg # -DDEBUG_ENABLED -DTRACE_ENABLED
LDFLAGS +=$(shell pkg-config --cflags --libs $(LIBS))
# === DEPENDENCY MANAGEMENT ===================================================
# SEE: http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
DEPDIR := .build/d
$(shell mkdir -p $(DEPDIR) >/dev/null)
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.Td
COMPILE.c = $(CC) $(DEPFLAGS) $(CFLAGS) $(TARGET_ARCH) -c
POSTCOMPILE = mv -f $(DEPDIR)/$*.Td $(DEPDIR)/$*.d
# === META ====================================================================
VERSION :=$(shell grep VERSION $(SOURCES)/h/parsing.h | cut -d'"' -f2)
MAJOR :=$(shell echo $(VERSION) | cut -d. -f1)
# === HELPERS =================================================================
YELLOW =`tput setaf 11`
GREEN =`tput setaf 10`
CYAN =`tput setaf 14`
RED =`tput setaf 1`
GRAY =`tput setaf 7`
RESET =`tput sgr0`
TIMESTAMP :=$(shell date +'%F')
BUILD_ID :=$(shell git rev-parse --verify HEAD)
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
MAKEFILE_DIR := $(notdir $(patsubst %/,%,$(dir $(MAKEFILE_PATH))))
# From: http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.DEFAULT_GOAL :=all
.PHONY : all info dist release tests update-python-version check clean help
# =============================================================================
# MAIN RULES
# =============================================================================
all: $(PRODUCTS) $(BUILD_ALL) ## Builds all the products
info: ## Displays information about the project
@echo libparsing: $(VERSION)
dist: $(PRODUCT) $(DIST_FILES) ## Creates source and binary Python distributions
$(PYTHON) setup.py check clean sdist bdist
release: $(PRODUCT) update-python-version $(SOURCES)/python/lib$(PROJECT)/_lib$(PROJECT).so
@echo "$(CYAN)๐ฆ dist: $(RESET)"
$(PYTHON) setup.py check clean
git commit -a -m "Release $(VERSION)" ; true
git tag $(VERSION) ; true
git push --all ; true
$(PYTHON) setup.py sdist bdist register upload
tests: $(TEST_PRODUCTS)
ffi: $(SOURCES)/alt$(PROJECT)/$(PROJECT).ffi ## Re-generates the FFI interface
update-python-version: $(SOURCES)/h/parsing.h
sed -i 's/VERSION \+= *"[^"]\+"/VERSION = "$(VERSION)"/' $(SOURCES)/python/$(PYMODULE)/__init__.py
sed -i 's/VERSION \+= *"[^"]\+"/VERSION = "$(VERSION)"/' setup.py
check: $(SOURCES_C) $(SOURCES_H) ## Runs checks on the source code
@# SEE: http://sourceforge.net/p/cppcheck/wiki/ListOfChecks/
@echo "$(CYAN)๐ฆ Checking: $(SOURCES_C) $(RESET)"
@echo "$(RED)"
@cppcheck --suppress=unusedFunction -Isrc/h --enable=all $(SOURCES_C)
@echo "$(RESET)"
@echo "$(CYAN)๐ฆ Checking: $(SOURCES_PY) $(RESET)"
@echo "$(RED)"
@pychecker $(SOURCES_PY)
@echo "$(RESET)"
clean: ## Cleans the build files
@find . -name __pycache__ -exec rm -rf '{}' ';' ; true
@echo $(PRODUCTS) $(BUILD_ALL) | xargs -n1 rm 2> /dev/null ; true
@test -d $(DIST) && rm -rf $(DIST) ; true
help: ## Displays a description of the different Makefile rules
@echo "$(CYAN)โ
โ
โ
$(PROJECT) makefile โ
โ
โ
$(RESET)"
@grep -E -o '((\w|-)+):[^#]+(##.*)$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":|##"}; {printf "make \033[01;32m%-15s\033[0m๐ฎ %s\n", $$1, $$3}'
# =============================================================================
# PRODUCTS
# =============================================================================
$(DIST)/lib$(PROJECT).so: $(BUILD_SOURCES_O)
@echo "$(GREEN)๐ $@ [SO]$(RESET)"
@mkdir -p `dirname $@`
@echo "$(CYAN)โ " $(BUILD_SOURCES_O) "$(RESET)"
$(LD) -shared $(LDFLAGS) $(BUILD_SOURCES_O) -o $@
$(DIST)/lib$(PROJECT).so.$(VERSION): $(DIST)/lib$(PROJECT).so
@echo "$(GREEN)๐ $@ [SO $(VERSION)]$(RESET)"
@cp $< $@
$(DIST)/c-%: $(BUILD)/c-%.o $(SOURCES_O) $(DIST)/lib$(PROJECT).so
@echo "$(GREEN)๐ $@ [EXE]$(RESET)"
@mkdir -p `dirname $@`
$(CC) -L$(DIST) -l$(PROJECT) $(LDFLAGS) $(OUTPUT_OPTION) $?
chmod +x $@
# =============================================================================
# PYTHON MODULE
# =============================================================================
$(SOURCES)/python/lib$(PROJECT)/lib$(PROJECT).so: $(DIST)/lib$(PROJECT).so
@echo "$(GREEN)๐ $@ [PYTHON SO]$(RESET)"
@cp $< $@
$(SOURCES)/python/lib$(PROJECT)/_libparsing.ffi: $(SOURCES)/h/$(PROJECT).h
@echo "$(GREEN)๐ $@ [FFI]$(RESET)"
@mkdir -p `dirname $@`
./bin/ffigen.py $< > $@
$(SOURCES)/python/lib$(PROJECT)/_libparsing.c: $(SOURCES_C) $(SOURCES_H)
$(CC) $(CFLAGS) -E -DWITH_CFFI $(SOURCES_C) | egrep -v '^#' > $@
$(SOURCES)/python/lib$(PROJECT)/_libparsing.so: $(BUILD_PY_FFI)
$(PYTHON) $(SOURCES)/python/lib$(PROJECT)/_buildext.py $@
# =============================================================================
# OBJECTS
# =============================================================================
$(BUILD)/c-%.o: $(TESTS)/c-%.c $(SOURCES_H) $(DIST)/lib$(PROJECT).so Makefile
@echo "$(GREEN)๐ $@ [C TEST]$(RESET)"
@mkdir -p `dirname $@`
$(COMPILE.c) -shared -Og -g $(OUTPUT_OPTION) $<
$(BUILD)/%.o: $(SOURCES)/c/%.c $(DEPDIR)/%.d Makefile
@echo "$(GREEN)๐ $@ [C SOURCE]$(RESET)"
@mkdir -p `dirname $@`
$(COMPILE.c) $(OUTPUT_OPTION) $<
$(DEPDIR)/%.d: ;
.PRECIOUS: $(DEPDIR)/%.d
# === HELPERS =================================================================
print-%:
@echo $*=
@echo $($*) | xargs -n1 echo | sort -dr
clean-%:
@echo $($*) | xargs -n1 echo | sort -dr | xargs -n1 rm
-include $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))
# EOF