如何将.txt文件行作为makefile的输入?

时间:2018-12-10 15:17:05

标签: makefile

我有一个makefile,我想添加一个新目标,该目标可以读取.txt文件的特定行并从$ 4开始执行该过程。

我已经运行了代码,并使用URL中的图像制作了数据库并进行了转换($ 2和$ 3)。

顺便说一句,如果您认为这样做不是一种合理的方法(传递特定的输入并收集相应的输出),请让我知道如何以一种合理的方式进行?

    #Setup test images and check target
    #
    #URLS of images to test
    IMAGE_URLS=     http://farm1.static.flickr.com/93/238836380_a4db5526a9.jpg \
            http://farm1.static.flickr.com/203/495381063_67fe69a64f.jpg \
            http://farm1.static.flickr.com/93/238836380_a4db5526a9.jpg \
            http://farm1.static.flickr.com/203/495381063_67fe69a64f.jpg \
            http://farm3.static.flickr.com/2068/2218230147_c6559cd7ac.jpg \
            http://farm2.static.flickr.com/1020/1459940961_6a54469e1e.jpg \
            http://farm2.static.flickr.com/1140/1026808473_e4a2a76ded.jpg \
            http://farm1.static.flickr.com/143/341257611_e730dfea3d.jpg \
            http://farm3.static.flickr.com/2079/2226345732_0152a169fd.jpg \
            http://farm3.static.flickr.com/2168/1834178819_e866ed3c04.jpg \
            http://farm1.static.flickr.com/149/409910004_068c0fdec1.jpg \
    #Classification index of test images (not important)
    CLASS_IDX=      281\
            281\
            285\
            291\
            728\
            279\
            285\
            281\
            281

    # Getters
    JOINED   = $(join $(addsuffix @,$(IMAGE_URLS)),$(CLASS_IDX))
    GET_URL  = $(word 1,$(subst @, ,$1))
    GET_IDX  = $(word 2,$(subst @, ,$1))

    #Bash coloring
    RED=\033[0;31m
    GREEN=\033[0;32m
    NC=\033[0m

    #$1=URL $2=NAME $3=CONVERTED_PNG $4=CHECK $5=IDX

    define IMAGE_BUILD_RULES

    #download image
    $2:
        wget "$(strip $1)" -O $2

    #convert
    $3:$2
        convert $2 -resize 224x224! $3

    #check if correct class is identified. If not error
    $4:$3 $(EXE)
        @echo "Evaluating image $3"
        ./$(EXE) $3 | tee $4
        @grep -q "Detected class: $(strip $5)" $4 && echo "$(GREEN)correctly identified image $2$(NC)" ||  (echo "$(RED)Did not correctly identify image $2$(NC)")

    endef

    #check if all images are classified correctly
    check_all: $(foreach URL, $(IMAGE_URLS), check_$(basename $(notdir $(URL))))
        @echo "$(GREEN)All correct!$(NC)"

    #define build rules for all images
    $(foreach j,$(JOINED),$(eval $(call IMAGE_BUILD_RULES,\
        $(call GET_URL, $j),\
        $(notdir $(call GET_URL, $j)),\
        converted_$(basename $(notdir $(call GET_URL, $j))).png,\
        check_$(basename $(notdir $(call GET_URL, $j))),\
        $(call GET_IDX,$j)\
    )))

1 个答案:

答案 0 :(得分:0)

我不知道这是否对您有帮助,但是您似乎还是在重新思考您的问题。您的makefile看起来至少在远程进行某种构建管理。 (请注意,您可以使用模式来编写规则,但是您似乎要针对不同的目标。)对makefile的以下重写使用了GNUmake table toolkit,看起来很适合您的目的:

include gmtt/gmtt.mk

# Direct definition of the table in the file:
# define IMAGES :=
# 2
# http://farm1.static.flickr.com/93/238836380_a4db5526a9.jpg       281
# http://farm1.static.flickr.com/203/495381063_67fe69a64f.jpg    281
# http://farm1.static.flickr.com/93/238836380_a4db5526a9.jpg     285
# http://farm1.static.flickr.com/203/495381063_67fe69a64f.jpg    291
# http://farm3.static.flickr.com/2068/2218230147_c6559cd7ac.jpg      728
# http://farm2.static.flickr.com/1020/1459940961_6a54469e1e.jpg      279
# http://farm2.static.flickr.com/1140/1026808473_e4a2a76ded.jpg      285
# http://farm1.static.flickr.com/143/341257611_e730dfea3d.jpg    281
# http://farm3.static.flickr.com/2079/2226345732_0152a169fd.jpg      281
# http://farm3.static.flickr.com/2168/1834178819_e866ed3c04.jpg    ???
# http://farm1.static.flickr.com/149/409910004_068c0fdec1.jpg      ???
# endef

# Reading table from a file. File *must* be two columns (url, class) and nothing else - no comments either!
IMAGES := $(file < imagelist.txt)
$(info Processing file list:$(newline)$(IMAGES))
IMAGES := 2 $(IMAGES) # add the number of columns in front to make it a gmtt table

#Bash coloring
RED=\033[0;31m
GREEN=\033[0;32m
NC=\033[0m

#$1=URL $2=NAME $3=CONVERTED_PNG $4=CHECK $5=IDX
define IMAGE_BUILD_RULES
#download image
$2:
    wget "$(strip $1)" -O $2

#convert
$3:$2
    convert $2 -resize 224x224! $3

#check if correct class is identified. If not error
$4:$3 $(EXE)
    @echo "Evaluating image $3"
    ./$(EXE) $3 | tee $4
    @grep -q "Detected class: $(strip $5)" $4 && echo "$(GREEN)correctly identified image $2$(NC)" ||  (echo "$(RED)Did not correctly identify image $2$(NC)")

endef

#check if all images are classified correctly
check_all: $(foreach URL, $(IMAGE_URLS), check_$(basename $(notdir $(URL))))
    @echo "$(GREEN)All correct!$(NC)"


# Just as a check for now, remove the whole $(info ...) if output too large    
$(info Generating the following rules: $(newline)$(call map-select,1 2,$(IMAGES),t,$$(call IMAGE_BUILD_RULES,$$1,$$(notdir $$1),converted_$$(basename $$(notdir $$1)).png,check_$$(basename $$(notdir $$1)),$$2)))

# Rule definition - no need to call $(eval)
# Use the function `map-select` which applies a function on each selected line of a table. Syntax is $(call map-select,_column-numbers_,_table_,_where-clause_,_mapping-function_)
# We select columns one and two of the table. Where-clause is always true (t). Function is calling IMAGE_BUILD_RULES with the selected columns enumerated as $1,$2,$3,etc. - this parameter must be quoted ($$)
$(call map-select,1 2,$(IMAGES),t,$$(call IMAGE_BUILD_RULES,$$1,$$(notdir $$1),converted_$$(basename $$(notdir $$1)).png,check_$$(basename $$(notdir $$1)),$$2))

引用的文件imagelist.txt必须看起来像这样:

http://farm1.static.flickr.com/93/238836380_a4db5526a9.jpg       281
http://farm1.static.flickr.com/203/495381063_67fe69a64f.jpg      281
http://farm1.static.flickr.com/93/238836380_a4db5526a9.jpg   285
http://farm1.static.flickr.com/203/495381063_67fe69a64f.jpg      291
http://farm3.static.flickr.com/2068/2218230147_c6559cd7ac.jpg    728
http://farm2.static.flickr.com/1020/1459940961_6a54469e1e.jpg    279
http://farm2.static.flickr.com/1140/1026808473_e4a2a76ded.jpg    285
http://farm1.static.flickr.com/143/341257611_e730dfea3d.jpg      281
http://farm3.static.flickr.com/2079/2226345732_0152a169fd.jpg    281
http://farm3.static.flickr.com/2168/1834178819_e866ed3c04.jpg    ???
http://farm1.static.flickr.com/149/409910004_068c0fdec1.jpg      ???

请注意,您的测试数据中包含重复的网址。