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