d40d692aa5d99038ef00c3e4b2469697619d4055
[oweals/openwrt.git] / openwrt / package / rules.mk
1 # default target
2 ifneq ($(DUMP),)
3 all: dumpinfo
4 else
5 all: compile
6 endif
7
8 define Build/DefaultTargets
9 $(PKG_BUILD_DIR)/.prepared:
10         rm -rf $(PKG_BUILD_DIR)
11         mkdir -p $(PKG_BUILD_DIR)
12         $(call Build/Prepare)
13         touch $$@
14
15 $(PKG_BUILD_DIR)/.configured:
16         $(call Build/Configure)
17         touch $$@
18
19 $(PKG_BUILD_DIR)/.built:
20         $(call Build/Compile)
21         touch $$@
22
23 define Build/DefaultTargets
24 endef
25 endef
26
27
28 define Package/Default
29 CONFIGFILE:=
30 SECTION:=opt
31 CATEGORY:=Extra packages
32 DEPENDS:=
33 MAINTAINER:=OpenWrt Developers Team <openwrt-devel@openwrt.org>
34 SOURCE:=$(patsubst $(TOPDIR)/%,%,${shell pwd})
35 VERSION:=$(PKG_VERSION)-$(PKG_RELEASE)
36 PKGARCH:=$(ARCH)
37 PRIORITY:=optional
38 TITLE:=
39 DESCRIPTION:=
40 endef
41
42 define BuildPackage
43 $$(eval $$(call Package/Default))
44 $$(eval $$(call Package/$(1)))
45
46 ifeq ($$(TITLE),)
47 $$(error Package $(1) has no TITLE)
48 endif
49 ifeq ($$(CATEGORY),)
50 $$(error Package $(1) has no CATEGORY)
51 endif
52 ifeq ($$(PRIORITY),)
53 $$(error Package $(1) has no PRIORITY)
54 endif
55 ifeq ($$(VERSION),)
56 $$(error Package $(1) has no VERSION)
57 endif
58 ifeq ($$(PKGARCH),)
59 PKGARCH:=$(ARCH)
60 endif
61
62 IPKG_$(1):=$(PACKAGE_DIR)/$(1)_$(VERSION)_$(PKGARCH).ipk
63 IDIR_$(1):=$(PKG_BUILD_DIR)/ipkg/$(1)
64 INFO_$(1):=$(IPKG_STATE_DIR)/info/$(1).list
65
66 ifneq ($(PACKAGE_$(1)),)
67 compile-targets: $$(IPKG_$(1))
68 endif
69 ifneq ($(DEVELOPER),)
70 compile-targets: $$(IPKG_$(1))
71 endif
72 ifeq ($(PACKAGE_$(1)),y)
73 install-targets: $$(INFO_$(1))
74 endif
75
76 IDEPEND_$(1):=$$(strip $$(DEPENDS))
77 CONTROLINFO_$(1) = \
78         echo "Package: $(1)" > $$(IDIR_$(1))/CONTROL/control; \
79         echo "Version: $$(VERSION)" >> $$(IDIR_$(1))/CONTROL/control; \
80         echo "Depends: $$(IDEPEND_$(1))" >> $$(IDIR_$(1))/CONTROL/control; \
81         echo "Source: $$(SOURCE)" >> $$(IDIR_$(1))/CONTROL/control; \
82         echo "Section: $$(SECTION)" >> $$(IDIR_$(1))/CONTROL/control; \
83         echo "Priority: $$(PRIORITY)" >> $$(IDIR_$(1))/CONTROL/control; \
84         echo "Maintainer: $$(MAINTAINER)" >> $$(IDIR_$(1))/CONTROL/control; \
85         echo "Architecture: $$(PKGARCH)" >> $$(IDIR_$(1))/CONTROL/control; \
86         echo "Description: $$(TITLE)" >> $$(IDIR_$(1))/CONTROL/control; \
87         echo "$$(DESCRIPTION)" | sed -e 's,\\,\n ,g' >> $$(IDIR_$(1))/CONTROL/control;
88
89 $$(IDIR_$(1))/CONTROL/control: $(PKG_BUILD_DIR)/.prepared
90         mkdir -p $$(IDIR_$(1))/CONTROL
91         $$(CONTROLINFO_$(1))
92         chmod 644 $$(IDIR_$(1))/CONTROL/control
93         for file in conffiles preinst postinst prerm postrm; do \
94                 [ -f ./ipkg/$(1).$$$$file ] && cp ./ipkg/$(1).$$$$file $$(IDIR_$(1))/CONTROL/$$$$file || true; \
95         done
96
97 $$(IPKG_$(1)): $$(IDIR_$(1))/CONTROL/control $(PKG_BUILD_DIR)/.built $(PACKAGE_DIR)
98         $(call Package/$(1)/install,$$(IDIR_$(1)))
99         $(IPKG_BUILD) $$(IDIR_$(1)) $(PACKAGE_DIR)
100
101 $$(INFO_$(1)): $$(IPKG_$(1))
102         $(IPKG) install $$(IPKG_$(1))
103
104 $(1)-clean:
105         rm -f $$(IPKG_$(1))
106 clean: $(1)-clean
107
108 DUMPINFO += \
109         echo "Package: $(1)"; \
110         echo "Version: $$(VERSION)"; \
111         echo "Depends: $$(IDEPEND_$(1))"; \
112         echo "Title: $$(TITLE)"; \
113         echo "$$(DESCRIPTION)" | sed -e 's,\\,\n,g'; \
114         echo; \
115         echo "$$(URL)"; \
116         echo "@@";
117
118 PACKAGES += $(1)
119
120 ifneq ($(__DEFAULT_TARGETS),1)
121 $(eval $(call Build/DefaultTargets))
122 endif
123
124 endef
125
126 ifneq ($(strip $(PKG_SOURCE)),)
127 $(DL_DIR)/$(PKG_SOURCE):
128         @$(CMD_TRACE) "downloading... "
129         $(SCRIPT_DIR)/download.pl "$(DL_DIR)" "$(PKG_SOURCE)" "$(PKG_MD5SUM)" $(PKG_SOURCE_URL) $(MAKE_TRACE) 
130         
131 $(PKG_BUILD_DIR)/.prepared: $(DL_DIR)/$(PKG_SOURCE)
132 endif
133
134 ifneq ($(strip $(PKG_CAT)),)
135 define Build/Prepare/Default
136         if [ "$(PKG_CAT)" = "unzip" ]; then \
137                 unzip -d $(PKG_BUILD_DIR) $(DL_DIR)/$(PKG_SOURCE) ; \
138         else \
139                 $(PKG_CAT) $(DL_DIR)/$(PKG_SOURCE) | tar -C $(PKG_BUILD_DIR)/.. $(TAR_OPTIONS) - ; \
140         fi                                                
141         if [ -d ./patches ]; then \
142                 $(PATCH) $(PKG_BUILD_DIR) ./patches ; \
143         fi
144 endef
145 endif
146
147 define Build/Prepare
148 $(call Build/Prepare/Default)
149 endef
150
151 define Build/Configure/Default
152 # TODO: add configurable default command
153 endef
154
155 define Build/Configure
156 $(call Build/Configure/Default)
157 endef
158
159 define Build/Compile/Default
160 # TODO: add configurable default command
161 endef
162
163 define Build/Compile
164 $(call Build/Compile/Default)
165 endef
166
167 ifneq ($(DUMP),)
168 dumpinfo:
169         $(DUMPINFO)
170 else
171
172 source: $(DL_DIR)/$(PKG_SOURCE)
173 prepare: source
174         @[ -f $(PKG_BUILD_DIR)/.prepared ] || { \
175                 $(CMD_TRACE) "preparing... "; \
176                 $(MAKE) $(PKG_BUILD_DIR)/.prepared $(MAKE_TRACE); \
177         }
178
179 configure: prepare
180         @[ -f $(PKG_BUILD_DIR)/.configured ] || { \
181                 $(CMD_TRACE) "configuring... "; \
182                 $(MAKE) $(PKG_BUILD_DIR)/.configured $(MAKE_TRACE); \
183         }
184
185 compile-targets:
186 compile: configure
187         @$(CMD_TRACE) "compiling... " 
188         @$(MAKE) compile-targets $(MAKE_TRACE)
189
190 install-targets:
191 install:
192         @$(CMD_TRACE) "installing... "
193         @$(MAKE) install-targets $(MAKE_TRACE)
194
195 mostlyclean:
196 rebuild:
197         $(CMD_TRACE) "rebuilding... "
198         @-$(MAKE) mostlyclean 2>&1 >/dev/null
199         if [ -f $(PKG_BUILD_DIR)/.built ]; then \
200                 $(MAKE) clean $(MAKE_TRACE); \
201         fi
202         $(MAKE) compile $(MAKE_TRACE)
203
204 $(PACKAGE_DIR):
205         mkdir -p $@
206
207 clean-targets:
208 clean: 
209         @$(CMD_TRACE) "cleaning... " 
210         @$(MAKE) clean-targets $(MAKE_TRACE)
211         rm -rf $(PKG_BUILD_DIR)
212 endif
213
214 .PHONY: all source prepare compile install clean dumpinfo