build: move symvers files to kernel build dir
[oweals/openwrt.git] / include / kernel.mk
1 #
2 # Copyright (C) 2006-2015 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 ifneq ($(filter check,$(MAKECMDGOALS)),)
9 CHECK:=1
10 DUMP:=1
11 endif
12
13 ifneq ($(SOURCE_DATE_EPOCH),)
14   ifndef DUMP
15     KBUILD_BUILD_TIMESTAMP:=$(shell perl -e 'print scalar gmtime($(SOURCE_DATE_EPOCH))')
16   endif
17 endif
18
19 ifeq ($(__target_inc),)
20   ifndef CHECK
21     include $(INCLUDE_DIR)/target.mk
22   endif
23 endif
24
25 ifeq ($(DUMP),1)
26   KERNEL?=<KERNEL>
27   BOARD?=<BOARD>
28   LINUX_VERSION?=<LINUX_VERSION>
29   LINUX_VERMAGIC?=<LINUX_VERMAGIC>
30 else
31   ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
32     export GCC_HONOUR_COPTS=s
33   endif
34
35   LINUX_KMOD_SUFFIX=ko
36
37   ifneq (,$(findstring uml,$(BOARD)))
38     KERNEL_CC?=$(HOSTCC)
39     KERNEL_CROSS?=
40   else
41     KERNEL_CC?=$(TARGET_CC)
42     KERNEL_CROSS?=$(TARGET_CROSS)
43   endif
44
45   ifeq ($(TARGET_BUILD),1)
46     PATCH_DIR ?= $(CURDIR)/patches$(if $(wildcard ./patches-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
47     FILES_DIR ?= $(foreach dir,$(wildcard $(CURDIR)/files $(CURDIR)/files-$(KERNEL_PATCHVER)),"$(dir)")
48   endif
49   KERNEL_BUILD_DIR ?= $(BUILD_DIR)/linux-$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET))
50   LINUX_DIR ?= $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)
51   LINUX_UAPI_DIR=uapi/
52   LINUX_VERMAGIC:=$(strip $(shell cat $(LINUX_DIR)/.vermagic 2>/dev/null))
53   LINUX_VERMAGIC:=$(if $(LINUX_VERMAGIC),$(LINUX_VERMAGIC),unknown)
54
55   LINUX_UNAME_VERSION:=$(KERNEL_BASE)
56   ifneq ($(findstring -rc,$(LINUX_VERSION)),)
57     LINUX_UNAME_VERSION:=$(LINUX_UNAME_VERSION)-$(strip $(lastword $(subst -, ,$(LINUX_VERSION))))
58   endif
59
60   LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
61
62   ifneq (,$(findstring -rc,$(LINUX_VERSION)))
63       LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.gz
64   else
65       LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.xz
66   endif
67
68   ifneq (,$(findstring -rc,$(LINUX_VERSION)))
69       LINUX_SITE:=https://git.kernel.org/torvalds/t
70   else ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
71       LINUX_SITE:=@KERNEL/linux/kernel/v$(word 1,$(subst ., ,$(KERNEL_BASE))).x
72   else
73       LINUX_UNAME_VERSION:=$(strip $(shell cat $(LINUX_DIR)/include/config/kernel.release 2>/dev/null))
74   endif
75
76   MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
77   TARGET_MODULES_DIR:=$(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
78
79   ifneq ($(TARGET_BUILD),1)
80     PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(if $(BUILD_VARIANT),$(PKG_NAME)-$(BUILD_VARIANT)/)$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
81   endif
82 endif
83
84 ifneq (,$(findstring uml,$(BOARD)))
85   LINUX_KARCH=um
86 else ifneq (,$(findstring $(ARCH) , aarch64 aarch64_be ))
87   LINUX_KARCH := arm64
88 else ifneq (,$(findstring $(ARCH) , arceb ))
89   LINUX_KARCH := arc
90 else ifneq (,$(findstring $(ARCH) , armeb ))
91   LINUX_KARCH := arm
92 else ifneq (,$(findstring $(ARCH) , mipsel mips64 mips64el ))
93   LINUX_KARCH := mips
94 else ifneq (,$(findstring $(ARCH) , powerpc64 ))
95   LINUX_KARCH := powerpc
96 else ifneq (,$(findstring $(ARCH) , sh2 sh3 sh4 ))
97   LINUX_KARCH := sh
98 else ifneq (,$(findstring $(ARCH) , i386 x86_64 ))
99   LINUX_KARCH := x86
100 else
101   LINUX_KARCH := $(ARCH)
102 endif
103
104 KERNEL_MAKE = $(MAKE) $(KERNEL_MAKEOPTS)
105
106 KERNEL_MAKE_FLAGS = \
107         KCFLAGS="$(call iremap,$(BUILD_DIR),$(notdir $(BUILD_DIR)))" \
108         HOSTCFLAGS="$(HOST_CFLAGS) -Wall -Wmissing-prototypes -Wstrict-prototypes" \
109         CROSS_COMPILE="$(KERNEL_CROSS)" \
110         ARCH="$(LINUX_KARCH)" \
111         KBUILD_HAVE_NLS=no \
112         KBUILD_BUILD_USER="$(call qstrip,$(CONFIG_KERNEL_BUILD_USER))" \
113         KBUILD_BUILD_HOST="$(call qstrip,$(CONFIG_KERNEL_BUILD_DOMAIN))" \
114         KBUILD_BUILD_TIMESTAMP="$(KBUILD_BUILD_TIMESTAMP)" \
115         KBUILD_BUILD_VERSION="0" \
116         HOST_LOADLIBES="-L$(STAGING_DIR_HOST)/lib" \
117         KBUILD_HOSTLDLIBS="-L$(STAGING_DIR_HOST)/lib" \
118         CONFIG_SHELL="$(BASH)" \
119         $(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='') \
120         $(if $(PKG_BUILD_ID),LDFLAGS_MODULE=--build-id=0x$(PKG_BUILD_ID)) \
121         cmd_syscalls=
122
123 ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
124   KERNEL_MAKE_FLAGS += \
125         KERNELRELEASE=$(LINUX_VERSION)
126 endif
127
128 KERNEL_MAKEOPTS := -C $(LINUX_DIR) $(KERNEL_MAKE_FLAGS)
129
130 ifdef CONFIG_USE_SPARSE
131   KERNEL_MAKEOPTS += C=1 CHECK=$(STAGING_DIR_HOST)/bin/sparse
132 endif
133
134 ifneq ($(HOST_OS),Linux)
135   KERNEL_MAKEOPTS += CONFIG_STACK_VALIDATION=
136   export SKIP_STACK_VALIDATION:=1
137 endif
138
139 PKG_EXTMOD_SUBDIRS ?= .
140
141 PKG_SYMVERS_DIR = $(KERNEL_BUILD_DIR)/symvers
142
143 define populate_module_symvers
144         @mkdir -p $(PKG_SYMVERS_DIR)
145         cat /dev/null > $(PKG_SYMVERS_DIR)/$(PKG_NAME).symvers; \
146         for subdir in $(PKG_EXTMOD_SUBDIRS); do \
147                 cat $(PKG_SYMVERS_DIR)/*.symvers 2>/dev/null > $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers; \
148         done
149 endef
150
151 define collect_module_symvers
152         for subdir in $(PKG_EXTMOD_SUBDIRS); do \
153                 realdir=$$$$(readlink -f $(PKG_BUILD_DIR)); \
154                 grep -F $(PKG_BUILD_DIR) $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
155                 [ "$(PKG_BUILD_DIR)" = "$$$$realdir" ] || \
156                         grep -F $$$$realdir $(PKG_BUILD_DIR)/$$$$subdir/Module.symvers >> $(PKG_BUILD_DIR)/Module.symvers.tmp; \
157         done; \
158         sort -u $(PKG_BUILD_DIR)/Module.symvers.tmp > $(PKG_BUILD_DIR)/Module.symvers; \
159         mv $(PKG_BUILD_DIR)/Module.symvers $(PKG_SYMVERS_DIR)/$(PKG_NAME).symvers
160 endef
161
162 define KernelPackage/hooks
163   ifneq ($(PKG_NAME),kernel)
164     Hooks/Compile/Pre += populate_module_symvers
165     Hooks/Compile/Post += collect_module_symvers
166   endif
167   define KernelPackage/hooks
168   endef
169 endef
170
171 define KernelPackage/Defaults
172   FILES:=
173   AUTOLOAD:=
174   MODPARAMS:=
175   PKGFLAGS+=nonshared
176 endef
177
178 # 1: name
179 # 2: install prefix
180 # 3: module priority prefix
181 # 4: required for boot
182 # 5: module list
183 define ModuleAutoLoad
184   $(if $(5), \
185     mkdir -p $(2)/etc/modules.d; \
186     ($(foreach mod,$(5), \
187       echo "$(mod)$(if $(MODPARAMS.$(mod)), $(MODPARAMS.$(mod)),$(if $(MODPARAMS), $(MODPARAMS)))"; )) > $(2)/etc/modules.d/$(3)$(1); \
188     $(if $(4), \
189       mkdir -p $(2)/etc/modules-boot.d; \
190       ln -sf ../modules.d/$(3)$(1) $(2)/etc/modules-boot.d/;))
191 endef
192
193 ifeq ($(DUMP)$(TARGET_BUILD),)
194   -include $(LINUX_DIR)/.config
195 endif
196
197 define KernelPackage/depends
198   $(STAMP_BUILT): $(LINUX_DIR)/.config
199   define KernelPackage/depends
200   endef
201 endef
202
203 define KernelPackage
204   NAME:=$(1)
205   $(eval $(call Package/Default))
206   $(eval $(call KernelPackage/Defaults))
207   $(eval $(call KernelPackage/$(1)))
208   $(eval $(call KernelPackage/$(1)/$(BOARD)))
209   $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
210
211   define Package/kmod-$(1)
212     TITLE:=$(TITLE)
213     SECTION:=kernel
214     CATEGORY:=Kernel modules
215     DESCRIPTION:=$(DESCRIPTION)
216     EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
217     VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
218     PKGFLAGS:=$(PKGFLAGS)
219     $(call KernelPackage/$(1))
220     $(call KernelPackage/$(1)/$(BOARD))
221     $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
222   endef
223
224   ifdef KernelPackage/$(1)/conffiles
225     define Package/kmod-$(1)/conffiles
226 $(call KernelPackage/$(1)/conffiles)
227     endef
228   endif
229
230   ifdef KernelPackage/$(1)/description
231     define Package/kmod-$(1)/description
232 $(call KernelPackage/$(1)/description)
233     endef
234   endif
235
236   ifdef KernelPackage/$(1)/config
237     define Package/kmod-$(1)/config
238 $(call KernelPackage/$(1)/config)
239     endef
240   endif
241
242   $(call KernelPackage/depends)
243   $(call KernelPackage/hooks)
244
245   ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
246     define Package/kmod-$(1)/install
247                   @for mod in $$(call version_filter,$$(FILES)); do \
248                         if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
249                                 echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
250                         elif [ -e $$$$$$$$mod ]; then \
251                                 mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
252                                 $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
253                         else \
254                                 echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
255                                 exit 1; \
256                         fi; \
257                   done;
258                   $(call ModuleAutoLoad,$(1),$$(1),$(filter-out 0-,$(word 1,$(AUTOLOAD))-),$(filter-out 0,$(word 2,$(AUTOLOAD))),$(sort $(wordlist 3,99,$(AUTOLOAD))))
259                   $(call KernelPackage/$(1)/install,$$(1))
260     endef
261   $(if $(CONFIG_PACKAGE_kmod-$(1)),
262     else
263       compile: $(1)-disabled
264       $(1)-disabled:
265                 @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
266
267       define Package/kmod-$(1)/install
268                 true
269       endef
270   )
271   endif
272   $$(eval $$(call BuildPackage,kmod-$(1)))
273
274   $$(IPKG_kmod-$(1)): $$(wildcard $$(call version_filter,$$(FILES)))
275
276 endef
277
278 version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
279
280 # 1: priority (optional)
281 # 2: module list
282 # 3: boot flag
283 define AutoLoad
284   $(if $(1),$(1),0) $(if $(3),1,0) $(call version_filter,$(2))
285 endef
286
287 # 1: module list
288 # 2: boot flag
289 define AutoProbe
290   $(call AutoLoad,,$(1),$(2))
291 endef
292
293 version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
294 kernel_version_merge=$$(( ($(call version_field,1,$(1)) << 24) + ($(call version_field,2,$(1)) << 16) + ($(call version_field,3,$(1)) << 8) + $(call version_field,4,$(1)) ))
295
296 ifdef DUMP
297   kernel_version_cmp=
298 else
299   kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
300 endif
301
302 CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
303
304 kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
305 kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
306 kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
307 kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
308 kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))
309