add support for per-package prereq checks, run global prereq checks before (menu...
[openwrt-10.03/.git] / include / package.mk
1
2 # Copyright (C) 2006 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7 ifneq ($(DUMP),)
8   all: dumpinfo
9 else
10   all: compile
11 endif
12
13 include $(INCLUDE_DIR)/prereq.mk
14
15 define Build/DefaultTargets
16   ifeq ($(DUMP),)
17     ifeq ($(CONFIG_AUTOREBUILD),y)
18       ifneq ($$(shell $(SCRIPT_DIR)/timestamp.pl -p $(PKG_BUILD_DIR) .),$(PKG_BUILD_DIR))
19         $(PKG_BUILD_DIR)/.prepared: package-clean
20       endif
21
22       ifneq ($$(shell $(SCRIPT_DIR)/timestamp.pl -p -x ipkg $(IPKG_$(1)) $(PKG_BUILD_DIR)),$(IPKG_$(1)))
23         $(PKG_BUILD_DIR)/.built: package-rebuild
24       endif
25     endif
26   endif
27
28
29   $(PKG_BUILD_DIR)/.prepared:
30         @-rm -rf $(PKG_BUILD_DIR)
31         @mkdir -p $(PKG_BUILD_DIR)
32         $(call Build/Prepare)
33         touch $$@
34
35   $(PKG_BUILD_DIR)/.configured: $(PKG_BUILD_DIR)/.prepared
36         $(call Build/Configure)
37         touch $$@
38
39   $(PKG_BUILD_DIR)/.built: $(PKG_BUILD_DIR)/.configured
40         $(call Build/Compile)
41         touch $$@
42
43   $(STAGING_DIR)/stampfiles/.$(PKG_NAME)-installed: $(PKG_BUILD_DIR)/.built
44         mkdir -p $(STAGING_DIR)/stampfiles
45         $(call Build/InstallDev)
46         touch $$@
47         
48   ifdef Build/InstallDev
49     compile-targets: $(STAGING_DIR)/stampfiles/.$(PKG_NAME)-installed
50   endif
51
52   package-clean: FORCE
53         $(call Build/Clean)
54         $(call Build/UninstallDev)
55         rm -f $(STAGING_DIR)/stampfiles/.$(PKG_NAME)-installed
56
57   package-rebuild: FORCE
58         @-rm -f $(PKG_BUILD_DIR)/.built
59
60   define Build/DefaultTargets
61   endef
62 endef
63
64 define Package/Default
65   CONFIGFILE:=
66   SECTION:=opt
67   CATEGORY:=Extra packages
68   DEPENDS:=
69   EXTRA_DEPENDS:=
70   MAINTAINER:=OpenWrt Developers Team <openwrt-devel@openwrt.org>
71   SOURCE:=$(patsubst $(TOPDIR)/%,%,${shell pwd})
72   ifneq ($(PKG_VERSION),)
73     ifneq ($(PKG_RELEASE),)
74       VERSION:=$(PKG_VERSION)-$(PKG_RELEASE)
75     else
76       VERSION:=$(PKG_VERSION)
77     endif
78   else
79     VERSION:=$(PKG_RELEASE)
80   endif
81   PKGARCH:=$(ARCH)
82   PRIORITY:=optional
83   DEFAULT:=
84   MENU:=
85   SUBMENU:=
86   SUBMENUDEP:=
87   TITLE:=
88   DESCRIPTION:=
89 endef
90
91 define BuildIPKGVariable
92 pkg_$(subst .,_,$(subst -,_,$(1)))_$(2) = $$(Package/$(1)/$(2))
93 export pkg_$(subst .,_,$(subst -,_,$(1))_$(2))
94 $(1)_COMMANDS += if [ -n "$$$$$$$$pkg_$(subst .,_,$(subst -,_,$(1)))_$(2)" ]; then echo "$$$$$$$$pkg_$(subst .,_,$(subst -,_,$(1)))_$(2)" > $(2); fi;
95 endef
96
97 define BuildPackage
98   $(eval $(call Package/Default))
99   $(eval $(call Package/$(1)))
100
101   $(foreach FIELD, TITLE CATEGORY PRIORITY VERSION,
102     ifeq ($($(FIELD)),)
103       $$(error Package/$(1) is missing the $(FIELD) field)
104     endif
105   )
106
107   ifeq ($(PKGARCH),)
108     PKGARCH:=$(ARCH)
109   endif
110
111   ifeq ($(DESCRIPTION),)
112     $(eval DESCRIPTION:=$(TITLE))
113   endif
114
115   IPKG_$(1):=$(PACKAGE_DIR)/$(1)_$(VERSION)_$(PKGARCH).ipk
116   IDIR_$(1):=$(PKG_BUILD_DIR)/ipkg/$(1)
117   INFO_$(1):=$(IPKG_STATE_DIR)/info/$(1).list
118
119   ifdef Package/$(1)/install
120     ifeq ($(CONFIG_PACKAGE_$(1)),y)
121       install-targets: $$(INFO_$(1))
122     endif
123
124     ifneq ($(CONFIG_PACKAGE_$(1))$(DEVELOPER),)
125       compile-targets: $$(IPKG_$(1))
126     endif
127   endif
128
129   ifeq ($(FORCEREBUILD),y)
130     $$(IPKG_$(1)): FORCE
131   endif
132
133   IDEPEND_$(1):=$$(strip $$(DEPENDS))
134
135   DUMPINFO += \
136         echo "Package: $(1)"; 
137
138   ifneq ($(MENU),)
139     DUMPINFO += \
140         echo "Menu: $(MENU)";
141   endif
142
143   ifneq ($(SUBMENU),)
144     DUMPINFO += \
145         echo "Submenu: $(SUBMENU)";
146     ifneq ($(SUBMENUDEP),)
147       DUMPINFO += \
148           echo "Submenu-Depends: $(SUBMENUDEP)";
149     endif
150   endif
151
152   ifneq ($(DEFAULT),)
153     DUMPINFO += \
154         echo "Default: $(DEFAULT)";
155   endif
156
157   DUMPINFO += \
158         echo "Version: $(VERSION)"; \
159         echo "Depends: $$(IDEPEND_$(1))"; \
160         echo "Build-Depends: $(PKG_BUILDDEP)"; \
161         echo "Category: $(CATEGORY)"; \
162         echo "Title: $(TITLE)"; \
163         echo "Description: $(DESCRIPTION)" | sed -e 's,\\,\n,g';
164
165   ifneq ($(URL),)
166     DUMPINFO += \
167         echo; \
168         echo "$(URL)";
169   endif
170
171   DUMPINFO += \
172         echo "@@";
173
174   ifneq ($(CONFIG),)
175     DUMPINFO += \
176         echo "Config: $(CONFIG)" | sed -e 's,\\,\n,g'; \
177         echo "@@";
178   endif
179
180   $(eval $(call BuildIPKGVariable,$(1),conffiles))
181   $(eval $(call BuildIPKGVariable,$(1),preinst))
182   $(eval $(call BuildIPKGVariable,$(1),postinst))
183   $(eval $(call BuildIPKGVariable,$(1),prerm))
184   $(eval $(call BuildIPKGVariable,$(1),postrm))
185   $$(IDIR_$(1))/CONTROL/control: Makefile $(PKG_BUILD_DIR)/.prepared $(PKG_BUILD_DIR)/.version-$(1)_$(VERSION)_$(PKGARCH)
186         mkdir -p $$(IDIR_$(1))/CONTROL
187         echo "Package: $(1)" > $$(IDIR_$(1))/CONTROL/control
188         echo "Version: $(VERSION)" >> $$(IDIR_$(1))/CONTROL/control
189         ( \
190                 DEPENDS=; \
191                 for depend in $$(filter-out @%,$$(IDEPEND_$(1))); do \
192                         DEPENDS=$$$${DEPENDS:+$$$$DEPENDS, }$$$${depend##+}; \
193                 done; \
194                 echo "Depends: $(EXTRA_DEPENDS) $$$$DEPENDS" >> $$(IDIR_$(1))/CONTROL/control; \
195         )
196         echo "Source: $(SOURCE)" >> $$(IDIR_$(1))/CONTROL/control
197         echo "Section: $(SECTION)" >> $$(IDIR_$(1))/CONTROL/control
198         echo "Priority: $(PRIORITY)" >> $$(IDIR_$(1))/CONTROL/control
199         echo "Maintainer: $(MAINTAINER)" >> $$(IDIR_$(1))/CONTROL/control
200         echo "Architecture: $(PKGARCH)" >> $$(IDIR_$(1))/CONTROL/control
201         echo "Description: $(DESCRIPTION)" | sed -e 's,\\,\n ,g' >> $$(IDIR_$(1))/CONTROL/control
202         chmod 644 $$(IDIR_$(1))/CONTROL/control
203         (cd $$(IDIR_$(1))/CONTROL; \
204                 $($(1)_COMMANDS) \
205         )
206
207   $$(IPKG_$(1)): $$(IDIR_$(1))/CONTROL/control $(PKG_BUILD_DIR)/.built
208         $(call Package/$(1)/install,$$(IDIR_$(1)))
209         mkdir -p $(PACKAGE_DIR)
210         -find $$(IDIR_$(1)) -name CVS | xargs rm -rf
211         -find $$(IDIR_$(1)) -name .svn | xargs rm -rf
212         -find $$(IDIR_$(1)) -name '.#*' | xargs rm -f
213         $(RSTRIP) $$(IDIR_$(1))
214         $(IPKG_BUILD) $$(IDIR_$(1)) $(PACKAGE_DIR)
215         @[ -f $$(IPKG_$(1)) ] || false 
216
217   $$(INFO_$(1)): $$(IPKG_$(1))
218         $(IPKG) install $$(IPKG_$(1))
219
220   $(1)-clean:
221         rm -f $(PACKAGE_DIR)/$(1)_*
222
223   clean: $(1)-clean
224
225   $(PKG_BUILD_DIR)/.version-$(1)_$(VERSION)_$(PKGARCH): $(PKG_BUILD_DIR)/.prepared
226         -rm $(PKG_BUILD_DIR)/.version-$(1)_*
227         @touch $$@
228
229   $$(eval $$(call Build/DefaultTargets,$(1)))
230
231 endef
232
233 ifneq ($(strip $(PKG_CAT)),)
234   ifeq ($(PKG_CAT),unzip)
235     UNPACK=unzip -d $(PKG_BUILD_DIR) $(DL_DIR)/$(PKG_SOURCE)
236   else
237     UNPACK=$(PKG_CAT) $(DL_DIR)/$(PKG_SOURCE) | tar -C $(PKG_BUILD_DIR)/.. $(TAR_OPTIONS) -
238   endif
239   define Build/Prepare/Default
240         $(UNPACK)
241         @if [ -d ./patches ]; then \
242                 $(PATCH) $(PKG_BUILD_DIR) ./patches; \
243         fi
244   endef
245 endif
246
247 define Build/Prepare
248   $(call Build/Prepare/Default)
249 endef
250
251 define Build/Configure/Default
252         @(cd $(PKG_BUILD_DIR)/$(3); \
253         [ -x configure ] && \
254                 $(2) \
255                 $(TARGET_CONFIGURE_OPTS) \
256                 CFLAGS="$(TARGET_CFLAGS)" \
257                 CPPFLAGS="-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include" \
258                 LDFLAGS="-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib" \
259                 ./configure \
260                 --target=$(GNU_TARGET_NAME) \
261                 --host=$(GNU_TARGET_NAME) \
262                 --build=$(GNU_HOST_NAME) \
263                 --program-prefix="" \
264                 --program-suffix="" \
265                 --prefix=/usr \
266                 --exec-prefix=/usr \
267                 --bindir=/usr/bin \
268                 --sbindir=/usr/sbin \
269                 --libexecdir=/usr/lib \
270                 --sysconfdir=/etc \
271                 --datadir=/usr/share \
272                 --localstatedir=/var \
273                 --mandir=/usr/man \
274                 --infodir=/usr/info \
275                 $(DISABLE_NLS) \
276                 $(1); \
277                 true; \
278         )
279 endef
280
281 define Build/Configure
282   $(call Build/Configure/Default,)
283 endef
284
285 define Build/Compile/Default
286         $(MAKE) -C $(PKG_BUILD_DIR) \
287                 $(TARGET_CONFIGURE_OPTS) \
288                 CC=$(TARGET_CC) \
289                 CROSS="$(TARGET_CROSS)" \
290                 EXTRA_CFLAGS="$(TARGET_CFLAGS) -I$(STAGING_DIR)/include -I$(STAGING_DIR)/usr/include" \
291                 ARCH="$(ARCH)" \
292                 $(1);
293 endef
294
295 define Build/Compile
296   $(call Build/Compile/Default,)
297 endef
298
299 ifneq ($(DUMP),)
300   dumpinfo: FORCE
301         @$(DUMPINFO)
302 else
303   $(PACKAGE_DIR):
304         mkdir -p $@
305                 
306   ifneq ($(strip $(PKG_SOURCE)),)
307     download: $(DL_DIR)/$(PKG_SOURCE)
308
309     $(DL_DIR)/$(PKG_SOURCE):
310                 mkdir -p $(DL_DIR)
311                 $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(PKG_SOURCE)" "$(PKG_MD5SUM)" $(PKG_SOURCE_URL)
312
313     $(PKG_BUILD_DIR)/.prepared: $(DL_DIR)/$(PKG_SOURCE)
314   endif
315
316   download:
317   prepare: $(PKG_BUILD_DIR)/.prepared
318   configure: $(PKG_BUILD_DIR)/.configured
319
320   compile-targets:
321   compile: compile-targets
322
323   install-targets:
324   install: install-targets
325
326   clean-targets:
327   clean: FORCE
328         @$(MAKE) clean-targets
329         $(call Build/Clean)
330         rm -rf $(PKG_BUILD_DIR)
331 endif