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