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