include/kernel.mk: enhance built-in/missing modules detection when packaging kmods
[openwrt.git] / include / kernel.mk
1
2 # Copyright (C) 2006-2011 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 ifeq ($(__target_inc),)
9   include $(INCLUDE_DIR)/target.mk
10 endif
11
12 ifeq ($(DUMP),1)
13   KERNEL?=<KERNEL>
14   BOARD?=<BOARD>
15   LINUX_VERSION?=<LINUX_VERSION>
16 else
17   ifeq ($(CONFIG_EXTERNAL_TOOLCHAIN),)
18     export GCC_HONOUR_COPTS=s
19   endif
20
21   LINUX_KMOD_SUFFIX=ko
22
23   ifneq (,$(findstring uml,$(BOARD)))
24     KERNEL_CC?=$(HOSTCC)
25     KERNEL_CROSS?=
26   else
27     KERNEL_CC?=$(TARGET_CC)
28     KERNEL_CROSS?=$(TARGET_CROSS)
29   endif
30
31   ifeq ($(TARGET_BUILD),1)
32     PATCH_DIR ?= ./patches$(if $(wildcard ./patches-$(KERNEL_PATCHVER)),-$(KERNEL_PATCHVER))
33     FILES_DIR ?= $(foreach dir,$(wildcard ./files ./files-$(KERNEL_PATCHVER)),"$(dir)")
34   endif
35   KERNEL_BUILD_DIR ?= $(BUILD_DIR_BASE)/linux-$(BOARD)$(if $(SUBTARGET),_$(SUBTARGET))$(if $(BUILD_SUFFIX),_$(BUILD_SUFFIX))
36   LINUX_DIR ?= $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)
37
38   LINUX_UNAME_VERSION:=$(if $(word 3,$(subst ., ,$(KERNEL_BASE))),$(KERNEL_BASE),$(KERNEL_BASE).0)
39   ifneq ($(findstring -rc,$(LINUX_VERSION)),)
40     LINUX_UNAME_VERSION:=$(LINUX_UNAME_VERSION)-$(strip $(lastword $(subst -, ,$(LINUX_VERSION))))
41   endif
42
43   MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
44   TARGET_MODULES_DIR := $(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
45
46   LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
47
48   LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.bz2
49   TESTING:=$(if $(findstring -rc,$(LINUX_VERSION)),/testing,)
50   ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
51     ifeq ($(word 1,$(subst ., ,$(KERNEL_BASE))),3)
52       LINUX_SITE:=@KERNEL/linux/kernel/v3.x$(TESTING)
53     else
54       LINUX_SITE:=@KERNEL/linux/kernel/v$(KERNEL)$(TESTING)
55     endif
56   endif
57
58   ifneq ($(TARGET_BUILD),1)
59     PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
60   endif
61 endif
62
63 ifneq (,$(findstring uml,$(BOARD)))
64   LINUX_KARCH=um
65 else
66   ifeq (,$(LINUX_KARCH))
67     LINUX_KARCH=$(strip $(subst i386,x86,$(subst armeb,arm,$(subst mipsel,mips,$(subst mips64,mips,$(subst mips64el,mips,$(subst sh2,sh,$(subst sh3,sh,$(subst sh4,sh,$(ARCH))))))))))
68   endif
69 endif
70
71
72 define KernelPackage/Defaults
73   FILES:=
74   AUTOLOAD:=
75 endef
76
77 define ModuleAutoLoad
78         $(SH_FUNC) \
79         export modules=; \
80         add_module() { \
81                 priority="$$$$$$$$1"; \
82                 mods="$$$$$$$$2"; \
83                 boot="$$$$$$$$3"; \
84                 shift 3; \
85                 for mod in $$$$$$$$mods; do \
86                         if [ -e $(2)/$(MODULES_SUBDIR)/$$$$$$$$mod.ko ]; then \
87                                 mkdir -p $(2)/etc/modules.d; \
88                                 echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$$$$$$$$priority-$(1); \
89                         fi; \
90                 done; \
91                 if [ -e $(2)/etc/modules.d/$$$$$$$$priority-$(1) ]; then \
92                         if [ "$$$$$$$$boot" = "1" ]; then \
93                                 mkdir -p $(2)/etc/modules-boot.d; \
94                                 ln -s ../modules.d/$$$$$$$$priority-$(1) $(2)/etc/modules-boot.d/; \
95                         fi; \
96                         modules="$$$$$$$${modules:+$$$$$$$$modules }$$$$$$$$priority-$(1)"; \
97                 fi; \
98         }; \
99         $(3) \
100         if [ -n "$$$$$$$$modules" ]; then \
101                 mkdir -p $(2)/etc/modules.d; \
102                 mkdir -p $(2)/CONTROL; \
103                 echo "#!/bin/sh" > $(2)/CONTROL/postinst; \
104                 echo "[ -z \"\$$$$$$$$IPKG_INSTROOT\" ] || exit 0" >> $(2)/CONTROL/postinst; \
105                 echo ". /etc/functions.sh" >> $(2)/CONTROL/postinst; \
106                 echo "load_modules $$$$$$$$modules" >> $(2)/CONTROL/postinst; \
107                 chmod 0755 $(2)/CONTROL/postinst; \
108         fi
109 endef
110
111 ifeq ($(DUMP)$(TARGET_BUILD),)
112   -include $(LINUX_DIR)/.config
113 endif
114
115 define KernelPackage/depends
116   $(STAMP_BUILT): $(LINUX_DIR)/.config
117   define KernelPackage/depends
118   endef
119 endef
120
121 define KernelPackage
122   NAME:=$(1)
123   $(eval $(call Package/Default))
124   $(eval $(call KernelPackage/Defaults))
125   $(eval $(call KernelPackage/$(1)))
126   $(eval $(call KernelPackage/$(1)/$(BOARD)))
127
128   define Package/kmod-$(1)
129     TITLE:=$(TITLE)
130     SECTION:=kernel
131     CATEGORY:=Kernel modules
132     DESCRIPTION:=$(DESCRIPTION)
133     EXTRA_DEPENDS:=kernel (=$(LINUX_VERSION)-$(LINUX_RELEASE))
134     VERSION:=$(LINUX_VERSION)$(if $(PKG_VERSION),+$(PKG_VERSION))-$(if $(PKG_RELEASE),$(PKG_RELEASE),$(LINUX_RELEASE))
135     $(call KernelPackage/$(1))
136     $(call KernelPackage/$(1)/$(BOARD))
137   endef
138
139   ifdef KernelPackage/$(1)/description
140     define Package/kmod-$(1)/description
141 $(call KernelPackage/$(1)/description)
142     endef
143   endif
144
145   ifdef KernelPackage/$(1)/config
146     define Package/kmod-$(1)/config
147 $(call KernelPackage/$(1)/config)
148     endef
149   endif
150
151   $(call KernelPackage/depends)
152
153   ifneq ($(if $(filter-out %=y %=n %=m,$(KCONFIG)),$(filter m y,$(foreach c,$(filter-out %=y %=n %=m,$(KCONFIG)),$($(c)))),.),)
154     ifneq ($(strip $(FILES)),)
155       define Package/kmod-$(1)/install
156                   @for mod in $$(FILES); do \
157                         if [ -e $$$$$$$$mod ]; then \
158                                 mkdir -p $$(1)/$(MODULES_SUBDIR) ; \
159                                 $(CP) -L $$$$$$$$mod $$(1)/$(MODULES_SUBDIR)/ ; \
160                         elif  grep -q "$$$$$$$${mod##$(LINUX_DIR)/}" "$(LINUX_DIR)/modules.builtin"; then \
161                                 echo "NOTICE: module '$$$$$$$$mod' is built-in."; \
162                         else \
163                                 echo "ERROR: module '$$$$$$$$mod' is missing."; \
164                                 exit 1; \
165                         fi; \
166                   done;
167                   $(call ModuleAutoLoad,$(1),$$(1),$(AUTOLOAD))
168                   $(call KernelPackage/$(1)/install,$$(1))
169       endef
170     endif
171   $(if $(CONFIG_PACKAGE_kmod-$(1)),
172     else
173       compile: kmod-$(1)-unavailable
174       kmod-$(1)-unavailable:
175                 @echo "WARNING: kmod-$(1) is not available in the kernel config"
176   )
177   endif
178   $$(eval $$(call BuildPackage,kmod-$(1)))
179
180   $$(IPKG_kmod-$(1)): $$(wildcard $$(FILES))
181 endef
182
183 define AutoLoad
184   add_module "$(1)" "$(2)" "$(3)";
185 endef
186
187 version_field=$(if $(word $(1),$(2)),$(word $(1),$(2)),0)
188 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)) ))
189
190 ifdef DUMP
191   kernel_version_cmp=
192 else
193   kernel_version_cmp=$(shell [ $(call kernel_version_merge,$(call split_version,$(2))) $(1) $(call kernel_version_merge,$(call split_version,$(3))) ] && echo 1 )
194 endif
195
196 CompareKernelPatchVer=$(if $(call kernel_version_cmp,-$(2),$(1),$(3)),1,0)
197
198 kernel_patchver_gt=$(call kernel_version_cmp,-gt,$(KERNEL_PATCHVER),$(1))
199 kernel_patchver_ge=$(call kernel_version_cmp,-ge,$(KERNEL_PATCHVER),$(1))
200 kernel_patchver_eq=$(call kernel_version_cmp,-eq,$(KERNEL_PATCHVER),$(1))
201 kernel_patchver_le=$(call kernel_version_cmp,-le,$(KERNEL_PATCHVER),$(1))
202 kernel_patchver_lt=$(call kernel_version_cmp,-lt,$(KERNEL_PATCHVER),$(1))
203