rules.mk 12 KB

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