rules.mk 9.85 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
18
TMP_DIR:=$(TOPDIR)/tmp

Florian Fainelli's avatar
Florian Fainelli committed
19
20
21
GREP_OPTIONS=
export GREP_OPTIONS

22
qstrip=$(strip $(subst ",,$(1)))
23
#"))
Mike Baker's avatar
Mike Baker committed
24

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

31
32
33
34
define sep

endef

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))
Felix Fietkau's avatar
Felix Fietkau committed
41
TARGET_SUFFIX=$(call qstrip,$(CONFIG_TARGET_SUFFIX))
42
BUILD_SUFFIX:=$(call qstrip,$(CONFIG_BUILD_SUFFIX))
43
SUBDIR:=$(patsubst $(TOPDIR)/%,%,${CURDIR})
44
export SHELL:=/usr/bin/env bash
Mike Baker's avatar
Mike Baker committed
45

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

48
49
50
51
52
53
ifeq ($(ARCH),powerpc)
  FPIC:=-fPIC
else
  FPIC:=-fpic
endif

54
55
HOST_FPIC:=-fPIC

56
ARCH_SUFFIX:=
57
58
59
60
61
62
GCC_ARCH:=

ifneq ($(filter -march=armv%,$(TARGET_OPTIMIZATION)),)
  ARCH_SUFFIX:=_$(patsubst -march=arm%,%,$(filter -march=armv%,$(TARGET_OPTIMIZATION)))
  GCC_ARCH:=$(patsubst -march=%,%,$(filter -march=armv%,$(TARGET_OPTIMIZATION)))
endif
63
ifneq ($(filter -mips%r2,$(TARGET_OPTIMIZATION)),)
64
65
  ARCH_SUFFIX:=_r2
endif
66
67
68
ifdef CONFIG_HAS_SPE_FPU
  TARGET_SUFFIX:=$(TARGET_SUFFIX)spe
endif
69

Felix Fietkau's avatar
Felix Fietkau committed
70
DL_DIR:=$(if $(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(TOPDIR)/dl)
71
BIN_DIR:=$(TOPDIR)/bin/$(BOARD)
72
INCLUDE_DIR:=$(TOPDIR)/include
Mike Baker's avatar
cleanup    
Mike Baker committed
73
SCRIPT_DIR:=$(TOPDIR)/scripts
74
75
76
BUILD_DIR_BASE:=$(TOPDIR)/build_dir
BUILD_DIR_HOST:=$(BUILD_DIR_BASE)/host
STAGING_DIR_HOST:=$(TOPDIR)/staging_dir/host
77
78
79
80
81
82
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
83
  DIR_SUFFIX:=_$(LIBC)-$(LIBCV)$(if $(CONFIG_arm),_eabi)
84
  BIN_DIR:=$(BIN_DIR)$(if $(CONFIG_USE_UCLIBC),,-$(LIBC))
85
  BUILD_DIR:=$(BUILD_DIR_BASE)/target-$(ARCH)$(ARCH_SUFFIX)$(DIR_SUFFIX)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
86
  STAGING_DIR:=$(TOPDIR)/staging_dir/target-$(ARCH)$(ARCH_SUFFIX)$(DIR_SUFFIX)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
87
88
  BUILD_DIR_TOOLCHAIN:=$(BUILD_DIR_BASE)/toolchain-$(ARCH)$(ARCH_SUFFIX)_gcc-$(GCCV)$(DIR_SUFFIX)
  TOOLCHAIN_DIR:=$(TOPDIR)/staging_dir/toolchain-$(ARCH)$(ARCH_SUFFIX)_gcc-$(GCCV)$(DIR_SUFFIX)
89
  PACKAGE_DIR:=$(BIN_DIR)/packages
90
91
92
93
94
95
96
97
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)
  BUILD_DIR:=$(BUILD_DIR_BASE)/target-$(GNU_TARGET_NAME)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
98
  STAGING_DIR:=$(TOPDIR)/staging_dir/target-$(GNU_TARGET_NAME)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
99
100
  BUILD_DIR_TOOLCHAIN:=$(BUILD_DIR_BASE)/toolchain-$(GNU_TARGET_NAME)
  TOOLCHAIN_DIR:=$(TOPDIR)/staging_dir/toolchain-$(GNU_TARGET_NAME)
101
  PACKAGE_DIR:=$(BIN_DIR)/packages
102
endif
Mike Baker's avatar
Mike Baker committed
103
STAMP_DIR:=$(BUILD_DIR)/stamp
104
STAMP_DIR_HOST=$(BUILD_DIR_HOST)/stamp
105
106
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)
107
STAGING_DIR_ROOT:=$(STAGING_DIR)/root-$(BOARD)
108
BUILD_LOG_DIR:=$(TOPDIR)/logs
109
PKG_INFO_DIR := $(STAGING_DIR)/pkginfo
Mike Baker's avatar
cleanup    
Mike Baker committed
110

