rules.mk 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. TMP_DIR:=$(TOPDIR)/tmp
  15. GREP_OPTIONS=
  16. export GREP_OPTIONS
  17. qstrip=$(strip $(subst ",,$(1)))
  18. #"))
  19. empty:=
  20. space:= $(empty) $(empty)
  21. merge=$(subst $(space),,$(1))
  22. confvar=$(call merge,$(foreach v,$(1),$(if $($(v)),y,n)))
  23. strip_last=$(patsubst %.$(lastword $(subst .,$(space),$(1))),%,$(1))
  24. define sep
  25. endef
  26. _SINGLE=export MAKEFLAGS=$(space);
  27. CFLAGS:=
  28. ARCH:=$(subst i486,i386,$(subst i586,i386,$(subst i686,i386,$(call qstrip,$(CONFIG_ARCH)))))
  29. ARCH_PACKAGES:=$(call qstrip,$(CONFIG_TARGET_ARCH_PACKAGES))
  30. BOARD:=$(call qstrip,$(CONFIG_TARGET_BOARD))
  31. TARGET_OPTIMIZATION:=$(call qstrip,$(CONFIG_TARGET_OPTIMIZATION))
  32. TARGET_SUFFIX=$(call qstrip,$(CONFIG_TARGET_SUFFIX))
  33. BUILD_SUFFIX:=$(call qstrip,$(CONFIG_BUILD_SUFFIX))
  34. SUBDIR:=$(patsubst $(TOPDIR)/%,%,${CURDIR})
  35. export SHELL:=/usr/bin/env bash
  36. OPTIMIZE_FOR_CPU=$(subst i386,i486,$(ARCH))
  37. ifeq ($(ARCH),powerpc)
  38. FPIC:=-fPIC
  39. else
  40. FPIC:=-fpic
  41. endif
  42. HOST_FPIC:=-fPIC
  43. ARCH_SUFFIX:=
  44. GCC_ARCH:=
  45. ifneq ($(filter -march=armv%,$(TARGET_OPTIMIZATION)),)
  46. ARCH_SUFFIX:=_$(patsubst -march=arm%,%,$(filter -march=armv%,$(TARGET_OPTIMIZATION)))
  47. GCC_ARCH:=$(patsubst -march=%,%,$(filter -march=armv%,$(TARGET_OPTIMIZATION)))
  48. endif
  49. ifneq ($(findstring -mips32r2,$(TARGET_OPTIMIZATION)),)
  50. ARCH_SUFFIX:=_r2
  51. endif
  52. ifdef CONFIG_HAS_SPE_FPU
  53. TARGET_SUFFIX:=$(TARGET_SUFFIX)spe
  54. endif
  55. DL_DIR:=$(if $(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(call qstrip,$(CONFIG_DOWNLOAD_FOLDER)),$(TOPDIR)/dl)
  56. BIN_DIR:=$(TOPDIR)/bin/$(BOARD)
  57. INCLUDE_DIR:=$(TOPDIR)/include
  58. SCRIPT_DIR:=$(TOPDIR)/scripts
  59. BUILD_DIR_BASE:=$(TOPDIR)/build_dir
  60. BUILD_DIR_HOST:=$(BUILD_DIR_BASE)/host
  61. STAGING_DIR_HOST:=$(TOPDIR)/staging_dir/host
  62. ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  63. GCCV:=$(call qstrip,$(CONFIG_GCC_VERSION))
  64. LIBC:=$(call qstrip,$(CONFIG_LIBC))
  65. LIBCV:=$(call qstrip,$(CONFIG_LIBC_VERSION))
  66. REAL_GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))
  67. GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-openwrt-linux
  68. DIR_SUFFIX:=_$(LIBC)-$(LIBCV)$(if $(CONFIG_arm),_eabi)
  69. BUILD_DIR:=$(BUILD_DIR_BASE)/target-$(ARCH)$(ARCH_SUFFIX)$(DIR_SUFFIX)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
  70. STAGING_DIR:=$(TOPDIR)/staging_dir/target-$(ARCH)$(ARCH_SUFFIX)$(DIR_SUFFIX)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
  71. BUILD_DIR_TOOLCHAIN:=$(BUILD_DIR_BASE)/toolchain-$(ARCH)$(ARCH_SUFFIX)_gcc-$(GCCV)$(DIR_SUFFIX)
  72. TOOLCHAIN_DIR:=$(TOPDIR)/staging_dir/toolchain-$(ARCH)$(ARCH_SUFFIX)_gcc-$(GCCV)$(DIR_SUFFIX)
  73. PACKAGE_DIR:=$(BIN_DIR)/packages
  74. else
  75. ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
  76. GNU_TARGET_NAME=$(call qstrip,$(CONFIG_TARGET_NAME))
  77. else
  78. GNU_TARGET_NAME=$(shell gcc -dumpmachine)
  79. endif
  80. REAL_GNU_TARGET_NAME=$(GNU_TARGET_NAME)
  81. BUILD_DIR:=$(BUILD_DIR_BASE)/target-$(GNU_TARGET_NAME)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
  82. STAGING_DIR:=$(TOPDIR)/staging_dir/target-$(GNU_TARGET_NAME)$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
  83. BUILD_DIR_TOOLCHAIN:=$(BUILD_DIR_BASE)/toolchain-$(GNU_TARGET_NAME)
  84. TOOLCHAIN_DIR:=$(TOPDIR)/staging_dir/toolchain-$(GNU_TARGET_NAME)
  85. PACKAGE_DIR:=$(BIN_DIR)/packages
  86. endif
  87. STAMP_DIR:=$(BUILD_DIR)/stamp
  88. STAMP_DIR_HOST=$(BUILD_DIR_HOST)/stamp
  89. TARGET_ROOTFS_DIR?=$(if $(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(call qstrip,$(CONFIG_TARGET_ROOTFS_DIR)),$(BUILD_DIR))
  90. TARGET_DIR:=$(TARGET_ROOTFS_DIR)/root-$(BOARD)
  91. STAGING_DIR_ROOT:=$(STAGING_DIR)/root-$(BOARD)
  92. BUILD_LOG_DIR:=$(TOPDIR)/logs
  93. TARGET_PATH:=$(STAGING_DIR_HOST)/bin:$(subst $(space),:,$(filter-out .,$(filter-out ./,$(subst :,$(space),$(PATH)))))
  94. TARGET_CFLAGS:=$(TARGET_OPTIMIZATION)$(if $(CONFIG_DEBUG), -g3)
  95. TARGET_CXXFLAGS = $(TARGET_CFLAGS)
  96. TARGET_CPPFLAGS:=-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include
  97. TARGET_LDFLAGS:=-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib
  98. ifneq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  99. LIBGCC_S_PATH=$(realpath $(wildcard $(call qstrip,$(CONFIG_LIBGCC_ROOT_DIR))/$(call qstrip,$(CONFIG_LIBGCC_FILE_SPEC))))
  100. LIBGCC_S=$(if $(LIBGCC_S_PATH),-L$(dir $(LIBGCC_S_PATH)) -lgcc_s)
  101. LIBGCC_A=$(realpath $(wildcard $(dir $(LIBGCC_S_PATH))/gcc/*/*/libgcc.a))
  102. else
  103. LIBGCC_A=$(wildcard $(TOOLCHAIN_DIR)/lib/gcc/*/*/libgcc.a)
  104. LIBGCC_S=$(if $(wildcard $(TOOLCHAIN_DIR)/lib/libgcc_s.so),-L$(TOOLCHAIN_DIR)/lib -lgcc_s,$(LIBGCC_A))
  105. endif
  106. LIBRPC=-lrpc
  107. LIBRPC_DEPENDS=+librpc
  108. ifneq ($(findstring $(ARCH) , mips64 x86_64 ),)
  109. LIB_SUFFIX:=64
  110. endif
  111. ifndef DUMP
  112. ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  113. -include $(TOOLCHAIN_DIR)/info.mk
  114. export GCC_HONOUR_COPTS:=0
  115. TARGET_CROSS:=$(if $(TARGET_CROSS),$(TARGET_CROSS),$(OPTIMIZE_FOR_CPU)-openwrt-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))-)
  116. TARGET_CFLAGS+= -fhonour-copts $(if $(CONFIG_GCC_VERSION_4_4)$(CONFIG_GCC_VERSION_4_5),,-Wno-error=unused-but-set-variable)
  117. TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/usr/include -I$(TOOLCHAIN_DIR)/include
  118. TARGET_LDFLAGS+= -L$(TOOLCHAIN_DIR)/usr/lib -L$(TOOLCHAIN_DIR)/lib
  119. TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
  120. else
  121. ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
  122. TARGET_CROSS:=$(call qstrip,$(CONFIG_TOOLCHAIN_PREFIX))
  123. TOOLCHAIN_ROOT_DIR:=$(call qstrip,$(CONFIG_TOOLCHAIN_ROOT))
  124. TOOLCHAIN_BIN_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_BIN_PATH)))
  125. TOOLCHAIN_INC_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_INC_PATH)))
  126. TOOLCHAIN_LIB_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_LIB_PATH)))
  127. ifneq ($(TOOLCHAIN_BIN_DIRS),)
  128. TARGET_PATH:=$(subst $(space),:,$(TOOLCHAIN_BIN_DIRS)):$(TARGET_PATH)
  129. endif
  130. ifneq ($(TOOLCHAIN_INC_DIRS),)
  131. TARGET_CPPFLAGS+= $(patsubst %,-I%,$(TOOLCHAIN_INC_DIRS))
  132. endif
  133. ifneq ($(TOOLCHAIN_LIB_DIRS),)
  134. TARGET_LDFLAGS+= $(patsubst %,-L%,$(TOOLCHAIN_LIB_DIRS))
  135. endif
  136. TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
  137. endif
  138. endif
  139. endif
  140. TARGET_PATH_PKG:=$(STAGING_DIR)/host/bin:$(TARGET_PATH)
  141. ifeq ($(CONFIG_SOFT_FLOAT),y)
  142. SOFT_FLOAT_CONFIG_OPTION:=--with-float=soft
  143. TARGET_CFLAGS+= -msoft-float
  144. else
  145. SOFT_FLOAT_CONFIG_OPTION:=
  146. endif
  147. export PATH:=$(TARGET_PATH)
  148. export STAGING_DIR
  149. export SH_FUNC:=. $(INCLUDE_DIR)/shell.sh;
  150. PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
  151. export PKG_CONFIG
  152. HOSTCC:=gcc
  153. HOST_CPPFLAGS:=-I$(STAGING_DIR_HOST)/include
  154. HOST_CFLAGS:=-O2 $(HOST_CPPFLAGS)
  155. HOST_LDFLAGS:=-L$(STAGING_DIR_HOST)/lib
  156. TARGET_CC:=$(TARGET_CROSS)gcc
  157. TARGET_AR:=$(TARGET_CROSS)ar
  158. TARGET_RANLIB:=$(TARGET_CROSS)ranlib
  159. TARGET_CXX:=$(if $(CONFIG_INSTALL_LIBSTDCPP),$(TARGET_CROSS)g++,no)
  160. KPATCH:=$(SCRIPT_DIR)/patch-kernel.sh
  161. SED:=$(STAGING_DIR_HOST)/bin/sed -i -e
  162. CP:=cp -fpR
  163. LN:=ln -sf
  164. INSTALL_BIN:=install -m0755
  165. INSTALL_DIR:=install -d -m0755
  166. INSTALL_DATA:=install -m0644
  167. INSTALL_CONF:=install -m0600
  168. TARGET_CC_NOCACHE:=$(TARGET_CC)
  169. TARGET_CXX_NOCACHE:=$(TARGET_CXX)
  170. HOSTCC_NOCACHE:=$(HOSTCC)
  171. export TARGET_CC_NOCACHE
  172. export TARGET_CXX_NOCACHE
  173. export HOSTCC_NOCACHE
  174. ifneq ($(CONFIG_CCACHE),)
  175. TARGET_CC:= ccache_cc
  176. TARGET_CXX:= ccache_cxx
  177. HOSTCC:= ccache $(HOSTCC)
  178. endif
  179. TARGET_CONFIGURE_OPTS = \
  180. AR=$(TARGET_CROSS)ar \
  181. AS="$(TARGET_CC) -c $(TARGET_CFLAGS)" \
  182. LD=$(TARGET_CROSS)ld \
  183. NM=$(TARGET_CROSS)nm \
  184. CC="$(TARGET_CC)" \
  185. GCC="$(TARGET_CC)" \
  186. CXX="$(TARGET_CXX)" \
  187. RANLIB=$(TARGET_CROSS)ranlib \
  188. STRIP=$(TARGET_CROSS)strip \
  189. OBJCOPY=$(TARGET_CROSS)objcopy \
  190. OBJDUMP=$(TARGET_CROSS)objdump \
  191. SIZE=$(TARGET_CROSS)size
  192. # strip an entire directory
  193. ifneq ($(CONFIG_NO_STRIP),)
  194. RSTRIP:=:
  195. STRIP:=:
  196. else
  197. ifneq ($(CONFIG_USE_STRIP),)
  198. STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
  199. else
  200. ifneq ($(CONFIG_USE_SSTRIP),)
  201. STRIP:=$(STAGING_DIR_HOST)/bin/sstrip
  202. endif
  203. endif
  204. RSTRIP:= \
  205. export CROSS="$(TARGET_CROSS)" \
  206. $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
  207. $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
  208. NM="$(TARGET_CROSS)nm" \
  209. STRIP="$(STRIP)" \
  210. STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
  211. $(SCRIPT_DIR)/rstrip.sh
  212. endif
  213. ifeq ($(CONFIG_ENABLE_LOCALE),true)
  214. DISABLE_NLS:=--enable-nls
  215. else
  216. DISABLE_NLS:=--disable-nls
  217. endif
  218. ifeq ($(CONFIG_IPV6),y)
  219. DISABLE_IPV6:=
  220. else
  221. DISABLE_IPV6:=--disable-ipv6
  222. endif
  223. ifeq ($(CONFIG_TAR_VERBOSITY),y)
  224. TAR_OPTIONS:=-xvf -
  225. else
  226. TAR_OPTIONS:=-xf -
  227. endif
  228. ifeq ($(CONFIG_BUILD_LOG),y)
  229. BUILD_LOG:=1
  230. endif
  231. define shvar
  232. V_$(subst .,_,$(subst -,_,$(subst /,_,$(1))))
  233. endef
  234. define shexport
  235. $(call shvar,$(1))=$$(call $(1))
  236. export $(call shvar,$(1))
  237. endef
  238. define include_mk
  239. $(eval -include $(if $(DUMP),,$(STAGING_DIR)/mk/$(strip $(1))))
  240. endef
  241. # Execute commands under flock
  242. # $(1) => The shell expression.
  243. # $(2) => The lock name. If not given, the global lock will be used.
  244. define locked
  245. SHELL= \
  246. $(STAGING_DIR_HOST)/bin/flock \
  247. $(TMP_DIR)/.$(if $(2),$(strip $(2)),global).flock \
  248. -c '$(subst ','\'',$(1))'
  249. endef
  250. # file extension
  251. ext=$(word $(words $(subst ., ,$(1))),$(subst ., ,$(1)))
  252. all:
  253. FORCE: ;
  254. .PHONY: FORCE
  255. val.%:
  256. @$(if $(filter undefined,$(origin $*)),\
  257. echo "$* undefined" >&2, \
  258. echo '$(subst ','"'"',$($*))' \
  259. )
  260. var.%:
  261. @$(if $(filter undefined,$(origin $*)),\
  262. echo "$* undefined" >&2, \
  263. echo "$*='"'$(subst ','"'\"'\"'"',$($*))'"'" \
  264. )
  265. endif #__rules_inc