How to define a variable with {} expansion

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to define a variable with {} expansion

Blake McBride
Greetings,

Please forgive the basic question.

If I want a variable set to:   abc/def/ghi.a abc/def/klm.a abc/def/opq.a

On other make systems, I would do:  abc/def/{ghi kln opq}.a

How can I do this in GNU Make?

Thank you!

Blake McBride
Reply | Threaded
Open this post in threaded view
|

Re: How to define a variable with {} expansion

Paul Smith-20
On Wed, 2021-01-27 at 12:10 -0600, Blake McBride wrote:
> If I want a variable set to:   abc/def/ghi.a abc/def/klm.a
> abc/def/opq.a
>
> On other make systems, I would do:  abc/def/{ghi kln opq}.a

I don't know of any make systems that support that syntax; definitely
it's not anything defined in the POSIX standard for make.

In GNU make you can do something like:

  ROOTS = jhi klm opq

  OUT = $(patsubst %,abc/def/%.a,$(ROOTS))

See https://www.gnu.org/software/make/manual/html_node/Text-Functions.html
for some other possibilities.


Reply | Threaded
Open this post in threaded view
|

Re: How to define a variable with {} expansion

Blake McBride
Thanks!  (It's in DMAKE)

On Wed, Jan 27, 2021 at 1:16 PM Paul Smith <[hidden email]> wrote:

> On Wed, 2021-01-27 at 12:10 -0600, Blake McBride wrote:
> > If I want a variable set to:   abc/def/ghi.a abc/def/klm.a
> > abc/def/opq.a
> >
> > On other make systems, I would do:  abc/def/{ghi kln opq}.a
>
> I don't know of any make systems that support that syntax; definitely
> it's not anything defined in the POSIX standard for make.
>
> In GNU make you can do something like:
>
>   ROOTS = jhi klm opq
>
>   OUT = $(patsubst %,abc/def/%.a,$(ROOTS))
>
> See https://www.gnu.org/software/make/manual/html_node/Text-Functions.html
> for some other possibilities.
>
>
Reply | Threaded
Open this post in threaded view
|

RE: How to define a variable with {} expansion

Cook, Malcolm-2
In reply to this post by Blake McBride
>Thanks!  That's exactly what I was looking for. 

Glad it worked.  I use shell assignment exactly for this convenient purpose myself.

> (Perhaps you should CC the list with this.)

Doh!  Of course! Done below:

>On Thu, Jan 28, 2021 at 12:50 AM Cook, Malcolm <mailto:[hidden email]> wrote:
>This is called “brace expansion” in bash manual.

>If you are using a newer version of Gnu Make, and Make’s .SHELL is set to /bin/bash (or another shell which supports brace expansion), you can use Make’s new-ish “shell assignment operator”, !=, like this

>                x!= echo abc/def/{ghi,kln,opq}.a

>(note use commas to separate alternatives to expand, not spaces)

>here’s a one-liner demo:

>make --eval 'x!=echo abc/def/{ghi,kln,opq}.a' --eval '$(info $x)'


>>From: Help-make <help-make-bounces+mec=mailto:[hidden email]> On Behalf Of Blake McBride
>>Sent: Wednesday, January 27, 2021 12:11
>>To: mailto:[hidden email]
>>Subject: How to define a variable with {} expansion
>> 
>>Greetings,
>>
>>Please forgive the basic question.
>>
>>If I want a variable set to: abc/def/ghi.a abc/def/klm.a abc/def/opq.a
>>
>>On other make systems, I would do: abc/def/{ghi kln opq}.a
>>
>>How can I do this in GNU Make?
>>
>>Thank you!
>>
>>Blake McBride

Reply | Threaded
Open this post in threaded view
|

Re: How to define a variable with {} expansion

Paul Smith-20
On Thu, 2021-01-28 at 17:43 +0000, Cook, Malcolm wrote:
> > Thanks!  That's exactly what I was looking for.

If it works for you that's good.

But note there are downsides to this; whether they matter to you is for
you to decide.

The first one is performance: you're invoking an entire shell process
just to do a little string manipulation.  If you only do this once per
build session obviously it's likely not a big deal.

The second one is you're relying on either (a) the underlying operating
system's /bin/sh being really bash (this is not true on, for example,
Debian and Ubuntu GNU/Linux systems), or (b) adding SHELL = /bin/bash
to your makefile which impacts all your recipes not just this.

And the third one is you need bash available in the first place, which
it may not even be on some systems, such as Windows for example.

Using make built-in string manipulation functions doesn't have any of
these concerns.

People should use what works best for them, of course, I'm just filling
in the blanks :)


> > On Thu, Jan 28, 2021 at 12:50 AM Cook, Malcolm <mailto:
> > [hidden email]> wrote:
> > This is called “brace expansion” in bash manual.
> >  
> > If you are using a newer version of Gnu Make, and Make’s .SHELL is
> > set to /bin/bash (or another shell which supports brace expansion),
> > you can use Make’s new-ish “shell assignment operator”, !=, like
> > this
> >  
> >                 x!= echo abc/def/{ghi,kln,opq}.a