0

I would like do something like following. I would like to have a variable argument list for a Makefile.

make VAR_ARG_LIST=src1,src2,src3,src4

Can I do like this? If I can, how do I extract src1,src2 or src3 from the variable VAR_ARG_LIST inside the Makefile?

Thanks,

user1293997
  • 855
  • 5
  • 12
  • 20

2 Answers2

1

If you want a list of targets in a macro for make to use, use blanks to separate them (and quotes to enclose them) on the command line:

make VAR_ARG_LIST="src1 src2 src3 src4"

This can be used inside the makefile without much trouble at all:

PROGRAMS = ${VAR_ARG_LIST}

all:  ${PROGRAMS}

and it will go off and create the programs src1, ... src4 from the rest of the rules in the makefile.

If that isn't roughly what you're after, then you need to clarify your question.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

You really haven't provided enough information for a solution. Why do you want to extract those values? What do you want to do with them?

However, I can answer the question you asked and hope it is useful. If you're using GNU make you can do this:

COMMA := ,
VAR_ARG_LIST_A := $(subst $(COMMA), ,$(VAR_ARG_LIST))
VAR_ARG_LIST_1 := $(word 1,$(VAR_ARG_LIST_A))
VAR_ARG_LIST_2 := $(word 2,$(VAR_ARG_LIST_A))

etc.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Actually I wanted to define macros based on the variable argument list. For example, if "src1" is found in the VAR_ARG_LIST I would like to define the switch #SRC1 inside the makefile. If not SRC1 is not defined. Your answer seem to close enough for me. Let me try that. If I'm able to extract comma separated strings from VAR_ARG_LIST, I can achieve it. – user1293997 Apr 01 '12 at 18:40
  • I don't quite know what `define the switch #SRC1 inside the makefile` means, but if you're just trying to see whether a word exists in a comma-separated list you can use `$(filter src1,$(subst $(COMMA), ,$(VAR_ARG_LIST)))` which will be `src1` if it's present or empty if not. – MadScientist Apr 02 '12 at 04:26