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