build: add defaults for PKG_SOURCE, PKG_SOURCE_SUBDIR, PKG_VERSION
[lede.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   MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
55   TARGET_MODULES_DIR := $(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
56
57   LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
58
59   LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.xz
60   TESTING:=$(if $(findstring -rc,$(LINUX_VERSION)),/testing,)
61   ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
62       LINUX_SITE:=@KERNEL/linux/kernel/v$(word 1,$(subst ., ,$(KERNEL_BASE))).x$(TESTING)
63   endif
64
65   ifneq ($(TARGET_BUILD),1)
66     PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
67   endif
68 endif
69
70 ifneq (,$(findstring uml,$(BOARD)))
71   LINUX_KARCH=um
72 else ifneq (,$(findstring $(ARCH) , aarch64 aarch64_be ))
73   LINUX_KARCH := arm64
74 else ifneq (,$(findstring $(ARCH) , arceb ))
75   LINUX_KARCH := arc
76 else ifneq (,$(findstring $(ARCH) , armeb ))
77   LINUX_KARCH := arm
78 else ifneq (,$(findstring $(ARCH) , mipsel mips64 mips64el ))
79   LINUX_KARCH := mips
80 else ifneq (,$(findstring $(ARCH) , sh2 sh3 sh4 ))
81   LINUX_KARCH := sh
82 else ifneq (,$(findstring $(ARCH) , i386 x86_64 ))
83   LINUX_KARCH := x86
84 else
85   LINUX_KARCH := $(ARCH)
86 endif
87
88 define KernelPackage/Defaults
89   FILES:=
90   AUTOLOAD:=
91   PKGFLAGS+=nonshared
92 endef
93
94 define ModuleAutoLoad
95         $(SH_FUNC) \
96         export modules=; \
97         probe_module() { \
98                 mods="$$$$$$$$1"; \
99                 boot="$$$$$$$$2"; \
100                 shift 2; \
101                 for mod in $(sort $$$$$$$$mods); do \
102                         mkdir -p $(2)/etc/modules.d; \
103                         echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$(1); \
104                 done; \
105                 if [ -e $(2)/etc/modules.d/$(1) ]; then \
106                         if [ "$$$$$$$$boot" = "1" ]; then \
107                                 mkdir -p $(2)/etc/modules-boot.d; \
108                                 ln -s ../modules.d/$(1) $(2)/etc/modules-boot.d/; \
109                         fi; \
110                         modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$mods"; \
111                 fi; \
112         }; \
113         add_module() { \
114                 priority="$$$$$$$$1"; \
115                 mods="$$$$$$$$2"; \
116                 boot="$$$$$$$$3"; \
117                 shift 3; \
118                 for mod in $(sort $$$$$$$$mods); do \
119                         mkdir -p $(2)/etc/modules.d; \
120                         echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$$$$$$$$priority-$(1); \
121                 done; \
122                 if [ -e $(2)/etc/modules.d/$$$$$$$$priority-$(1) ]; then \
123                         if [ "$$$$$$$$boot" = "1" ]; then \
124                                 mkdir -p $(2)/etc/modules-boot.d; \
125                                 ln -s ../modules.d/$$$$$$$$priority-$(1) $(2)/etc/modules-boot.d/; \
126                         fi; \
127                         modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$priority-$(1)"; \
128                 fi; \
129         }; \
130         $(3) \
131         if [ -n "$$$$$$$$modules" ]; then \
132                 mkdir -p $(2)/etc/modules.d; \
133                 mkdir -p $(2)/CONTROL; \
134                 echo "#!/bin/sh" > $(2)/CONTROL/postinst-pkg; \
135                 echo "[ -z \"\$$$$$$$$IPKG_INSTROOT\" ] || exit 0" >> $(2)/CONTROL/postinst-pkg; \
136                 echo ". /lib/functions.sh" >> $(2)/CONTROL/postinst-pkg; \
137                 echo "insert_modules $$$$$$$$modules" >> $(2)/CONTROL/postinst-pkg; \
138                 chmod 0755 $(2)/CONTROL/postinst-pkg; \
139         fi
140 endef
141
142 ifeq ($(DUMP)$(TARGET_BUILD),)
143   -include $(LINUX_DIR)/.config
144 endif
145
146 define KernelPackage/depends
147   $(STAMP_BUILT): $(LINUX_DIR)/.config
148   define KernelPackage/depends
149   endef
150 endef
151
152 define KernelPackage
153   NAME:=$(1)
154   $(eval $(call Package/Default))
155   $(eval $(call KernelPackage/Defaults))
156   $(eval $(call KernelPackage/$(1)))
157   $(eval $(call KernelPackage/$(1)/$(BOARD)))
158
159   define Package/kmod-$(1)
160     TITLE:=$(TITLE)
161     SECTION:=kernel
162     CATEGORY:=Kernel modules
163     DESCRIPTION:=$(DESCRIPTION)
164     EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE)-$(LINUX_VERMAGIC))
165     VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
166     PKGFLAGS:=$(PKGFLAGS)
167     $(call KernelPackage/$(1))
168     $(call KernelPackage/$(1)/$(BOARD))
169   endef
170
171   ifdef KernelPackage/$(1)/conffiles
172     define Package/kmod-$(1)/conffiles
173 $(call KernelPackage/$(1)/conffiles)
174     endef
175   endif
176
177   ifdef KernelPackage/$(1)/description
178     define Package/kmod-$(1)/description
179 $(call KernelPackage/$(1)/description)
180     endef
181   endif
182
183   ifdef KernelPackage/$(1)/config
184     define Package/kmod-$(1)/config
185 $(call KernelPackage/$(1)/config)
186     endef
187   endif
188
189   $(call KernelPackage/depends)
190
191   ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
192     ifneq ($(strip $(FILES)),)
193       define Package/kmod-$(1)/install
194                   @for mod in $$(call version_filter,$$(FILES)); do \
195                         if grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
196                                 echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
197                         elif [ -e $$$$$$$$mod ]; then \
198                                 mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
199                                 $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
200                         else \
201                                 echo "ERROR: module '$$$$$$$$mod' is missing." >&2; \
202                                 exit 1; \
203                         fi; \
204                   done;
205                   $(call ModuleAutoLoad,$(1),$$(1),$(AUTOLOAD))
206                   $(call KernelPackage/$(1)/install,$$(1))
207       endef
208     endif
209   $(if $(CONFIG_PACKAGE_kmod-$(1)),
210     else
211       compile: $(1)-disabled
212       $(1)-disabled:
213                 @echo "WARNING: kmod-$(1) is not available in the kernel config - generating empty package" >&2
214
215       define Package/kmod-$(1)/install
216                 true
217       endef
218   )
219   endif
220   $$(eval $$(call BuildPackage,kmod-$(1)))
221
222   $$(IPKG_kmod-$(1)): $$(wildcard $$(FILES))
223 endef
224
225 version_filter=$(if $(findstring @,$(1)),$(shell $(SCRIPT_DIR)/package-metadata.pl version_filter $(KERNEL_PATCHVER) $(1)),$(1))
226
227 define AutoLoad
228   add_module "$(1)" "$(call version_filter,$(2))" "$(3)";
229 endef
230
231 define AutoProbe
232   probe_module "$(call version_filter,$(1))" "$(2)";
233 endef
234
235 version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
236 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)) ))
237
238 ifdef DUMP
239   kernel_version_cmp=
240 else
241   kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
242 endif
243
244 CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
245
246 kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
247 kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
248 kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
249 kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
250 kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))
251