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