2 # Copyright (C) 2006-2010 OpenWrt.org
3 # Copyright (C) 2016 LEDE Project
5 # This is free software, licensed under the GNU General Public License v2.
6 # See /LICENSE for more information.
9 ifneq ($(__rules_inc
),1)
13 -include $(TOPDIR
)/.config
15 include $(TOPDIR
)/include/debug.mk
16 include $(TOPDIR
)/include/verbose.mk
18 ifneq ($(filter check,$(MAKECMDGOALS
)),)
23 export TMP_DIR
:=$(TOPDIR
)/tmp
24 export TMPDIR
:=$(TMP_DIR
)
26 qstrip
=$(strip $(subst ",,$(1)))
30 space
:= $(empty
) $(empty
)
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))
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
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)
56 $(eval toupper = $(call __tr_template,$(chars_lower),$(chars_upper)))
57 $(eval tolower = $(call __tr_template,$(chars_upper),$(chars_lower)))
59 version_abbrev = $(if $(if $(CHECK),,$(DUMP)),$(1),$(shell printf '%.8s
' $(1)))
61 _SINGLE=export MAKEFLAGS=$(space);
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
75 IS_PACKAGE_BUILD := $(if $(filter package/%,$(BUILD_SUBDIR)),1)
77 OPTIMIZE_FOR_CPU=$(subst i386,i486,$(ARCH))
79 ifeq ($(ARCH),powerpc)
87 ARCH_SUFFIX:=$(call qstrip,$(CONFIG_CPU_TYPE))
90 ifneq ($(ARCH_SUFFIX),)
91 ARCH_SUFFIX:=_$(ARCH_SUFFIX)
93 ifneq ($(filter -march=armv%,$(TARGET_OPTIMIZATION)),)
94 GCC_ARCH:=$(patsubst -march=%,%,$(filter -march=armv%,$(TARGET_OPTIMIZATION)))
96 ifdef CONFIG_HAS_SPE_FPU
97 TARGET_SUFFIX:=$(TARGET_SUFFIX)spe
99 ifdef CONFIG_MIPS64_ABI
100 ifneq ($(CONFIG_MIPS64_ABI_O32),y)
101 ARCH_SUFFIX:=$(ARCH_SUFFIX)_$(call qstrip,$(CONFIG_MIPS64_ABI))
105 DEFAULT_SUBDIR_TARGETS:=clean download prepare compile update refresh prereq dist distcheck configure check check-depends
107 define DefaultTargets
108 $(foreach t,$(DEFAULT_SUBDIR_TARGETS) $(1),
115 DL_DIR:=$(if $(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(TOPDIR)/dl)
116 OUTPUT_DIR:=$(if $(call qstrip,$(CONFIG_BINARY_FOLDER)),$(call qstrip,$(CONFIG_BINARY_FOLDER)),$(TOPDIR)/bin)
117 BIN_DIR:=$(OUTPUT_DIR)/targets/$(BOARD)/$(SUBTARGET)
118 INCLUDE_DIR:=$(TOPDIR)/include
119 SCRIPT_DIR:=$(TOPDIR)/scripts
120 BUILD_DIR_BASE:=$(TOPDIR)/build_dir
121 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
122 GCCV:=$(call qstrip,$(CONFIG_GCC_VERSION))
123 LIBC:=$(call qstrip,$(CONFIG_LIBC))
124 REAL_GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))
125 GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux
126 DIR_SUFFIX:=_$(LIBC)$(if $(CONFIG_arm),_eabi)
127 BIN_DIR:=$(BIN_DIR)$(if $(CONFIG_USE_MUSL),,-$(LIBC))
128 TARGET_DIR_NAME = target-$(ARCH)$(ARCH_SUFFIX)$(DIR_SUFFIX)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
129 TOOLCHAIN_DIR_NAME = toolchain-$(ARCH)$(ARCH_SUFFIX)_gcc-$(GCCV)$(DIR_SUFFIX)
131 ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
132 GNU_TARGET_NAME=$(call qstrip,$(CONFIG_TARGET_NAME))
134 GNU_TARGET_NAME=$(shell gcc -dumpmachine)
136 REAL_GNU_TARGET_NAME=$(GNU_TARGET_NAME)
137 LIBC:=$(call qstrip,$(CONFIG_LIBC))
138 TARGET_DIR_NAME:=target-$(GNU_TARGET_NAME)_$(LIBC)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
139 TOOLCHAIN_DIR_NAME:=toolchain-$(GNU_TARGET_NAME)
142 ifeq ($(or $(CONFIG_EXTERNAL_TOOLCHAIN),$(CONFIG_GCC_VERSION_4_8),$(CONFIG_TARGET_uml)),)
143 ifeq ($(CONFIG_GCC_USE_EMBEDDED_PATH_REMAP),y)
144 iremap = -fmacro-prefix-map=$(1)=$(2)
146 iremap = -iremap$(1):$(2)
150 PACKAGE_DIR:=$(BIN_DIR)/packages
151 PACKAGE_DIR_ALL:=$(TOPDIR)/staging_dir/packages/$(BOARD)
152 BUILD_DIR:=$(BUILD_DIR_BASE)/$(TARGET_DIR_NAME)
153 STAGING_DIR:=$(TOPDIR)/staging_dir/$(TARGET_DIR_NAME)
154 BUILD_DIR_TOOLCHAIN:=$(BUILD_DIR_BASE)/$(TOOLCHAIN_DIR_NAME)
155 TOOLCHAIN_DIR:=$(TOPDIR)/staging_dir/$(TOOLCHAIN_DIR_NAME)
156 STAMP_DIR:=$(BUILD_DIR)/stamp
157 STAMP_DIR_HOST=$(BUILD_DIR_HOST)/stamp
158 TARGET_ROOTFS_DIR?=$(if $(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(BUILD_DIR))
159 TARGET_DIR:=$(TARGET_ROOTFS_DIR)/root-$(BOARD)
160 STAGING_DIR_ROOT:=$(STAGING_DIR)/root-$(BOARD)
161 STAGING_DIR_IMAGE:=$(STAGING_DIR)/image
162 BUILD_LOG_DIR:=$(TOPDIR)/logs
163 PKG_INFO_DIR := $(STAGING_DIR)/pkginfo
165 BUILD_DIR_HOST:=$(if $(IS_PACKAGE_BUILD),$(BUILD_DIR_BASE)/hostpkg,$(BUILD_DIR_BASE)/host)
166 STAGING_DIR_HOST:=$(TOPDIR)/staging_dir/host
167 STAGING_DIR_HOSTPKG:=$(TOPDIR)/staging_dir/hostpkg
169 TARGET_PATH:=$(subst $(space),:,$(filter-out .,$(filter-out ./,$(subst :,$(space),$(PATH)))))
170 TARGET_INIT_PATH:=$(call qstrip,$(CONFIG_TARGET_INIT_PATH))
171 TARGET_INIT_PATH:=$(if $(TARGET_INIT_PATH),$(TARGET_INIT_PATH),/usr/sbin:/sbin:/usr/bin:/bin)
172 TARGET_CFLAGS:=$(TARGET_OPTIMIZATION)$(if $(CONFIG_DEBUG), -g3) $(call qstrip,$(CONFIG_EXTRA_OPTIMIZATION))
173 TARGET_CXXFLAGS = $(TARGET_CFLAGS)
174 TARGET_ASFLAGS_DEFAULT = $(TARGET_CFLAGS)
175 TARGET_ASFLAGS = $(TARGET_ASFLAGS_DEFAULT)
176 TARGET_CPPFLAGS:=-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include
177 TARGET_LDFLAGS:=-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib
178 ifneq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
179 LIBGCC_S_PATH=$(realpath $(wildcard $(call qstrip,$(CONFIG_LIBGCC_ROOT_DIR))/$(call qstrip,$(CONFIG_LIBGCC_FILE_SPEC))))
180 LIBGCC_S=$(if $(LIBGCC_S_PATH),-L$(dir $(LIBGCC_S_PATH)) -lgcc_s)
181 LIBGCC_A=$(realpath $(lastword $(wildcard $(dir $(LIBGCC_S_PATH))/gcc/*/*/libgcc.a)))
183 LIBGCC_A=$(lastword $(wildcard $(TOOLCHAIN_DIR)/lib/gcc/*/*/libgcc.a))
184 LIBGCC_S=$(if $(wildcard $(TOOLCHAIN_DIR)/lib/libgcc_s.so),-L$(TOOLCHAIN_DIR)/lib -lgcc_s,$(LIBGCC_A))
187 ifeq ($(CONFIG_ARCH_64BIT),y)
192 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
193 -include $(TOOLCHAIN_DIR)/info.mk
194 export GCC_HONOUR_COPTS:=0
195 TARGET_CROSS:=$(if $(TARGET_CROSS),$(TARGET_CROSS),$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))-)
196 TARGET_CFLAGS+= -fhonour-copts -Wno-error=unused-but-set-variable -Wno-error=unused-result
197 TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/usr/include
198 ifeq ($(CONFIG_USE_MUSL),y)
199 TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/include/fortify
201 TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/include
202 TARGET_LDFLAGS+= -L$(TOOLCHAIN_DIR)/usr/lib -L$(TOOLCHAIN_DIR)/lib
203 TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
205 ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
206 TARGET_CROSS:=$(call qstrip,$(CONFIG_TOOLCHAIN_PREFIX))
207 TOOLCHAIN_ROOT_DIR:=$(call qstrip,$(CONFIG_TOOLCHAIN_ROOT))
208 TOOLCHAIN_BIN_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_BIN_PATH)))
209 TOOLCHAIN_INC_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_INC_PATH)))
210 TOOLCHAIN_LIB_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_LIB_PATH)))
211 ifneq ($(TOOLCHAIN_BIN_DIRS),)
212 TARGET_PATH:=$(subst $(space),:,$(TOOLCHAIN_BIN_DIRS)):$(TARGET_PATH)
214 ifneq ($(TOOLCHAIN_INC_DIRS),)
215 TARGET_CPPFLAGS+= $(patsubst %,-I%,$(TOOLCHAIN_INC_DIRS))
217 ifneq ($(TOOLCHAIN_LIB_DIRS),)
218 TARGET_LDFLAGS+= $(patsubst %,-L%,$(TOOLCHAIN_LIB_DIRS))
220 TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
224 TARGET_PATH_PKG:=$(STAGING_DIR)/host/bin:$(STAGING_DIR_HOSTPKG)/bin:$(TARGET_PATH)
226 ifeq ($(CONFIG_SOFT_FLOAT),y)
227 SOFT_FLOAT_CONFIG_OPTION:=--with-float=soft
228 ifeq ($(CONFIG_arm),y)
229 TARGET_CFLAGS+= -mfloat-abi=soft
231 TARGET_CFLAGS+= -msoft-float
234 SOFT_FLOAT_CONFIG_OPTION:=
235 ifeq ($(CONFIG_arm),y)
236 TARGET_CFLAGS+= -mfloat-abi=hard
240 export PATH:=$(TARGET_PATH)
241 export STAGING_DIR STAGING_DIR_HOST STAGING_DIR_HOSTPKG
242 export SH_FUNC:=. $(INCLUDE_DIR)/shell.sh;
244 PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
250 HOST_CPPFLAGS:=-I$(STAGING_DIR_HOST)/include $(if $(IS_PACKAGE_BUILD),-I$(STAGING_DIR_HOSTPKG)/include -I$(STAGING_DIR)/host/include)
251 HOST_CFLAGS:=-O2 $(HOST_CPPFLAGS)
252 HOST_LDFLAGS:=-L$(STAGING_DIR_HOST)/lib $(if $(IS_PACKAGE_BUILD),-L$(STAGING_DIR_HOSTPKG)/lib -L$(STAGING_DIR)/host/lib)
254 ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
255 TARGET_AR:=$(TARGET_CROSS)gcc-ar
256 TARGET_RANLIB:=$(TARGET_CROSS)gcc-ranlib
257 TARGET_NM:=$(TARGET_CROSS)gcc-nm
259 TARGET_AR:=$(TARGET_CROSS)ar
260 TARGET_RANLIB:=$(TARGET_CROSS)ranlib
261 TARGET_NM:=$(TARGET_CROSS)nm
264 BUILD_KEY=$(TOPDIR)/key-build
266 TARGET_CC:=$(TARGET_CROSS)gcc
267 TARGET_CXX:=$(TARGET_CROSS)g++
268 KPATCH:=$(SCRIPT_DIR)/patch-kernel.sh
269 SED:=$(STAGING_DIR_HOST)/bin/sed -i -e
270 ESED:=$(STAGING_DIR_HOST)/bin/sed -E -i -e
281 INSTALL_BIN:=install -m0755
282 INSTALL_SUID:=install -m4755
283 INSTALL_DIR:=install -d -m0755
284 INSTALL_DATA:=install -m0644
285 INSTALL_CONF:=install -m0600
287 TARGET_CC_NOCACHE:=$(TARGET_CC)
288 TARGET_CXX_NOCACHE:=$(TARGET_CXX)
289 HOSTCC_NOCACHE:=$(HOSTCC)
290 HOSTCXX_NOCACHE:=$(HOSTCXX)
291 export TARGET_CC_NOCACHE
292 export TARGET_CXX_NOCACHE
293 export HOSTCC_NOCACHE
295 ifneq ($(CONFIG_CCACHE),)
296 TARGET_CC:= ccache_cc
297 TARGET_CXX:= ccache_cxx
298 HOSTCC:= ccache $(HOSTCC)
299 HOSTCXX:= ccache $(HOSTCXX)
302 TARGET_CONFIGURE_OPTS = \
304 AS="$(TARGET_CC) -c $(TARGET_ASFLAGS)" \
305 LD=$(TARGET_CROSS)ld \
309 CXX="$(TARGET_CXX)" \
310 RANLIB="$(TARGET_RANLIB)" \
311 STRIP=$(TARGET_CROSS)strip \
312 OBJCOPY=$(TARGET_CROSS)objcopy \
313 OBJDUMP=$(TARGET_CROSS)objdump \
314 SIZE=$(TARGET_CROSS)size
316 # strip an entire directory
317 ifneq ($(CONFIG_NO_STRIP),)
321 ifneq ($(CONFIG_USE_STRIP),)
322 STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
324 ifneq ($(CONFIG_USE_SSTRIP),)
325 STRIP:=$(STAGING_DIR_HOST)/bin/sstrip
329 export CROSS="$(TARGET_CROSS)" \
330 $(if $(PKG_BUILD_ID),KEEP_BUILD_ID=1) \
331 $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
332 $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
333 NM="$(TARGET_CROSS)nm" \
335 STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
336 PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
337 $(SCRIPT_DIR)/rstrip.sh
340 ifeq ($(CONFIG_IPV6),y)
343 DISABLE_IPV6:=--disable-ipv6
348 ifeq ($(CONFIG_BUILD_LOG),y)
352 export BISON_PKGDATADIR:=$(STAGING_DIR_HOST)/share/bison
353 export M4:=$(STAGING_DIR_HOST)/bin/m4
356 V_$(subst .,_,$(subst -,_,$(subst /,_,$(1))))
360 export $(call shvar,$(1))=$$(call $(1))
363 # Execute commands under flock
364 # $(1) => The shell expression.
365 # $(2) => The lock name. If not given, the global lock will be used.
366 ifneq ($(wildcard $(STAGING_DIR_HOST)/bin/flock),)
370 $(TMP_DIR)/.$(if $(2),$(strip $(2)),global).flock \
371 -c '$(subst ','\'',$(1))'
377 # Recursively copy paths into another directory, purge dangling
379 # $(1) => File glob expression
380 # $(2) => Destination directory
382 for src_dir in
$(sort $(foreach d
,$(wildcard $(1)),$(dir $(d
)))); do \
383 ( cd
$$src_dir; find
-type f
-or
-type d
) | \
384 ( cd
$(2); while
:; do \
386 [ -z
"$$FILE" ] && break
; \
387 [ -L
"$$FILE" ] || continue
; \
388 echo
"Removing symlink $(2)/$$FILE"; \
395 # Calculate sha256sum of any plain file within a given directory
396 # $(1) => Input directory
397 # $(2) => If set, recurse into subdirectories
399 (cd
$(1); find .
$(if
$(2),,-maxdepth
1) -type f
-not
-name
'sha256sums' -printf
"%P\n" |
sort | \
400 xargs
-r
$(STAGING_DIR_HOST
)/bin
/mkhash
-n sha256 | sed
-ne
's!^\(.*\) \(.*\)$$!\1 *\2!p' > sha256sums
)
404 ext
=$(word $(words $(subst .
, ,$(1))),$(subst .
, ,$(1)))
414 @
$(if
$(filter undefined
,$(origin $*)),\
415 echo
"$* undefined" >&2, \
416 echo
'$(subst ','"'"',$($*))' \
420 @$(if $(filter undefined,$(origin $*)),\
421 echo "$* undefined
" >&2, \
422 echo "$*='"'$(subst ','"'\"'\"'"',$($*))'"'" \