111
TARGET_PATH:=$(STAGING_DIR_HOST)/bin:$(subst $(space),:,$(filter-out .,$(filter-out ./,$(subst :,$(space),$(PATH)))))
112
TARGET_CFLAGS:=$(TARGET_OPTIMIZATION)$(if $(CONFIG_DEBUG), -g3)
113
TARGET_CXXFLAGS = $(TARGET_CFLAGS)
114
TARGET_CPPFLAGS:=-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include
115
TARGET_LDFLAGS:=-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib
116
117
118
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)
119
LIBGCC_A=$(realpath $(lastword $(wildcard $(dir $(LIBGCC_S_PATH))/gcc/*/*/libgcc.a)))
120
else
121
LIBGCC_A=$(lastword $(wildcard $(TOOLCHAIN_DIR)/lib/gcc/*/*/libgcc.a))
122
LIBGCC_S=$(if $(wildcard $(TOOLCHAIN_DIR)/lib/libgcc_s.so),-L$(TOOLCHAIN_DIR)/lib -lgcc_s,$(LIBGCC_A))
123
endif
124
LIBRPC=-lrpc
125
LIBRPC_DEPENDS=+librpc
126

127
128
129
130
ifneq ($(findstring $(ARCH) , mips64 x86_64 ),)
  LIB_SUFFIX:=64
endif

131
ifndef DUMP
132
133
  ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
    -include $(TOOLCHAIN_DIR)/info.mk
134
    export GCC_HONOUR_COPTS:=0
135
    TARGET_CROSS:=$(if $(TARGET_CROSS),$(TARGET_CROSS),$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))-)
136
    TARGET_CFLAGS+= -fhonour-copts $(if $(CONFIG_GCC_VERSION_4_4)$(CONFIG_GCC_VERSION_4_5),,-Wno-error=unused-but-set-variable)
137
138
    TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/usr/include -I$(TOOLCHAIN_DIR)/include
    TARGET_LDFLAGS+= -L$(TOOLCHAIN_DIR)/usr/lib -L$(TOOLCHAIN_DIR)/lib
139
    TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  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
156
      TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
157
158
    endif
  endif
159
endif
160
TARGET_PATH_PKG:=$(STAGING_DIR)/host/bin:$(TARGET_PATH)
161

Imre Kaloz's avatar
Imre Kaloz committed
162
ifeq ($(CONFIG_SOFT_FLOAT),y)
Nicolas Thill's avatar
Nicolas Thill committed
163
164
  SOFT_FLOAT_CONFIG_OPTION:=--with-float=soft
  TARGET_CFLAGS+= -msoft-float
Imre Kaloz's avatar
Imre Kaloz committed
165
else
Nicolas Thill's avatar
Nicolas Thill committed
166
  SOFT_FLOAT_CONFIG_OPTION:=
Imre Kaloz's avatar
Imre Kaloz committed
167
168
endif

Mike Baker's avatar
Mike Baker committed
169
export PATH:=$(TARGET_PATH)
Felix Fietkau's avatar
Felix Fietkau committed
170
export STAGING_DIR
171
export SH_FUNC:=. $(INCLUDE_DIR)/shell.sh;
Mike Baker's avatar
Mike Baker committed
172

John Crispin's avatar
John Crispin committed
173
174
175
176
PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config

export PKG_CONFIG

Mike Baker's avatar
cleanup    
Mike Baker committed
177
HOSTCC:=gcc
178
179
HOST_CPPFLAGS:=-I$(STAGING_DIR_HOST)/include
HOST_CFLAGS:=-O2 $(HOST_CPPFLAGS)
180
181
HOST_LDFLAGS:=-L$(STAGING_DIR_HOST)/lib

Mike Baker's avatar
cleanup    
Mike Baker committed
182
TARGET_CC:=$(TARGET_CROSS)gcc
183
184
TARGET_AR:=$(TARGET_CROSS)ar
TARGET_RANLIB:=$(TARGET_CROSS)ranlib
185
TARGET_CXX:=$(if $(CONFIG_INSTALL_LIBSTDCPP),$(TARGET_CROSS)g++,no)
Jo-Philipp Wich's avatar
Jo-Philipp Wich committed
186
KPATCH:=$(SCRIPT_DIR)/patch-kernel.sh
187
SED:=$(STAGING_DIR_HOST)/bin/sed -i -e
Mike Baker's avatar
cleanup    
Mike Baker committed
188
CP:=cp -fpR
189
LN:=ln -sf
Mike Baker's avatar
Mike Baker committed
190

191
192
193
INSTALL_BIN:=install -m0755
INSTALL_DIR:=install -d -m0755
INSTALL_DATA:=install -m0644
194
INSTALL_CONF:=install -m0600
195

John Crispin's avatar
John Crispin committed
196
197
198
199
200
201
202
TARGET_CC_NOCACHE:=$(TARGET_CC)
TARGET_CXX_NOCACHE:=$(TARGET_CXX)
HOSTCC_NOCACHE:=$(HOSTCC)
export TARGET_CC_NOCACHE
export TARGET_CXX_NOCACHE
export HOSTCC_NOCACHE

