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