-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
161 lines (125 loc) · 3.75 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
.PHONY: prepare
.PHONY: clean
.PHONY: install
.PHONY: uninstall
PROJECT_NAME := ipinfo
DEBUG_BUILD_MODE := 1
OBJ_DIR := obj
SRC_DIR := src
INCLUDE_DIR := include
TARGET_DIR := target
MAJOR_LIB_VER := 1
MINOR_LIB_VER := 0
PATCH_LIB_VER := 0
FULL_LIB_VER := $(MAJOR_LIB_VER).$(MINOR_LIB_VER).$(PATCH_LIB_VER)
LIB_FILE_EXTENSION := so
LIB_FILE_NAME_NO_VER := lib$(PROJECT_NAME).$(LIB_FILE_EXTENSION)
TARGET := $(TARGET_DIR)/$(LIB_FILE_NAME_NO_VER).$(FULL_LIB_VER)
SOURCE_CODE_FILES := \
$(shell find $(SRC_DIR)/ipinfo \
-iname "*.cpp" \
-type f \
-printf "%P ")
OBJECT_CODE_FILES := $(SOURCE_CODE_FILES:%=$(OBJ_DIR)/%.o)
PREFIX ?= /usr/local
INSTALL_LIB_DIR := $(DESTDIR)$(PREFIX)/lib
INSTALL_INCLUDE_DIR := $(DESTDIR)$(PREFIX)/include/ipinfo
INSTALL_LIB_FILE_RIGHTS := 755
INSTALL_HEADER_FILES_RIGHTS := 644
INSTALL_LIB_FILE := $(TARGET)
UNINSTALL_LIB_FILE := $(INSTALL_LIB_DIR)/$(shell basename $(INSTALL_LIB_FILE))
# There are only headers needed
# by user will be installed.
INSTALL_HEADER_FILES := \
$(shell find $(INCLUDE_DIR)/ipinfo \
\( ! -name "*requester*" \) -and \
\( ! -name "*parser*" \) -and \
\( ! -name "*utiler*" \) -and \
-iname "*.hpp" -type f -printf "%p ")
CXX := g++
CXXFLAGS := -std=c++20
CXXFLAGS += -Wall
CXXFLAGS += -Wextra
CXXFLAGS += -Wpedantic
CXXFLAGS += -Wconversion
CXXFLAGS += -Wunreachable-code
CXXFLAGS += -Wsign-conversion
CXXFLAGS += -Wlogical-op
CXXFLAGS += -pipe
ifeq ($(DEBUG_BUILD_MODE), 1)
CXXFLAGS += -g3
CXXFLAGS += -O0
CXXFLAGS += -DIPINFO_DEBUG_BUILD_MODE
else
CXXFLAGS += -Os
CXXFLAGS += -flto
CXXFLAGS += -march=native
endif
LDFLAGS := -Wl,-rpath=$(PREFIX)/lib
LDFLAGS += -Wl,-rpath=./lib
LDLIBS := -lcjson
LDLIBS += -lcpr
create_dir = @ (test -d $(1)) || mkdir -p $(1)
remove_dir = @ (test -d $(1) && rm -r $(1)) || true
create_link = @ ln -sf $(1) $(2)
remove_link = @ (test -L $(1) && unlink $(1)) || true
remove_file = @ (test -f $(1) && rm $(1)) || true
all: build
build: prepare $(TARGET)
$(info build of the $(shell basename $(TARGET)) is completed)
$(TARGET): $(OBJECT_CODE_FILES)
@ $(foreach obj, \
$(shell find $(OBJ_DIR) -iname "*.cpp.o" -type f), \
$(info link $(shell basename $(obj))))
@ $(CXX) \
$(LDFLAGS) \
$(LDLIBS) \
-shared \
$^ \
-o $@
$(OBJ_DIR)/%.cpp.o: $(SRC_DIR)/$(PROJECT_NAME)/%.cpp
$(info compile $(shell basename $<))
@ $(CXX) \
$(CXXFLAGS) \
-I$(INCLUDE_DIR) \
-fPIC \
-c $< \
-o $@
prepare:
$(info create build directories)
$(call create_dir,$(OBJ_DIR))
$(call create_dir,$(TARGET_DIR))
clean:
$(info remove build diectories)
$(call remove_dir,$(OBJ_DIR))
$(call remove_dir,$(TARGET_DIR))
install: build
$(info create installation folders)
$(call create_dir,$(INSTALL_LIB_DIR))
$(call create_dir,$(INSTALL_INCLUDE_DIR))
$(info install library to $(INSTALL_LIB_DIR))
@ install -Dm$(INSTALL_LIB_FILE_RIGHTS) \
$(INSTALL_LIB_FILE) \
$(INSTALL_LIB_DIR)
$(info install headers to $(INSTALL_INCLUDE_DIR))
@ $(foreach header,\
$(INSTALL_HEADER_FILES),\
install -Dm=$(INSTALL_HEADER_FILES_RIGHTS) \
$(header) \
$(INSTALL_INCLUDE_DIR); )
$(info create soft links in $(INSTALL_LIB_DIR))
$(call create_link,\
$(INSTALL_LIB_DIR)/$(LIB_FILE_NAME_NO_VER).$(FULL_LIB_VER),\
$(INSTALL_LIB_DIR)/$(LIB_FILE_NAME_NO_VER).$(MAJOR_LIB_VER))
$(call create_link,\
$(INSTALL_LIB_DIR)/$(LIB_FILE_NAME_NO_VER).$(MAJOR_LIB_VER),\
$(INSTALL_LIB_DIR)/$(LIB_FILE_NAME_NO_VER))
uninstall:
$(info remove library: $(UNINSTALL_LIB_FILE))
$(call remove_file,$(UNINSTALL_LIB_FILE))
$(info remove headers directory: $(INSTALL_INCLUDE_DIR))
$(call remove_dir,$(INSTALL_INCLUDE_DIR))
$(info remove soft links from $(INSTALL_LIB_DIR))
$(call remove_link,\
$(INSTALL_LIB_DIR)/$(LIB_FILE_NAME_NO_VER).$(MAJOR_LIB_VER))
$(call remove_link,$(INSTALL_LIB_DIR)/$(LIB_FILE_NAME_NO_VER))