3dc944dbfa0a21774866fd1e0027c91f5ae7d18f
[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   ifdef Build/InstallDev
44     $(STAGING_DIR)/stampfiles/.$(PKG_NAME)-installed: $(PKG_BUILD_DIR)/.built
45         mkdir -p $(STAGING_DIR)/stampfiles
46         $(call Build/InstallDev)
47         touch $$@
48         
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   ifneq ($(DUMP),)
136     DUMPINFO += \
137         echo "Package: $(1)"; 
138
139     ifneq ($(MENU),)
140       DUMPINFO += \
141         echo "Menu: $(MENU)";
142     endif
143
144     ifneq ($(SUBMENU),)
145       DUMPINFO += \
146         echo "Submenu: $(SUBMENU)";
147       ifneq ($(SUBMENUDEP),)
148         DUMPINFO += \
149           echo "Submenu-Depends: $(SUBMENUDEP)";
150       endif
151     endif
152
153     ifneq ($(DEFAULT),)
154       DUMPINFO += \
155         echo "Default: $(DEFAULT)";
156     endif
157
158     DUMPINFO += \
159         echo "Version: $(VERSION)"; \
160         echo "Depends: $$(IDEPEND_$(1))"; \
161         echo "Build-Depends: $(PKG_BUILDDEP)"; \
162         echo "Category: $(CATEGORY)"; \
163         echo "Title: $(TITLE)"; \
164         echo "Description: $(DESCRIPTION)" | sed -e 's,\\,\n,g';
165
166     ifneq ($(URL),)
167       DUMPINFO += \
168         echo; \
169         echo "$(URL)";
170     endif
171
172     DUMPINFO += \
173         echo "@@";
174
175     ifneq ($(CONFIG),)
176       DUMPINFO += \
177         echo "Config: $(CONFIG)" | sed -e 's,\\,\n,g'; \
178         echo "@@";
179     endif
180   endif
181
182   $(eval $(call BuildIPKGVariable,$(1),conffiles))
183   $(eval $(call BuildIPKGVariable,$(1),preinst))
184   $(eval $(call BuildIPKGVariable,$(1),postinst))
185   $(eval $(call BuildIPKGVariable,$(1),prerm))
186   $(eval $(call BuildIPKGVariable,$(1),postrm))
187   $$(IDIR_$(1))/CONTROL/control: $(PKG_BUILD_DIR)/.version-$(1)_$(VERSION)_$(PKGARCH)
188         mkdir -p $$(IDIR_$(1))/CONTROL
189         echo "Package: $(1)" > $$(IDIR_$(1))/CONTROL/control
190         echo "Version: $(VERSION)" >> $$(IDIR_$(1))/CONTROL/control
191         ( \
192                 DEPENDS=; \
193                 for depend in $$(filter-out @%,$$(IDEPEND_$(1))); do \
194                         DEPENDS=$$$${DEPENDS:+$$$$DEPENDS, }$$$${depend##+}; \
195                 done; \
196                 echo "Depends: $(EXTRA_DEPENDS) $$$$DEPENDS" >> $$(IDIR_$(1))/CONTROL/control; \
197         )
198         echo "Source: $(SOURCE)" >> $$(IDIR_$(1))/CONTROL/control
199         echo "Section: $(SECTION)" >> $$(IDIR_$(1))/CONTROL/control
200         echo "Priority: $(PRIORITY)" >> $$(IDIR_$(1))/CONTROL/control
201         echo "Maintainer: $(MAINTAINER)" >> $$(IDIR_$(1))/CONTROL/control
202         echo "Architecture: $(PKGARCH)" >> $$(IDIR_$(1))/CONTROL/control
203         echo "Description: $(DESCRIPTION)" | sed -e 's,\\,\n ,g' >> $$(IDIR_$(1))/CONTROL/control
204         chmod 644 $$(IDIR_$(1))/CONTROL/control
205         (cd $$(IDIR_$(1))/CONTROL; \
206                 $($(1)_COMMANDS) \
207         )
208
209   $$(IPKG_$(1)): $(PKG_BUILD_DIR)/.built $$(IDIR_$(1))/CONTROL/control
210         $(call Package/$(1)/install,$$(IDIR_$(1)))
211         mkdir -p $(PACKAGE_DIR)
212         -find $$(IDIR_$(1)) -name CVS | xargs rm -rf
213         -find $$(IDIR_$(1)) -name .svn | xargs rm -rf
214         -find $$(IDIR_$(1)) -name '.#*' | xargs rm -f
215         $(RSTRIP) $$(IDIR_$(1))
216         $(IPKG_BUILD) $$(IDIR_$(1)) $(PACKAGE_DIR)
217         @[ -f $$(IPKG_$(1)) ] || false 
218
219   $$(INFO_$(1)): $$(IPKG_$(1))
220         $(IPKG) install $$(IPKG_$(1))
221
222   $(1)-clean:
223         rm -f $(PACKAGE_DIR)/$(1)_*
224
225   clean: $(1)-clean
226
227   $(PKG_BUILD_DIR)/.version-$(1)_$(VERSION)_$(PKGARCH): $(PKG_BUILD_DIR)/.prepared
228         -rm $(PKG_BUILD_DIR)/.version-$(1)_*
229         @touch $$@
230
231   $$(eval $$(call Build/DefaultTargets,$(1)))
232 endef
233
234 ifneq ($(strip $(PKG_CAT)),)
235   ifeq ($(PKG_CAT),unzip)
236     UNPACK=unzip -d $(PKG_BUILD_DIR) $(DL_DIR)/$(PKG_SOURCE)
237   else
238     UNPACK=$(PKG_CAT) $(DL_DIR)/$(PKG_SOURCE) | tar -C $(PKG_BUILD_DIR)/.. $(TAR_OPTIONS) -
239   endif
240   define Build/Prepare/Default
241         $(UNPACK)
242         @if [ -d ./patches ]; then \
243                 $(PATCH) $(PKG_BUILD_DIR) ./patches; \
244         fi
245   endef
246 endif
247
248 define Build/Prepare
249   $(call Build/Prepare/Default)
250 endef
251
252 define Build/Configure/Default
253         @(cd $(PKG_BUILD_DIR)/$(3); \
254         [ -x configure ] && \
255                 $(2) \
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                 ./configure \
261                 --target=$(GNU_TARGET_NAME) \
262                 --host=$(GNU_TARGET_NAME) \
263                 --build=$(GNU_HOST_NAME) \
264                 --program-prefix="" \
265                 --program-suffix="" \
266                 --prefix=/usr \
267                 --exec-prefix=/usr \
268                 --bindir=/usr/bin \
269                 --sbindir=/usr/sbin \
270                 --libexecdir=/usr/lib \
271                 --sysconfdir=/etc \
272                 --datadir=/usr/share \
273                 --localstatedir=/var \
274                 --mandir=/usr/man \
275                 --infodir=/usr/info \
276                 $(DISABLE_NLS) \
277                 $(1); \
278                 true; \
279         )
280 endef
281
282 define Build/Configure
283   $(call Build/Configure/Default,)
284 endef
285
286 define Build/Compile/Default
287         $(MAKE) -C $(PKG_BUILD_DIR) \
288                 $(TARGET_CONFIGURE_OPTS) \
289                 CC=$(TARGET_CC) \
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