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