rules.mk 10.4 KB
Newer Older
1
#
2
# Copyright (C) 2006-2010 OpenWrt.org
Mike Baker's avatar
Mike Baker committed
3
4
5
6
7
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

8
9
10
ifneq ($(__rules_inc),1)
__rules_inc=1

11
12
13
ifeq ($(DUMP),)
  -include $(TOPDIR)/.config
endif
14
include $(TOPDIR)/include/debug.mk
Mike Baker's avatar
Mike Baker committed
15
include $(TOPDIR)/include/verbose.mk
Felix Fietkau's avatar
Felix Fietkau committed
16

17
export TMP_DIR:=$(TOPDIR)/tmp
18

19
qstrip=$(strip $(subst ",,$(1)))
20
#"))
Mike Baker's avatar
Mike Baker committed
21

22
23
empty:=
space:= $(empty) $(empty)
24
comma:=,
25
26
merge=$(subst $(space),,$(1))
confvar=$(call merge,$(foreach v,$(1),$(if $($(v)),y,n)))
27
strip_last=$(patsubst %.$(lastword $(subst .,$(space),$(1))),%,$(1))
28

29
30
define sep

31
32
33
34
35
endef

define newline


36
37
endef

38
_SINGLE=export MAKEFLAGS=$(space);
39
CFLAGS:=
40
ARCH:=$(subst i486,i386,$(subst i586,i386,$(subst i686,i386,$(call qstrip,$(CONFIG_ARCH)))))
41
ARCH_PACKAGES:=$(call qstrip,$(CONFIG_TARGET_ARCH_PACKAGES))
42
BOARD:=$(call qstrip,$(CONFIG_TARGET_BOARD))
43
TARGET_OPTIMIZATION:=$(call qstrip,$(CONFIG_TARGET_OPTIMIZATION))
44
export EXTRA_OPTIMIZATION:=$(call qstrip,$(CONFIG_EXTRA_OPTIMIZATION))
Felix Fietkau's avatar
Felix Fietkau committed
45
TARGET_SUFFIX=$(call qstrip,$(CONFIG_TARGET_SUFFIX))
46
BUILD_SUFFIX:=$(call qstrip,$(CONFIG_BUILD_SUFFIX))
47
SUBDIR:=$(patsubst $(TOPDIR)/%,%,${CURDIR})
48
export SHELL:=/usr/bin/env bash
Mike Baker's avatar
Mike Baker committed
49

50
OPTIMIZE_FOR_CPU=$(subst i386,i486,$(ARCH))
Mike Baker's avatar
Mike Baker committed
51

52
53
54
55
56
57
ifeq ($(ARCH),powerpc)
  FPIC:=-fPIC
else
  FPIC:=-fpic
endif

58
59
HOST_FPIC:=-fPIC

60
ARCH_SUFFIX:=$(call qstrip,$(CONFIG_CPU_TYPE))
61
62
GCC_ARCH:=

63
64
65
ifneq ($(ARCH_SUFFIX),)
  ARCH_SUFFIX:=_$(ARCH_SUFFIX)
endif
66
67
68
ifneq ($(filter -march=armv%,$(TARGET_OPTIMIZATION)),)
  GCC_ARCH:=$(patsubst -march=%,%,$(filter -march=armv%,$(TARGET_OPTIMIZATION)))
endif
69
70
71
ifdef CONFIG_HAS_SPE_FPU
  TARGET_SUFFIX:=$(TARGET_SUFFIX)spe
endif
72
73
ifdef CONFIG_MIPS64_ABI
  ifneq ($(CONFIG_MIPS64_ABI_O32),y)
74
     ARCH_SUFFIX:=$(ARCH_SUFFIX)_$(call qstrip,$(CONFIG_MIPS64_ABI))
75
76
  endif
endif
77

Felix Fietkau's avatar
Felix Fietkau committed
78
DL_DIR:=$(if $(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(TOPDIR)/dl)
79
BIN_DIR:=$(if $(call qstrip,$(CONFIG_BINARY_FOLDER)),$(call qstrip,$(CONFIG_BINARY_FOLDER)),$(TOPDIR)/bin/$(BOARD))
80
INCLUDE_DIR:=$(TOPDIR)/include
Mike Baker's avatar
cleanup    
Mike Baker committed
81
SCRIPT_DIR:=$(TOPDIR)/scripts
82
83
84
BUILD_DIR_BASE:=$(TOPDIR)/build_dir
BUILD_DIR_HOST:=$(BUILD_DIR_BASE)/host
STAGING_DIR_HOST:=$(TOPDIR)/staging_dir/host
85
86
87
88
89
90
ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  GCCV:=$(call qstrip,$(CONFIG_GCC_VERSION))
  LIBC:=$(call qstrip,$(CONFIG_LIBC))
  LIBCV:=$(call qstrip,$(CONFIG_LIBC_VERSION))
  REAL_GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))
  GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux
Imre Kaloz's avatar
Imre Kaloz committed
91
  DIR_SUFFIX:=_$(LIBC)-$(LIBCV)$(if $(CONFIG_arm),_eabi)
92
  BIN_DIR:=$(BIN_DIR)$(if $(CONFIG_USE_UCLIBC),,-$(LIBC))
93
94
  TARGET_DIR_NAME = target-$(ARCH)$(ARCH_SUFFIX)$(DIR_SUFFIX)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
  TOOLCHAIN_DIR_NAME = toolchain-$(ARCH)$(ARCH_SUFFIX)_gcc-$(GCCV)$(DIR_SUFFIX)
95
96
97
98
99
100
101
else
  ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
    GNU_TARGET_NAME=$(call qstrip,$(CONFIG_TARGET_NAME))
  else
    GNU_TARGET_NAME=$(shell gcc -dumpmachine)
  endif
  REAL_GNU_TARGET_NAME=$(GNU_TARGET_NAME)
102
103
  TARGET_DIR_NAME:=target-$(GNU_TARGET_NAME)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
  TOOLCHAIN_DIR_NAME:=toolchain-$(GNU_TARGET_NAME)
104
endif
105
106
107
108
109
110

PACKAGE_DIR:=$(BIN_DIR)/packages
BUILD_DIR:=$(BUILD_DIR_BASE)/$(TARGET_DIR_NAME)
STAGING_DIR:=$(TOPDIR)/staging_dir/$(TARGET_DIR_NAME)
BUILD_DIR_TOOLCHAIN:=$(BUILD_DIR_BASE)/$(TOOLCHAIN_DIR_NAME)
TOOLCHAIN_DIR:=$(TOPDIR)/staging_dir/$(TOOLCHAIN_DIR_NAME)
Mike Baker's avatar
Mike Baker committed
111
STAMP_DIR:=$(BUILD_DIR)/stamp
112
STAMP_DIR_HOST=$(BUILD_DIR_HOST)/stamp
113
114
TARGET_ROOTFS_DIR?=$(if $(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(BUILD_DIR))
TARGET_DIR:=$(TARGET_ROOTFS_DIR)/root-$(BOARD)
115
STAGING_DIR_ROOT:=$(STAGING_DIR)/root-$(BOARD)
116
BUILD_LOG_DIR:=$(TOPDIR)/logs
117
PKG_INFO_DIR := $(STAGING_DIR)/pkginfo
Mike Baker's avatar
cleanup    
Mike Baker committed
118

119
TARGET_PATH:=$(subst $(space),:,$(filter-out .,$(filter-out ./,$(subst :,$(space),$(PATH)))))
120
TARGET_CFLAGS:=$(TARGET_OPTIMIZATION)$(if $(CONFIG_DEBUG), -g3) $(EXTRA_OPTIMIZATION)
121
TARGET_CXXFLAGS = $(TARGET_CFLAGS)
122
123
TARGET_ASFLAGS_DEFAULT = $(TARGET_CFLAGS)
TARGET_ASFLAGS = $(TARGET_ASFLAGS_DEFAULT)
124
TARGET_CPPFLAGS:=-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include
125
TARGET_LDFLAGS:=-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib
126
127
128
ifneq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
LIBGCC_S_PATH=$(realpath $(wildcard $(call qstrip,$(CONFIG_LIBGCC_ROOT_DIR))/$(call qstrip,$(CONFIG_LIBGCC_FILE_SPEC))))
LIBGCC_S=$(if $(LIBGCC_S_PATH),-L$(dir $(LIBGCC_S_PATH)) -lgcc_s)
129
LIBGCC_A=$(realpath $(lastword $(wildcard $(dir $(LIBGCC_S_PATH))/gcc/*/*/libgcc.a)))
130
else
131
LIBGCC_A=$(lastword $(wildcard $(TOOLCHAIN_DIR)/lib/gcc/*/*/libgcc.a))
132
LIBGCC_S=$(if $(wildcard $(TOOLCHAIN_DIR)/lib/libgcc_s.so),-L$(TOOLCHAIN_DIR)/lib -lgcc_s,$(LIBGCC_A))
133
endif
134
LIBRPC=-lrpc
135
LIBRPC_DEPENDS=+librpc
136

137
ifeq ($(CONFIG_ARCH_64BIT),y)
138
139
140
  LIB_SUFFIX:=64
endif

141
ifndef DUMP
142
143
  ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
    -include $(TOOLCHAIN_DIR)/info.mk
144
    export GCC_HONOUR_COPTS:=0
145
    TARGET_CROSS:=$(if $(TARGET_CROSS),$(TARGET_CROSS),$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))-)
146
    TARGET_CFLAGS+= -fhonour-copts $(if $(CONFIG_GCC_VERSION_4_4)$(CONFIG_GCC_VERSION_4_5),,-Wno-error=unused-but-set-variable)
147
148
    TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/usr/include -I$(TOOLCHAIN_DIR)/include
    TARGET_LDFLAGS+= -L$(TOOLCHAIN_DIR)/usr/lib -L$(TOOLCHAIN_DIR)/lib
149
    TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  else
    ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
      TARGET_CROSS:=$(call qstrip,$(CONFIG_TOOLCHAIN_PREFIX))
      TOOLCHAIN_ROOT_DIR:=$(call qstrip,$(CONFIG_TOOLCHAIN_ROOT))
      TOOLCHAIN_BIN_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_BIN_PATH)))
      TOOLCHAIN_INC_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_INC_PATH)))
      TOOLCHAIN_LIB_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_LIB_PATH)))
      ifneq ($(TOOLCHAIN_BIN_DIRS),)
        TARGET_PATH:=$(subst $(space),:,$(TOOLCHAIN_BIN_DIRS)):$(TARGET_PATH)
      endif
      ifneq ($(TOOLCHAIN_INC_DIRS),)
        TARGET_CPPFLAGS+= $(patsubst %,-I%,$(TOOLCHAIN_INC_DIRS))
      endif
      ifneq ($(TOOLCHAIN_LIB_DIRS),)
        TARGET_LDFLAGS+= $(patsubst %,-L%,$(TOOLCHAIN_LIB_DIRS))
      endif
