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