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