1. 程式人生 > 其它 >【轉載】Makefile 條件編譯"-y"

【轉載】Makefile 條件編譯"-y"

以下所有內容均基於 "u-boot-2012.04.01"。

在u-boot中的子目錄makefile中,有大量的類似於下面的語句:
Makefile (drivers\gpio):
COBJS-$(CONFIG_AT91_GPIO) += at91_gpio.o
COBJS-$(CONFIG_KIRKWOOD_GPIO) += kw_gpio.o
COBJS-$(CONFIG_MARVELL_GPIO) += mvgpio.o
COBJS-$(CONFIG_MARVELL_MFP) += mvmfp.o
COBJS-$(CONFIG_MXC_GPIO) += mxc_gpio.o
COBJS-$(CONFIG_MXS_GPIO) += mxs_gpio.o
COBJS-$(CONFIG_PCA953X) += pca953x.o
COBJS-$(CONFIG_PCA9698) += pca9698.o
COBJS-$(CONFIG_S5P) += s5p_gpio.o
COBJS-$(CONFIG_SANDBOX_GPIO) += sandbox.o
COBJS-$(CONFIG_TEGRA2_GPIO) += tegra2_gpio.o
COBJS-$(CONFIG_DA8XX_GPIO) += da8xx_gpio.o
COBJS-$(CONFIG_ALTERA_PIO) += altera_pio.o
COBJS-$(CONFIG_MPC83XX_GPIO) += mpc83xx_gpio.o

以" COBJS-$(CONFIG_MXS_GPIO) += mxs_gpio.o"為例,這裡"$( CONFIG_MXS_GPIO) "到底表示什麼?

在標頭檔案M28evk.h (include\configs)中:
#define CONFIG_MXS_GPIO /* GPIO control */

在頂層makefile中:
all: /* sinclude類似於-include,這裡不做解釋 */
sinclude $(obj)include/autoconf.mk.dep
sinclude $(obj)include/autoconf.mk
可以看出,依賴檔案包括" autoconf.mk.dep"和" autoconf.mk",但在原始碼樹中根本就找不到這兩個檔案,是怎麼回事?
在頂層makefile中:
#
# Auto-generate the autoconf.mk file (which is included by all makefiles)
#
# This target actually generates 2 files; autoconf.mk and autoconf.mk.dep.
# the dep file is only include in this top level makefile to determine when
# to regenerate the autoconf.mk file.
$(obj)include/autoconf.mk.dep: $(obj)include/config.h include/common.h
@$(XECHO) Generating $@ ; \
set -e ; \
: Generate the dependancies ; \
$(CC) -x c -DDO_DEPS_ONLY -M $(CFLAGS) $(CPPFLAGS) \
-MQ $(obj)include/autoconf.mk include/common.h > $@

$(obj)include/autoconf.mk: $(obj)include/config.h
@$(XECHO) Generating $@ ; \
set -e ; \
: Extract the config macros ; \
$(CPP) $(CFLAGS) -DDO_DEPS_ONLY -dM include/common.h | \
sed -n -f tools/scripts/define2mk.sed > [email protected] && \
mv [email protected] $@
原來, 檔案" autoconf.mk"是基於" include/config.h "和 " include/common.h "自動生成的,這裡會用到一個指令碼" tools/scripts/define2mk.sed",看一下這個指令碼:
#
# Sed script to parse CPP macros and generate output usable by make
#
# It is expected that this script is fed the output of 'gpp -dM'
# which preprocesses the common.h header files and outputs the final
# list of CPP macros (and whitespace is sanitized)
#

# Only process values prefixed with #define CONFIG_
/^#define CONFIG_[A-Za-z0-9_][A-Za-z0-9_]*/ {
# Strip the #define prefix
s/#define *//;
# Change to form CONFIG_*=VALUE
s/ */=/;
# Drop trailing spaces
s/ *$//;
# drop quotes around string values
s/=".∗.∗"$/=\1/;
# Concatenate string values
s/" *"//g;
# Assume strings as default - add quotes around values
s/=..∗..∗/="\1"/;
# but remove again from decimal numbers
s/="[0−9][0−9]∗[0−9][0−9]∗"/=\1/;
# ... and from hex numbers
s/="0[Xx][0−9a−fA−F][0−9a−fA−F]∗0[Xx][0−9a−fA−F][0−9a−fA−F]∗"/=\1/;
# Change '1' and empty values to "y" (not perfect, but
# supports conditional compilation in the makefiles
s/=$/=y/;
s/=1$/=y/;
# print the line
p
}
這個指令碼的功能大概就是:處理標頭檔案中以"CONFIG_"開始的巨集定義,將定義為"空"或為"1"的巨集轉換為"y",並生成一個可以被makefile識別的"autoconf.mk"檔案,在這個檔案裡就會出現"-y",如下:
CONFIG_CMD_FAT=y
CONFIG_USB_OHCI=y
CONFIG_BOOTM_NETBSD=y
CONFIG_BOARD_EARLY_INIT_F=y
CONFIG_SYS_CLK_FREQ=12000000
CONFIG_CMD_ITEST=y

原文連結:https://blog.csdn.net/shaodongju/article/details/51578342