Chinaunix

标题: 内核模块Makefile KBUILD_CFLAGS参数不生效 [打印本页]

作者: 0x1024    时间: 2016-11-15 11:56
标题: 内核模块Makefile KBUILD_CFLAGS参数不生效
Makefile如下:
kernel_incdir=/home/candoit/l2fwd/kernel/linux


KBUILD_CFLAGS +=-march=core2 -mtune=core2 -msse3 -mssse3 -mmmx -mno-sse4
KBUILD_CFLAGS +=-Wno-declaration-after-statement -Wno-strict-prototypes

#EXTRA_CFLAGS可以给Kbuild系统添加外部系统头文件
EXTRA_CFLAGS=-I$(PWD)/include/hs -I$(PWD)
targets = default
.PHONY: all
all: ${targets}
DEBUG_HS=1
ifneq ($(KERNELRELEASE),)
# call from kernel build system

obj-m        := hs.o
else

KERNELDIR = $(kernel_incdir)
PWD       := $(shell pwd)

default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif

install:
        mkdir -p $(INSTDIR)
        cp *.ko $(INSTDIR)       
clean:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) clean



编译如下参数:
gcc -Wp,-MD,/home/candoit/hs-kernel/.hs_main.o.d  -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/4.8/include -I./arch/x86/include -Iarch/x86/include/generated  -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -m64 -mno-80387 -mno-fp-ret-in-387 -march=core2 -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0 -Wframe-larger-than=1024 -fno-stack-protector -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -DCC_HAVE_ASM_GOTO -march=core2 -mtune=core2 -msse3 -mssse3 -mmmx -mno-sse4 -Wno-declaration-after-statement -Wno-strict-prototypes -I/home/candoit/hs-kernel/include/hs -I/home/candoit/hs-kernel  -DMODULE  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(hs_main)"  -D"KBUILD_MODNAME=KBUILD_STR(hs)" -c -o /home/candoit/hs-kernel/.tmp_hs_main.o /home/candoit/hs-kernel/hs_main.c


最后运行结果:
Nov 11 18:17:14 HD_DPDK kernel: [   87.493555] HD: __SSE4_2__
Nov 11 18:17:14 HD_DPDK kernel: [   87.493561] HD: __SSE4_2__



-msse3 -mssse3 -mmmx -mno-sse4 参数为何没有生效, 求大神解释!!!



作者: captivated    时间: 2016-11-16 17:17
本帖最后由 captivated 于 2016-11-16 17:19 编辑

LZ 是编译外部模块,不知道是不是因为编译外部模块时并没有引用自行添加的 KBUILD_CFLAGS 的原因。
不过,像 LZ 的这种情况,不应该使用 KBUILD_CFLAGS,正常的话应该使用 ccflags-y
因为只有 top Makefile owns KBUILD_CFLAGS.
见 Documentation/kbuild/makefiles.txt

287 --- 3.7 Compilation flags
288
289     ccflags-y, asflags-y and ldflags-y
290         These three flags apply only to the kbuild makefile in which they
291         are assigned. They are used for all the normal cc, as and ld
292         invocations happening during a recursive build.
293         Note: Flags with the same behaviour were previously named:
294         EXTRA_CFLAGS, EXTRA_AFLAGS and EXTRA_LDFLAGS.
295         They are still supported but their usage is deprecated.
296
297         ccflags-y specifies options for compiling with $(CC).
298
299         Example:
300                 # drivers/acpi/Makefile
301                 ccflags-y := -Os
302                 ccflags-$(CONFIG_ACPI_DEBUG) += -DACPI_DEBUG_OUTPUT
303
304         This variable is necessary because the top Makefile owns the
305         variable $(KBUILD_CFLAGS) and uses it for compilation flags for the
306         entire tree.
307
308         asflags-y specifies options for assembling with $(AS).
309
310         Example:
311                 #arch/sparc/kernel/Makefile
312                 asflags-y := -ansi

作者: 0x1024    时间: 2016-11-16 20:39
回复 2# captivated

