ead4b5742cb508676b5fbd0238fd969a75f5e0be
[openwrt/openwrt.git] / include / package-ipkg.mk
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # Copyright (C) 2006-2020 OpenWrt.org
4
5 ifndef DUMP
6 include $(INCLUDE_DIR)/feeds.mk
7 endif
8
9 IPKG_REMOVE:= \
10 $(SCRIPT_DIR)/ipkg-remove
11
12 IPKG_STATE_DIR:=$(TARGET_DIR)/usr/lib/opkg
13
14 # Generates a make statement to return a wildcard for candidate ipkg files
15 # 1: package name
16 define gen_ipkg_wildcard
17 $(1)$$(if $$(filter -%,$$(ABIV_$(1))),,[^a-z-])*
18 endef
19
20 # 1: package name
21 # 2: candidate ipk files
22 define remove_ipkg_files
23 $(if $(strip $(2)),$(IPKG_REMOVE) $(1) $(2))
24 endef
25
26 # 1: package name
27 # 2: variable name
28 # 3: variable suffix
29 # 4: file is a script
30 define BuildIPKGVariable
31 ifdef Package/$(1)/$(2)
32 $$(IPKG_$(1)) : VAR_$(2)$(3)=$$(Package/$(1)/$(2))
33 $(call shexport,Package/$(1)/$(2))
34 $(1)_COMMANDS += echo "$$$$$$$$$(call shvar,Package/$(1)/$(2))" > $(2)$(3); $(if $(4),chmod 0755 $(2)$(3);)
35 endif
36 endef
37
38 PARENL :=(
39 PARENR :=)
40
41 dep_split=$(subst :,$(space),$(1))
42 dep_rem=$(subst !,,$(subst $(strip $(PARENL)),,$(subst $(strip $(PARENR)),,$(word 1,$(call dep_split,$(1))))))
43 dep_and=dep_and_res:=$$(and $(subst $(space),$(comma),$(foreach cond,$(subst &&, ,$(1)),$$(CONFIG_$(cond)))))
44 dep_confvar=$(strip $(foreach cond,$(subst ||, ,$(call dep_rem,$(1))),$(eval $(call dep_and,$(cond)))$(dep_and_res)))
45 dep_pos=$(if $(call dep_confvar,$(1)),$(call dep_val,$(1)))
46 dep_neg=$(if $(call dep_confvar,$(1)),,$(call dep_val,$(1)))
47 dep_if=$(if $(findstring !,$(1)),$(call dep_neg,$(1)),$(call dep_pos,$(1)))
48 dep_val=$(word 2,$(call dep_split,$(1)))
49 strip_deps=$(strip $(subst +,,$(filter-out @%,$(1))))
50 filter_deps=$(foreach dep,$(call strip_deps,$(1)),$(if $(findstring :,$(dep)),$(call dep_if,$(dep)),$(dep)))
51
52 define AddDependency
53 $$(if $(1),$$(if $(2),$$(foreach pkg,$(1),$$(IPKG_$$(pkg))): $$(foreach pkg,$(2),$$(IPKG_$$(pkg)))))
54 endef
55
56 define FixupReverseDependencies
57 DEPS := $$(filter %:$(1),$$(IDEPEND))
58 DEPS := $$(patsubst %:$(1),%,$$(DEPS))
59 DEPS := $$(filter $$(DEPS),$$(IPKGS))
60 $(call AddDependency,$$(DEPS),$(1))
61 endef
62
63 define FixupDependencies
64 DEPS := $$(filter $(1):%,$$(IDEPEND))
65 DEPS := $$(patsubst $(1):%,%,$$(DEPS))
66 DEPS := $$(filter $$(DEPS),$$(IPKGS))
67 $(call AddDependency,$(1),$$(DEPS))
68 endef
69
70 ifneq ($(PKG_NAME),toolchain)
71 define CheckDependencies
72 @( \
73 rm -f $(PKG_INFO_DIR)/$(1).missing; \
74 ( \
75 export \
76 READELF=$(TARGET_CROSS)readelf \
77 OBJCOPY=$(TARGET_CROSS)objcopy \
78 XARGS="$(XARGS)"; \
79 $(SCRIPT_DIR)/gen-dependencies.sh "$$(IDIR_$(1))"; \
80 ) | while read FILE; do \
81 grep -qxF "$$$$FILE" $(PKG_INFO_DIR)/$(1).provides || \
82 echo "$$$$FILE" >> $(PKG_INFO_DIR)/$(1).missing; \
83 done; \
84 if [ -f "$(PKG_INFO_DIR)/$(1).missing" ]; then \
85 echo "Package $(1) is missing dependencies for the following libraries:" >&2; \
86 cat "$(PKG_INFO_DIR)/$(1).missing" >&2; \
87 false; \
88 fi; \
89 )
90 endef
91 endif
92
93 _addsep=$(word 1,$(1))$(foreach w,$(wordlist 2,$(words $(1)),$(1)),$(strip $(2) $(w)))
94 _cleansep=$(subst $(space)$(2)$(space),$(2)$(space),$(1))
95 mergelist=$(call _cleansep,$(call _addsep,$(1),$(comma)),$(comma))
96 addfield=$(if $(strip $(2)),$(1): $(2))
97 _define=define
98 _endef=endef
99
100 ifeq ($(DUMP),)
101 define BuildTarget/ipkg
102 ABIV_$(1):=$(call GetABISuffix,$(1))
103 PDIR_$(1):=$(call FeedPackageDir,$(1))
104 IPKG_$(1):=$$(PDIR_$(1))/$(1)$$(ABIV_$(1))_$(VERSION)_$(PKGARCH).ipk
105 IDIR_$(1):=$(PKG_BUILD_DIR)/ipkg-$(PKGARCH)/$(1)
106 KEEP_$(1):=$(strip $(call Package/$(1)/conffiles))
107
108 ifeq ($(BUILD_VARIANT),$$(if $$(VARIANT),$$(VARIANT),$(BUILD_VARIANT)))
109 do_install=
110 ifdef Package/$(1)/install
111 do_install=yes
112 endif
113 ifdef Package/$(1)/install-overlay
114 do_install=yes
115 endif
116 ifdef do_install
117 ifneq ($(CONFIG_PACKAGE_$(1))$(DEVELOPER),)
118 IPKGS += $(1)
119 $(_pkg_target)compile: $$(IPKG_$(1)) $(PKG_INFO_DIR)/$(1).provides $(PKG_BUILD_DIR)/.pkgdir/$(1).installed
120 prepare-package-install: $$(IPKG_$(1))
121 compile: $(STAGING_DIR_ROOT)/stamp/.$(1)_installed
122 else
123 $(if $(CONFIG_PACKAGE_$(1)),$$(info WARNING: skipping $(1) -- package not selected))
124 endif
125
126 .PHONY: $(PKG_INSTALL_STAMP).$(1)
127 ifeq ($(CONFIG_PACKAGE_$(1)),y)
128 compile: $(PKG_INSTALL_STAMP).$(1)
129 endif
130 $(PKG_INSTALL_STAMP).$(1): prepare-package-install
131 echo "$(1)" >> $(PKG_INSTALL_STAMP)
132 else
133 $(if $(CONFIG_PACKAGE_$(1)),$$(warning WARNING: skipping $(1) -- package has no install section))
134 endif
135 endif
136
137 DEPENDS:=$(call PKG_FIXUP_DEPENDS,$(1),$(DEPENDS))
138 IDEPEND_$(1):=$$(call filter_deps,$$(DEPENDS))
139 IDEPEND += $$(patsubst %,$(1):%,$$(IDEPEND_$(1)))
140 $(FixupDependencies)
141 $(FixupReverseDependencies)
142
143 $(eval $(call BuildIPKGVariable,$(1),conffiles))
144 $(eval $(call BuildIPKGVariable,$(1),preinst,,1))
145 $(eval $(call BuildIPKGVariable,$(1),postinst,-pkg,1))
146 $(eval $(call BuildIPKGVariable,$(1),prerm,-pkg,1))
147 $(eval $(call BuildIPKGVariable,$(1),postrm,,1))
148
149 $(PKG_BUILD_DIR)/.pkgdir/$(1).installed : export PATH=$$(TARGET_PATH_PKG)
150 $(PKG_BUILD_DIR)/.pkgdir/$(1).installed: $(STAMP_BUILT)
151 rm -rf $$@ $(PKG_BUILD_DIR)/.pkgdir/$(1)
152 mkdir -p $(PKG_BUILD_DIR)/.pkgdir/$(1)
153 $(call Package/$(1)/install,$(PKG_BUILD_DIR)/.pkgdir/$(1))
154 $(call Package/$(1)/install_lib,$(PKG_BUILD_DIR)/.pkgdir/$(1))
155 touch $$@
156
157 $(STAGING_DIR_ROOT)/stamp/.$(1)_installed: $(PKG_BUILD_DIR)/.pkgdir/$(1).installed
158 mkdir -p $(STAGING_DIR_ROOT)/stamp
159 $(if $(ABI_VERSION),echo '$(ABI_VERSION)' | cmp -s - $(PKG_INFO_DIR)/$(1).version || echo '$(ABI_VERSION)' > $(PKG_INFO_DIR)/$(1).version)
160 $(call locked,$(CP) $(PKG_BUILD_DIR)/.pkgdir/$(1)/. $(STAGING_DIR_ROOT)/,root-copy)
161 touch $$@
162
163 Package/$(1)/DEPENDS := $$(call mergelist,$$(foreach dep,$$(filter-out @%,$$(IDEPEND_$(1))),$$(dep)$$(call GetABISuffix,$$(dep))))
164 ifneq ($$(EXTRA_DEPENDS),)
165 Package/$(1)/DEPENDS := $$(EXTRA_DEPENDS)$$(if $$(Package/$(1)/DEPENDS),$$(comma) $$(Package/$(1)/DEPENDS))
166 endif
167
168 $(_define) Package/$(1)/CONTROL
169 Package: $(1)$$(ABIV_$(1))
170 Version: $(VERSION)
171 $$(call addfield,Depends,$$(Package/$(1)/DEPENDS)
172 )$$(call addfield,Conflicts,$$(call mergelist,$(CONFLICTS))
173 )$$(call addfield,Provides,$$(call mergelist,$$(filter-out $(1)$$(ABIV_$(1)),$(PROVIDES)$$(if $$(ABIV_$(1)), $(1) $(foreach provide,$(PROVIDES),$(provide)$$(call GetABISuffix,$(provide))))))
174 )$$(call addfield,Alternatives,$$(call mergelist,$(ALTERNATIVES))
175 )$$(call addfield,Source,$(SOURCE)
176 )$$(call addfield,SourceName,$(1)
177 )$$(call addfield,License,$(LICENSE)
178 )$$(call addfield,LicenseFiles,$(LICENSE_FILES)
179 )$$(call addfield,Section,$(SECTION)
180 )$$(call addfield,Require-User,$(USERID)
181 )$$(call addfield,SourceDateEpoch,$(PKG_SOURCE_DATE_EPOCH)
182 )$(if $(PKG_CPE_ID),CPE-ID: $(PKG_CPE_ID)
183 )$(if $(filter hold,$(PKG_FLAGS)),Status: unknown hold not-installed
184 )$(if $(filter essential,$(PKG_FLAGS)),Essential: yes
185 )$(if $(MAINTAINER),Maintainer: $(MAINTAINER)
186 )Architecture: $(PKGARCH)
187 Installed-Size: 0
188 $(_endef)
189
190 $$(IPKG_$(1)) : export CONTROL=$$(Package/$(1)/CONTROL)
191 $$(IPKG_$(1)) : export DESCRIPTION=$$(Package/$(1)/description)
192 $$(IPKG_$(1)) : export PATH=$$(TARGET_PATH_PKG)
193 $$(IPKG_$(1)) : export PKG_SOURCE_DATE_EPOCH:=$(PKG_SOURCE_DATE_EPOCH)
194 $(PKG_INFO_DIR)/$(1).provides $$(IPKG_$(1)): $(STAMP_BUILT) $(INCLUDE_DIR)/package-ipkg.mk
195 @rm -rf $$(IDIR_$(1)); \
196 $$(call remove_ipkg_files,$(1),$$(call opkg_package_files,$(call gen_ipkg_wildcard,$(1))))
197 mkdir -p $(PACKAGE_DIR) $$(IDIR_$(1))/CONTROL $(PKG_INFO_DIR)
198 $(call Package/$(1)/install,$$(IDIR_$(1)))
199 $(if $(Package/$(1)/install-overlay),mkdir -p $(PACKAGE_DIR) $$(IDIR_$(1))/rootfs-overlay)
200 $(call Package/$(1)/install-overlay,$$(IDIR_$(1))/rootfs-overlay)
201 -find $$(IDIR_$(1)) -name 'CVS' -o -name '.svn' -o -name '.#*' -o -name '*~'| $(XARGS) rm -rf
202 @( \
203 find $$(IDIR_$(1)) -name lib\*.so\* -or -name \*.ko | awk -F/ '{ print $$$$NF }'; \
204 for file in $$(patsubst %,$(PKG_INFO_DIR)/%.provides,$$(IDEPEND_$(1))); do \
205 if [ -f "$$$$file" ]; then \
206 cat $$$$file; \
207 fi; \
208 done; $(Package/$(1)/extra_provides) \
209 ) | sort -u > $(PKG_INFO_DIR)/$(1).provides
210 $(if $(PROVIDES),@for pkg in $(filter-out $(1),$(PROVIDES)); do cp $(PKG_INFO_DIR)/$(1).provides $(PKG_INFO_DIR)/$$$$pkg.provides; done)
211 $(CheckDependencies)
212
213 $(RSTRIP) $$(IDIR_$(1))
214
215 ifneq ($$(CONFIG_IPK_FILES_CHECKSUMS),)
216 (cd $$(IDIR_$(1)); \
217 ( \
218 find . -type f \! -path ./CONTROL/\* -exec mkhash sha256 -n \{\} \; 2> /dev/null | \
219 sed 's|\([[:blank:]]\)\./| \1/|' > $$(IDIR_$(1))/CONTROL/files-sha256sum \
220 ) || true \
221 )
222 endif
223 (cd $$(IDIR_$(1))/CONTROL; \
224 ( \
225 echo "$$$$CONTROL"; \
226 printf "Description: "; echo "$$$$DESCRIPTION" | sed -e 's,^[[:space:]]*, ,g'; \
227 ) > control; \
228 chmod 644 control; \
229 ( \
230 echo "#!/bin/sh"; \
231 echo "[ \"\$$$${IPKG_NO_SCRIPT}\" = \"1\" ] && exit 0"; \
232 echo "[ -s "\$$$${IPKG_INSTROOT}/lib/functions.sh" ] || exit 0"; \
233 echo ". \$$$${IPKG_INSTROOT}/lib/functions.sh"; \
234 echo "default_postinst \$$$$0 \$$$$@"; \
235 ) > postinst; \
236 ( \
237 echo "#!/bin/sh"; \
238 echo "[ -s "\$$$${IPKG_INSTROOT}/lib/functions.sh" ] || exit 0"; \
239 echo ". \$$$${IPKG_INSTROOT}/lib/functions.sh"; \
240 echo "default_prerm \$$$$0 \$$$$@"; \
241 ) > prerm; \
242 chmod 0755 postinst prerm; \
243 $($(1)_COMMANDS) \
244 )
245
246 ifneq ($$(KEEP_$(1)),)
247 @( \
248 keepfiles=""; \
249 for x in $$(KEEP_$(1)); do \
250 [ -f "$$(IDIR_$(1))/$$$$x" ] || keepfiles="$$$${keepfiles:+$$$$keepfiles }$$$$x"; \
251 done; \
252 [ -z "$$$$keepfiles" ] || { \
253 mkdir -p $$(IDIR_$(1))/lib/upgrade/keep.d; \
254 for x in $$$$keepfiles; do echo $$$$x >> $$(IDIR_$(1))/lib/upgrade/keep.d/$(1); done; \
255 }; \
256 )
257 endif
258
259 $(INSTALL_DIR) $$(PDIR_$(1))
260 $(FAKEROOT) $(SCRIPT_DIR)/ipkg-build -m "$(FILE_MODES)" $$(IDIR_$(1)) $$(PDIR_$(1))
261 @[ -f $$(IPKG_$(1)) ]
262
263 $(1)-clean:
264 $$(call remove_ipkg_files,$(1),$$(call opkg_package_files,$(call gen_ipkg_wildcard,$(1))))
265
266 clean: $(1)-clean
267
268 endef
269 endif