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