Search code examples
makefilegnu-make

string array pattern in Makefiles


There's one pattern I like on bash scripts which commands have too complex options:

#!/bin/bash
PROGRAM_FLAGS=(
        --someoption # my comments for setting such option
        --otheropen=4 # my reasoning for using 4
        # --otheropen=5 # a commented out alternative for easy experimentation
)
PROGRAM_CALL="program ${PROGRAM_FLAGS[@]}"

Is there any way to use this pattern on Makefiles? Ideally in a portable way, but if not possible for gnumake?


Solution

  • It helps if you describe in text what it is you want: else only people who are versed in Bash array syntax will know what your example means.

    You can use this:

    PROGRAM_FLAGS += --someoption # my comments for setting such option
    PROGRAM_FLAGS += --otheropen=4 # my reasoning for using 4
    # PROGRAM_FLAGS += --otheropen=5 # a commented out alternative for easy experimentation
    
    PROGRAM_CALL = program ${PROGRAM_FLAGS}
    

    This is portable IF your version of make is conforming to the very latest POSIX standards. += was not available in earlier versions of POSIX. In those versions, there's no option except to create differently-named variables and add them all in one at a time.