unique filenames in Makefile

unique filenames in Makefile

Post by Dom Vectr » Thu, 28 Mar 2002 09:38:52



Hello everyone,

Can someone tell me how to create unique filenames from within a Makefile.

I know in shell you can do:

touch file.$$

which creates a file.2345 where 2345 is the PID.

But I can't seem to get that to work in a Makefile.

Thanks
Michael

 
 
 

unique filenames in Makefile

Post by Andreas K?h? » Thu, 28 Mar 2002 12:55:39


Submitted by "Dom Vectra" to comp.unix.programmer:

Quote:> Hello everyone,

> Can someone tell me how to create unique filenames from within a Makefile.

> I know in shell you can do:

> touch file.$$

> which creates a file.2345 where 2345 is the PID.

> But I can't seem to get that to work in a Makefile.

> Thanks
> Michael

That's becayse there is no $$ variable in Make, try accessing
the shell variable $$ instead: $$$$

--
Andreas K?h?ri
----------------------------------------------------------------
NetBSD: Because you're worth it.    http://www.netbsd.org/

 
 
 

unique filenames in Makefile

Post by Barry Margoli » Fri, 29 Mar 2002 03:55:50




>Submitted by "Dom Vectra" to comp.unix.programmer:
>> Hello everyone,

>> Can someone tell me how to create unique filenames from within a Makefile.

>> I know in shell you can do:

>> touch file.$$

>> which creates a file.2345 where 2345 is the PID.

>> But I can't seem to get that to work in a Makefile.

>That's becayse there is no $$ variable in Make, try accessing
>the shell variable $$ instead: $$$$

If you do this, make sure you put all the commands that need to access the
file in the same shell invocation; otherwise, they'll each have different
values of $$.

BAD:

target: dependency
  touch file.$$$$
  ...
  rm file.$$$$

That won't work because each line causes a separate shell process to be run
to execute the command.

GOOD:

target: dependency
  touch file.$$$$ ; \
  ... ; \
  rm file.$$$$

The backslashes cause all the lines to be appended, and they'll all be
executed in the same shell process (it's the same reason why you need to do
this if your shell command contains if-then).

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

unique filenames in Makefile

Post by Andreas K?h? » Fri, 29 Mar 2002 06:39:48


Submitted by "Barry Margolin" to comp.unix.programmer:



>>Submitted by "Dom Vectra" to comp.unix.programmer:
>>> Hello everyone,

>>> Can someone tell me how to create unique filenames from within a Makefile.

>>> I know in shell you can do:

>>> touch file.$$

>>> which creates a file.2345 where 2345 is the PID.

>>> But I can't seem to get that to work in a Makefile.

>>That's becayse there is no $$ variable in Make, try accessing
>>the shell variable $$ instead: $$$$

> If you do this, make sure you put all the commands that need to access the
> file in the same shell invocation; otherwise, they'll each have different
> values of $$.

> BAD:

> target: dependency
>   touch file.$$$$
>   ...
>   rm file.$$$$

> That won't work because each line causes a separate shell process to be run
> to execute the command.

> GOOD:

> target: dependency
>   touch file.$$$$ ; \
>   ... ; \
>   rm file.$$$$

> The backslashes cause all the lines to be appended, and they'll all be
> executed in the same shell process (it's the same reason why you need to do
> this if your shell command contains if-then).

Another way to do it is to declare a variable, like so:

THEFILE =   thefile.$$$$

and then use it:

targer: dep
        touch $(THEFILE)
        ...
        rm $(THEFILE)

Works ok in most situations.

--
Andreas K?h?ri
----------------------------------------------------------------
NetBSD: Because you're worth it.    http://www.netbsd.org/

 
 
 

unique filenames in Makefile

Post by Paul D. Smit » Fri, 29 Mar 2002 08:16:12



  ak> Another way to do it is to declare a variable, like so:

  ak> THEFILE        =   thefile.$$$$

  ak> and then use it:

  ak> targer:        dep
  ak>        touch $(THEFILE)
  ak>        ...
  ak>        rm $(THEFILE)

  ak> Works ok in most situations.

