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