Create comma-separated lists in GNU Make

后端 未结 3 2164
夕颜
夕颜 2021-02-12 21:57

I have a Makefile with a set of booleans which must be used to control the flags for an external application. The problem is that the flag must be passed as a comma-separated s

3条回答
  •  清酒与你
    2021-02-12 22:54

    First you create the two white-space separated lists, either using your method, or thiton's. Then you use the little trick from the end of section 6.2 of the GNU make manual to create a variable holding a single space, and one holding a comma. You can then use these in $(subst ...) to change the two lists to comma-separated.

    PARTS  := A B C
    
    BOOL_A := y
    BOOL_B := n
    BOOL_C := y
    
    WITH_LIST    := $(foreach part, $(PARTS), $(if $(filter y, $(BOOL_$(part))), $(part)))
    WITHOUT_LIST := $(filter-out $(WITH_LIST), $(PARTS))
    
    null  :=
    space := $(null) #
    comma := ,
    
    WITH_LIST    := $(subst $(space),$(comma),$(strip $(WITH_LIST)))
    WITHOUT_LIST := $(subst $(space),$(comma),$(strip $(WITHOUT_LIST)))
    
    all:
        ./app --with=$(WITH_LIST) --with-out=$(WITHOUT_LIST)
    

提交回复
热议问题