build: fix STAGING_DIR cleaning for packages
[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 __package_mk:=1
9
10 all: $(if $(DUMP),dumpinfo,$(if $(CHECK),check,compile))
11
12 include $(INCLUDE_DIR)/download.mk
13
14 PKG_BUILD_DIR ?= $(BUILD_DIR)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
15 PKG_INSTALL_DIR ?= $(PKG_BUILD_DIR)/ipkg-install
16 PKG_BUILD_PARALLEL ?=
17 PKG_USE_MIPS16 ?= 1
18 PKG_IREMAP ?= 1
19
20 MAKE_J:=$(if $(MAKE_JOBSERVER),$(MAKE_JOBSERVER) $(if $(filter 3.% 4.0 4.1,$(MAKE_VERSION)),-j))
21
22 ifeq ($(strip $(PKG_BUILD_PARALLEL)),0)
23 PKG_JOBS?=-j1
24 else
25 PKG_JOBS?=$(if $(PKG_BUILD_PARALLEL),$(MAKE_J),-j1)
26 endif
27 ifdef CONFIG_USE_MIPS16
28 ifeq ($(strip $(PKG_USE_MIPS16)),1)
29 TARGET_ASFLAGS_DEFAULT = $(filter-out -mips16 -minterlink-mips16,$(TARGET_CFLAGS))
30 TARGET_CFLAGS += -mips16 -minterlink-mips16
31 endif
32 endif
33 ifeq ($(strip $(PKG_IREMAP)),1)
34 IREMAP_CFLAGS = $(call iremap,$(PKG_BUILD_DIR),$(notdir $(PKG_BUILD_DIR)))
35 TARGET_CFLAGS += $(IREMAP_CFLAGS)
36 endif
37
38 include $(INCLUDE_DIR)/hardening.mk
39 include $(INCLUDE_DIR)/prereq.mk
40 include $(INCLUDE_DIR)/unpack.mk
41 include $(INCLUDE_DIR)/depends.mk
42
43 ifneq ($(if $(CONFIG_SRC_TREE_OVERRIDE),$(wildcard ./git-src)),)
44 USE_GIT_TREE:=1
45 QUILT:=1
46 endif
47 ifdef USE_SOURCE_DIR
48 QUILT:=1
49 endif
50 ifneq ($(wildcard $(PKG_BUILD_DIR)/.source_dir),)
51 QUILT:=1
52 endif
53
54 include $(INCLUDE_DIR)/quilt.mk
55
56 find_library_dependencies = $(wildcard $(patsubst %,$(STAGING_DIR)/pkginfo/%.version, \
57 $(filter-out $(BUILD_PACKAGES),$(foreach dep, \
58 $(filter-out @%, $(patsubst +%,%,$(1))), \
59 $(if $(findstring :,$(dep)), \
60 $(word 2,$(subst :,$(space),$(dep))), \
61 $(dep) \
62 ) \
63 ))))
64
65 PKG_DIR_NAME:=$(lastword $(subst /,$(space),$(CURDIR)))
66 STAMP_NO_AUTOREBUILD=$(wildcard $(PKG_BUILD_DIR)/.no_autorebuild)
67 PREV_STAMP_PREPARED:=$(if $(STAMP_NO_AUTOREBUILD),$(wildcard $(PKG_BUILD_DIR)/.prepared*))
68 ifneq ($(PREV_STAMP_PREPARED),)
69 STAMP_PREPARED:=$(PREV_STAMP_PREPARED)
70 CONFIG_AUTOREBUILD:=
71 else
72 STAMP_PREPARED=$(PKG_BUILD_DIR)/.prepared$(if $(QUILT)$(DUMP),,_$(shell $(call find_md5,${CURDIR} $(PKG_FILE_DEPENDS),))_$(call confvar,CONFIG_AUTOREMOVE $(PKG_PREPARED_DEPENDS)))
73 endif
74 STAMP_CONFIGURED=$(PKG_BUILD_DIR)/.configured$(if $(DUMP),,_$(call confvar,$(PKG_CONFIG_DEPENDS)))
75 STAMP_CONFIGURED_WILDCARD=$(PKG_BUILD_DIR)/.configured_*
76 STAMP_BUILT:=$(PKG_BUILD_DIR)/.built
77 STAMP_INSTALLED:=$(STAGING_DIR)/stamp/.$(PKG_DIR_NAME)$(if $(BUILD_VARIANT),.$(BUILD_VARIANT),)_installed
78
79 STAGING_FILES_LIST:=$(PKG_DIR_NAME)$(if $(BUILD_VARIANT),.$(BUILD_VARIANT),).list
80
81 define CleanStaging
82 rm -f $(STAMP_INSTALLED)
83 @-(\
84 if [ -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) ]; then \
85 $(SCRIPT_DIR)/clean-package.sh \
86 "$(STAGING_DIR)/packages/$(STAGING_FILES_LIST)" \
87 "$(STAGING_DIR)"; \
88 fi; \
89 )
90 endef
91
92
93 PKG_INSTALL_STAMP:=$(PKG_INFO_DIR)/$(PKG_DIR_NAME).$(if $(BUILD_VARIANT),$(BUILD_VARIANT),default).install
94
95 include $(INCLUDE_DIR)/package-defaults.mk
96 include $(INCLUDE_DIR)/package-dumpinfo.mk
97 include $(INCLUDE_DIR)/package-ipkg.mk
98 include $(INCLUDE_DIR)/package-bin.mk
99 include $(INCLUDE_DIR)/autotools.mk
100
101 _pkg_target:=$(if $(QUILT),,.)
102
103 override MAKEFLAGS=
104 CONFIG_SITE:=$(INCLUDE_DIR)/site/$(ARCH)
105 CUR_MAKEFILE:=$(filter-out Makefile,$(firstword $(MAKEFILE_LIST)))
106 SUBMAKE:=$(NO_TRACE_MAKE) $(if $(CUR_MAKEFILE),-f $(CUR_MAKEFILE))
107 PKG_CONFIG_PATH=$(STAGING_DIR)/usr/lib/pkgconfig:$(STAGING_DIR)/usr/share/pkgconfig
108 unexport QUIET CONFIG_SITE
109
110 ifeq ($(DUMP)$(filter prereq clean refresh update,$(MAKECMDGOALS)),)
111 ifneq ($(if $(QUILT),,$(CONFIG_AUTOREBUILD)),)
112 define Build/Autoclean
113 $(PKG_BUILD_DIR)/.dep_files: $(STAMP_PREPARED)
114 $(call rdep,${CURDIR} $(PKG_FILE_DEPENDS),$(STAMP_PREPARED),$(PKG_BUILD_DIR)/.dep_files,-x "*/.dep_*")
115 $(if $(filter prepare,$(MAKECMDGOALS)),,$(call rdep,$(PKG_BUILD_DIR),$(STAMP_BUILT),,-x "*/.dep_*" -x "*/ipkg*"))
116 endef
117 endif
118 endif
119
120 ifdef USE_GIT_TREE
121 define Build/Prepare/Default
122 mkdir -p $(PKG_BUILD_DIR)
123 ln -s $(CURDIR)/git-src $(PKG_BUILD_DIR)/.git
124 ( cd $(PKG_BUILD_DIR); \
125 git checkout .; \
126 git submodule update --recursive; \
127 git submodule foreach git config --unset core.worktree; \
128 git submodule foreach git checkout .; \
129 )
130 endef
131 endif
132 ifdef USE_SOURCE_DIR
133 define Build/Prepare/Default
134 rm -rf $(PKG_BUILD_DIR)
135 $(if $(wildcard $(USE_SOURCE_DIR)/*),,@echo "Error: USE_SOURCE_DIR=$(USE_SOURCE_DIR) path not found"; false)
136 ln -snf $(USE_SOURCE_DIR) $(PKG_BUILD_DIR)
137 touch $(PKG_BUILD_DIR)/.source_dir
138 endef
139 endif
140
141 define Build/Exports/Default
142 $(1) : export ACLOCAL_INCLUDE=$$(foreach p,$$(wildcard $$(STAGING_DIR)/usr/share/aclocal $$(STAGING_DIR)/usr/share/aclocal-* $$(STAGING_DIR_HOSTPKG)/share/aclocal $$(STAGING_DIR_HOSTPKG)/share/aclocal-* $$(STAGING_DIR)/host/share/aclocal $$(STAGING_DIR)/host/share/aclocal-*),-I $$(p))
143 $(1) : export STAGING_PREFIX=$$(STAGING_DIR)/usr
144 $(1) : export PATH=$$(TARGET_PATH_PKG)
145 $(1) : export CONFIG_SITE:=$$(CONFIG_SITE)
146 $(1) : export PKG_CONFIG_PATH:=$$(PKG_CONFIG_PATH)
147 $(1) : export PKG_CONFIG_LIBDIR:=$$(PKG_CONFIG_PATH)
148 $(if $(CONFIG_CCACHE),$(1) : export CCACHE_DIR:=$(STAGING_DIR)/ccache)
149 endef
150 Build/Exports=$(Build/Exports/Default)
151
152 define Build/CoreTargets
153 STAMP_PREPARED:=$$(STAMP_PREPARED)
154 STAMP_CONFIGURED:=$$(STAMP_CONFIGURED)
155
156 $(if $(QUILT),$(Build/Quilt))
157 $(call Build/Autoclean)
158 $(call DefaultTargets)
159
160 download:
161 $(foreach hook,$(Hooks/Download),
162 $(call $(hook))$(sep)
163 )
164
165 $(STAMP_PREPARED) : export PATH=$$(TARGET_PATH_PKG)
166 $(STAMP_PREPARED): $(STAMP_PREPARED_DEPENDS)
167 @-rm -rf $(PKG_BUILD_DIR)
168 @mkdir -p $(PKG_BUILD_DIR)
169 touch $$@_check
170 $(foreach hook,$(Hooks/Prepare/Pre),$(call $(hook))$(sep))
171 $(Build/Prepare)
172 $(foreach hook,$(Hooks/Prepare/Post),$(call $(hook))$(sep))
173 touch $$@
174
175 $(call Build/Exports,$(STAMP_CONFIGURED))
176 $(STAMP_CONFIGURED): $(STAMP_PREPARED) $(STAMP_CONFIGURED_DEPENDS)
177 rm -f $(STAMP_CONFIGURED_WILDCARD)
178 $(CleanStaging)
179 $(foreach hook,$(Hooks/Configure/Pre),$(call $(hook))$(sep))
180 $(Build/Configure)
181 $(foreach hook,$(Hooks/Configure/Post),$(call $(hook))$(sep))
182 touch $$@
183
184 $(call Build/Exports,$(STAMP_BUILT))
185 $(STAMP_BUILT): $(STAMP_CONFIGURED) $(STAMP_BUILT_DEPENDS)
186 rm -f $$@
187 touch $$@_check
188 $(foreach hook,$(Hooks/Compile/Pre),$(call $(hook))$(sep))
189 $(Build/Compile)
190 $(foreach hook,$(Hooks/Compile/Post),$(call $(hook))$(sep))
191 $(Build/Install)
192 $(foreach hook,$(Hooks/Install/Post),$(call $(hook))$(sep))
193 touch $$@
194
195 $(STAMP_INSTALLED) : export PATH=$$(TARGET_PATH_PKG)
196 $(STAMP_INSTALLED): $(STAMP_BUILT)
197 rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
198 mkdir -p $(TMP_DIR)/stage-$(PKG_DIR_NAME)/host $(STAGING_DIR)/packages $(STAGING_DIR_HOST)/packages
199 $(foreach hook,$(Hooks/InstallDev/Pre),\
200 $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
201 )
202 $(call Build/InstallDev,$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)
203 $(foreach hook,$(Hooks/InstallDev/Post),\
204 $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
205 )
206 if [ -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) ]; then \
207 $(SCRIPT_DIR)/clean-package.sh \
208 "$(STAGING_DIR)/packages/$(STAGING_FILES_LIST)" \
209 "$(STAGING_DIR)"; \
210 fi
211 if [ -d $(TMP_DIR)/stage-$(PKG_DIR_NAME) ]; then \
212 (cd $(TMP_DIR)/stage-$(PKG_DIR_NAME); find ./ > $(TMP_DIR)/stage-$(PKG_DIR_NAME).files); \
213 $(call locked, \
214 mv $(TMP_DIR)/stage-$(PKG_DIR_NAME).files $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) && \
215 $(CP) $(TMP_DIR)/stage-$(PKG_DIR_NAME)/* $(STAGING_DIR)/; \
216 ,staging-dir); \
217 fi
218 rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
219 touch $$@
220
221 ifdef Build/InstallDev
222 $(_pkg_target)compile: $(STAMP_INSTALLED)
223 endif
224
225 $(_pkg_target)prepare: $(STAMP_PREPARED)
226 $(_pkg_target)configure: $(STAMP_CONFIGURED)
227 $(_pkg_target)dist: $(STAMP_CONFIGURED)
228 $(_pkg_target)distcheck: $(STAMP_CONFIGURED)
229
230 ifneq ($(CONFIG_AUTOREMOVE),)
231 compile:
232 -touch -r $(PKG_BUILD_DIR)/.built $(PKG_BUILD_DIR)/.autoremove 2>/dev/null >/dev/null
233 $(FIND) $(PKG_BUILD_DIR) -mindepth 1 -maxdepth 1 -not '(' -type f -and -name '.*' -and -size 0 ')' -and -not -name '.pkgdir' | \
234 $(XARGS) rm -rf
235 endif
236 endef
237
238 define Build/DefaultTargets
239 $(if $(USE_SOURCE_DIR)$(USE_GIT_TREE),,$(if $(strip $(PKG_SOURCE_URL)),$(call Download,default)))
240 $(if $(DUMP),,$(Build/CoreTargets))
241
242 define Build/DefaultTargets
243 endef
244 endef
245
246 define BuildPackage
247 $(eval $(Package/Default))
248 $(eval $(Package/$(1)))
249
250 ifdef DESCRIPTION
251 $$(error DESCRIPTION:= is obsolete, use Package/PKG_NAME/description)
252 endif
253
254 ifndef Package/$(1)/description
255 define Package/$(1)/description
256 $(TITLE)
257 endef
258 endif
259
260 BUILD_PACKAGES += $(1)
261 $(STAMP_PREPARED): $$(if $(QUILT)$(DUMP),,$(call find_library_dependencies,$(DEPENDS)))
262
263 $(foreach FIELD, TITLE CATEGORY SECTION VERSION,
264 ifeq ($($(FIELD)),)
265 $$(error Package/$(1) is missing the $(FIELD) field)
266 endif
267 )
268
269 $(if $(DUMP), \
270 $(if $(CHECK),,$(Dumpinfo/Package)), \
271 $(foreach target, \
272 $(if $(Package/$(1)/targets),$(Package/$(1)/targets), \
273 $(if $(PKG_TARGETS),$(PKG_TARGETS), ipkg) \
274 ), $(BuildTarget/$(target)) \
275 ) \
276 )
277 $(if $(PKG_HOST_ONLY),,$(call Build/DefaultTargets,$(1)))
278 endef
279
280 define pkg_install_files
281 $(foreach install_file,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_file)`; $(INSTALL_DATA) $(2)/$(install_file) $(3)/`dirname $(install_file)`;)
282 endef
283
284 define pkg_install_bin
285 $(foreach install_apps,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_apps)`; $(INSTALL_BIN) $(2)/$(install_apps) $(3)/`dirname $(install_apps)`;)
286 endef
287
288 Build/Prepare=$(call Build/Prepare/Default,)
289 Build/Configure=$(call Build/Configure/Default,)
290 Build/Compile=$(call Build/Compile/Default,)
291 Build/Install=$(if $(PKG_INSTALL),$(call Build/Install/Default,))
292 Build/Dist=$(call Build/Dist/Default,)
293 Build/DistCheck=$(call Build/DistCheck/Default,)
294
295 .NOTPARALLEL:
296
297 .PHONY: prepare-package-install
298 prepare-package-install:
299 @mkdir -p $(PKG_INFO_DIR)
300 @rm -f $(PKG_INSTALL_STAMP)
301 @echo "$(filter-out essential nonshared,$(PKG_FLAGS))" > $(PKG_INSTALL_STAMP).flags
302
303 $(PACKAGE_DIR):
304 mkdir -p $@
305
306 compile:
307 .install: .compile
308 install: compile
309
310 force-clean-build: FORCE
311 rm -rf $(PKG_BUILD_DIR)
312
313 clean-build: $(if $(wildcard $(PKG_BUILD_DIR)/.autoremove),force-clean-build)
314
315 clean: force-clean-build
316 $(CleanStaging)
317 $(call Build/UninstallDev,$(STAGING_DIR),$(STAGING_DIR_HOST))
318 $(Build/Clean)
319 rm -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) $(STAGING_DIR_HOST)/packages/$(STAGING_FILES_LIST)
320
321 dist:
322 $(Build/Dist)
323
324 distcheck:
325 $(Build/DistCheck)