在第三方设备驱动程序上,我想“定义”一个在make release
和make debug
之间进行区分的标志。
我一直在用常规代码使用此构造,但是在编译此设备驱动程序时不起作用。
最小的Makefile如下:
KERNEL_VERSION := $(shell uname -r)
KERNEL_DIR := /lib/modules/$(KERNEL_VERSION)/build
INSTALL_MOD_DIR := kernel/drivers/misc
PWD := $(shell pwd)
EXTRA_CFLAGS := -I$(M)/someDir
ccflags-y := -std=gnu99 -Wno-declaration-after-statement
CCOPT += -DmyOptA
CCOPT += -DmyOptB
## CONDITIONAL HERE ##
ifeq ($(MAKECMDGOALS),debug)
$(info Compiling for debug)
CCOPT += -DmyOptC
endif
CCOPT += -DmyOptD
EXTRA_CFLAGS += $(CCOPT)
WARNINGFLAGS = -fdiagnostics-color=auto
obj-m := driverModule.o
debug release:
@echo ; echo "Building '$(MAKECMDGOALS)'..."
@echo CCOPT=$(CCOPT)
@echo EXTRA_CFLAGS=$(EXTRA_CFLAGS)
$(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules
因此,应始终设置myOptA
,myOptB
和myOptD
,而应仅为myOptC
设置make debug
。
myOptC
是在此Makefile中设置的,但不会传播到内核Makefile中。但是myOptA,C,D
被传播了。
make debug
的输出清楚地显示了myOptC
和CCOPT
中包含的EXTRA_CFLAGS
,但是下面的编译行中没有显示它。
为什么不呢?
$ make debug V=1
Compiling for debug
Building 'debug'...
CCOPT=-DmyOptA -DmyOptB -DmyOptC -DmyOptD
EXTRA_CFLAGS=-I/someDir -DmyOptA -DmyOptB -DmyOptC -DmyOptD
make -C /lib/modules/4.4.127-1.el6.elrepo.i686/build M=/xfiles modules
make[1]: Entering directory `/usr/src/kernels/4.4.127-1.el6.elrepo.i686'
test -e include/generated/autoconf.h -a -e include/config/auto.conf || ( \
echo >&2; \
echo >&2 " ERROR: Kernel configuration is invalid."; \
echo >&2 " include/generated/autoconf.h or include/config/auto.conf are missing.";\
echo >&2 " Run 'make oldconfig && make prepare' on kernel src to fix it."; \
echo >&2 ; \
/bin/false)
mkdir -p /xfiles/.tmp_versions ; rm -f /xfiles/.tmp_versions/*
make -f ./scripts/Makefile.build obj=/xfiles
gcc -Wp,-MD,/xfiles/.driverModule.o.d -nostdinc
-isystem /usr/lib/gcc/i686-redhat-linux/4.4.7/include
-I./arch/x86/include -Iarch/x86/include/generated/uapi
-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 -fno-PIE -mno-sse
-mno-mmx -mno-sse2 -mno-3dnow -mno-avx -m32
-msoft-float -mregparm=3 -freg-struct-return -fno-pic
-mpreferred-stack-boundary=2 -march=i686
-mtune=generic -maccumulate-outgoing-args
-Wa,-mtune=generic32 -ffreestanding -DCONFIG_AS_CFI=1
-DCONFIG_AS_CFI_SIGNAL_FRAME=1
-DCONFIG_AS_CFI_SECTIONS=1 -DCONFIG_AS_SSSE3=1
-DCONFIG_AS_CRC32=1 -DCONFIG_AS_AVX=1
-DCONFIG_AS_AVX2=1 -pipe -Wno-sign-compare
-fno-asynchronous-unwind-tables
-fno-delete-null-pointer-checks -O2 -DCC_HAVE_ASM_GOTO
-Wframe-larger-than=2048 -fstack-protector
-Wno-unused-but-set-variable -fno-omit-frame-pointer
-fno-optimize-sibling-calls
-fno-var-tracking-assignments -pg
-fno-inline-functions-called-once
-Wdeclaration-after-statement -Wno-pointer-sign
-fno-strict-overflow -fno-merge-all-constants
-fmerge-constants -fno-stack-check -fconserve-stack
-Werror=implicit-int -Werror=strict-prototypes
-std=gnu99 -Wno-declaration-after-statement
-I/xfiles/someDir -DmyOptA -DmyOptB -DmyOptD -DMODULE
-D"KBUILD_STR(s)=#s"
-D"KBUILD_BASENAME=KBUILD_STR(driverModule)"
-D"KBUILD_MODNAME=KBUILD_STR(driverModule)" -c -o
/xfiles/.tmp_driverModule.o /xfiles/driverModule.c
文件“ driverModule.c”可以具有与该问题无关的任何内容...
答案 0 :(得分:0)
默认情况下,制作变量在子制作中(MAKEFLAGS除外)不可见。您必须将它们导出到环境中(这样它们才能在任何子进程中可见),或者在命令行中传递它们,如下所示:$(MAKE) -C $(KERNEL_DIR) M=$(PWD) EXTRA_CFLAGS=$(EXTRA_CFLAGS) modules