166
      TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
167
168
    endif
  endif
169
endif
170
TARGET_PATH_PKG:=$(STAGING_DIR)/host/bin:$(TARGET_PATH)
171

Imre Kaloz's avatar
Imre Kaloz committed
172
ifeq ($(CONFIG_SOFT_FLOAT),y)
Nicolas Thill's avatar
Nicolas Thill committed
173
  SOFT_FLOAT_CONFIG_OPTION:=--with-float=soft
174
175
176
177
178
  ifeq ($(CONFIG_arm),y)
    TARGET_CFLAGS+= -mfloat-abi=soft
  else
    TARGET_CFLAGS+= -msoft-float
  endif
Imre Kaloz's avatar
Imre Kaloz committed
179
else
Nicolas Thill's avatar
Nicolas Thill committed
180
  SOFT_FLOAT_CONFIG_OPTION:=
181
182
183
  ifeq ($(CONFIG_arm),y)
    TARGET_CFLAGS+= -mfloat-abi=hard
  endif
Imre Kaloz's avatar
Imre Kaloz committed
184
185
endif

Mike Baker's avatar
Mike Baker committed
186
export PATH:=$(TARGET_PATH)
Felix Fietkau's avatar
Felix Fietkau committed
187
export STAGING_DIR
188
export SH_FUNC:=. $(INCLUDE_DIR)/shell.sh;
Mike Baker's avatar
Mike Baker committed
189

John Crispin's avatar
John Crispin committed
190
191
192
193
PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config

export PKG_CONFIG

Mike Baker's avatar
cleanup    
Mike Baker committed
194
HOSTCC:=gcc
195
HOSTCXX:=g++
196
HOST_CPPFLAGS:=-I$(STAGING_DIR_HOST)/include -I$(STAGING_DIR_HOST)/usr/include
197
HOST_CFLAGS:=-O2 $(HOST_CPPFLAGS)
198
HOST_LDFLAGS:=-L$(STAGING_DIR_HOST)/lib -L$(STAGING_DIR_HOST)/usr/lib
199

200
ifeq ($(CONFIG_GCC_VERSION_4_4)$(CONFIG_GCC_VERSION_4_6)$(CONFIG_EXTERNAL_TOOLCHAIN),)
201
202
203
204
205
206
207
208
209
  TARGET_AR:=$(TARGET_CROSS)gcc-ar
  TARGET_RANLIB:=$(TARGET_CROSS)gcc-ranlib
  TARGET_NM:=$(TARGET_CROSS)gcc-nm
else
  TARGET_AR:=$(TARGET_CROSS)ar
  TARGET_RANLIB:=$(TARGET_CROSS)ranlib
  TARGET_NM:=$(TARGET_CROSS)nm
endif

210
211
BUILD_KEY=$(TOPDIR)/key-build

Mike Baker's avatar
cleanup    
Mike Baker committed
212
TARGET_CC:=$(TARGET_CROSS)gcc
213
TARGET_CXX:=$(TARGET_CROSS)g++
Jo-Philipp Wich's avatar
Jo-Philipp Wich committed
214
KPATCH:=$(SCRIPT_DIR)/patch-kernel.sh
215
SED:=$(STAGING_DIR_HOST)/bin/sed -i -e
Mike Baker's avatar
cleanup    
Mike Baker committed
216
CP:=cp -fpR
217
LN:=ln -sf
218
XARGS:=xargs -r
Mike Baker's avatar
Mike Baker committed
219

220
221
222
223
224
BASH:=bash
TAR:=tar
FIND:=find
PATCH:=patch
PYTHON:=python
225

226
227
228
INSTALL_BIN:=install -m0755
INSTALL_DIR:=install -d -m0755
INSTALL_DATA:=install -m0644
229
INSTALL_CONF:=install -m0600
230

John Crispin's avatar
John Crispin committed
231
232
233
TARGET_CC_NOCACHE:=$(TARGET_CC)
TARGET_CXX_NOCACHE:=$(TARGET_CXX)
HOSTCC_NOCACHE:=$(HOSTCC)
234
HOSTCXX_NOCACHE:=$(HOSTCXX)
John Crispin's avatar
John Crispin committed
235
236
237
238
export TARGET_CC_NOCACHE
export TARGET_CXX_NOCACHE
export HOSTCC_NOCACHE

239
ifneq ($(CONFIG_CCACHE),)
John Crispin's avatar
John Crispin committed
240
241
  TARGET_CC:= ccache_cc
  TARGET_CXX:= ccache_cxx
242
  HOSTCC:= ccache $(HOSTCC)
243
  HOSTCXX:= ccache $(HOSTCXX)
244
245
endif

246
TARGET_CONFIGURE_OPTS = \
247
  AR="$(TARGET_AR)" \
248
  AS="$(TARGET_CC) -c $(TARGET_ASFLAGS)" \
Mike Baker's avatar
cleanup    
Mike Baker committed
249
  LD=$(TARGET_CROSS)ld \
250
  NM="$(TARGET_NM)" \
251
252
  CC="$(TARGET_CC)" \
  GCC="$(TARGET_CC)" \
