rules.mk 11 KB

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