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