406fd46343271df27e9e5cc9e9e58f243eb1cee3
[openwrt/openwrt.git] / include / kernel-defaults.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 KERNEL_MAKEOPTS := -C $(LINUX_DIR) \
9 HOSTCFLAGS="$(HOST_CFLAGS) -Wall -Wmissing-prototypes -Wstrict-prototypes" \
10 CROSS_COMPILE="$(KERNEL_CROSS)" \
11 ARCH="$(LINUX_KARCH)" \
12 KBUILD_HAVE_NLS=no \
13 KBUILD_BUILD_USER="$(call qstrip,$(CONFIG_KERNEL_BUILD_USER))" \
14 KBUILD_BUILD_HOST="$(call qstrip,$(CONFIG_KERNEL_BUILD_DOMAIN))" \
15 CONFIG_SHELL="$(BASH)" \
16 $(if $(findstring c,$(OPENWRT_VERBOSE)),V=1,V='') \
17 $(if $(PKG_BUILD_ID),LDFLAGS_MODULE=--build-id=0x$(PKG_BUILD_ID))
18
19 ifdef CONFIG_STRIP_KERNEL_EXPORTS
20 KERNEL_MAKEOPTS += \
21 EXTRA_LDSFLAGS="-I$(KERNEL_BUILD_DIR) -include symtab.h"
22 endif
23
24 INITRAMFS_EXTRA_FILES ?= $(GENERIC_PLATFORM_DIR)/image/initramfs-base-files.txt
25
26 ifneq (,$(KERNEL_CC))
27 KERNEL_MAKEOPTS += CC="$(KERNEL_CC)"
28 endif
29
30 ifdef CONFIG_USE_SPARSE
31 KERNEL_MAKEOPTS += C=1 CHECK=$(STAGING_DIR_HOST)/bin/sparse
32 endif
33
34 export HOST_EXTRACFLAGS=-I$(STAGING_DIR_HOST)/include
35
36 # defined in quilt.mk
37 Kernel/Patch:=$(Kernel/Patch/Default)
38
39 KERNEL_GIT_OPTS:=
40 ifneq ($(strip $(CONFIG_KERNEL_GIT_LOCAL_REPOSITORY)),"")
41 KERNEL_GIT_OPTS+=--reference $(CONFIG_KERNEL_GIT_LOCAL_REPOSITORY)
42 endif
43
44 ifneq ($(strip $(CONFIG_KERNEL_GIT_BRANCH)),"")
45 KERNEL_GIT_OPTS+=--branch $(CONFIG_KERNEL_GIT_BRANCH)
46 endif
47
48 ifeq ($(strip $(CONFIG_EXTERNAL_KERNEL_TREE)),"")
49 ifeq ($(strip $(CONFIG_KERNEL_GIT_CLONE_URI)),"")
50 define Kernel/Prepare/Default
51 xzcat $(DL_DIR)/$(LINUX_SOURCE) | $(TAR) -C $(KERNEL_BUILD_DIR) $(TAR_OPTIONS)
52 $(Kernel/Patch)
53 touch $(LINUX_DIR)/.quilt_used
54 endef
55 else
56 define Kernel/Prepare/Default
57 git clone $(KERNEL_GIT_OPTS) $(CONFIG_KERNEL_GIT_CLONE_URI) $(LINUX_DIR)
58 endef
59 endif
60 else
61 define Kernel/Prepare/Default
62 mkdir -p $(KERNEL_BUILD_DIR)
63 if [ -d $(LINUX_DIR) ]; then \
64 rmdir $(LINUX_DIR); \
65 fi
66 ln -s $(CONFIG_EXTERNAL_KERNEL_TREE) $(LINUX_DIR)
67 endef
68 endif
69
70 ifeq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),y)
71 ifeq ($(strip $(CONFIG_EXTERNAL_CPIO)),"")
72 define Kernel/SetInitramfs/PreConfigure
73 grep -v -e INITRAMFS -e CONFIG_RD_ -e CONFIG_BLK_DEV_INITRD $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config
74 echo 'CONFIG_BLK_DEV_INITRD=y' >> $(LINUX_DIR)/.config
75 echo 'CONFIG_INITRAMFS_SOURCE="$(strip $(TARGET_DIR) $(INITRAMFS_EXTRA_FILES))"' >> $(LINUX_DIR)/.config
76 endef
77 else
78 define Kernel/SetInitramfs/PreConfigure
79 grep -v INITRAMFS $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config
80 echo 'CONFIG_INITRAMFS_SOURCE="$(call qstrip,$(CONFIG_EXTERNAL_CPIO))"' >> $(LINUX_DIR)/.config
81 endef
82 endif
83
84 define Kernel/SetInitramfs
85 rm -f $(LINUX_DIR)/.config.prev
86 mv $(LINUX_DIR)/.config $(LINUX_DIR)/.config.old
87 $(call Kernel/SetInitramfs/PreConfigure)
88 echo 'CONFIG_INITRAMFS_ROOT_UID=$(shell id -u)' >> $(LINUX_DIR)/.config
89 echo 'CONFIG_INITRAMFS_ROOT_GID=$(shell id -g)' >> $(LINUX_DIR)/.config
90 echo "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_NONE),CONFIG_INITRAMFS_COMPRESSION_NONE=y,# CONFIG_INITRAMFS_COMPRESSION_NONE is not set)" >> $(LINUX_DIR)/.config
91 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_GZIP),CONFIG_INITRAMFS_COMPRESSION_GZIP=y\nCONFIG_RD_GZIP=y,# CONFIG_INITRAMFS_COMPRESSION_GZIP is not set\n# CONFIG_RD_GZIP is not set)" >> $(LINUX_DIR)/.config
92 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_BZIP2),CONFIG_INITRAMFS_COMPRESSION_BZIP2=y\nCONFIG_RD_BZIP2=y,# CONFIG_INITRAMFS_COMPRESSION_BZIP2 is not set\n# CONFIG_RD_BZIP2 is not set)" >> $(LINUX_DIR)/.config
93 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZMA),CONFIG_INITRAMFS_COMPRESSION_LZMA=y\nCONFIG_RD_LZMA=y,# CONFIG_INITRAMFS_COMPRESSION_LZMA is not set\n# CONFIG_RD_LZMA is not set)" >> $(LINUX_DIR)/.config
94 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZO),CONFIG_INITRAMFS_COMPRESSION_LZO=y\nCONFIG_RD_LZO=y,# CONFIG_INITRAMFS_COMPRESSION_LZO is not set\n# CONFIG_RD_LZO is not set)" >> $(LINUX_DIR)/.config
95 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_XZ),CONFIG_INITRAMFS_COMPRESSION_XZ=y\nCONFIG_RD_XZ=y,# CONFIG_INITRAMFS_COMPRESSION_XZ is not set\n# CONFIG_RD_XZ is not set)" >> $(LINUX_DIR)/.config
96 echo -e "$(if $(CONFIG_TARGET_INITRAMFS_COMPRESSION_LZ4),CONFIG_INITRAMFS_COMPRESSION_LZ4=y\nCONFIG_RD_LZ4=y,# CONFIG_INITRAMFS_COMPRESSION_LZ4 is not set\n# CONFIG_RD_LZ4 is not set)" >> $(LINUX_DIR)/.config
97 endef
98 else
99 endif
100
101 define Kernel/SetNoInitramfs
102 mv $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.old
103 grep -v INITRAMFS $(LINUX_DIR)/.config.old > $(LINUX_DIR)/.config.set
104 echo 'CONFIG_INITRAMFS_SOURCE=""' >> $(LINUX_DIR)/.config.set
105 endef
106
107 define Kernel/Configure/Default
108 $(LINUX_CONF_CMD) > $(LINUX_DIR)/.config.target
109 # copy CONFIG_KERNEL_* settings over to .config.target
110 awk '/^(#[[:space:]]+)?CONFIG_KERNEL/{sub("CONFIG_KERNEL_","CONFIG_");print}' $(TOPDIR)/.config >> $(LINUX_DIR)/.config.target
111 echo "# CONFIG_KALLSYMS_EXTRA_PASS is not set" >> $(LINUX_DIR)/.config.target
112 echo "# CONFIG_KALLSYMS_ALL is not set" >> $(LINUX_DIR)/.config.target
113 echo "CONFIG_KALLSYMS_UNCOMPRESSED=y" >> $(LINUX_DIR)/.config.target
114 $(SCRIPT_DIR)/metadata.pl kconfig $(TMP_DIR)/.packageinfo $(TOPDIR)/.config $(KERNEL_PATCHVER) > $(LINUX_DIR)/.config.override
115 $(SCRIPT_DIR)/kconfig.pl 'm+' '+' $(LINUX_DIR)/.config.target /dev/null $(LINUX_DIR)/.config.override > $(LINUX_DIR)/.config.set
116 $(call Kernel/SetNoInitramfs)
117 rm -rf $(KERNEL_BUILD_DIR)/modules
118 cmp -s $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev || { \
119 cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config; \
120 cp $(LINUX_DIR)/.config.set $(LINUX_DIR)/.config.prev; \
121 }
122 $(_SINGLE) [ -d $(LINUX_DIR)/user_headers ] || $(MAKE) $(KERNEL_MAKEOPTS) INSTALL_HDR_PATH=$(LINUX_DIR)/user_headers headers_install
123 $(SH_FUNC) grep '=[ym]' $(LINUX_DIR)/.config.set | LC_ALL=C sort | md5s > $(LINUX_DIR)/.vermagic
124 endef
125
126 define Kernel/Configure/Initramfs
127 $(call Kernel/SetInitramfs)
128 endef
129
130 define Kernel/CompileModules/Default
131 rm -f $(LINUX_DIR)/vmlinux $(LINUX_DIR)/System.map
132 +$(MAKE) $(KERNEL_MAKEOPTS) modules
133 endef
134
135 OBJCOPY_STRIP = -R .reginfo -R .notes -R .note -R .comment -R .mdebug -R .note.gnu.build-id
136
137 # AMD64 shares the location with x86
138 ifeq ($(LINUX_KARCH),x86_64)
139 IMAGES_DIR:=../../x86/boot
140 endif
141
142 define Kernel/CopyImage
143 cmp -s $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug || { \
144 $(KERNEL_CROSS)objcopy -O binary $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(LINUX_KERNEL)$(1); \
145 $(KERNEL_CROSS)objcopy $(OBJCOPY_STRIP) -S $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).elf; \
146 $(CP) $(LINUX_DIR)/vmlinux $(KERNEL_BUILD_DIR)/vmlinux$(1).debug; \
147 $(foreach k, \
148 $(if $(KERNEL_IMAGES),$(KERNEL_IMAGES),$(filter-out dtbs,$(KERNELNAME))), \
149 $(CP) $(LINUX_DIR)/arch/$(LINUX_KARCH)/boot/$(IMAGES_DIR)/$(k) $(KERNEL_BUILD_DIR)/$(k)$(1); \
150 ) \
151 }
152 endef
153
154 define Kernel/CompileImage/Default
155 rm -f $(TARGET_DIR)/init
156 +$(MAKE) $(KERNEL_MAKEOPTS) $(if $(KERNELNAME),$(KERNELNAME),all) modules
157 $(call Kernel/CopyImage)
158 endef
159
160 ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
161 define Kernel/CompileImage/Initramfs
162 $(call Kernel/Configure/Initramfs)
163 $(CP) $(GENERIC_PLATFORM_DIR)/base-files/init $(TARGET_DIR)/init
164 rm -rf $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)/usr/initramfs_data.cpio*
165 +$(MAKE) $(KERNEL_MAKEOPTS) $(if $(KERNELNAME),$(KERNELNAME),all) modules
166 $(call Kernel/CopyImage,-initramfs)
167 endef
168 else
169 define Kernel/CompileImage/Initramfs
170 endef
171 endif
172
173 define Kernel/Clean/Default
174 rm -f $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION)/.configured
175 rm -f $(LINUX_KERNEL)
176 $(_SINGLE)$(MAKE) -C $(KERNEL_BUILD_DIR)/linux-$(LINUX_VERSION) clean
177 endef
178
179