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