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