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