home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!spillman!tye
- From: tye@spillman.uucp (E. Tye McQueen)
- Subject: Re: Makefile parsing (challenge)
- Message-ID: <1993Jan11.165901.26885@spillman.uucp>
- Date: Mon, 11 Jan 1993 16:59:01 GMT
- References: <1993Jan9.215626.25646@uvaarpa.Virginia.EDU>
- Organization: Spillman Data Systems
- Lines: 63
-
- Kevin Burton <noran!iowa!kburton@uunet.uu.net> writes:
- )I want to extract certain variables defined in a makefile. If the makefile
- )looks like:
- )
- ) SOURCES= foo.c boo.c\
- ) tmp.c tmp1.c \
- ) a.c b.c c.c
- )
- )I want to end up with a single string of "foo.c boo.c tmp.c tmp1.c a.c b.c
- ) c.c".
- )I want to be able to specifie the string or expression for a string that
- )describes the variable that I am trying to extract.
-
- A subsequent post to get make to tell you should work more often
- then this. But it is still an interesting example (maybe you picked
- up a package with a makefile but you don't have make and you want to
- compile it by hand?).
-
- It is not too hard to correct this to differentiate "$$(VAR)" from
- "$(VAR)", but I leave this as an exercise to the reader. [Hint:
- it involves "(^|[^\$])(\$\$)*".]
-
- Interestingly, I couldn't find any way to get (at least one version
- of) make to "quote" a # or tailing \ so this example is the same as
- make in that respect.
-
- #!/usr/bin/perl
-
- if( ! @ARGV ) {
- die "Usage: $0 VAR1 [VAR2 [...]] <makefile\n";
- }
-
- while( <STDIN> ) {
- if( m!^\s*([a-zA-Z]\w*)\s*=! ) { # "VAR=VAL" line:
- $var= $1; # Save the variable name.
- $var{$var}= ""; # In case this redefines a variable.
- $_= $'; # At least the first part of the value.
- while( 1 ) {
- if( /#/ ) { # Comment on end of line:
- $var{$var} .= $`; # Just use part before comment.
- last; # Value can't continue onto next line.
- }
- $var{$var} .= $_; # Why isn't this an L-value?
- chop( $var{$var} ); # Remove the newline
- last unless m!\\$!; # Value is not continued onto next line.
- chop( $var{$var} ); # Remove the back slash
- $_= <STDIN>; # Get next line of value.
- }
- # Expand "$(var)" strings in value:
- while( $var{$var} =~ m!\$\(([a-zA-Z]\w*)\)! ) { # Wrong for `$$(val)'
- $var{$var}= $` . ( $var{$1} || $ENV{$1} ) . $';
- }
- }
- }
-
- for( @ARGV ) {
- print "$_=`$var{$_}'\n";
- }
-
- tye@spillman.com Tye McQueen, E.
- ----------------------------------------------------------
- Nothing is obvious unless you are overlooking something.
- ----------------------------------------------------------
-