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