From: Andrew Gaffney [mailto:[EMAIL PROTECTED] Sent: Saturday, 21 February 2004 4:53 PM
To: beginners
Subject: parsing Makefiles
I'm looking to write a script that will parse a toplevel Makefile in a source tree and then descend into all the directories and parse those Makefiles in order to determine how many 'make' operations will actually be launched. At the moment, I am just playing around,
May I suggest, in a helpful way, that this is a rather silly thing to be doing. As such its just the sort of thing I love to do.
Suggestions: 1) Check out the perl 'Make' module which implements the make command. You may be able to perform a full 'make -n' using it and count the actions.
2) Rather than implement:
a) Include files
b) Variable expansion
c) Implicit rules and suffixes etc
etc
Consider launching 'make -p' and parsing the database dump,
or even 'make -n -d' and parsing the diagnostics (rough guess
can be the count of 'Must remake ...' lines in the output.)
Depending on your version of Make, '-n' may or may not recurse (gnu make ignores the '-n' for commands which are '$(MAKE) -f <newmakefile>' but passes the '-n' to the sub-make to make sure nothing gets built) If it doesn't, you may still have to perform the recursion manually while parsing the parent makefile, because each subdirectory Makefile may depend on arguments and ENVIRONMENT passed from the parent.
HTH
Have fun with that. Send some code to the list when it works!
I've come up with some *simple* code that seems to work:
#!/usr/bin/perl
use strict; use warnings;
$| = 1; my $total = `make -n | wc -l`; my ($count, $line); open MAKE, "make |"; foreach $line (<MAKE>) { $count++; my $percent = int(($count / $total) * 100); print "..$percent"; }
Although, for some reason, even with the '$| = 1;', it waits until its done to print everything. Anyone know why? I need to add a "scrollback buffer" of sorts so that it can display 15 or so lines of errors if 'make' returns an error code. What is the best way to do that?
-- Andrew Gaffney Network Administrator Skyline Aeronautics, LLC. 636-357-1548
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>