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