1
0

rules.mk 12 KB

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