Makefile具有参数的多个值

时间:2016-03-04 12:43:10

标签: makefile

目前,我正在研究一个带有参数“CLASS = xxx”的makefile,然后编译并使用该值进行处理。 最后,它在一堆文件上运行一个应用程序($ APP)。

我输入此命令:

make default CLASS=Test_UART

并且makefile因此处理它:

pc: $(APP)
    make -C BUILDENV CLASS=$(CLASS) BUILD=just_filelist OUTPUT=filelist.txt SKIPSELF=yes
    ../classCvt/classCvt <./Applications/$(CLASS).class> ./Applications/$(CLASS).ujc
    time -p ./$(APP) ./Applications/$(CLASS).ujc `cat filelist.txt`

因此它调用了我的BUILDENV文件夹中的makefile,它执行以下操作:

#USAGE:  make -C <PATH_TO_THIS_FILES_PARENT_DIR> CLASS=<MY_JAVA_FILE_NAME_WITHOUT_JAVA_EXTENSION> OUT=<OUTPUT_FILE_NAME>


SELF    := $(dir $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
CLASS   ?= PLEASE_SPECIFY_CLASS_PARAM
DIR     := $(PWD)#print working directory
CCVT    ?= $(SELF)/../../classCvt/classCvt
TOBIN   ?= $(SELF)/../../classCvt/tobin
OUTPUT  ?= 

### Comment: Defining CMD.
ifeq ($(BUILD), just_filelist)
    CMD = echo
else
    ifeq ($(BUILD), PC)
        CMD = echo
    else
        ifeq ($(BUILD), unopt)

            CMD = $(TOBIN)
        else
            ### Comment: Optimized CMD = tobin -c ccvt
            CMD = $(TOBIN) -c $(CCVT)
        endif
    endif
endif

ifeq ($(OUTPUT), )
    OUT = &1
else
    OUT = $(DIR)/$(OUTPUT)
endif

ifeq ($(SKIPSELF), yes)
    MYCLASS = 
else
    MYCLASS = $(DIR)/Applications/$(CLASS).class
endif

all:
    CLASSPATH=$(SELF)/RT/real:$(SELF)/RT/fake:$(DIR) javac $(DIR)/Applications/$(CLASS).java
    find $(SELF)/RT/real -iname "*.class" -type f > $(SELF)/files
    ls $(DIR)/Applications/*.class | grep -v "$(CLASS)\.class" >> $(SELF)/files || true
    cat $(SELF)/files | xargs $(CMD) $(MYCLASS) >$(OUT)
    rm -f $(SELF)/files

我想做的是发出如下命令:

make default CLASS=Test1,Test2,Test3

和makefile为3个类处理它,并将给定的类放在.txt中,将默认类放在不同的.txt中,如下所示:

pc: $(APP)
    make -C BUILDENV default_classes BUILD=list_default_classes OUTPUT=list_default_classes.txt
#   make -C BUILDENV given_classes BUILD=list_given_classes OUTPUT=list_given_classes.txt CLASS=$(CLASS) SKIPSELF=yes
    ../classCvt/classCvt `cat list_given_classes.txt`./Applications/$(CLASS).ujc
    #here the list_given_classes should now contain the .ujc files
    time -p ./$(APP) `cat list_given_classes.txt` `cat list_default_classes.txt`

对于BUILDENV中的makefile,我希望如下:

default_classes:
        CLASSPATH=$(SELF)/RT/real:$(SELF)/RT/fake:$(DIR) 
        find $(SELF)/RT/real -iname "*.class" -type f > $(SELF)/files
        ls $(DIR)/Applications/*.class | grep -v "$(CLASS1)\.class" "$(CLASS2)\.class">> $(SELF)/files || true
        cat $(SELF)/files | xargs $(CMD) >$(OUT)
        rm -f $(SELF)/files

given_classes:
    javac $(DIR)/Applications/$(CLASS).java
    find $(SELF)/RT/real -iname "*.class" -type f > $(SELF)/files
    ls $(DIR)/Applications/*.class | grep -v "$(CLASS)\.class" >> $(SELF)/files || true
    cat $(SELF)/files | xargs $(CMD) $(MYCLASS) >$(OUT)
    rm -f $(SELF)/files

但是,我不知道如何为包含多个类的CLASS参数执行此操作。 我正在考虑尝试将Test1,Test2,Test3值解析为1,2,3的列表,然后迭代它。但不知道这是一个好方法,甚至是如何做到这一点。 你们有什么建议?

2 个答案:

答案 0 :(得分:0)

简单方法:

pc: $(APP)
    $(foreach C, $(shell echo $(CLASS) | tr ',' ' '), \
    make -C BUILDENV CLASS=$(C) BUILD=just_filelist OUTPUT=filelist.txt SKIPSELF=yes && \
    ../classCvt/classCvt <./Applications/$(C).class> ./Applications/$(C).ujc && \
    time -p ./$(APP) ./Applications/$(C).ujc `cat filelist.txt` &&) true

答案 1 :(得分:0)

漂亮的方式:

pc: $(APP)

define BUILD_CLASS

pc: pc-$(CLASS_SPLIT)

pc-$(CLASS_SPLIT):
    make -C BUILDENV CLASS=$(CLASS_SPLIT) BUILD=just_filelist OUTPUT=filelist.txt SKIPSELF=yes
    ../classCvt/classCvt <./Applications/$(CLASS_SPLIT).class> ./Applications/$(CLASS_SPLIT).ujc
    time -p ./$(APP) ./Applications/$(CLASS_SPLIT).ujc `cat filelist.txt`

endef

CLASSES := $(shell echo $(CLASS) | tr ',' ' ')

$(foreach CLASS_SPLIT, $(CLASSES), $(eval $(BUILD_CLASS)))