如何将文件名列表转换为makefile中的变量名集?
例如,我有文件名列表和变量集:
filename := file1 file2 file3 ...
file1 := opt1
file2 := opt2
file3 := opt3
...
...
现在我想创建一组新的变量:
file1_opt := $(file1)
file2_opt := $(file2)
file3_opt := $(file3)
...
...
如何在 Makefile 中做到这一点?
对于 bash,这很简单:
for name in $(filenames)
do
$($(subst .,_,$(subst /,_,name)))_opt := $($(subst .,_,$(subst /,_,name)))
done
但是如何在makefile中制作呢?
例子:
./module/files.mk
C_SRC := a_file.c b_file.c
CPP_SRC := c_file.cpp d_file.cpp
a_file_c := -O2
b_file_c := -DN_DEBUG
./Makefile
....
SRCDIR := module
include makef.mk
....
$(C_OBJ) : $(OBJDIR)/%.o : %.c
$(CC) -c $(C_FLAGS) $(C_FLAGS_$(subst .,_,$(subst /,_,$<))) $< -o $@
....
./makef.mk
SAVE_C_SRC := $(C_SRC)
SAVE_CPP_SRC := $(CPP_SRC)
C_SRC :=
CPP_SRC :=
include $(SRCDIR)/files.mk
MK_DIRS += $(OBJDIR)/$(SRCDIR)
----[ problem site ]----
# this work for bash but not for make
for name in $(C_SRC)
do
C_FLAGS_$(SRCDIR)_$($(subst .,_,$(subst /,_,name))) := $($(subst .,_,$(subst /,_,name)))
$($(subst .,_,$(subst /,_,name))) :=
done
----[ end of problem site ]----
SAVE_C_SRC += $(C_SRC:%=$(SRCDIR)/%)
SAVE_CPP_SRC += $(CPP_SRC:%=$(SRCDIR)/%)
C_SRC := $(SAVE_C_SRC)
CPP_SRC := $(SAVE_CPP_SRC)
伊利亚