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