Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use % twice in a patsubst

I'd like to generate two output words for each input word, that they be consecutive, and apply patsubst to them.

A non-working prototype of what I want:

$(patsubst %.o,%.a.o %.so.o,$(OBJ))

The second apearance of % is not substituted, and instead a plain % appears.

A working version that does not give the desired order:

$(patsubst %.o,%.a.o,$(OBJ)) $(patsubst %.o,%.so.o,$(OBJ))

Example:

Input:

foo.o bar.o

Desired output:

foo.a.o foo.so.o bar.a.o bar.so.o

How can I get the desired output?

like image 850
alx Avatar asked Oct 17 '25 06:10

alx


2 Answers

The join make function takes two word lists and concatenate them word by word. Not exactly what you want because there would be no space between foo.a.o and foo.so.o, but not far from it.

Assuming you know for sure that some string will never be part of your file names (e.g. !!!) you can combine substitution references (or the equivalent patsubst), join and subst:

$(subst !!!, ,$(join $(OBJ:o=a.o!!!),$(OBJ:o=so.o)))

Or, even simpler, if you know for sure that .a.o cannot be part of your files' basename:

$(subst .a.o,.a.o ,$(join $(OBJ:o=a.o),$(OBJ:o=so.o)))
like image 179
Renaud Pacalet Avatar answered Oct 21 '25 05:10

Renaud Pacalet


Two nested foreach loop could be solve your problem. One for the files and the other one for extensions. The below code should solve your problem.

OBJS = foo.o bar.o
EXTS = a.o so.o

OUT  = $(foreach file, ${OBJS}, \
         $(foreach ext, ${EXTS}, \
           $(patsubst %.o, %.${ext}, ${file})))
like image 22
kurtfu Avatar answered Oct 21 '25 03:10

kurtfu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!