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