KBUILD_CFLAGS 实际上和ccflags-y 是一样的效果,最后展开的gcc编译参数都一样,测试还是和主题一样!

作者: captivated    时间: 2016-11-17 09:42
回复 3# 0x1024

我知道问题了。把那个 ifneq ($(KERNELRELEASE),) 的判断去掉就行了。


1. 当执行 make 时, KERNELRELEASE 为空。所以走 else 部分。 else 部分进入 kernel 源码目录执行 make, 于是 make -C 走 kernel 的 top Makefile.
2. kernel 的 top Makefile 还是得引用当前的这个外部模块的 Makefile. 但是注意,这次 KERNELRELEASE 不为空了。所以走当前外部模块 Makefile 的 ifneq 那一部分。

我测试了,实际上 KBUILD_CFLAGS 和 ccflags-y 都是生效的(如果在 ifneq 后面用 info 打印 flags 的话)。


作者: 0x1024    时间: 2016-11-21 15:55
标题: 内核模块Makefile参数不生效
参数是带下去了的, 只是没有生效, 编译时使用V=1可以看到

如下所示

编译如下参数:
gcc -Wp,-MD,/home/candoit/hs-kernel/.hs_main.o.d  -nostdinc -isystem /usr/lib/gcc/x86_64-linux-gnu/4.8/include -I./arch/x86/include -Iarch/x86/include/generated  -Iinclude -I./arch/x86/include/uapi -Iarch/x86/include/generated/uapi -I./include/uapi -Iinclude/generated/uapi -include ./include/linux/kconfig.h -D__KERNEL__ -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Wno-format-security -std=gnu89 -m64 -mno-80387 -mno-fp-ret-in-387 -march=core2 -mno-red-zone -mcmodel=kernel -funit-at-a-time -maccumulate-outgoing-args -DCONFIG_AS_CFI=1 -DCONFIG_AS_CFI_SIGNAL_FRAME=1 -DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_FXSAVEQ=1 -DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1 -DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare -fno-asynchronous-unwind-tables -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -fno-delete-null-pointer-checks -O2 --param=allow-store-data-races=0 -Wframe-larger-than=1024 -fno-stack-protector -Wno-unused-but-set-variable -fno-omit-frame-pointer -fno-optimize-sibling-calls -fno-var-tracking-assignments -Wdeclaration-after-statement -Wno-pointer-sign -fno-strict-overflow -fconserve-stack -Werror=implicit-int -Werror=strict-prototypes -DCC_HAVE_ASM_GOTO -march=core2 -mtune=core2 -msse3 -mssse3 -mmmx -mno-sse4 -Wno-declaration-after-statement -Wno-strict-prototypes -I/home/candoit/hs-kernel/include/hs -I/home/candoit/hs-kernel  -DMODULE  -D"KBUILD_STR(s)=#s" -D"KBUILD_BASENAME=KBUILD_STR(hs_main)"  -D"KBUILD_MODNAME=KBUILD_STR(hs)" -c -o /home/candoit/hs-kernel/.tmp_hs_main.o /home/candoit/hs-kernel/hs_main.c


作者: nswcfd    时间: 2016-11-21 18:12
本帖最后由 nswcfd 于 2016-11-21 18:14 编辑

那就单独测一下几个-m参数吧。

作者: captivated    时间: 2016-11-21 20:34
回复 5# 0x1024

不会。
你在 Makefile 里面这样写:

ifneq ($(KERNELRELEASE),)
obj-m := xxx
$(info $(KBUILD_CFLAGS))
$(info $(ccflags-y))
else
...
endif

你看 info 出来的信息就行了。实际上,你模块里面用宏,也可以测试啊。
比如,你 KBUILD_CFLAGS += -DMYDEBUG
你代码里面写

#ifdef MYDEBUG
printk("debug version!\n");
#endif

你直接代码就可以测试 KBUILD_CFLAGS 有没有生效啊。

作者: 0x1024    时间: 2016-11-23 19:54
已找到解决方案




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2