build: fix possible issue with kmod package having multiple AutoLoad's
[openwrt/openwrt.git] / include / kernel.mk
index 4e66c0c76fd23845609382bee862ec3e0e178ba4..301f24be3e06606b6cc6010156acb7bee46194ac 100644 (file)
@@ -51,17 +51,19 @@ else
     LINUX_UNAME_VERSION:=$(LINUX_UNAME_VERSION)-$(strip $(lastword $(subst -, ,$(LINUX_VERSION))))
   endif
 
-  MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
-  TARGET_MODULES_DIR := $(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
-
   LINUX_KERNEL:=$(KERNEL_BUILD_DIR)/vmlinux
 
   LINUX_SOURCE:=linux-$(LINUX_VERSION).tar.xz
   TESTING:=$(if $(findstring -rc,$(LINUX_VERSION)),/testing,)
   ifeq ($(call qstrip,$(CONFIG_EXTERNAL_KERNEL_TREE))$(call qstrip,$(CONFIG_KERNEL_GIT_CLONE_URI)),)
       LINUX_SITE:=@KERNEL/linux/kernel/v$(word 1,$(subst ., ,$(KERNEL_BASE))).x$(TESTING)
+  else
+      LINUX_UNAME_VERSION:=$(strip $(shell cat $(LINUX_DIR)/include/config/kernel.release))
   endif
 
+  MODULES_SUBDIR:=lib/modules/$(LINUX_UNAME_VERSION)
+  TARGET_MODULES_DIR:=$(LINUX_TARGET_DIR)/$(MODULES_SUBDIR)
+
   ifneq ($(TARGET_BUILD),1)
     PKG_BUILD_DIR ?= $(KERNEL_BUILD_DIR)/$(PKG_NAME)$(if $(PKG_VERSION),-$(PKG_VERSION))
   endif
@@ -95,15 +97,16 @@ define ModuleAutoLoad
        $(SH_FUNC) \
        export modules=; \
        probe_module() { \
-               mods="$$$$$$$$1"; \
-               boot="$$$$$$$$2"; \
+               local mods="$$$$$$$$1"; \
+               local boot="$$$$$$$$2"; \
+               local mod; \
                shift 2; \
-               for mod in $(sort $$$$$$$$mods); do \
+               for mod in $$$$$$$$mods; do \
                        mkdir -p $(2)/etc/modules.d; \
                        echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$(1); \
                done; \
                if [ -e $(2)/etc/modules.d/$(1) ]; then \
-                       if [ "$$$$$$$$boot" = "1" ]; then \
+                       if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$(1) ]; then \
                                mkdir -p $(2)/etc/modules-boot.d; \
                                ln -s ../modules.d/$(1) $(2)/etc/modules-boot.d/; \
                        fi; \
@@ -111,16 +114,17 @@ define ModuleAutoLoad
                fi; \
        }; \
        add_module() { \
-               priority="$$$$$$$$1"; \
-               mods="$$$$$$$$2"; \
-               boot="$$$$$$$$3"; \
+               local priority="$$$$$$$$1"; \
+               local mods="$$$$$$$$2"; \
+               local boot="$$$$$$$$3"; \
+               local mod; \
                shift 3; \
-               for mod in $(sort $$$$$$$$mods); do \
+               for mod in $$$$$$$$mods; do \
                        mkdir -p $(2)/etc/modules.d; \
                        echo "$$$$$$$$mod" >> $(2)/etc/modules.d/$$$$$$$$priority-$(1); \
                done; \
                if [ -e $(2)/etc/modules.d/$$$$$$$$priority-$(1) ]; then \
-                       if [ "$$$$$$$$boot" = "1" ]; then \
+                       if [ "$$$$$$$$boot" = "1" -a ! -e $(2)/etc/modules-boot.d/$$$$$$$$priority-$(1) ]; then \
                                mkdir -p $(2)/etc/modules-boot.d; \
                                ln -s ../modules.d/$$$$$$$$priority-$(1) $(2)/etc/modules-boot.d/; \
                        fi; \
@@ -129,6 +133,7 @@ define ModuleAutoLoad
        }; \
        $(3) \
        if [ -n "$$$$$$$$modules" ]; then \
+               modules="$$$$$$$$(echo "$$$$$$$$modules" | tr ' ' '\n' | sort | uniq | paste -s -d' ')"; \
                mkdir -p $(2)/etc/modules.d; \
                mkdir -p $(2)/CONTROL; \
                echo "#!/bin/sh" > $(2)/CONTROL/postinst-pkg; \
@@ -155,6 +160,7 @@ define KernelPackage
   $(eval $(call KernelPackage/Defaults))
   $(eval $(call KernelPackage/$(1)))
   $(eval $(call KernelPackage/$(1)/$(BOARD)))
+  $(eval $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic)))
 
   define Package/kmod-$(1)
     TITLE:=$(TITLE)
@@ -166,6 +172,7 @@ define KernelPackage
     PKGFLAGS:=$(PKGFLAGS)
     $(call KernelPackage/$(1))
     $(call KernelPackage/$(1)/$(BOARD))
+    $(call KernelPackage/$(1)/$(BOARD)/$(if $(SUBTARGET),$(SUBTARGET),generic))
   endef
 
   ifdef KernelPackage/$(1)/conffiles