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