makefile: find a position of word in a variable

后端 未结 6 1431
我寻月下人不归
我寻月下人不归 2020-12-20 21:16

In my makefile, I need to make a variable assignment based on a command line variable value. for example, I do:

make var_1=xxx

where

6条回答
  •  北海茫月
    2020-12-20 21:53

    This works in mingw gnu make 4.3 :

    define get_index
    $(shell \
        $(eval _c = 1)\
        $(eval _b = n n) \
        $(foreach x,$(2), \
            $(eval _b += n \
            $(eval _c = $(words $(_b)) \
            $(if $(filter $(1),$(x)), \
                $(eval _r = $(_c)))))) \
                echo $(_r) \
        $(eval _c =) \
        $(eval _b =) \
        $(eval _r =) \
    )
    endef
    

    Takes two arguments; the word to search for and the list where to search. The index is 'returned' by echoing in $(shell ...)

    Example:

    list = cow horse chicken
    index = $(call get_index,horse,$(list))
    @echo $(index)
    
    # output: 2
    

提交回复
热议问题