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