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