pass the CFLAGS through the environment on compile and fix a few packages
[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       ifneq ($$(shell $(SCRIPT_DIR)/timestamp.pl -p $(PKG_BUILD_DIR) . $(PKG_FILE_DEPEND)),$(PKG_BUILD_DIR))
24         $$(info Forcing package rebuild)
25         $(PKG_BUILD_DIR)/.prepared: package-clean
26       endif
27     endif
28   endif
29
30   $(PKG_BUILD_DIR)/.prepared:
31         @-rm -rf $(PKG_BUILD_DIR)
32         @mkdir -p $(PKG_BUILD_DIR)
33         $(call Build/Prepare)
34         touch $$@
35
36   $(PKG_BUILD_DIR)/.configured: $(PKG_BUILD_DIR)/.prepared
37         $(call Build/Configure)
38         touch $$@
39
40   $(PKG_BUILD_DIR)/.built: $(PKG_BUILD_DIR)/.configured
41         $(call Build/Compile)
42         touch $$@
43
44   ifdef Build/InstallDev
45     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)
46       $(PKG_BUILD_DIR)/.built: package-rebuild
47     endif
48
49     $(STAGING_DIR)/stampfiles/.$(PKG_NAME)-installed: $(PKG_BUILD_DIR)/.built
50         mkdir -p $(STAGING_DIR)/stampfiles
51         $(call Build/InstallDev)
52         touch $$@
53         
54     compile-targets: $(STAGING_DIR)/stampfiles/.$(PKG_NAME)-installed
55   endif
56
57   package-clean: FORCE
58         $(call Build/Clean)
59         $(call Build/UninstallDev)
60         -rm -f $(STAGING_DIR)/stampfiles/.$(PKG_NAME)-installed
61
62   package-rebuild: FORCE
63         @-rm -f $(PKG_BUILD_DIR)/.built
64
65   define Build/DefaultTargets
66   endef
67 endef
68
69 define Package/Default
70   CONFIGFILE:=
71   SECTION:=opt
72   CATEGORY:=Extra packages
73   DEPENDS:=
74   PROVIDES:=
75   EXTRA_DEPENDS:=
76   MAINTAINER:=OpenWrt Developers Team <openwrt-devel@openwrt.org>
77   SOURCE:=$(patsubst $(TOPDIR)/%,%,${shell pwd})
78   ifneq ($(PKG_VERSION),)
79     ifneq ($(PKG_RELEASE),)
80       VERSION:=$(PKG_VERSION)-$(PKG_RELEASE)
81     else
82       VERSION:=$(PKG_VERSION)
83     endif
84   else
85     VERSION:=$(PKG_RELEASE)
86   endif
87   PKGARCH:=$(ARCH)
88   PRIORITY:=optional
89   DEFAULT:=
90   MENU:=
91   SUBMENU:=
92   SUBMENUDEP:=
93   TITLE:=
94   DESCRIPTION:=
95 endef
96
97 define BuildDescription
98   ifneq ($(DESCRIPTION),)
99     DESCRIPTION:=$(TITLE)\\ $(DESCRIPTION)
100   else
101     DESCRIPTION:=$(TITLE)
102   endif
103 endef
104
105 define BuildIPKGVariable
106   $(call shexport,Package/$(1)/$(2))
107   $(1)_COMMANDS += var2file "$(call shvar,Package/$(1)/$(2))" $(2);
108 endef
109
110 define BuildPackage
111   $(eval $(call Package/Default))
112   $(eval $(call Package/$(1)))
113   $(eval $(call BuildDescription))
114
115   $(foreach FIELD, TITLE CATEGORY PRIORITY SECTION VERSION,
116     ifeq ($($(FIELD)),)
117       $$(error Package/$(1) is missing the $(FIELD) field)
118     endif
119   )
120
121   IPKG_$(1):=$(PACKAGE_DIR)/$(1)_$(VERSION)_$(PKGARCH).ipk
122   IDIR_$(1):=$(PKG_BUILD_DIR)/ipkg/$(1)
123   INFO_$(1):=$(IPKG_STATE_DIR)/info/$(1).list
124
125   ifdef Package/$(1)/install
126     ifeq ($(CONFIG_PACKAGE_$(1)),y)
127       install-targets: $$(INFO_$(1))
128     endif
129
130     ifneq ($(CONFIG_PACKAGE_$(1))$(DEVELOPER)$(SDK),)
131       compile-targets: $$(IPKG_$(1))
132     else
133       compile-targets: $(1)-disabled
134       $(1)-disabled:
135         @echo "WARNING: skipping $(1) -- package not selected"
136     endif
137   endif
138
139   ifeq ($(FORCEREBUILD),y)
140     $$(IPKG_$(1)): FORCE
141   endif
142
143   IDEPEND_$(1):=$$(strip $$(DEPENDS))
144
145   ifneq ($(DUMP),)
146     dumpinfo: dumpinfo-$(1)
147     dumpinfo-$(1): FORCE
148                 @$$(DUMPINFO_$(call shvar,$(1)))
149                 
150     DUMPINFO_$(call shvar,$(1)) += \
151         echo "Package: $(1)"; 
152
153     ifneq ($(MENU),)
154       DUMPINFO_$(call shvar,$(1)) += \
155         echo "Menu: $(MENU)";
156     endif
157
158     ifneq ($(SUBMENU),)
159       DUMPINFO_$(call shvar,$(1)) += \
160         echo "Submenu: $(SUBMENU)";
161       ifneq ($(SUBMENUDEP),)
162         DUMPINFO_$(call shvar,$(1)) += \
163           echo "Submenu-Depends: $(SUBMENUDEP)";
164       endif
165     endif
166
167     ifneq ($(DEFAULT),)
168       DUMPINFO_$(call shvar,$(1)) += \
169         echo "Default: $(DEFAULT)";
170     endif
171
172         $(call shexport,Package/$(1)/description)
173
174     DUMPINFO_$(call shvar,$(1)) += \
175         if [ "$$$$PREREQ_CHECK" = 1 ]; then echo "Prereq-Check: 1"; fi; \
176         echo "Version: $(VERSION)"; \
177         echo "Depends: $$(IDEPEND_$(1))"; \
178         echo "Provides: $(PROVIDES)"; \
179         echo "Build-Depends: $(PKG_BUILDDEP)"; \
180         echo "Section: $(SECTION)"; \
181         echo "Category: $(CATEGORY)"; \
182         echo "Title: $(TITLE)"; \
183         echo "Maintainer: $(MAINTAINER)"; \
184         if isset $(call shvar,Package/$(1)/description); then \
185                 echo -n "Description: "; \
186                 getvar $(call shvar,Package/$(1)/description); \
187         else \
188                 echo "Description: $(patsubst \\,\\\\,$(DESCRIPTION))" | perl -ne 's/\\/\n/g, print'; \
189         fi;
190         
191     ifneq ($(URL),)
192       DUMPINFO_$(call shvar,$(1)) += \
193                 echo; \
194                 echo "$(URL)";
195     endif
196         
197         DUMPINFO_$(call shvar,$(1)) += \
198                 echo "@@";
199
200         $(call shexport,Package/$(1)/config)
201         DUMPINFO_$(call shvar,$(1)) += \
202                 if isset $(call shvar,Package/$(1)/config); then \
203                         echo "Config: "; \
204                         getvar $(call shvar,Package/$(1)/config); \
205                         echo "@@"; \
206                 fi;
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='$(EXTRA_DEPENDS)'; \
221                 for depend in $$(filter-out @%,$$(IDEPEND_$(1))); do \
222                         DEPENDS=$$$${DEPENDS:+$$$$DEPENDS, }$$$${depend##+}; \
223                 done; \
224                 echo "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))$(DEVELOPER)$(SDK),)
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             $(PKG_BUILD_DIR)/.built: package-rebuild
267             $$(info Rebuilding $(subst $(TOPDIR)/,,$$(IPKG_$(1))))
268           endif
269         endif
270       endif
271     endif
272   endif
273 endef
274
275 ifneq ($(strip $(PKG_UNPACK)),)
276   define Build/Prepare/Default
277         $(PKG_UNPACK)
278         @if [ -d ./patches -a "$$$$(ls ./patches | wc -l)" -gt 0 ]; then \
279                 $(PATCH) $(PKG_BUILD_DIR) ./patches; \
280         fi
281   endef
282 endif
283
284 define Build/Prepare
285   $(call Build/Prepare/Default,)
286 endef
287
288 define Build/Configure/Default
289         (cd $(PKG_BUILD_DIR)/$(strip $(3)); \
290         if [ -x configure ]; then \
291                 $(TARGET_CONFIGURE_OPTS) \
292                 CFLAGS="$(TARGET_CFLAGS)" \
293                 CXXFLAGS="$(TARGET_CFLAGS)" \
294                 CPPFLAGS="-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include" \
295                 LDFLAGS="-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib" \
296                 PKG_CONFIG_PATH="$(STAGING_DIR)/usr/lib/pkgconfig" \
297                 PKG_CONFIG_LIBDIR="$(STAGING_DIR)/usr/lib/pkgconfig" \
298                 $(2) \
299                 $(PKG_CONFIG_PATH)/configure \
300                 --target=$(GNU_TARGET_NAME) \
301                 --host=$(GNU_TARGET_NAME) \
302                 --build=$(GNU_HOST_NAME) \
303                 --program-prefix="" \
304                 --program-suffix="" \
305                 --prefix=/usr \
306                 --exec-prefix=/usr \
307                 --bindir=/usr/bin \
308                 --sbindir=/usr/sbin \
309                 --libexecdir=/usr/lib \
310                 --sysconfdir=/etc \
311                 --datadir=/usr/share \
312                 --localstatedir=/var \
313                 --mandir=/usr/man \
314                 --infodir=/usr/info \
315                 $(DISABLE_NLS) \
316                 $(1); \
317         fi; \
318         )
319 endef
320
321 define Build/Configure
322   $(call Build/Configure/Default,)
323 endef
324
325 define Build/Compile/Default
326         CFLAGS="$(TARGET_CFLAGS) $(EXTRA_CPPFLAGS) " \
327         LDFLAGS="$(EXTRA_LDFLAGS) " \
328         $(MAKE) -C $(PKG_BUILD_DIR) \
329                 $(TARGET_CONFIGURE_OPTS) \
330                 CROSS="$(TARGET_CROSS)" \
331                 CXXFLAGS="$(TARGET_CFLAGS) $(EXTRA_CPPFLAGS) " \
332                 ARCH="$(ARCH)" \
333                 $(1);
334 endef
335
336 define Build/Compile
337   $(call Build/Compile/Default,)
338 endef
339
340 ifneq ($(DUMP),)
341   dumpinfo:
342 else
343   $(PACKAGE_DIR):
344         mkdir -p $@
345                 
346   ifneq ($(strip $(PKG_SOURCE_URL)),)
347     download: $(DL_DIR)/$(PKG_SOURCE)
348
349     $(DL_DIR)/$(PKG_SOURCE):
350                 mkdir -p $(DL_DIR)
351                 $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(PKG_SOURCE)" "$(PKG_MD5SUM)" $(PKG_SOURCE_URL)
352
353     $(PKG_BUILD_DIR)/.prepared: $(DL_DIR)/$(PKG_SOURCE)
354   endif
355
356   download:
357   prepare: $(PKG_BUILD_DIR)/.prepared
358   configure: $(PKG_BUILD_DIR)/.configured
359
360   compile-targets:
361   compile: compile-targets
362
363   install-targets:
364   install: install-targets
365
366   clean-targets:
367   clean: FORCE
368         @$(MAKE) clean-targets
369         $(call Build/Clean)
370         rm -rf $(PKG_BUILD_DIR)
371 endif