-
Notifications
You must be signed in to change notification settings - Fork 2
/
makefile
123 lines (95 loc) · 2.98 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
#########################################################################
#
# plain 'make' builds 3 files: a shared .so, a static .a
# and, from the objects compiled with -fPIC, a static *pic.a
#
# 'make test' compiles and runs an additional test file
#
# a compiler with c++11 support is required
# tested with gcc-4.8 and clang-3.2
debug = 0
libname = srl
out = bin
cache = cache
CXXFLAGS = -std=c++17 -Wfatal-errors
CFLAGS = -std=c99 -O3
ifeq ($(debug), 1)
CXXFLAGS += -g -O0 -DDEBUG -pedantic -Wall -Wextra -Wshadow -ftrapv -Wcast-align
out := $(out)/debug
cache := $(cache)/debug
else
CXXFLAGS += -O3 -DNDEBUG
endif
header = -Iinclude
libsrcdir = src/lib
fpconvdir = $(libsrcdir)/fpconv
testsrcdir = src/tests
testfile = tests
linklibs =
uname = $(shell uname -s)
ifeq ($(uname), Darwin)
linklibs += -liconvpair
endif
########################################################################
lib = $(out)/lib$(libname)
libsrcs = $(patsubst %.cpp,%.o, $(wildcard $(libsrcdir)/*.cpp))
libsrcs += $(patsubst %.c,%.o, $(wildcard $(fpconvdir)/*.c))
libobjs_pic = $(patsubst %.o,$(cache)/pic/%.o, $(libsrcs))
libobjs_pdc = $(patsubst %.o,$(cache)/pdc/%.o, $(libsrcs))
testsrcs = $(wildcard $(testsrcdir)/*.cpp)
testobjs = $(patsubst %.cpp,$(cache)/pdc/%.o, $(testsrcs))
allobjects = $(libobjs_pic) $(libobjs_pdc) $(testobjs)
.PHONY: clean distclean print
all: print $(lib).so $(lib)pic.a $(lib).a
@echo "...complete."
install:
@echo "No install routine provided.\nYou can find the compiled libs in $(out)/ after running 'make'."
@echo "'cat makefile' for further info."
print:
@echo "building $(out)/$(libname) with...\n\t$(CXX) $(CXXFLAGS)"
# shared lib
$(lib).so: $(libobjs_pic)
@echo "\tlinking $(lib).so"
@$(CXX) $(CXXFLAGS) -shared -o $(lib).so $(libobjs_pic) $(linklibs)
# static lib w/ -fPIC
$(lib)pic.a: $(libobjs_pic)
@echo "\tpacking $(lib)pic.a"
@$(AR) rcs $(lib)pic.a $(libobjs_pic)
# static lib w/o -fPIC
$(lib).a: $(libobjs_pdc)
@echo "\tpacking $(lib).a"
@$(AR) rcs $(lib).a $(libobjs_pdc)
# compile files
$(cache)/pic/%.o: %.cpp
@$(call compile,$(CXX),$(CXXFLAGS) -fPIC,$@,$<)
$(cache)/pdc/%.o: %.cpp
@$(call compile,$(CXX),$(CXXFLAGS),$@,$<)
$(cache)/pic/%.o: %.c
@$(call compile,$(CC),$(CFLAGS) -fPIC,$@,$<)
$(cache)/pdc/%.o: %.c
@$(call compile,$(CC),$(CFLAGS),$@,$<)
# build and run test
test: print run
@echo "...complete."
run: $(out)/$(testfile)
@echo "\tRunning $(out)/$(testfile)"
@./$(out)/$(testfile)
$(out)/$(testfile): $(lib).a $(testobjs)
@echo "\tlinking $(out)/$(testfile)"
@$(CXX) $(CXXFLAGS) -o $(out)/$(testfile) $(testobjs) $(lib).a $(linklibs)
# $1 -> compiler $2 -> flags $3 -> output object-file $4 -> source-file
define compile
mkdir -p $(out)
mkdir -p $(dir $3)
echo "\tcompiling $3"
#compile and generate dependencies
$1 $2 $4 $(header) -MMD -MP -c -o $3
endef
# include dependencies
-include $(allobjects:%.o=%.d)
clean:
@rm -r -f $(cache)
@echo "clean"
distclean: clean
@rm -r -f $(out)
@echo "distclean"