在使用makefile创建文件之前验证Id的正确性

在使用makefile创建文件之前验证Id的正确性,makefile,gnu-make,Makefile,Gnu Make,我需要用模式(abbbb yyy)验证ID 例如: ID := A12345-789 B98765-123 C58730-417 VARIANT := test1 test2 test3 生成和后处理将生成文件,具体取决于变量: `sw_main_test1.hex ,sw_main_test1.hex and sw_main_test1.hex ` .PHONY : SW_TEST SW_TEST : if <ID is correct> cp sw_main

我需要用模式(abbbb yyy)验证ID

例如:

ID := A12345-789 B98765-123 C58730-417
VARIANT := test1 test2 test3
生成和后处理将生成文件,具体取决于变量:

`sw_main_test1.hex ,sw_main_test1.hex and sw_main_test1.hex  `

.PHONY : SW_TEST
SW_TEST :
    if <ID is correct>
    cp sw_main_test1.hex --> A12345-789.hex 
    cp sw_main_test2.hex --> B98765-123.hex
    cp sw_main_test3.hex --> C58730-417.hex
其中:
A=[A-Z];b=[0-9];y=[0-9]


请让我知道如何使用任何工具或实用程序在Makefile中使用正则表达式验证ID是否正确在这个脚本中,我假设您从一个文件(我在这里称它为
someidcontent.txt
)获取ID。然后您可以编写这样的脚本(假设您只在Linux上工作)

编辑

我在以前的剧本中犯了一个错误。我没有检查ID是否正确。现在,我的新脚本执行以下操作:我从文件中读取ID并检查其正确性。若ID是正确的,那个么一些文件将被复制到带有ID号的目标目录中

# get ID from a file
getID := $(shell cat someidcontent.txt)
# need a hack for successful checking
idToCheck := $(getID)

# check procedure
checkID := $(shell echo $(idToCheck) | grep "[A-Z][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]$$")

all:
ifeq "$(checkID)" "$(idToCheck)"
   echo found
   cp -v output.txt ./delivery/$(idToCheck).txt;
endif

.PHONY: all
编辑2

好吧,这有点挑战性,但我不知怎么解决了。也许还有其他更好的方法来解决这个问题。在我的解决方案中,我假设具有ID和源文件名的文件如下所示(换句话说,这是我的
someidcontent.txt
)的内容:

这是我的makefile,附带注释以作进一步解释。我希望它们足够了

# retrieve id and filename data from other file
listContent := $(shell cat someidcontent.txt)

# extract only IDs from other files
checkIDs = $(shell echo $(listContent) | grep -o "[A-Z][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]")

all:
# iterate only over IDs
# first, give me the ID
# second retrieve the filename part for successful copy procedure
# and copy the file to the target dir with ID as filename
   @$(foreach x,$(checkIDs), \
      echo $(x); \
      cp -v $(shell echo $(listContent) | grep -o "$(x):[A-Z0-9a-z\.]*" | sed "s/[-A-Z0-9]*://g") ./delivery/$(x).t$
   )

.PHONY: all

您可以从make中检查简单的字符串模式,非常正常(不想说“很好”):

[A-F] := A B C D E F#
[a-f] := a b c d e f#
[A-Z] := $([A-F]) G H I J K L M N O P Q R S T U V W X Y Z#
[a-z] := $([a-f]) g h i j k l m n o p q r s t u v w x y z#
[0-9] := 0 1 2 3 4 5 6 7 8 9#

######################################################################
##### $(call explode,_stringlist_,_string_)
## Insert a blank after every occurrence of the strings from _stringlist_ in _string_.
## This function serves mainly to convert a string into a list.
## Example: `$(call explode,0 1 2 3 4 5 6 7 8 9,0xl337c0de)` --> `0 xl3 3 7 c0 de`
explode = $(if $1,$(subst $(firstword $1),$(firstword $1) ,$(call explode,$(wordlist 2,255,$1),$2)),$2)


ID := A12345-789 B98765-123 C58730-417 123456+328

############################################################
# $(call check-id,_id-string_)
# Return 'malformed' or the given id
check-id = $(if $(call check-id-1,$(call explode,- $([A-Z]) $([0-9]),$1)),malformed,$1)
check-id-1 = $(strip $(filter-out $([A-Z]),$(wordlist 1,1,$1)) $(filter-out $([0-9]),$(wordlist 2,6,$1)) $(filter-out -,$(word 7,$1)) $(filter-out $([0-9]),$(wordlist 8,10,$1)) )

$(info $(foreach w,$(ID),$(call check-id,$(w))))

如何做到这一点,取决于其他一些因素。当一个不同的目标作为副产品生成时,这些文件是自己的目标,还是只是生成的?如果文件是自己的目标,我假设您的makefile是自己生成的,您是如何做到这一点的?请阅读。我建议将名称测试委托给脚本。您对什么脚本语言比较熟悉?@Beta:我对python比较熟悉数据(
ID
)来自哪里?从一个文件?Kristain你的答案很好,但是如果我们有一个ID和一个输出文件,它就会工作如果我们有ID列表和输出文件列表,它就会工作fail@Vicky:有没有办法确定哪个输出文件属于哪个ID?@kristain其序列第一个ID列表元素对应于第一个变量列表element@Vicky我犯了一个错误编辑到我的解决方案。我希望,这可以帮助您创建所需的
Makefile
A2345-678:output1.txt
B3456-123:output.txt
C0987-987:thirdfile.txt
# retrieve id and filename data from other file
listContent := $(shell cat someidcontent.txt)

# extract only IDs from other files
checkIDs = $(shell echo $(listContent) | grep -o "[A-Z][0-9][0-9][0-9][0-9]-[0-9][0-9][0-9]")

all:
# iterate only over IDs
# first, give me the ID
# second retrieve the filename part for successful copy procedure
# and copy the file to the target dir with ID as filename
   @$(foreach x,$(checkIDs), \
      echo $(x); \
      cp -v $(shell echo $(listContent) | grep -o "$(x):[A-Z0-9a-z\.]*" | sed "s/[-A-Z0-9]*://g") ./delivery/$(x).t$
   )

.PHONY: all
[A-F] := A B C D E F#
[a-f] := a b c d e f#
[A-Z] := $([A-F]) G H I J K L M N O P Q R S T U V W X Y Z#
[a-z] := $([a-f]) g h i j k l m n o p q r s t u v w x y z#
[0-9] := 0 1 2 3 4 5 6 7 8 9#

######################################################################
##### $(call explode,_stringlist_,_string_)
## Insert a blank after every occurrence of the strings from _stringlist_ in _string_.
## This function serves mainly to convert a string into a list.
## Example: `$(call explode,0 1 2 3 4 5 6 7 8 9,0xl337c0de)` --> `0 xl3 3 7 c0 de`
explode = $(if $1,$(subst $(firstword $1),$(firstword $1) ,$(call explode,$(wordlist 2,255,$1),$2)),$2)


ID := A12345-789 B98765-123 C58730-417 123456+328

############################################################
# $(call check-id,_id-string_)
# Return 'malformed' or the given id
check-id = $(if $(call check-id-1,$(call explode,- $([A-Z]) $([0-9]),$1)),malformed,$1)
check-id-1 = $(strip $(filter-out $([A-Z]),$(wordlist 1,1,$1)) $(filter-out $([0-9]),$(wordlist 2,6,$1)) $(filter-out -,$(word 7,$1)) $(filter-out $([0-9]),$(wordlist 8,10,$1)) )

$(info $(foreach w,$(ID),$(call check-id,$(w))))