rules.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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)-librecmc-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))
  73. GNU_TARGET_NAME=$(OPTIMIZE_FOR_CPU)-librecmc-linux
  74. DIR_SUFFIX:=_$(LIBC)-$(LIBCV)$(if $(CONFIG_arm),_eabi)
  75. BIN_DIR:=$(BIN_DIR)$(if $(CONFIG_USE_MUSL),,-$(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_CFLAGS:=$(TARGET_OPTIMIZATION)$(if $(CONFIG_DEBUG), -g3) $(EXTRA_OPTIMIZATION)
  102. TARGET_CXXFLAGS = $(TARGET_CFLAGS)
  103. TARGET_ASFLAGS_DEFAULT = $(TARGET_CFLAGS)
  104. TARGET_ASFLAGS = $(TARGET_ASFLAGS_DEFAULT)
  105. TARGET_CPPFLAGS:=-I$(STAGING_DIR)/usr/include -I$(STAGING_DIR)/include
  106. TARGET_LDFLAGS:=-L$(STAGING_DIR)/usr/lib -L$(STAGING_DIR)/lib
  107. ifneq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  108. LIBGCC_S_PATH=$(realpath $(wildcard $(call qstrip,$(CONFIG_LIBGCC_ROOT_DIR))/$(call qstrip,$(CONFIG_LIBGCC_FILE_SPEC))))
  109. LIBGCC_S=$(if $(LIBGCC_S_PATH),-L$(dir $(LIBGCC_S_PATH)) -lgcc_s)
  110. LIBGCC_A=$(realpath $(lastword $(wildcard $(dir $(LIBGCC_S_PATH))/gcc/*/*/libgcc.a)))
  111. else
  112. LIBGCC_A=$(lastword $(wildcard $(TOOLCHAIN_DIR)/lib/gcc/*/*/libgcc.a))
  113. LIBGCC_S=$(if $(wildcard $(TOOLCHAIN_DIR)/lib/libgcc_s.so),-L$(TOOLCHAIN_DIR)/lib -lgcc_s,$(LIBGCC_A))
  114. endif
  115. LIBRPC=-lrpc
  116. LIBRPC_DEPENDS=+librpc
  117. ifeq ($(CONFIG_ARCH_64BIT),y)
  118. LIB_SUFFIX:=64
  119. endif
  120. ifndef DUMP
  121. ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
  122. -include $(TOOLCHAIN_DIR)/info.mk
  123. export GCC_HONOUR_COPTS:=0
  124. TARGET_CROSS:=$(if $(TARGET_CROSS),$(TARGET_CROSS),$(OPTIMIZE_FOR_CPU)-librecmc-linux$(if $(TARGET_SUFFIX),-$(TARGET_SUFFIX))-)
  125. TARGET_CFLAGS+= -fhonour-copts $(if $(CONFIG_GCC_VERSION_4_4)$(CONFIG_GCC_VERSION_4_5),,-Wno-error=unused-but-set-variable)
  126. TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/usr/include
  127. ifeq ($(CONFIG_USE_MUSL),y)
  128. TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/include/fortify
  129. endif
  130. TARGET_CPPFLAGS+= -I$(TOOLCHAIN_DIR)/include
  131. TARGET_LDFLAGS+= -L$(TOOLCHAIN_DIR)/usr/lib -L$(TOOLCHAIN_DIR)/lib
  132. TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
  133. else
  134. ifeq ($(CONFIG_NATIVE_TOOLCHAIN),)
  135. TARGET_CROSS:=$(call qstrip,$(CONFIG_TOOLCHAIN_PREFIX))
  136. TOOLCHAIN_ROOT_DIR:=$(call qstrip,$(CONFIG_TOOLCHAIN_ROOT))
  137. TOOLCHAIN_BIN_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_BIN_PATH)))
  138. TOOLCHAIN_INC_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_INC_PATH)))
  139. TOOLCHAIN_LIB_DIRS:=$(patsubst ./%,$(TOOLCHAIN_ROOT_DIR)/%,$(call qstrip,$(CONFIG_TOOLCHAIN_LIB_PATH)))
  140. ifneq ($(TOOLCHAIN_BIN_DIRS),)
  141. TARGET_PATH:=$(subst $(space),:,$(TOOLCHAIN_BIN_DIRS)):$(TARGET_PATH)
  142. endif
  143. ifneq ($(TOOLCHAIN_INC_DIRS),)
  144. TARGET_CPPFLAGS+= $(patsubst %,-I%,$(TOOLCHAIN_INC_DIRS))
  145. endif
  146. ifneq ($(TOOLCHAIN_LIB_DIRS),)
  147. TARGET_LDFLAGS+= $(patsubst %,-L%,$(TOOLCHAIN_LIB_DIRS))
  148. endif
  149. TARGET_PATH:=$(TOOLCHAIN_DIR)/bin:$(TARGET_PATH)
  150. endif
  151. endif
  152. endif
  153. TARGET_PATH_PKG:=$(STAGING_DIR)/host/bin:$(TARGET_PATH)
  154. ifeq ($(CONFIG_SOFT_FLOAT),y)
  155. SOFT_FLOAT_CONFIG_OPTION:=--with-float=soft
  156. ifeq ($(CONFIG_arm),y)
  157. TARGET_CFLAGS+= -mfloat-abi=soft
  158. else
  159. TARGET_CFLAGS+= -msoft-float
  160. endif
  161. else
  162. SOFT_FLOAT_CONFIG_OPTION:=
  163. ifeq ($(CONFIG_arm),y)
  164. TARGET_CFLAGS+= -mfloat-abi=hard
  165. endif
  166. endif
  167. export PATH:=$(TARGET_PATH)
  168. export STAGING_DIR
  169. export SH_FUNC:=. $(INCLUDE_DIR)/shell.sh;
  170. PKG_CONFIG:=$(STAGING_DIR_HOST)/bin/pkg-config
  171. export PKG_CONFIG
  172. HOSTCC:=gcc
  173. HOSTCXX:=g++
  174. HOST_CPPFLAGS:=-I$(STAGING_DIR_HOST)/include -I$(STAGING_DIR_HOST)/usr/include
  175. HOST_CFLAGS:=-O2 $(HOST_CPPFLAGS)
  176. HOST_LDFLAGS:=-L$(STAGING_DIR_HOST)/lib -L$(STAGING_DIR_HOST)/usr/lib
  177. ifeq ($(CONFIG_GCC_VERSION_4_4)$(CONFIG_GCC_VERSION_4_6)$(CONFIG_EXTERNAL_TOOLCHAIN),)
  178. TARGET_AR:=$(TARGET_CROSS)gcc-ar
  179. TARGET_RANLIB:=$(TARGET_CROSS)gcc-ranlib
  180. TARGET_NM:=$(TARGET_CROSS)gcc-nm
  181. else
  182. TARGET_AR:=$(TARGET_CROSS)ar
  183. TARGET_RANLIB:=$(TARGET_CROSS)ranlib
  184. TARGET_NM:=$(TARGET_CROSS)nm
  185. endif
  186. BUILD_KEY=$(TOPDIR)/key-build
  187. TARGET_CC:=$(TARGET_CROSS)gcc
  188. TARGET_CXX:=$(TARGET_CROSS)g++
  189. KPATCH:=$(SCRIPT_DIR)/patch-kernel.sh
  190. SED:=$(STAGING_DIR_HOST)/bin/sed -i -e
  191. CP:=cp -fpR
  192. LN:=ln -sf
  193. XARGS:=xargs -r
  194. BASH:=bash
  195. TAR:=tar
  196. FIND:=find
  197. PATCH:=patch
  198. PYTHON:=python
  199. INSTALL_BIN:=install -m0755
  200. INSTALL_DIR:=install -d -m0755
  201. INSTALL_DATA:=install -m0644
  202. INSTALL_CONF:=install -m0600
  203. TARGET_CC_NOCACHE:=$(TARGET_CC)
  204. TARGET_CXX_NOCACHE:=$(TARGET_CXX)
  205. HOSTCC_NOCACHE:=$(HOSTCC)
  206. HOSTCXX_NOCACHE:=$(HOSTCXX)
  207. export TARGET_CC_NOCACHE
  208. export TARGET_CXX_NOCACHE
  209. export HOSTCC_NOCACHE
  210. ifneq ($(CONFIG_CCACHE),)
  211. TARGET_CC:= ccache_cc
  212. TARGET_CXX:= ccache_cxx
  213. HOSTCC:= ccache $(HOSTCC)
  214. HOSTCXX:= ccache $(HOSTCXX)
  215. endif
  216. TARGET_CONFIGURE_OPTS = \
  217. AR="$(TARGET_AR)" \
  218. AS="$(TARGET_CC) -c $(TARGET_ASFLAGS)" \
  219. LD=$(TARGET_CROSS)ld \
  220. NM="$(TARGET_NM)" \
  221. CC="$(TARGET_CC)" \
  222. GCC="$(TARGET_CC)" \
  223. CXX="$(TARGET_CXX)" \
  224. RANLIB="$(TARGET_RANLIB)" \
  225. STRIP=$(TARGET_CROSS)strip \
  226. OBJCOPY=$(TARGET_CROSS)objcopy \
  227. OBJDUMP=$(TARGET_CROSS)objdump \
  228. SIZE=$(TARGET_CROSS)size
  229. # strip an entire directory
  230. ifneq ($(CONFIG_NO_STRIP),)
  231. RSTRIP:=:
  232. STRIP:=:
  233. else
  234. ifneq ($(CONFIG_USE_STRIP),)
  235. STRIP:=$(TARGET_CROSS)strip $(call qstrip,$(CONFIG_STRIP_ARGS))
  236. else
  237. ifneq ($(CONFIG_USE_SSTRIP),)
  238. STRIP:=$(STAGING_DIR_HOST)/bin/sstrip
  239. endif
  240. endif
  241. RSTRIP:= \
  242. export CROSS="$(TARGET_CROSS)" \
  243. $(if $(CONFIG_KERNEL_KALLSYMS),NO_RENAME=1) \
  244. $(if $(CONFIG_KERNEL_PROFILING),KEEP_SYMBOLS=1); \
  245. NM="$(TARGET_CROSS)nm" \
  246. STRIP="$(STRIP)" \
  247. STRIP_KMOD="$(SCRIPT_DIR)/strip-kmod.sh" \
  248. PATCHELF="$(STAGING_DIR_HOST)/bin/patchelf" \
  249. $(SCRIPT_DIR)/rstrip.sh
  250. endif
  251. ifeq ($(CONFIG_IPV6),y)
  252. DISABLE_IPV6:=
  253. else
  254. DISABLE_IPV6:=--disable-ipv6
  255. endif
  256. TAR_OPTIONS:=-xf -
  257. ifeq ($(CONFIG_BUILD_LOG),y)
  258. BUILD_LOG:=1
  259. endif
  260. export BISON_PKGDATADIR:=$(STAGING_DIR_HOST)/share/bison
  261. export M4:=$(STAGING_DIR_HOST)/bin/m4
  262. define shvar
  263. V_$(subst .,_,$(subst -,_,$(subst /,_,$(1))))
  264. endef
  265. define shexport
  266. export $(call shvar,$(1))=$$(call $(1))
  267. endef
  268. define include_mk
  269. $(eval -include $(if $(DUMP),,$(STAGING_DIR)/mk/$(strip $(1))))
  270. endef
  271. # Execute commands under flock
  272. # $(1) => The shell expression.
  273. # $(2) => The lock name. If not given, the global lock will be used.
  274. define locked
  275. SHELL= \
  276. $(STAGING_DIR_HOST)/bin/flock \
  277. $(TMP_DIR)/.$(if $(2),$(strip $(2)),global).flock \
  278. -c '$(subst ','\'',$(1))'
  279. endef
  280. # Recursively copy paths into another directory, purge dangling
  281. # symlinks before.
  282. # $(1) => File glob expression
  283. # $(2) => Destination directory
  284. define file_copy
  285. for src_dir in $(sort $(foreach d,$(wildcard $(1)),$(dir $(d)))); do \
  286. ( cd $$src_dir; find -type f -or -type d ) | \
  287. ( cd $(2); while :; do \
  288. read FILE; \
  289. [ -z "$$FILE" ] && break; \
  290. [ -L "$$FILE" ] || continue; \
  291. echo "Removing symlink $(2)/$$FILE"; \
  292. rm -f "$$FILE"; \
  293. done; ); \
  294. done; \
  295. $(CP) $(1) $(2)
  296. endef
  297. # file extension
  298. ext=$(word $(words $(subst ., ,$(1))),$(subst ., ,$(1)))
  299. all:
  300. FORCE: ;
  301. .PHONY: FORCE
  302. val.%:
  303. @$(if $(filter undefined,$(origin $*)),\
  304. echo "$* undefined" >&2, \
  305. echo '$(subst ','"'"',$($*))' \
  306. )
  307. var.%:
  308. @$(if $(filter undefined,$(origin $*)),\
  309. echo "$* undefined" >&2, \
  310. echo "$*='"'$(subst ','"'\"'\"'"',$($*))'"'" \
  311. )
  312. endif #__rules_inc