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