253
  CXX="$(TARGET_CXX)" \
254
  RANLIB="$(TARGET_RANLIB)" \
255
  STRIP=$(TARGET_CROSS)strip \
256
  OBJCOPY=$(TARGET_CROSS)objcopy \
257
258
  OBJDUMP=$(TARGET_CROSS)objdump \
  SIZE=$(TARGET_CROSS)size
Mike Baker's avatar
cleanup    
Mike Baker committed
259
260

# strip an entire directory
261
ifneq ($(CONFIG_NO_STRIP),)
262
  RSTRIP:=:
263
  STRIP:=:
264
else
265
  ifneq ($(CONFIG_USE_STRIP),)
266
    STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
267
268
269
270
271
  else
    ifneq ($(CONFIG_USE_SSTRIP),)
      STRIP:=$(STAGING_DIR_HOST)/bin/sstrip
    endif
  endif
272
  RSTRIP:= \
273
274
275
    export CROSS="$(TARGET_CROSS)" \
		$(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
		$(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
276
277
    NM="$(TARGET_CROSS)nm" \
    STRIP="$(STRIP)" \
278
    STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
279
    PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
280
281
    $(SCRIPT_DIR)/rstrip.sh
endif
Mike Baker's avatar
cleanup    
Mike Baker committed
282

283
ifeq ($(CONFIG_IPV6),y)
284
  DISABLE_IPV6:=
285
286
287
288
else
  DISABLE_IPV6:=--disable-ipv6
endif

289
290
TAR_OPTIONS:=-xf -

291
292
293
294
ifeq ($(CONFIG_BUILD_LOG),y)
  BUILD_LOG:=1
endif

295
296
297
export BISON_PKGDATADIR:=$(STAGING_DIR_HOST)/share/bison
export M4:=$(STAGING_DIR_HOST)/bin/m4

298
299
300
301
302
define shvar
V_$(subst .,_,$(subst -,_,$(subst /,_,$(1))))
endef

define shexport
303
export $(call shvar,$(1))=$$(call $(1))
304
305
endef

306
307
308
309
define include_mk
$(eval -include $(if $(DUMP),,$(STAGING_DIR)/mk/$(strip $(1))))
endef

310
311
312
313
# Execute commands under flock
# $(1) => The shell expression.
# $(2) => The lock name. If not given, the global lock will be used.
define locked
314
315
316
317
	SHELL= \
	$(STAGING_DIR_HOST)/bin/flock \
		$(TMP_DIR)/.$(if $(2),$(strip $(2)),global).flock \
		-c '$(subst ','\'',$(1))'
318
319
endef

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Recursively copy paths into another directory, purge dangling
# symlinks before.
# $(1) => File glob expression
# $(2) => Destination directory
define file_copy
	for src_dir in $(sort $(foreach d,$(wildcard $(1)),$(dir $(d)))); do \
		( cd $$src_dir; find -type f -or -type d ) | \
			( cd $(2); while :; do \
				read FILE; \
				[ -z "$$FILE" ] && break; \
				[ -L "$$FILE" ] || continue; \
				echo "Removing symlink $(2)/$$FILE"; \
				rm -f "$$FILE"; \
			done; ); \
	done; \
335
	$(CP) $(1) $(2)
336
337
endef

338
339
340
# file extension
ext=$(word $(words $(subst ., ,$(1))),$(subst ., ,$(1)))

Mike Baker's avatar
Mike Baker committed
341
all:
Mike Baker's avatar
cleanup    
Mike Baker committed
342
FORCE: ;
Mike Baker's avatar
Mike Baker committed
343
.PHONY: FORCE
344

345
346
347
348
349
350
351
352
353
354
355
356
val.%:
	@$(if $(filter undefined,$(origin $*)),\
		echo "$* undefined" >&2, \
		echo '$(subst ','"'"',$($*))' \
	)

var.%:
	@$(if $(filter undefined,$(origin $*)),\
		echo "$* undefined" >&2, \
		echo "$*='"'$(subst ','"'\"'\"'"',$($*))'"'" \
	)

357
endif #__rules_inc