e804b02b049c3cb1d4bdce9acfb560ac7511ff9c
[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 cd "$(STAGING_DIR)"; \
85 if [ -f packages/$(STAGING_FILES_LIST) ]; then \
86 cat packages/$(STAGING_FILES_LIST) | xargs -r rm -f 2>/dev/null; \
87 fi; \
88 )
89 endef
90
91
92 PKG_INSTALL_STAMP:=$(PKG_INFO_DIR)/$(PKG_DIR_NAME).$(if $(BUILD_VARIANT),$(BUILD_VARIANT),default).install
93
94 include $(INCLUDE_DIR)/package-defaults.mk
95 include $(INCLUDE_DIR)/package-dumpinfo.mk
96 include $(INCLUDE_DIR)/package-ipkg.mk
97 include $(INCLUDE_DIR)/package-bin.mk
98 include $(INCLUDE_DIR)/autotools.mk
99
100 _pkg_target:=$(if $(QUILT),,.)
101
102 override MAKEFLAGS=
103 CONFIG_SITE:=$(INCLUDE_DIR)/site/$(ARCH)
104 CUR_MAKEFILE:=$(filter-out Makefile,$(firstword $(MAKEFILE_LIST)))
105 SUBMAKE:=$(NO_TRACE_MAKE) $(if $(CUR_MAKEFILE),-f $(CUR_MAKEFILE))
106 PKG_CONFIG_PATH=$(STAGING_DIR)/usr/lib/pkgconfig:$(STAGING_DIR)/usr/share/pkgconfig
107 unexport QUIET CONFIG_SITE
108
109 ifeq ($(DUMP)$(filter prereq clean refresh update,$(MAKECMDGOALS)),)
110 ifneq ($(if $(QUILT),,$(CONFIG_AUTOREBUILD)),)
111 define Build/Autoclean
112 $(PKG_BUILD_DIR)/.dep_files: $(STAMP_PREPARED)
113 $(call rdep,${CURDIR} $(PKG_FILE_DEPENDS),$(STAMP_PREPARED),$(PKG_BUILD_DIR)/.dep_files,-x "*/.dep_*")
114 $(if $(filter prepare,$(MAKECMDGOALS)),,$(call rdep,$(PKG_BUILD_DIR),$(STAMP_BUILT),,-x "*/.dep_*" -x "*/ipkg*"))
115 endef
116 endif
117 endif
118
119 ifdef USE_GIT_TREE
120 define Build/Prepare/Default
121 mkdir -p $(PKG_BUILD_DIR)
122 ln -s $(CURDIR)/git-src $(PKG_BUILD_DIR)/.git
123 ( cd $(PKG_BUILD_DIR); git checkout .)
124 endef
125 endif
126 ifdef USE_SOURCE_DIR
127 define Build/Prepare/Default
128 rm -rf $(PKG_BUILD_DIR)
129 $(if $(wildcard $(USE_SOURCE_DIR)/*),,@echo "Error: USE_SOURCE_DIR=$(USE_SOURCE_DIR) path not found"; false)
130 ln -snf $(USE_SOURCE_DIR) $(PKG_BUILD_DIR)
131 touch $(PKG_BUILD_DIR)/.source_dir
132 endef
133 endif
134
135 define Build/Exports/Default
136 $(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))
137 $(1) : export STAGING_PREFIX=$$(STAGING_DIR)/usr
138 $(1) : export PATH=$$(TARGET_PATH_PKG)
139 $(1) : export CONFIG_SITE:=$$(CONFIG_SITE)
140 $(1) : export PKG_CONFIG_PATH:=$$(PKG_CONFIG_PATH)
141 $(1) : export PKG_CONFIG_LIBDIR:=$$(PKG_CONFIG_PATH)
142 $(1) : export CCACHE_DIR:=$(STAGING_DIR)/ccache
143 endef
144 Build/Exports=$(Build/Exports/Default)
145
146 define Build/CoreTargets
147 STAMP_PREPARED:=$$(STAMP_PREPARED)
148 STAMP_CONFIGURED:=$$(STAMP_CONFIGURED)
149
150 $(if $(QUILT),$(Build/Quilt))
151 $(call Build/Autoclean)
152 $(call DefaultTargets)
153
154 download:
155 $(foreach hook,$(Hooks/Download),
156 $(call $(hook))$(sep)
157 )
158
159 $(STAMP_PREPARED) : export PATH=$$(TARGET_PATH_PKG)
160 $(STAMP_PREPARED): $(STAMP_PREPARED_DEPENDS)
161 @-rm -rf $(PKG_BUILD_DIR)
162 @mkdir -p $(PKG_BUILD_DIR)
163 touch $$@_check
164 $(foreach hook,$(Hooks/Prepare/Pre),$(call $(hook))$(sep))
165 $(Build/Prepare)
166 $(foreach hook,$(Hooks/Prepare/Post),$(call $(hook))$(sep))
167 touch $$@
168
169 $(call Build/Exports,$(STAMP_CONFIGURED))
170 $(STAMP_CONFIGURED): $(STAMP_PREPARED) $(STAMP_CONFIGURED_DEPENDS)
171 $(CleanStaging)
172 $(foreach hook,$(Hooks/Configure/Pre),$(call $(hook))$(sep))
173 $(Build/Configure)
174 $(foreach hook,$(Hooks/Configure/Post),$(call $(hook))$(sep))
175 rm -f $(STAMP_CONFIGURED_WILDCARD)
176 touch $$@
177
178 $(call Build/Exports,$(STAMP_BUILT))
179 $(STAMP_BUILT): $(STAMP_CONFIGURED) $(STAMP_BUILT_DEPENDS)
180 rm -f $$@
181 touch $$@_check
182 $(foreach hook,$(Hooks/Compile/Pre),$(call $(hook))$(sep))
183 $(Build/Compile)
184 $(foreach hook,$(Hooks/Compile/Post),$(call $(hook))$(sep))
185 $(Build/Install)
186 $(foreach hook,$(Hooks/Install/Post),$(call $(hook))$(sep))
187 touch $$@
188
189 $(STAMP_INSTALLED) : export PATH=$$(TARGET_PATH_PKG)
190 $(STAMP_INSTALLED): $(STAMP_BUILT)
191 rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
192 mkdir -p $(TMP_DIR)/stage-$(PKG_DIR_NAME)/host $(STAGING_DIR)/packages $(STAGING_DIR_HOST)/packages
193 $(foreach hook,$(Hooks/InstallDev/Pre),\
194 $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
195 )
196 $(call Build/InstallDev,$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)
197 $(foreach hook,$(Hooks/InstallDev/Post),\
198 $(call $(hook),$(TMP_DIR)/stage-$(PKG_DIR_NAME),$(TMP_DIR)/stage-$(PKG_DIR_NAME)/host)$(sep)\
199 )
200 if [ -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) ]; then \
201 $(SCRIPT_DIR)/clean-package.sh \
202 "$(STAGING_DIR)/packages/$(STAGING_FILES_LIST)" \
203 "$(STAGING_DIR)"; \
204 fi
205 if [ -d $(TMP_DIR)/stage-$(PKG_DIR_NAME) ]; then \
206 (cd $(TMP_DIR)/stage-$(PKG_DIR_NAME); find ./ > $(TMP_DIR)/stage-$(PKG_DIR_NAME).files); \
207 $(call locked, \
208 mv $(TMP_DIR)/stage-$(PKG_DIR_NAME).files $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) && \
209 $(CP) $(TMP_DIR)/stage-$(PKG_DIR_NAME)/* $(STAGING_DIR)/; \
210 ,staging-dir); \
211 fi
212 rm -rf $(TMP_DIR)/stage-$(PKG_DIR_NAME)
213 touch $$@
214
215 ifdef Build/InstallDev
216 $(_pkg_target)compile: $(STAMP_INSTALLED)
217 endif
218
219 $(_pkg_target)prepare: $(STAMP_PREPARED)
220 $(_pkg_target)configure: $(STAMP_CONFIGURED)
221 $(_pkg_target)dist: $(STAMP_CONFIGURED)
222 $(_pkg_target)distcheck: $(STAMP_CONFIGURED)
223
224 ifneq ($(CONFIG_AUTOREMOVE),)
225 compile:
226 -touch $(PKG_BUILD_DIR)/.autoremove 2>/dev/null >/dev/null
227 $(FIND) $(PKG_BUILD_DIR) -mindepth 1 -maxdepth 1 -not '(' -type f -and -name '.*' -and -size 0 ')' -and -not -name '.pkgdir' | \
228 $(XARGS) rm -rf
229 endif
230 endef
231
232 define Build/DefaultTargets
233 $(if $(USE_SOURCE_DIR)$(USE_GIT_TREE),,$(if $(strip $(PKG_SOURCE_URL)),$(call Download,default)))
234 $(if $(DUMP),,$(Build/CoreTargets))
235
236 define Build/DefaultTargets
237 endef
238 endef
239
240 define BuildPackage
241 $(eval $(Package/Default))
242 $(eval $(Package/$(1)))
243
244 ifdef DESCRIPTION
245 $$(error DESCRIPTION:= is obsolete, use Package/PKG_NAME/description)
246 endif
247
248 ifndef Package/$(1)/description
249 define Package/$(1)/description
250 $(TITLE)
251 endef
252 endif
253
254 BUILD_PACKAGES += $(1)
255 $(STAMP_PREPARED): $$(if $(QUILT)$(DUMP),,$(call find_library_dependencies,$(DEPENDS)))
256
257 $(foreach FIELD, TITLE CATEGORY SECTION VERSION,
258 ifeq ($($(FIELD)),)
259 $$(error Package/$(1) is missing the $(FIELD) field)
260 endif
261 )
262
263 $(if $(DUMP), \
264 $(if $(CHECK),,$(Dumpinfo/Package)), \
265 $(foreach target, \
266 $(if $(Package/$(1)/targets),$(Package/$(1)/targets), \
267 $(if $(PKG_TARGETS),$(PKG_TARGETS), ipkg) \
268 ), $(BuildTarget/$(target)) \
269 ) \
270 )
271 $(if $(PKG_HOST_ONLY),,$(call Build/DefaultTargets,$(1)))
272 endef
273
274 define pkg_install_files
275 $(foreach install_file,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_file)`; $(INSTALL_DATA) $(2)/$(install_file) $(3)/`dirname $(install_file)`;)
276 endef
277
278 define pkg_install_bin
279 $(foreach install_apps,$(1),$(INSTALL_DIR) $(3)/`dirname $(install_apps)`; $(INSTALL_BIN) $(2)/$(install_apps) $(3)/`dirname $(install_apps)`;)
280 endef
281
282 Build/Prepare=$(call Build/Prepare/Default,)
283 Build/Configure=$(call Build/Configure/Default,)
284 Build/Compile=$(call Build/Compile/Default,)
285 Build/Install=$(if $(PKG_INSTALL),$(call Build/Install/Default,))
286 Build/Dist=$(call Build/Dist/Default,)
287 Build/DistCheck=$(call Build/DistCheck/Default,)
288
289 .NOTPARALLEL:
290
291 .PHONY: prepare-package-install
292 prepare-package-install:
293 @mkdir -p $(PKG_INFO_DIR)
294 @rm -f $(PKG_INSTALL_STAMP)
295 @echo "$(filter-out essential nonshared,$(PKG_FLAGS))" > $(PKG_INSTALL_STAMP).flags
296
297 $(PACKAGE_DIR):
298 mkdir -p $@
299
300 compile:
301 .install: .compile
302 install: compile
303
304 force-clean-build: FORCE
305 rm -rf $(PKG_BUILD_DIR)
306
307 clean-build: $(if $(wildcard $(PKG_BUILD_DIR)/.autoremove),force-clean-build)
308
309 clean: force-clean-build
310 $(CleanStaging)
311 $(call Build/UninstallDev,$(STAGING_DIR),$(STAGING_DIR_HOST))
312 $(Build/Clean)
313 rm -f $(STAGING_DIR)/packages/$(STAGING_FILES_LIST) $(STAGING_DIR_HOST)/packages/$(STAGING_FILES_LIST)
314
315 dist:
316 $(Build/Dist)
317
318 distcheck:
319 $(Build/DistCheck)