免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4614 | 回复: 7
打印 上一主题 下一主题

[内核模块] 内核模块Makefile KBUILD_CFLAGS参数不生效 [复制链接]

论坛徽章:
4
金牛座
日期:2013-12-12 16:11:30巨蟹座
日期:2013-12-13 10:20:3615-16赛季CBA联赛之佛山
日期:2016-11-15 13:45:3515-16赛季CBA联赛之广夏
日期:2016-11-23 19:54:58
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-11-15 11:56 |只看该作者 |倒序浏览
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 参数为何没有生效, 求大神解释!!!


论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
2 [报告]
发表于 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

论坛徽章:
4
金牛座
日期:2013-12-12 16:11:30巨蟹座
日期:2013-12-13 10:20:3615-16赛季CBA联赛之佛山
日期:2016-11-15 13:45:3515-16赛季CBA联赛之广夏
日期:2016-11-23 19:54:58
3 [报告]
发表于 2016-11-16 20:39 |只看该作者
回复 2# captivated

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

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
4 [报告]
发表于 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 的话)。

论坛徽章:
4
金牛座
日期:2013-12-12 16:11:30巨蟹座
日期:2013-12-13 10:20:3615-16赛季CBA联赛之佛山
日期:2016-11-15 13:45:3515-16赛季CBA联赛之广夏
日期:2016-11-23 19:54:58
5 [报告]
发表于 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

论坛徽章:
20
程序设计版块每日发帖之星
日期:2015-08-17 06:20:00程序设计版块每日发帖之星
日期:2016-07-16 06:20:00程序设计版块每日发帖之星
日期:2016-07-18 06:20:00每日论坛发贴之星
日期:2016-07-18 06:20:00黑曼巴
日期:2016-12-26 16:00:3215-16赛季CBA联赛之江苏
日期:2017-06-26 11:05:5615-16赛季CBA联赛之上海
日期:2017-07-21 18:12:5015-16赛季CBA联赛之青岛
日期:2017-09-04 17:32:0515-16赛季CBA联赛之吉林
日期:2018-03-26 10:02:16程序设计版块每日发帖之星
日期:2016-07-15 06:20:0015-16赛季CBA联赛之江苏
日期:2016-07-07 18:37:512015亚冠之萨济拖拉机
日期:2015-08-17 12:21:08
6 [报告]
发表于 2016-11-21 18:12 |只看该作者
本帖最后由 nswcfd 于 2016-11-21 18:14 编辑

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

论坛徽章:
3
15-16赛季CBA联赛之山东
日期:2016-10-30 08:47:3015-16赛季CBA联赛之佛山
日期:2016-12-17 00:06:31CU十四周年纪念徽章
日期:2017-12-03 01:04:02
7 [报告]
发表于 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 有没有生效啊。

论坛徽章:
4
金牛座
日期:2013-12-12 16:11:30巨蟹座
日期:2013-12-13 10:20:3615-16赛季CBA联赛之佛山
日期:2016-11-15 13:45:3515-16赛季CBA联赛之广夏
日期:2016-11-23 19:54:58
8 [报告]
发表于 2016-11-23 19:54 |只看该作者
已找到解决方案
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP