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