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