base-files: fix links in banner.failsafe
[openwrt/staging/chunkeey.git] / rules.mk
1 #
2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2016 LEDE Project
4 #
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
7 #
8
9 ifneq ($(__rules_inc),1)
10 __rules_inc=1
11
12 ifeq ($(DUMP),)
13 -include $(TOPDIR)/.config
14 endif
15 include $(TOPDIR)/include/debug.mk
16 include $(TOPDIR)/include/verbose.mk
17
18 ifneq ($(filter check,$(MAKECMDGOALS)),)
19 CHECK:=1
20 DUMP:=1
21 endif
22
23 export TMP_DIR:=$(TOPDIR)/tmp
24 export TMPDIR:=$(TMP_DIR)
25
26 qstrip=$(strip $(subst ",,$(1)))
27 #"))
28
29 empty:=
30 space:= $(empty) $(empty)
31 comma:=,
32 merge=$(subst $(space),,$(1))
33 confvar=$(shell echo '$(foreach v,$(1),$(v)=$(subst ','\'',$($(v))))' | $(STAGING_DIR_HOST)/bin/mkhash md5)
34 strip_last=$(patsubst %.$(lastword $(subst .,$(space),$(1))),%,$(1))
35
36 paren_left = (
37 paren_right = )
38 chars_lower = a b c d e f g h i j k l m n o p q r s t u v w x y z
39 chars_upper = A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
40
41 define sep
42
43 endef
44
45 define newline
46
47
48 endef
49
50 __tr_list = $(join $(join $(1),$(foreach char,$(1),$(comma))),$(2))
51 __tr_head_stripped = $(subst $(space),,$(foreach cv,$(call __tr_list,$(1),$(2)),$$$(paren_left)subst$(cv)$(comma)))
52 __tr_head = $(subst $(paren_left)subst,$(paren_left)subst$(space),$(__tr_head_stripped))
53 __tr_tail = $(subst $(space),,$(foreach cv,$(1),$(paren_right)))
54 __tr_template = $(__tr_head)$$(1)$(__tr_tail)
55
56 $(eval toupper = $(call __tr_template,$(chars_lower),$(chars_upper)))
57 $(eval tolower = $(call __tr_template,$(chars_upper),$(chars_lower)))
58
59 version_abbrev = $(if $(if $(CHECK),,$(DUMP)),$(1),$(shell printf '%.8s' $(1)))
60
61 _SINGLE=export MAKEFLAGS=$(space);
62 CFLAGS:=
63 ARCH:=$(subst i486,i386,$(subst i586,i386,$(subst i686,i386,$(call qstrip,$(CONFIG_ARCH)))))
64 ARCH_PACKAGES:=$(call qstrip,$(CONFIG_TARGET_ARCH_PACKAGES))
65 BOARD:=$(call qstrip,$(CONFIG_TARGET_BOARD))
66 SUBTARGET:=$(call qstrip,$(CONFIG_TARGET_SUBTARGET))
67 TARGET_OPTIMIZATION:=$(call qstrip,$(CONFIG_TARGET_OPTIMIZATION))
68 export EXTRA_OPTIMIZATION:=$(filter-out -fno-plt,$(call qstrip,$(CONFIG_EXTRA_OPTIMIZATION)))
69 TARGET_SUFFIX=$(call qstrip,$(CONFIG_TARGET_SUFFIX))
70 BUILD_SUFFIX:=$(call qstrip,$(CONFIG_BUILD_SUFFIX))
71 SUBDIR:=$(patsubst $(TOPDIR)/%,%,${CURDIR})
72 BUILD_SUBDIR:=$(patsubst $(TOPDIR)/%,%,${CURDIR})
73 export SHELL:=/usr/bin/env bash
74
75 IS_PACKAGE_BUILD := $(if $(filter package/%,$(BUILD_SUBDIR)),1)
76
77 OPTIMIZE_FOR_CPU=$(subst i386,i486,$(ARCH))
78
79 ifeq ($(ARCH),powerpc)
80 FPIC:=-fPIC
81 else
82 FPIC:=-fpic
83 endif
84
85 HOST_FPIC:=-fPIC
86
87 ARCH_SUFFIX:=$(call qstrip,$(CONFIG_CPU_TYPE))
88 GCC_ARCH:=
89
90 ifneq ($(ARCH_SUFFIX),)
91 ARCH_SUFFIX:=_$(ARCH_SUFFIX)
92 endif
93 ifneq ($(filter -march=armv%,$(TARGET_OPTIMIZATION)),)
94 GCC_ARCH:=$(patsubst -march=%,%,$(filter -march=armv%,$(TARGET_OPTIMIZATION)))
95 endif
96 ifdef CONFIG_HAS_SPE_FPU
97 TARGET_SUFFIX:=$(TARGET_SUFFIX)spe
98 endif
99 ifdef CONFIG_MIPS64_ABI
100 ifneq ($(CONFIG_MIPS64_ABI_O32),y)
101 ARCH_SUFFIX:=$(ARCH_SUFFIX)_$(call qstrip,$(CONFIG_MIPS64_ABI))
102 endif
103 endif
104
105 DL_DIR:=$(if $(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(TOPDIR)/dl)
106 OUTPUT_DIR:=$(if $(call qstrip,$(CONFIG_BINARY_FOLDER)),$(call qstrip,$(CONFIG_BINARY_FOLDER)),$(TOPDIR)/bin)
107 BIN_DIR:=$(OUTPUT_DIR)/targets/$(BOARD)/$(SUBTARGET)
108 INCLUDE_DIR:=$(TOPDIR)/include
109 SCRIPT_DIR:=$(TOPDIR)/scripts
110 BUILD_DIR_BASE:=$(TOPDIR)/build_dir
111 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
112 GCCV:=$(call qstrip,$(CONFIG_GCC_VERSION))
113 LIBC:=$(call qstrip,$(CONFIG_LIBC))
114 LIBCV:=$(call qstrip,$(CONFIG_LIBC_VERSION))
115 REAL_GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))
116 GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux
117 DIR_SUFFIX:=_$(LIBC)-$(LIBCV)$(if $(CONFIG_arm),_eabi)
118 BIN_DIR:=$(BIN_DIR)$(if $(CONFIG_USE_MUSL),,-$(LIBC))
119 TARGET_DIR_NAME = target-$(ARCH)$(ARCH_SUFFIX)$(DIR_SUFFIX)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
120 TOOLCHAIN_DIR_NAME = toolchain-$(ARCH)$(ARCH_SUFFIX)_gcc-$(GCCV)$(DIR_SUFFIX)
121 else
122 ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
123 GNU_TARGET_NAME=$(call qstrip,$(CONFIG_TARGET_NAME))
124 else
125 GNU_TARGET_NAME=$(shell gcc -dumpmachine)
126 endif
127 REAL_GNU_TARGET_NAME=$(GNU_TARGET_NAME)
128 LIBC:=$(call qstrip,$(CONFIG_LIBC))
129 TARGET_DIR_NAME:=target-$(GNU_TARGET_NAME)_$(LIBC)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
130 TOOLCHAIN_DIR_NAME:=toolchain-$(GNU_TARGET_NAME)
131 endif
132
133 ifeq ($(or $(CONFIG_EXTERNAL_TOOLCHAIN),$(CONFIG_GCC_VERSION_4_8),$(CONFIG_TARGET_uml)),)
134 iremap = -iremap$(1):$(2)
135 endif
136
137 PACKAGE_DIR:=$(BIN_DIR)/packages
138 BUILD_DIR:=$(BUILD_DIR_BASE)/$(TARGET_DIR_NAME)
139 STAGING_DIR:=$(TOPDIR)/staging_dir/$(TARGET_DIR_NAME)
140 BUILD_DIR_TOOLCHAIN:=$(BUILD_DIR_BASE)/$(TOOLCHAIN_DIR_NAME)
141 TOOLCHAIN_DIR:=$(TOPDIR)/staging_dir/$(TOOLCHAIN_DIR_NAME)
142 STAMP_DIR:=$(BUILD_DIR)/stamp
143 STAMP_DIR_HOST=$(BUILD_DIR_HOST)/stamp
144 TARGET_ROOTFS_DIR?=$(if $(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(BUILD_DIR))
145 TARGET_DIR:=$(TARGET_ROOTFS_DIR)/root-$(BOARD)
146 STAGING_DIR_ROOT:=$(STAGING_DIR)/root-$(BOARD)
147 BUILD_LOG_DIR:=$(TOPDIR)/logs
148 PKG_INFO_DIR := $(STAGING_DIR)/pkginfo
149
150 BUILD_DIR_HOST:=$(if $(IS_PACKAGE_BUILD),$(BUILD_DIR)/host,$(BUILD_DIR_BASE)/host)
151 STAGING_DIR_HOST:=$(TOPDIR)/staging_dir/host
152 STAGING_DIR_HOSTPKG:=$(STAGING_DIR)/host
153
154 TARGET_PATH:=$(subst $(space),:,$(filter-out .,$(filter-out ./,$(subst :,$(space),$(PATH)))))
155 TARGET_INIT_PATH:=$(call qstrip,$(CONFIG_TARGET_INIT_PATH))
156 TARGET_INIT_PATH:=$(if $(TARGET_INIT_PATH),$(TARGET_INIT_PATH),/usr/sbin:/sbin:/usr/bin:/bin)
157 TARGET_CFLAGS:=$(TARGET_OPTIMIZATION)$(if $(CONFIG_DEBUG), -g3) $(call qstrip,$(CONFIG_EXTRA_OPTIMIZATION))
158 TARGET_CXXFLAGS = $(TARGET_CFLAGS)
159 TARGET_ASFLAGS_DEFAULT = $(TARGET_CFLAGS)
160 TARGET_ASFLAGS = $(TARGET_ASFLAGS_DEFAULT)
161 TARGET_CPPFLAGS:=-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include
162 TARGET_LDFLAGS:=-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib
163 ifneq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
164 LIBGCC_S_PATH=$(realpath $(wildcard $(call qstrip,$(CONFIG_LIBGCC_ROOT_DIR))/$(call qstrip,$(CONFIG_LIBGCC_FILE_SPEC))))
165 LIBGCC_S=$(if $(LIBGCC_S_PATH),-L$(dir $(LIBGCC_S_PATH)) -lgcc_s)
166 LIBGCC_A=$(realpath $(lastword $(wildcard $(dir $(LIBGCC_S_PATH))/gcc/*/*/libgcc.a)))
167 else
168 LIBGCC_A=$(lastword $(wildcard $(TOOLCHAIN_DIR)/lib/gcc/*/*/libgcc.a))
169 LIBGCC_S=$(if $(wildcard $(TOOLCHAIN_DIR)/lib/libgcc_s.so),-L$(TOOLCHAIN_DIR)/lib -lgcc_s,$(LIBGCC_A))
170 endif
171 LIBRPC=-lrpc
172 LIBRPC_DEPENDS=+librpc
173
174 ifeq ($(CONFIG_ARCH_64BIT),y)
175 LIB_SUFFIX:=64
176 endif
177
178 ifndef DUMP
179 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
180 -include $(TOOLCHAIN_DIR)/info.mk
181 export GCC_HONOUR_COPTS:=0
182 TARGET_CROSS:=$(if $(TARGET_CROSS),$(TARGET_CROSS),$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))-)
183 TARGET_CFLAGS+= -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result
184 TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/usr/include
185 ifeq ($(CONFIG_USE_MUSL),y)
186 TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/include/fortify
187 endif
188 TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/include
189 TARGET_LDFLAGS+= -L$(TOOLCHAIN_DIR)/usr/lib -L$(TOOLCHAIN_DIR)/lib
190 TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
191 else
192 ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
193 TARGET_CROSS:=$(call qstrip,$(CONFIG_TOOLCHAIN_PREFIX))
194 TOOLCHAIN_ROOT_DIR:=$(call qstrip,$(CONFIG_TOOLCHAIN_ROOT))
195 TOOLCHAIN_BIN_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_BIN_PATH)))
196 TOOLCHAIN_INC_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_INC_PATH)))
197 TOOLCHAIN_LIB_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_LIB_PATH)))
198 ifneq ($(TOOLCHAIN_BIN_DIRS),)
199 TARGET_PATH:=$(subst $(space),:,$(TOOLCHAIN_BIN_DIRS)):$(TARGET_PATH)
200 endif
201 ifneq ($(TOOLCHAIN_INC_DIRS),)
202 TARGET_CPPFLAGS+= $(patsubst %,-I%,$(TOOLCHAIN_INC_DIRS))
203 endif
204 ifneq ($(TOOLCHAIN_LIB_DIRS),)
205 TARGET_LDFLAGS+= $(patsubst %,-L%,$(TOOLCHAIN_LIB_DIRS))
206 endif
207 TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
208 endif
209 endif
210 endif
211 TARGET_PATH_PKG:=$(STAGING_DIR)/host/bin:$(TARGET_PATH)
212
213 ifeq ($(CONFIG_SOFT_FLOAT),y)
214 SOFT_FLOAT_CONFIG_OPTION:=--with-float=soft
215 ifeq ($(CONFIG_arm),y)
216 TARGET_CFLAGS+= -mfloat-abi=soft
217 else
218 TARGET_CFLAGS+= -msoft-float
219 endif
220 else
221 SOFT_FLOAT_CONFIG_OPTION:=
222 ifeq ($(CONFIG_arm),y)
223 TARGET_CFLAGS+= -mfloat-abi=hard
224 endif
225 endif
226
227 export PATH:=$(TARGET_PATH)
228 export STAGING_DIR STAGING_DIR_HOST
229 export SH_FUNC:=. $(INCLUDE_DIR)/shell.sh;
230
231 PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
232
233 export PKG_CONFIG
234
235 HOSTCC:=gcc
236 HOSTCXX:=g++
237 HOST_CPPFLAGS:=-I$(STAGING_DIR_HOST)/include -I$(STAGING_DIR_HOST)/usr/include $(if $(IS_PACKAGE_BUILD),-I$(STAGING_DIR)/host/include)
238 HOST_CFLAGS:=-O2 $(HOST_CPPFLAGS)
239 HOST_LDFLAGS:=-L$(STAGING_DIR_HOST)/lib -L$(STAGING_DIR_HOST)/usr/lib $(if $(IS_PACKAGE_BUILD),-L$(STAGING_DIR)/host/lib)
240
241 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
242 TARGET_AR:=$(TARGET_CROSS)gcc-ar
243 TARGET_RANLIB:=$(TARGET_CROSS)gcc-ranlib
244 TARGET_NM:=$(TARGET_CROSS)gcc-nm
245 else
246 TARGET_AR:=$(TARGET_CROSS)ar
247 TARGET_RANLIB:=$(TARGET_CROSS)ranlib
248 TARGET_NM:=$(TARGET_CROSS)nm
249 endif
250
251 BUILD_KEY=$(TOPDIR)/key-build
252
253 TARGET_CC:=$(TARGET_CROSS)gcc
254 TARGET_CXX:=$(TARGET_CROSS)g++
255 KPATCH:=$(SCRIPT_DIR)/patch-kernel.sh
256 SED:=$(STAGING_DIR_HOST)/bin/sed -i -e
257 CP:=cp -fpR
258 LN:=ln -sf
259 XARGS:=xargs -r
260
261 BASH:=bash
262 TAR:=tar
263 FIND:=find
264 PATCH:=patch
265 PYTHON:=python
266
267 INSTALL_BIN:=install -m0755
268 INSTALL_DIR:=install -d -m0755
269 INSTALL_DATA:=install -m0644
270 INSTALL_CONF:=install -m0600
271
272 TARGET_CC_NOCACHE:=$(TARGET_CC)
273 TARGET_CXX_NOCACHE:=$(TARGET_CXX)
274 HOSTCC_NOCACHE:=$(HOSTCC)
275 HOSTCXX_NOCACHE:=$(HOSTCXX)
276 export TARGET_CC_NOCACHE
277 export TARGET_CXX_NOCACHE
278 export HOSTCC_NOCACHE
279
280 ifneq ($(CONFIG_CCACHE),)
281 TARGET_CC:= ccache_cc
282 TARGET_CXX:= ccache_cxx
283 HOSTCC:= ccache $(HOSTCC)
284 HOSTCXX:= ccache $(HOSTCXX)
285 endif
286
287 TARGET_CONFIGURE_OPTS = \
288 AR="$(TARGET_AR)" \
289 AS="$(TARGET_CC) -c $(TARGET_ASFLAGS)" \
290 LD=$(TARGET_CROSS)ld \
291 NM="$(TARGET_NM)" \
292 CC="$(TARGET_CC)" \
293 GCC="$(TARGET_CC)" \
294 CXX="$(TARGET_CXX)" \
295 RANLIB="$(TARGET_RANLIB)" \
296 STRIP=$(TARGET_CROSS)strip \
297 OBJCOPY=$(TARGET_CROSS)objcopy \
298 OBJDUMP=$(TARGET_CROSS)objdump \
299 SIZE=$(TARGET_CROSS)size
300
301 # strip an entire directory
302 ifneq ($(CONFIG_NO_STRIP),)
303 RSTRIP:=:
304 STRIP:=:
305 else
306 ifneq ($(CONFIG_USE_STRIP),)
307 STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
308 else
309 ifneq ($(CONFIG_USE_SSTRIP),)
310 STRIP:=$(STAGING_DIR_HOST)/bin/sstrip
311 endif
312 endif
313 RSTRIP= \
314 export CROSS="$(TARGET_CROSS)" \
315 $(if $(PKG_BUILD_ID),KEEP_BUILD_ID=1) \
316 $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
317 $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
318 NM="$(TARGET_CROSS)nm" \
319 STRIP="$(STRIP)" \
320 STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
321 PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
322 $(SCRIPT_DIR)/rstrip.sh
323 endif
324
325 ifeq ($(CONFIG_IPV6),y)
326 DISABLE_IPV6:=
327 else
328 DISABLE_IPV6:=--disable-ipv6
329 endif
330
331 TAR_OPTIONS:=-xf -
332
333 ifeq ($(CONFIG_BUILD_LOG),y)
334 BUILD_LOG:=1
335 endif
336
337 export BISON_PKGDATADIR:=$(STAGING_DIR_HOST)/share/bison
338 export M4:=$(STAGING_DIR_HOST)/bin/m4
339
340 define shvar
341 V_$(subst .,_,$(subst -,_,$(subst /,_,$(1))))
342 endef
343
344 define shexport
345 export $(call shvar,$(1))=$$(call $(1))
346 endef
347
348 define include_mk
349 $(eval -include $(if $(DUMP),,$(STAGING_DIR)/mk/$(strip $(1))))
350 endef
351
352 # Execute commands under flock
353 # $(1) => The shell expression.
354 # $(2) => The lock name. If not given, the global lock will be used.
355 ifneq ($(wildcard $(STAGING_DIR_HOST)/bin/flock),)
356 define locked
357 SHELL= \
358 flock \
359 $(TMP_DIR)/.$(if $(2),$(strip $(2)),global).flock \
360 -c '$(subst ','\'',$(1))'
361 endef
362 else
363 locked=$(1)
364 endif
365
366 # Recursively copy paths into another directory, purge dangling
367 # symlinks before.
368 # $(1) => File glob expression
369 # $(2) => Destination directory
370 define file_copy
371 for src_dir in $(sort $(foreach d,$(wildcard $(1)),$(dir $(d)))); do \
372 ( cd $$src_dir; find -type f -or -type d ) | \
373 ( cd $(2); while :; do \
374 read FILE; \
375 [ -z "$$FILE" ] && break; \
376 [ -L "$$FILE" ] || continue; \
377 echo "Removing symlink $(2)/$$FILE"; \
378 rm -f "$$FILE"; \
379 done; ); \
380 done; \
381 $(CP) $(1) $(2)
382 endef
383
384 # Calculate sha256sum of any plain file within a given directory
385 # $(1) => Input directory
386 define sha256sums
387 (cd $(1); find . -maxdepth 1 -type f -not -name 'sha256sums' -printf "%P\n" | sort | \
388 xargs -r $(STAGING_DIR_HOST)/bin/mkhash -n sha256 | sed -ne 's!^\(.*\) \(.*\)$$!\1 *\2!p' > sha256sums)
389 endef
390
391 # file extension
392 ext=$(word $(words $(subst ., ,$(1))),$(subst ., ,$(1)))
393
394 all:
395 FORCE: ;
396 .PHONY: FORCE
397
398 check: FORCE
399 @true
400
401 val.%:
402 @$(if $(filter undefined,$(origin $*)),\
403 echo "$* undefined" >&2, \
404 echo '$(subst ','"'"',$($*))' \
405 )
406
407 var.%:
408 @$(if $(filter undefined,$(origin $*)),\
409 echo "$* undefined" >&2, \
410 echo "$*='"'$(subst ','"'\"'\"'"',$($*))'"'" \
411 )
412
413 endif #__rules_inc