No.  This will break the same way as Barry's BAD example.

Make does simple text substitution for variables, so there is _NO_
difference in behavior between your version and Barry's BAD example.

--
-------------------------------------------------------------------------------

 http://www.gnu.org                      http://www.paulandlesley.org/gmake/
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist

 
 
 

unique filenames in Makefile

Post by Jens.Toerr.. » Fri, 29 Mar 2002 09:08:50




>   ak> Another way to do it is to declare a variable, like so:
>   ak> THEFILE   =   thefile.$$$$
>   ak> and then use it:
>   ak> targer:   dep
>   ak>   touch $(THEFILE)
>   ak>   ...
>   ak>   rm $(THEFILE)
>   ak> Works ok in most situations.
> No.  This will break the same way as Barry's BAD example.
> Make does simple text substitution for variables, so there is _NO_
> difference in behavior between your version and Barry's BAD example.

Unless AFAIK if you use (at least for GNU make, I haven't dealt with too
many others)

THEFILE := thefile.$$$$

(note the colon before the '=').

Just a bit of nitpicking: Since PIDs get receycled (and as far as I heard
on some systems not only when the numbers "wrap around" but in a random
fashion for security reasons) just using the PID does not necessarily
guarantee that the file name you get this way is really unique... On some
systems there is the mktemp(1) command that is supposed to give you
a truly unique name.
                                              Regards, Jens
--
      _  _____  _____

  _  | |  | |    | |          AG Moebius, Institut fuer Molekuelphysik
 | |_| |  | |    | |          Fachbereich Physik, Freie Universitaet Berlin
  \___/ens|_|homs|_|oerring   Tel: ++49 (0)30 838 - 53394 / FAX: - 56046

 
 
 

unique filenames in Makefile

Post by Paul D. Smit » Fri, 29 Mar 2002 23:29:55




  ak> THEFILE        =   thefile.$$$$

  ak> targer:        dep
  ak> touch $(THEFILE)
  ak> ...
  ak> rm $(THEFILE)

  ak> Works ok in most situations.

  >> No.  This will break the same way as Barry's BAD example.

  >> Make does simple text substitution for variables, so there is _NO_
  >> difference in behavior between your version and Barry's BAD example.

  jt> Unless AFAIK if you use (at least for GNU make, I haven't dealt
  jt> with too many others)

  jt> THEFILE := thefile.$$$$

No.  This will not help.

Yes, this will resolve the value immediately rather than later, but
remember it's the _shell_ that is evaluating "$$", not make!  So
$(THEFILE) will contain the string thefile.$$ instead of thefile.$$$$,
but still every line in the script is run in a different shell and the
expansion of $$ will yield a different number on the two different lines
above.

You _MUST_ use backslashes to ensure that all the lines are invoked
inside the same shell or this will _not_ work.

--
-------------------------------------------------------------------------------

 http://www.gnu.org                      http://www.paulandlesley.org/gmake/
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist

 
 
 

unique filenames in Makefile

Post by Andy Isaacs » Sat, 30 Mar 2002 01:18:35





>  jt> Unless AFAIK if you use (at least for GNU make, I haven't dealt
>  jt> with too many others)

>  jt> THEFILE := thefile.$$$$

>No.  This will not help.

>Yes, this will resolve the value immediately rather than later, but
>remember it's the _shell_ that is evaluating "$$", not make!  So
>$(THEFILE) will contain the string thefile.$$ instead of thefile.$$$$,
>but still every line in the script is run in a different shell and the
>expansion of $$ will yield a different number on the two different lines
>above.

>You _MUST_ use backslashes to ensure that all the lines are invoked
>inside the same shell or this will _not_ work.

If this is not possible, you can use GNU make features to make a more
useful temporary file, assuming you have mktemp(1):

TMP := $(shell mktemp /tmp/foo.XXXXXX)
all: foo bar
foo:
        ls -l $(TMP)
bar:
        ls -l $(TMP)

mktemp(1) has the advantage of actually creating the file in a safe
manner; the "$$" examples that were given earlier in this thread can
be exploited by malicious users to cause the script to overwrite
files.

-andy