build: add support for fixing up library soname
[openwrt/openwrt.git] / include / package.mk
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2006-2020 OpenWrt.org
4
5 __package_mk:=1
6
7 all: $(if $(DUMP),dumpinfo,$(if $(CHECK),check,compile))
8
9 include $(INCLUDE_DIR)/download.mk
10
11 PKG_BUILD_DIR ?= $(BUILD_DIR)/$(if $(BUILD_VARIANT),$(PKG_NAME)-$(BUILD_VARIANT)/)$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
12 PKG_INSTALL_DIR ?= $(PKG_BUILD_DIR)/ipkg-install
13 PKG_BUILD_PARALLEL ?=
14 PKG_USE_MIPS16 ?= 1
15 PKG_IREMAP ?= 1
16
17 MAKE_J:=$(if $(MAKE_JOBSERVER),$(MAKE_JOBSERVER) $(if $(filter 3.% 4.0 4.1,$(MAKE_VERSION)),-j))
18
19 PKG_SOURCE_DATE_EPOCH:=$(if $(DUMP),,$(shell $(TOPDIR)/scripts/get_source_date_epoch.sh $(CURDIR)))
20
21 ifeq ($(strip $(PKG_BUILD_PARALLEL)),0)
22 PKG_JOBS?=-j1
23 else
24 PKG_JOBS?=$(if $(PKG_BUILD_PARALLEL),$(MAKE_J),-j1)
25 endif
26 ifdef CONFIG_USE_MIPS16
27 ifeq ($(strip $(PKG_USE_MIPS16)),1)
28 TARGET_ASFLAGS_DEFAULT = $(filter-out -mips16 -minterlink-mips16,$(TARGET_CFLAGS))
29 TARGET_CFLAGS += -mips16 -minterlink-mips16
30 endif
31 endif
32 ifeq ($(strip $(PKG_IREMAP)),1)
33 IREMAP_CFLAGS = $(call iremap,$(PKG_BUILD_DIR),$(notdir $(PKG_BUILD_DIR)))
34 TARGET_CFLAGS += $(IREMAP_CFLAGS)
35 endif
36
37 include $(INCLUDE_DIR)/hardening.mk
38 include $(INCLUDE_DIR)/prereq.mk
39 include $(INCLUDE_DIR)/unpack.mk
40 include $(INCLUDE_DIR)/depends.mk
41
42 ifneq ($(wildcard $(TOPDIR)/git-src/$(PKG_NAME)/.git),)
43 USE_GIT_SRC_CHECKOUT:=1
44 QUILT:=1
45 endif
46 ifneq ($(if $(CONFIG_SRC_TREE_OVERRIDE),$(wildcard ./git-src)),)
47 USE_GIT_TREE:=1
48 QUILT:=1
49 endif
50 ifdef USE_SOURCE_DIR
51 QUILT:=1
52 endif
53 ifneq ($(wildcard $(PKG_BUILD_DIR)/.source_dir),)
54 QUILT:=1
55 endif
56
57 include $(INCLUDE_DIR)/quilt.mk
58
59 find_library_dependencies = \
60 $(wildcard $(patsubst %,$(STAGING_DIR)/pkginfo/%.version, \
61 $(sort $(foreach dep4, \
62 $(sort $(foreach dep3, \
63 $(sort $(foreach dep2, \
64 $(sort $(foreach dep1, \
65 $(sort $(foreach dep0, \
66 $(Package/$(1)/depends), \
67 $(Package/$(dep0)/depends) $(dep0) \
68 )), \
69 $(Package/$(dep1)/depends) $(dep1) \
70 )), \
71 $(Package/$(dep2)/depends) $(dep2) \
72 )), \
73 $(Package/$(dep3)/depends) $(dep3) \
74 )), \
75 $(Package/$(dep4)/depends) $(dep4) \
76 )) \
77 ))
78
79
80 PKG_DIR_NAME:=$(lastword $(subst /,$(space),$(CURDIR)))
81 STAMP_NO_AUTOREBUILD=$(wildcard $(PKG_BUILD_DIR)/.no_autorebuild)
82 PREV_STAMP_PREPARED:=$(if $(STAMP_NO_AUTOREBUILD),$(wildcard $(PKG_BUILD_DIR)/.prepared*))
83 ifneq ($(PREV_STAMP_PREPARED),)
84 STAMP_PREPARED:=$(PREV_STAMP_PREPARED)
85 CONFIG_AUTOREBUILD:=
86 else
87 STAMP_PREPARED=$(PKG_BUILD_DIR)/.prepared$(if $(QUILT)$(DUMP),,_$(shell $(call find_md5,${CURDIR} $(PKG_FILE_DEPENDS),))_$(call confvar,CONFIG_AUTOREMOVE $(PKG_PREPARED_DEPENDS)))
88 endif
89 STAMP_CONFIGURED=$(PKG_BUILD_DIR)/.configured$(if $(DUMP),,_$(call confvar,$(PKG_CONFIG_DEPENDS)))
90 STAMP_CONFIGURED_WILDCARD=$(PKG_BUILD_DIR)/.configured_*
91 STAMP_BUILT:=$(PKG_BUILD_DIR)/.built
92 STAMP_INSTALLED:=$(STAGING_DIR)/stamp/.$(PKG_DIR_NAME)$(if $(BUILD_VARIANT),.$(BUILD_VARIANT),)_installed
93
94 STAGING_FILES_LIST:=$(PKG_DIR_NAME)$(if $(BUILD_VARIANT),.$(BUILD_VARIANT),).list
95
96 define CleanStaging
97 rm -f $(STAMP_INSTALLED)
98 @-(\
99 if [ -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) ]; then \
100 $(SCRIPT_DIR)/clean-package.sh \
101 "$(STAGING_DIR)/packages/$(STAGING_FILES_LIST)" \
102 "$(STAGING_DIR)"; \
103 fi; \
104 )
105 endef
106
107
108 PKG_INSTALL_STAMP:=$(PKG_INFO_DIR)/$(PKG_DIR_NAME).$(if $(BUILD_VARIANT),$(BUILD_VARIANT),default).install
109
110 include $(INCLUDE_DIR)/package-defaults.mk
111 include $(INCLUDE_DIR)/package-dumpinfo.mk
112 include $(INCLUDE_DIR)/package-ipkg.mk
113 include $(INCLUDE_DIR)/package-bin.mk
114 include $(INCLUDE_DIR)/autotools.mk
115
116 _pkg_target:=$(if $(QUILT),,.)
117
118 override MAKEFLAGS=
119 CONFIG_SITE:=$(INCLUDE_DIR)/site/$(ARCH)
120 CUR_MAKEFILE:=$(filter-out Makefile,$(firstword $(MAKEFILE_LIST)))
121 SUBMAKE:=$(NO_TRACE_MAKE) $(if $(CUR_MAKEFILE),-f $(CUR_MAKEFILE))
122 PKG_CONFIG_PATH=$(STAGING_DIR)/usr/lib/pkgconfig:$(STAGING_DIR)/usr/share/pkgconfig
123 unexport QUIET CONFIG_SITE
124
125 ifeq ($(DUMP)$(filter prereq clean refresh update,$(MAKECMDGOALS)),)
126 ifneq ($(if $(QUILT),,$(CONFIG_AUTOREBUILD)),)
127 define Build/Autoclean
128 $(PKG_BUILD_DIR)/.dep_files: $(STAMP_PREPARED)
129 $(call rdep,${CURDIR} $(PKG_FILE_DEPENDS),$(STAMP_PREPARED),$(PKG_BUILD_DIR)/.dep_files,-x "*/.dep_*")
130 $(if $(filter prepare,$(MAKECMDGOALS)),,$(call rdep,$(PKG_BUILD_DIR),$(STAMP_BUILT),,-x "*/.dep_*" -x "*/ipkg*"))
131 endef
132 endif
133 endif
134
135 ifdef USE_GIT_SRC_CHECKOUT
136 define Build/Prepare/Default
137 mkdir -p $(PKG_BUILD_DIR)
138 ln -s $(TOPDIR)/git-src/$(PKG_NAME)/.git $(PKG_BUILD_DIR)/.git
139 ( cd $(PKG_BUILD_DIR); \
140 git checkout .; \
141 git submodule update --recursive; \
142 git submodule foreach git config --unset core.worktree; \
143 git submodule foreach git checkout .; \
144 )
145 endef
146 endif
147 ifdef USE_GIT_TREE
148 define Build/Prepare/Default
149 mkdir -p $(PKG_BUILD_DIR)
150 ln -s $(CURDIR)/git-src $(PKG_BUILD_DIR)/.git
151 ( cd $(PKG_BUILD_DIR); \
152 git checkout .; \
153 git submodule update --recursive; \
154 git submodule foreach git config --unset core.worktree; \
155 git submodule foreach git checkout .; \
156 )
157 endef
158 endif
159 ifdef USE_SOURCE_DIR
160 define Build/Prepare/Default
161 rm -rf $(PKG_BUILD_DIR)
162 $(if $(wildcard $(USE_SOURCE_DIR)/*),,@echo "Error: USE_SOURCE_DIR=$(USE_SOURCE_DIR) path not found"; false)
163 ln -snf $(USE_SOURCE_DIR) $(PKG_BUILD_DIR)
164 touch $(PKG_BUILD_DIR)/.source_dir
165 endef
166 endif
167
168 define Build/Exports/Default
169 $(1) : export ACLOCAL_INCLUDE=$$(foreach p,$$(wildcard $$(STAGING_DIR)/usr/share/aclocal $$(STAGING_DIR)/usr/share/aclocal-* $$(STAGING_DIR_HOSTPKG)/share/aclocal $$(STAGING_DIR_HOSTPKG)/share/aclocal-* $$(STAGING_DIR)/host/share/aclocal $$(STAGING_DIR)/host/share/aclocal-*),-I $$(p))
170 $(1) : export STAGING_PREFIX=$$(STAGING_DIR)/usr
171 $(1) : export PATH=$$(TARGET_PATH_PKG)
172 $(1) : export CONFIG_SITE:=$$(CONFIG_SITE)
173 $(1) : export PKG_CONFIG_PATH:=$$(PKG_CONFIG_PATH)
174 $(1) : export PKG_CONFIG_LIBDIR:=$$(PKG_CONFIG_PATH)
175 endef
176 Build/Exports=$(Build/Exports/Default)
177
178 define Build/CoreTargets
179 STAMP_PREPARED:=$$(STAMP_PREPARED)
180 STAMP_CONFIGURED:=$$(STAMP_CONFIGURED)
181 PKG_ABI_VERSION:=$$(PKG_ABI_VERSION)
182
183 $(if $(QUILT),$(Build/Quilt))
184 $(call Build/Autoclean)
185 $(call DefaultTargets)
186
187 $(DL_DIR)/$(FILE): FORCE
188
189 download:
190 $(foreach hook,$(Hooks/Download),
191 $(call $(hook))$(sep)
192 )
193
194 $(STAMP_PREPARED) : export PATH=$$(TARGET_PATH_PKG)
195 $(STAMP_PREPARED): $(STAMP_PREPARED_DEPENDS)
196 @-rm -rf $(PKG_BUILD_DIR)
197 @mkdir -p $(PKG_BUILD_DIR)
198 touch $$@_check
199 $(foreach hook,$(Hooks/Prepare/Pre),$(call $(hook))$(sep))
200 $(Build/Prepare)
201 $(foreach hook,$(Hooks/Prepare/Post),$(call $(hook))$(sep))
202 touch $$@
203
204 $(call Build/Exports,$(STAMP_CONFIGURED))
205 $(STAMP_CONFIGURED): $(STAMP_PREPARED) $(STAMP_CONFIGURED_DEPENDS)
206 rm -f $(STAMP_CONFIGURED_WILDCARD)
207 $(CleanStaging)
208 $(foreach hook,$(Hooks/Configure/Pre),$(call $(hook))$(sep))
209 $(Build/Configure)
210 $(foreach hook,$(Hooks/Configure/Post),$(call $(hook))$(sep))
211 touch $$@
212
213 $(call Build/Exports,$(STAMP_BUILT))
214 $(STAMP_BUILT): $(STAMP_CONFIGURED) $(STAMP_BUILT_DEPENDS)
215 rm -f $$@
216 touch $$@_check
217 $(foreach hook,$(Hooks/Compile/Pre),$(call $(hook))$(sep))
218 $(Build/Compile)
219 $(foreach hook,$(Hooks/Compile/Post),$(call $(hook))$(sep))
220 $(Build/Install)
221 $(foreach hook,$(Hooks/Install/Post),$(call $(hook))$(sep))
222 touch $$@
223
224 $(STAMP_INSTALLED) : export PATH=$$(TARGET_PATH_PKG)
225 $(STAMP_INSTALLED): $(STAMP_BUILT)
226 rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
227 mkdir -p $(TMP_DIR)/stage-$(PKG_DIR_NAME)/host $(STAGING_DIR)/packages
228 $(foreach hook,$(Hooks/InstallDev/Pre),\
229 $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
230 )
231 $(call Build/InstallDev,$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)
232 $(foreach hook,$(Hooks/InstallDev/Post),\
233 $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
234 )
235 if [ -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) ]; then \
236 $(SCRIPT_DIR)/clean-package.sh \
237 "$(STAGING_DIR)/packages/$(STAGING_FILES_LIST)" \
238 "$(STAGING_DIR)"; \
239 fi
240 $(if $(PKG_ABI_VERSION),$(SET_ABI_VERSION) "$(PKG_ABI_VERSION)" $(TMP_DIR)/stage-$(PKG_DIR_NAME))
241 if [ -d $(TMP_DIR)/stage-$(PKG_DIR_NAME) ]; then \
242 (cd $(TMP_DIR)/stage-$(PKG_DIR_NAME); find ./ > $(TMP_DIR)/stage-$(PKG_DIR_NAME).files); \
243 $(call locked, \
244 mv $(TMP_DIR)/stage-$(PKG_DIR_NAME).files $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) && \
245 $(CP) $(TMP_DIR)/stage-$(PKG_DIR_NAME)/* $(STAGING_DIR)/; \
246 ,staging-dir); \
247 fi
248 rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
249 touch $$@
250
251 ifdef Build/InstallDev
252 $(_pkg_target)compile: $(STAMP_INSTALLED)
253 endif
254
255 $(_pkg_target)prepare: $(STAMP_PREPARED)
256 $(_pkg_target)configure: $(STAMP_CONFIGURED)
257 $(_pkg_target)dist: $(STAMP_CONFIGURED)
258 $(_pkg_target)distcheck: $(STAMP_CONFIGURED)
259
260 ifneq ($(CONFIG_AUTOREMOVE),)
261 compile:
262 -touch -r $(PKG_BUILD_DIR)/.built $(PKG_BUILD_DIR)/.autoremove 2>/dev/null >/dev/null
263 $(FIND) $(PKG_BUILD_DIR) -mindepth 1 -maxdepth 1 -not '(' -type f -and -name '.*' -and -size 0 ')' -and -not -name '.pkgdir' | \
264 $(XARGS) rm -rf
265 endif
266 endef
267
268 define Build/DefaultTargets
269 $(if $(USE_SOURCE_DIR)$(USE_GIT_TREE)$(USE_GIT_SRC_CHECKOUT),,$(if $(strip $(PKG_SOURCE_URL)),$(call Download,default)))
270 $(if $(DUMP),,$(Build/CoreTargets))
271
272 define Build/DefaultTargets
273 endef
274 endef
275
276 define BuildPackage
277 $(eval $(Package/Default))
278 $(eval $(Package/$(1)))
279
280 ifdef DESCRIPTION
281 $$(error DESCRIPTION:= is obsolete, use Package/PKG_NAME/description)
282 endif
283
284 ifndef Package/$(1)/description
285 define Package/$(1)/description
286 $(TITLE)
287 endef
288 endif
289
290 BUILD_PACKAGES += $(1)
291 $(STAMP_PREPARED): $$(if $(QUILT)$(DUMP),,$(call find_library_dependencies,$(1)))
292
293 $(foreach FIELD, TITLE CATEGORY SECTION VERSION,
294 ifeq ($($(FIELD)),)
295 $$(error Package/$(1) is missing the $(FIELD) field)
296 endif
297 )
298
299 $(if $(DUMP), \
300 $(if $(CHECK),,$(Dumpinfo/Package)), \
301 $(foreach target, \
302 $(if $(Package/$(1)/targets),$(Package/$(1)/targets), \
303 $(if $(PKG_TARGETS),$(PKG_TARGETS), ipkg) \
304 ), $(BuildTarget/$(target)) \
305 ) \
306 )
307 $(if $(PKG_HOST_ONLY),,$(call Build/DefaultTargets,$(1)))
308 endef
309
310 define pkg_install_files
311 $(foreach install_file,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_file)`; $(INSTALL_DATA) $(2)/$(install_file) $(3)/`dirname $(install_file)`;)
312 endef
313
314 define pkg_install_bin
315 $(foreach install_apps,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_apps)`; $(INSTALL_BIN) $(2)/$(install_apps) $(3)/`dirname $(install_apps)`;)
316 endef
317
318 Build/Prepare=$(call Build/Prepare/Default,)
319 Build/Configure=$(call Build/Configure/Default,)
320 Build/Compile=$(call Build/Compile/Default,)
321 Build/Install=$(if $(PKG_INSTALL),$(call Build/Install/Default,))
322 Build/Dist=$(call Build/Dist/Default,)
323 Build/DistCheck=$(call Build/DistCheck/Default,)
324
325 .NOTPARALLEL:
326
327 .PHONY: prepare-package-install
328 prepare-package-install:
329 @mkdir -p $(PKG_INFO_DIR)
330 @rm -f $(PKG_INSTALL_STAMP)
331 @echo "$(filter-out essential nonshared,$(PKG_FLAGS))" > $(PKG_INSTALL_STAMP).flags
332
333 $(PACKAGE_DIR):
334 mkdir -p $@
335
336 compile:
337 .install: .compile
338 install: compile
339
340 force-clean-build: FORCE
341 rm -rf $(PKG_BUILD_DIR)
342
343 clean-build: $(if $(wildcard $(PKG_BUILD_DIR)/.autoremove),force-clean-build)
344
345 clean: force-clean-build
346 $(CleanStaging)
347 $(call Build/UninstallDev,$(STAGING_DIR),$(STAGING_DIR)/host)
348 $(Build/Clean)
349 rm -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST)
350
351 dist:
352 $(Build/Dist)
353
354 distcheck:
355 $(Build/DistCheck)