build system: add support for download-time make hooks
[openwrt/openwrt.git] / include / package.mk
1 #
2 # Copyright (C) 2006-2008 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 all: $(if $(DUMP),dumpinfo,compile)
9
10 PKG_BUILD_DIR ?= $(BUILD_DIR)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
11 PKG_INSTALL_DIR ?= $(PKG_BUILD_DIR)/ipkg-install
12 PKG_MD5SUM ?= unknown
13 PKG_BUILD_PARALLEL ?=
14
15 ifeq ($(strip $(PKG_BUILD_PARALLEL)),0)
16 PKG_JOBS?=-j1
17 else
18 PKG_JOBS?=$(if $(PKG_BUILD_PARALLEL)$(CONFIG_PKG_DEFAULT_PARALLEL),\
19 $(if $(CONFIG_PKG_BUILD_PARALLEL),-j$(CONFIG_PKG_BUILD_JOBS),-j1),-j1)
20 endif
21
22 include $(INCLUDE_DIR)/prereq.mk
23 include $(INCLUDE_DIR)/host.mk
24 include $(INCLUDE_DIR)/unpack.mk
25 include $(INCLUDE_DIR)/depends.mk
26
27 STAMP_NO_AUTOREBUILD=$(wildcard $(PKG_BUILD_DIR)/.no_autorebuild)
28 PREV_STAMP_PREPARED:=$(if $(STAMP_NO_AUTOREBUILD),$(wildcard $(PKG_BUILD_DIR)/.prepared*))
29 ifneq ($(PREV_STAMP_PREPARED),)
30 STAMP_PREPARED:=$(PREV_STAMP_PREPARED)
31 CONFIG_AUTOREBUILD:=
32 else
33 STAMP_PREPARED=$(PKG_BUILD_DIR)/.prepared$(if $(QUILT)$(DUMP),,_$(shell $(call find_md5,${CURDIR} $(PKG_FILE_DEPENDS),)))
34 endif
35 STAMP_CONFIGURED:=$(PKG_BUILD_DIR)/.configured$(if $(DUMP),,_$(call confvar,$(PKG_CONFIG_DEPENDS)))
36 STAMP_BUILT:=$(PKG_BUILD_DIR)/.built
37 STAMP_INSTALLED:=$(STAGING_DIR)/stamp/.$(PKG_NAME)_installed
38
39 STAGING_FILES_LIST:=$(PKG_NAME)$(if $(BUILD_VARIANT),.$(BUILD_VARIANT),).list
40
41 include $(INCLUDE_DIR)/download.mk
42 include $(INCLUDE_DIR)/quilt.mk
43 include $(INCLUDE_DIR)/package-defaults.mk
44 include $(INCLUDE_DIR)/package-dumpinfo.mk
45 include $(INCLUDE_DIR)/package-ipkg.mk
46 include $(INCLUDE_DIR)/package-bin.mk
47 include $(INCLUDE_DIR)/autotools.mk
48
49 override MAKEFLAGS=
50 CONFIG_SITE:=$(INCLUDE_DIR)/site/$(REAL_GNU_TARGET_NAME)
51 CUR_MAKEFILE:=$(filter-out Makefile,$(firstword $(MAKEFILE_LIST)))
52 SUBMAKE:=$(NO_TRACE_MAKE) $(if $(CUR_MAKEFILE),-f $(CUR_MAKEFILE))
53 PKG_CONFIG_PATH=$(STAGING_DIR)/usr/lib/pkgconfig
54
55 ifeq ($(DUMP)$(filter prereq clean refresh update,$(MAKECMDGOALS)),)
56 ifneq ($(if $(QUILT),,$(CONFIG_AUTOREBUILD)),)
57 define Build/Autoclean
58 $(PKG_BUILD_DIR)/.dep_files: $(STAMP_PREPARED)
59 $(call rdep,${CURDIR} $(PKG_FILE_DEPENDS),$(STAMP_PREPARED),$(PKG_BUILD_DIR)/.dep_files,-x "*/.dep_*")
60 $(if $(filter prepare,$(MAKECMDGOALS)),,$(call rdep,$(PKG_BUILD_DIR),$(STAMP_BUILT),,-x "*/.dep_*" -x "*/ipkg*"))
61 endef
62 endif
63 endif
64
65 ifeq ($(CONFIG_$(PKG_NAME)_USE_CUSTOM_SOURCE_DIR),y)
66 # disable load stage
67 PKG_SOURCE_URL:=
68 # add hook to install a link to customer source path of dedicated package
69 Hooks/Prepare/Pre += prepare_custom_source_directory
70 # define empty default action
71 define Build/Prepare/Default
72 @:
73 endef
74 endif
75
76 define Download/default
77 FILE:=$(PKG_SOURCE)
78 URL:=$(PKG_SOURCE_URL)
79 SUBDIR:=$(PKG_SOURCE_SUBDIR)
80 PROTO:=$(PKG_SOURCE_PROTO)
81 $(if $(PKG_SOURCE_MIRROR),MIRROR:=$(filter 1,$(PKG_MIRROR)))
82 $(if $(PKG_MIRROR_MD5SUM),MIRROR_MD5SUM:=$(PKG_MIRROR_MD5SUM))
83 VERSION:=$(PKG_SOURCE_VERSION)
84 MD5SUM:=$(PKG_MD5SUM)
85 endef
86
87 define Build/Exports/Default
88 $(1) : export ACLOCAL_INCLUDE=$$(foreach p,$$(wildcard $$(STAGING_DIR)/usr/share/aclocal $$(STAGING_DIR)/usr/share/aclocal-* $$(STAGING_DIR)/host/share/aclocal $$(STAGING_DIR)/host/share/aclocal-*),-I $$(p))
89 $(1) : export STAGING_PREFIX=$$(STAGING_DIR)/usr
90 $(1) : export PATH=$$(TARGET_PATH_PKG)
91 $(1) : export CONFIG_SITE:=$$(CONFIG_SITE)
92 $(1) : export PKG_CONFIG_PATH:=$$(PKG_CONFIG_PATH)
93 $(1) : export PKG_CONFIG_LIBDIR:=$$(PKG_CONFIG_PATH)
94 $(1) : export CCACHE_DIR:=$(STAGING_DIR)/ccache
95 endef
96 Build/Exports=$(Build/Exports/Default)
97
98 define Build/DefaultTargets
99 $(if $(QUILT),$(Build/Quilt))
100 $(if $(strip $(PKG_SOURCE_URL)),$(call Download,default))
101 $(call Build/Autoclean)
102
103 download:
104 $(foreach hook,$(Hooks/Download),
105 $(call $(hook))$(sep)
106 )
107
108 $(STAMP_PREPARED) : export PATH=$$(TARGET_PATH_PKG)
109 $(STAMP_PREPARED):
110 @-rm -rf $(PKG_BUILD_DIR)
111 @mkdir -p $(PKG_BUILD_DIR)
112 $(foreach hook,$(Hooks/Prepare/Pre),$(call $(hook))$(sep))
113 $(Build/Prepare)
114 $(foreach hook,$(Hooks/Prepare/Post),$(call $(hook))$(sep))
115 touch $$@
116
117 $(call Build/Exports,$(STAMP_CONFIGURED))
118 $(STAMP_CONFIGURED): $(STAMP_PREPARED)
119 $(foreach hook,$(Hooks/Configure/Pre),$(call $(hook))$(sep))
120 $(Build/Configure)
121 $(foreach hook,$(Hooks/Configure/Post),$(call $(hook))$(sep))
122 touch $$@
123
124 $(call Build/Exports,$(STAMP_BUILT))
125 $(STAMP_BUILT): $(STAMP_CONFIGURED)
126 $(foreach hook,$(Hooks/Compile/Pre),$(call $(hook))$(sep))
127 $(Build/Compile)
128 $(foreach hook,$(Hooks/Compile/Post),$(call $(hook))$(sep))
129 $(Build/Install)
130 $(foreach hook,$(Hooks/Install/Post),$(call $(hook))$(sep))
131 touch $$@
132
133 $(STAMP_INSTALLED) : export PATH=$$(TARGET_PATH_PKG)
134 $(STAMP_INSTALLED): $(STAMP_BUILT)
135 $(SUBMAKE) -j1 clean-staging
136 rm -rf $(TMP_DIR)/stage-$(PKG_NAME)
137 mkdir -p $(TMP_DIR)/stage-$(PKG_NAME)/host $(STAGING_DIR)/packages $(STAGING_DIR_HOST)/packages
138 $(foreach hook,$(Hooks/InstallDev/Pre),\
139 $(call $(hook),$(TMP_DIR)/stage-$(PKG_NAME),$(TMP_DIR)/stage-$(PKG_NAME)/host)$(sep)\
140 )
141 $(call Build/InstallDev,$(TMP_DIR)/stage-$(PKG_NAME),$(TMP_DIR)/stage-$(PKG_NAME)/host)
142 $(foreach hook,$(Hooks/InstallDev/Post),\
143 $(call $(hook),$(TMP_DIR)/stage-$(PKG_NAME),$(TMP_DIR)/stage-$(PKG_NAME)/host)$(sep)\
144 )
145 if [ -d $(TMP_DIR)/stage-$(PKG_NAME) ]; then \
146 (cd $(TMP_DIR)/stage-$(PKG_NAME); find ./ > $(TMP_DIR)/stage-$(PKG_NAME).files); \
147 $(call locked, \
148 mv $(TMP_DIR)/stage-$(PKG_NAME).files $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) && \
149 $(CP) $(TMP_DIR)/stage-$(PKG_NAME)/* $(STAGING_DIR)/; \
150 ,staging-dir); \
151 fi
152 rm -rf $(TMP_DIR)/stage-$(PKG_NAME)
153 touch $$@
154
155 ifdef Build/InstallDev
156 compile: $(STAMP_INSTALLED)
157 endif
158
159 define Build/DefaultTargets
160 endef
161
162 prepare: $(STAMP_PREPARED)
163 configure: $(STAMP_CONFIGURED)
164 dist: $(STAMP_CONFIGURED)
165 distcheck: $(STAMP_CONFIGURED)
166 endef
167
168 define Build/IncludeOverlay
169 $(eval -include $(wildcard $(TOPDIR)/overlay/*/$(PKG_NAME).mk))
170 define Build/IncludeOverlay
171 endef
172 endef
173
174 define BuildPackage
175 $(Build/IncludeOverlay)
176 $(eval $(Package/Default))
177 $(eval $(Package/$(1)))
178
179 ifdef DESCRIPTION
180 $$(error DESCRIPTION:= is obsolete, use Package/PKG_NAME/description)
181 endif
182
183 ifndef Package/$(1)/description
184 define Package/$(1)/description
185 $(TITLE)
186 endef
187 endif
188
189 $(foreach FIELD, TITLE CATEGORY PRIORITY SECTION VERSION,
190 ifeq ($($(FIELD)),)
191 $$(error Package/$(1) is missing the $(FIELD) field)
192 endif
193 )
194
195 $(call shexport,Package/$(1)/description)
196 $(call shexport,Package/$(1)/config)
197
198 $(if $(DUMP), \
199 $(Dumpinfo/Package), \
200 $(foreach target, \
201 $(if $(Package/$(1)/targets),$(Package/$(1)/targets), \
202 $(if $(PKG_TARGETS),$(PKG_TARGETS), ipkg) \
203 ), $(BuildTarget/$(target)) \
204 ) \
205 )
206 $(if $(PKG_HOST_ONLY)$(DUMP),,$(call Build/DefaultTargets,$(1)))
207 endef
208
209 define pkg_install_files
210 $(foreach install_file,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_file)`; $(INSTALL_DATA) $(2)/$(install_file) $(3)/`dirname $(install_file)`;)
211 endef
212
213 define pkg_install_bin
214 $(foreach install_apps,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_apps)`; $(INSTALL_BIN) $(2)/$(install_apps) $(3)/`dirname $(install_apps)`;)
215 endef
216
217 Build/Prepare=$(call Build/Prepare/Default,)
218 Build/Configure=$(call Build/Configure/Default,)
219 Build/Compile=$(call Build/Compile/Default,)
220 Build/Install=$(if $(PKG_INSTALL),$(call Build/Install/Default,))
221 Build/Dist=$(call Build/Dist/Default,)
222 Build/DistCheck=$(call Build/DistCheck/Default,)
223
224 $(PACKAGE_DIR):
225 mkdir -p $@
226
227 dumpinfo:
228 download:
229 prepare:
230 configure:
231 compile:
232 install:
233 clean-staging: FORCE
234 rm -f $(STAMP_INSTALLED)
235 @-(\
236 cd "$(STAGING_DIR)"; \
237 if [ -f packages/$(STAGING_FILES_LIST) ]; then \
238 cat packages/$(STAGING_FILES_LIST) | xargs -r rm -f 2>/dev/null; \
239 fi; \
240 )
241
242 clean: clean-staging FORCE
243 $(call Build/UninstallDev,$(STAGING_DIR),$(STAGING_DIR_HOST))
244 $(Build/Clean)
245 rm -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) $(STAGING_DIR_HOST)/packages/$(STAGING_FILES_LIST)
246 rm -rf $(PKG_BUILD_DIR)
247
248 dist:
249 $(Build/Dist)
250
251 distcheck:
252 $(Build/DistCheck)