203
ifneq ($(CONFIG_CCACHE),)
John Crispin's avatar
John Crispin committed
204
205
  TARGET_CC:= ccache_cc
  TARGET_CXX:= ccache_cxx
206
  HOSTCC:= ccache $(HOSTCC)
207
208
endif

209
TARGET_CONFIGURE_OPTS = \
Mike Baker's avatar
cleanup    
Mike Baker committed
210
  AR=$(TARGET_CROSS)ar \
Mike Baker's avatar
Mike Baker committed
211
  AS="$(TARGET_CC) -c $(TARGET_CFLAGS)" \
Mike Baker's avatar
cleanup    
Mike Baker committed
212
213
  LD=$(TARGET_CROSS)ld \
  NM=$(TARGET_CROSS)nm \
214
215
  CC="$(TARGET_CC)" \
  GCC="$(TARGET_CC)" \
216
  CXX="$(TARGET_CXX)" \
Mike Baker's avatar
cleanup    
Mike Baker committed
217
  RANLIB=$(TARGET_CROSS)ranlib \
218
  STRIP=$(TARGET_CROSS)strip \
219
  OBJCOPY=$(TARGET_CROSS)objcopy \
220
221
  OBJDUMP=$(TARGET_CROSS)objdump \
  SIZE=$(TARGET_CROSS)size
Mike Baker's avatar
cleanup    
Mike Baker committed
222
223

# strip an entire directory
224
ifneq ($(CONFIG_NO_STRIP),)
225
  RSTRIP:=:
226
  STRIP:=:
227
else
228
  ifneq ($(CONFIG_USE_STRIP),)
229
    STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
230
231
232
233
234
  else
    ifneq ($(CONFIG_USE_SSTRIP),)
      STRIP:=$(STAGING_DIR_HOST)/bin/sstrip
    endif
  endif
235
  RSTRIP:= \
236
237
238
    export CROSS="$(TARGET_CROSS)" \
		$(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
		$(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
239
240
    NM="$(TARGET_CROSS)nm" \
    STRIP="$(STRIP)" \
241
    STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
242
243
    $(SCRIPT_DIR)/rstrip.sh
endif
Mike Baker's avatar
cleanup    
Mike Baker committed
244
245

ifeq ($(CONFIG_ENABLE_LOCALE),true)
246
  DISABLE_NLS:=--enable-nls
Mike Baker's avatar
Mike Baker committed
247
else
Mike Baker's avatar
cleanup    
Mike Baker committed
248
  DISABLE_NLS:=--disable-nls
Mike Baker's avatar
Mike Baker committed
249
250
endif

251
ifeq ($(CONFIG_IPV6),y)
252
  DISABLE_IPV6:=
253
254
255
256
else
  DISABLE_IPV6:=--disable-ipv6
endif

Mike Baker's avatar
cleanup    
Mike Baker committed
257
ifeq ($(CONFIG_TAR_VERBOSITY),y)
Felix Fietkau's avatar
Felix Fietkau committed
258
  TAR_OPTIONS:=-xvf -
Mike Baker's avatar
cleanup    
Mike Baker committed
259
else
Felix Fietkau's avatar
Felix Fietkau committed
260
  TAR_OPTIONS:=-xf -
Mike Baker's avatar
cleanup    
Mike Baker committed
261
endif
Mike Baker's avatar
Mike Baker committed
262

263
264
265
266
ifeq ($(CONFIG_BUILD_LOG),y)
  BUILD_LOG:=1
endif

267
268
269
270
271
272
273
274
275
define shvar
V_$(subst .,_,$(subst -,_,$(subst /,_,$(1))))
endef

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

276
277
278
279
define include_mk
$(eval -include $(if $(DUMP),,$(STAGING_DIR)/mk/$(strip $(1))))
endef

280
281
282
283
# Execute commands under flock
# $(1) => The shell expression.
# $(2) => The lock name. If not given, the global lock will be used.
define locked
284
285
286
287
	SHELL= \
	$(STAGING_DIR_HOST)/bin/flock \
		$(TMP_DIR)/.$(if $(2),$(strip $(2)),global).flock \
		-c '$(subst ','\'',$(1))'
288
289
endef

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# 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; \
305
	$(CP) $(1) $(2)
306
307
endef

308
309
310
# file extension
ext=$(word $(words $(subst ., ,$(1))),$(subst ., ,$(1)))

Mike Baker's avatar
Mike Baker committed
311
all:
Mike Baker's avatar
cleanup    
Mike Baker committed
312
FORCE: ;
Mike Baker's avatar
Mike Baker committed
313
.PHONY: FORCE
314

315
316
317
318
319
320
321
322
323
324
325
326
val.%:
	@$(if $(filter undefined,$(origin $*)),\
		echo "$* undefined" >&2, \
		echo '$(subst ','"'"',$($*))' \
	)

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

327
endif #__rules_inc