-
Notifications
You must be signed in to change notification settings - Fork 1
/
Make.rules
233 lines (213 loc) · 7.38 KB
/
Make.rules
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
# Build objects and libraries.
# ============================
# Compute the installation directory. By default, it is same as the relative
# path to the prerequisite in the source tree. Setting INSTALL_DIR= disables
# installation for a sub-tree (unless it is overridden deeper in the tree).
ifeq ($(flavor INSTALL_DIR),undefined)
ifeq ($(flavor RELATIVE_INSTALL_DIR),undefined)
RELATIVE_INSTALL_DIR = $(shell realpath --relative-to $(ROOT_DIR) $(shell pwd))
endif
ifdef RELATIVE_INSTALL_DIR
INSTALL_DIR = $(abspath $(PREFIX)/$(RELATIVE_INSTALL_DIR))
DIRS_TO_UNINSTALL += $(INSTALL_DIR)
endif
endif
# Submodules may override SRCS.
ifeq ($(flavor SRCS),undefined)
SRCS := $(wildcard *.c *.cpp)
endif
LOCKFILE := $(OBJECT_ROOT)/lock
FILES_TO_CLEAN += $(LOCKFILE)
# Update archives from .o files using a lock to prevent concurrent access.
.o.a:
flock $(LOCKFILE) $(AR) rvP -U $@ $<
# Contextual variables
# ====================
# These take values that depend on the submodule being built.
# Create the directory under $(OBJECT_ROOT) corresponding to the current source
# directory.
OBJECT_DIR := $(shell realpath --relative-to $(ROOT_DIR)/src $(shell pwd))
ifeq ($(findstring ..,$(OBJECT_DIR)),..)
OBJECT_DIR :=
else
OBJECT_DIR := $(abspath $(OBJECT_ROOT)/$(OBJECT_DIR))
$(shell mkdir -p $(OBJECT_DIR) >/dev/null)
DIRS_TO_CLEAN += $(OBJECT_DIR)
endif
# Submodules may override OBJS.
ifeq ($(flavor OBJS),undefined)
OBJS := $(patsubst %,%.o,$(basename $(SRCS)))
endif
OBJS_OUT := $(patsubst %,$(OBJECT_DIR)/%,$(OBJS))
FILES_TO_CLEAN += $(OBJS_OUT)
# Install objects, libraries, links, and programs.
# ================================================
# Submodules may set LIB to build a static library.
ifdef LIB
# Build.
LIB_OUT := $(OBJECT_ROOT)/$(LIB).a
FILES_TO_CLEAN += $(LIB_OUT)
$(LIB_OUT): $(LIB_OUT)($(LIB_DEPENDS))
libs:: $(LIB_OUT)
libs:: $(LIB_DEPENDS)
# Install.
ifdef INSTALL_DIR
LIB_BASENAME := $(notdir $(basename $(LIB)))
LIB_INSTALL = $(INSTALL_DIR)/$(LIB_BASENAME).a
DIRS_TO_UNINSTALL += $(dir $(LIB_INSTALL))
FILES_TO_UNINSTALL += $(LIB_INSTALL)
install:: $(LIB_INSTALL)
$(LIB_INSTALL): $(LIB_OUT)
@mkdir -p $(dir $@)
cp $< $@
endif
endif
# Submodules may set SHLIB to build a dynamic library.
ifdef SHLIB
ifdef INSTALL_DIR
# Build.
SHLIB_OUT := $(OBJECT_DIR)/$(SHLIB).so
FILES_TO_CLEAN += $(SHLIB_OUT)
$(SHLIB_OUT): $(SHLIB_DEPENDS) | $(SHLIB_EXTRA_DEPENDS)
$(CXX) -shared -o $@ $(SHLIB_PREDEP_LDFLAGS) $^ $(SHLIB_POSTDEP_LDFLAGS) $(LDFLAGS)
shlibs:: $(SHLIB_OUT)
shlibs:: $(SHLIB_DEPENDS)
# Install.
SHLIB_INSTALL = $(INSTALL_DIR)/$(SHLIB).so
DIRS_TO_UNINSTALL += $(dir $(SHLIB_INSTALL))
FILES_TO_UNINSTALL += $(SHLIB_INSTALL)
install:: $(SHLIB_INSTALL)
$(SHLIB_INSTALL): $(SHLIB_OUT)
@mkdir -p $(dir $@)
cp $< $@
endif
endif
# Submodules may set EXPORTED_HEADERS to export header files.
ifdef EXPORTED_HEADERS
ifdef HEADER_INSTALL_DIR
HEADERS_INSTALL = $(addprefix $(HEADER_INSTALL_DIR)/, $(EXPORTED_HEADERS))
DIRS_TO_UNINSTALL += $(dir $(HEADERS_INSTALL))
FILES_TO_UNINSTALL += $(HEADERS_INSTALL)
install:: $(HEADERS_INSTALL)
$(HEADER_INSTALL_DIR)/%.hpp : %.hpp
@mkdir -p $(dir $@)
ifdef SYMLINK_INTERFACES
ln -sf $(abspath $<) $@
else
cp $< $@
endif
$(HEADER_INSTALL_DIR)/%.hxx : %.hxx
@mkdir -p $(dir $@)
ifdef SYMLINK_INTERFACES
ln -sf $(abspath $<) $@
else
cp $< $@
endif
endif
endif
# Submodules may override LINKS.
#
# The following evaluates the .link file contents as a make expression to
# compute the link target.
define define_link_target =
LINK_TARGET_$1 := $(shell cat $1)
endef
ifeq ($(flavor LINKS),undefined)
LINKS = $(wildcard *.link)
endif
ifdef LINKS
ifdef INSTALL_DIR
LINKS_NOEXT := $(patsubst %.link,%,$(LINKS))
# Do not update FILES_TO_CLEAN. There are no intermediate files.
LINKS_INSTALL = $(addprefix $(INSTALL_DIR)/, $(LINKS_NOEXT))
DIRS_TO_UNINSTALL += $(dir $(LINKS_INSTALL))
FILES_TO_UNINSTALL += $(LINKS_INSTALL)
install:: $(LINKS_INSTALL)
$(foreach link,$(LINKS),$(eval $(call define_link_target,$(link))))
$(INSTALL_DIR)/% : %.link
@if [ -n "$(LINK_TARGET_$<)" ]; then \
mkdir -p $(dir $@); \
echo ln -sf $(LINK_TARGET_$<) $@; \
ln -sf $(LINK_TARGET_$<) $@; \
else \
echo 1>&2 "Skipping installation of link $@ because it has no target."; \
fi
endif
endif
# Submodules may override VARS.
#
# This type of file is processed exactly like a link, except that instead of a
# link, a regular file containing the expanded variable is created.
define define_var_target =
VAR_VALUE$1 := $(shell cat $1)
endef
ifeq ($(flavor VARS),undefined)
VARS = $(wildcard *.var)
endif
ifdef VARS
ifdef INSTALL_DIR
VARS_NOEXT := $(patsubst %.var,%,$(VARS))
# Do not update FILES_TO_CLEAN. There are no intermediate files.
VARS_INSTALL = $(addprefix $(INSTALL_DIR)/, $(VARS_NOEXT))
DIRS_TO_UNINSTALL += $(dir $(VARS_INSTALL))
FILES_TO_UNINSTALL += $(VARS_INSTALL)
install:: $(VARS_INSTALL)
$(foreach var,$(VARS),$(eval $(call define_var_target,$(var))))
$(INSTALL_DIR)/% : %.var
@mkdir -p $(dir $@)
echo $(VAR_VALUE$<) > $@
endif
endif
# Submodules may override SCRIPTS. Scripts are copied.
ifeq ($(flavor SCRIPTS),undefined)
SCRIPTS = $(shell ls *.script 2>/dev/null)
endif
ifdef SCRIPTS
ifdef INSTALL_DIR
SCRIPTS_NOEXT := $(patsubst %.script,%,$(SCRIPTS))
# Do not update FILES_TO_CLEAN. There are no intermediate files.
SCRIPTS_INSTALL = $(addprefix $(INSTALL_DIR)/, $(SCRIPTS_NOEXT))
DIRS_TO_UNINSTALL += $(dir $(SCRIPTS_INSTALL))
FILES_TO_UNINSTALL += $(SCRIPTS_INSTALL)
install:: $(SCRIPTS_INSTALL)
$(INSTALL_DIR)/% : %.script
@mkdir -p $(dir $@)
cp $< $@
endif
endif
# Submodules may override PYTHONFILES.
ifeq ($(flavor PYTHONFILES),undefined)
PYTHONFILES = $(wildcard *.py)
endif
ifdef PYTHONFILES
ifdef INSTALL_DIR
# Do not update FILES_TO_CLEAN. There are no intermediate files.
PYTHONFILES_INSTALL = $(addprefix $(INSTALL_DIR)/, $(PYTHONFILES))
DIRS_TO_UNINSTALL += $(dir $(PYTHONFILES_INSTALL))
FILES_TO_UNINSTALL += $(PYTHONFILES_INSTALL)
install:: $(PYTHONFILES_INSTALL)
$(INSTALL_DIR)/%.py : %.py
@mkdir -p $(dir $@)
ifdef SYMLINK_INTERFACES
ln -sf $(abspath $<) $@
else
cp $< $@
endif
endif
endif
# Sort removes duplicates. Sort by reverse depth.
reverse = $(shell printf "%s\n" $(strip $1) | tac)
FILES_TO_CLEAN := $(call reverse, $(sort $(foreach path,$(FILES_TO_CLEAN),$(abspath $(path)))))
DIRS_TO_REC_CLEAN := $(call reverse, $(sort $(foreach path,$(DIRS_TO_REC_CLEAN),$(abspath $(path)))))
DIRS_TO_CLEAN := $(call reverse, $(sort $(foreach path,$(DIRS_TO_CLEAN),$(abspath $(path)))))
FILES_TO_UNINSTALL := $(call reverse, $(sort $(foreach path,$(FILES_TO_UNINSTALL),$(abspath $(path)))))
DIRS_TO_REC_UNINSTALL := $(call reverse, $(sort $(foreach path,$(DIRS_TO_REC_UNINSTALL),$(abspath $(path)))))
DIRS_TO_UNINSTALL := $(call reverse, $(sort $(foreach path,$(DIRS_TO_UNINSTALL),$(abspath $(path)))))
safe-clean ::
$(call clean_files,$(FILES_TO_CLEAN),rm -f)
$(call clean_files,$(DIRS_TO_REC_CLEAN),rm -rf)
$(call clean_files,$(DIRS_TO_CLEAN),rmdir)
uninstall ::
$(call clean_files,$(FILES_TO_UNINSTALL),rm -f)
$(call clean_files,$(DIRS_TO_REC_UNINSTALL),rm -rf)
$(call clean_files,$(DIRS_TO_UNINSTALL),rmdir)