automake: refactor pre-processing of makefile fragments
authorStefano Lattarini <stefano.lattarini@gmail.com>
Mon, 16 Apr 2012 12:59:53 +0000 (14:59 +0200)
committerStefano Lattarini <stefano.lattarini@gmail.com>
Sat, 21 Apr 2012 07:49:13 +0000 (09:49 +0200)
This change will provide the automake script with a new function that
reads in a Makefile fragment *without* performing Automake ad-hoc parsing,
but only the pre-processing step, i.e., removal of '##' comments and
substitution of tokens like '%SUBDIRS%', '%?LIBTOOL%' or '?GENENRIC?'.

This will very likely be useful for the work on Automake-NG.

This is a pure refactoring, with no intended functional or semantic
changes.

* automake.in (preprocess_file): New function, extracted ...
(make_paragraphs): ... from here.

Signed-off-by: Stefano Lattarini <stefano.lattarini@gmail.com>
automake.in

index 38e09dc..43a7ba6 100644 (file)
@@ -6938,13 +6938,13 @@ sub transform ($$)
     }
 }
 
-
-# @PARAGRAPHS
-# &make_paragraphs ($MAKEFILE, [%TRANSFORM])
-# ------------------------------------------
-# Load a $MAKEFILE, apply the %TRANSFORM, and return it as a list of
-# paragraphs.
-sub make_paragraphs ($%)
+# $TEXT
+# preprocess_file ($MAKEFILE, [%TRANSFORM])
+# -----------------------------------------
+# Load a $MAKEFILE, apply the %TRANSFORM, and return the result.
+# No extra parsing of post-processing is done (i.e., recognition of
+# rules declaration or of make variables definitions).
+sub preprocess_file ($%)
 {
   my ($file, %transform) = @_;
 
@@ -6977,13 +6977,9 @@ sub make_paragraphs ($%)
 
                 'LIBTOOL'      => !! var ('LIBTOOL'),
                 'NONLIBTOOL'   => 1,
-                'FIRST'        => ! $transformed_files{$file},
                %transform);
 
-  $transformed_files{$file} = 1;
-  $_ = $am_file_cache{$file};
-
-  if (! defined $_)
+  if (! defined ($_ = $am_file_cache{$file}))
     {
       verb "reading $file";
       # Swallow the whole file.
@@ -6993,11 +6989,10 @@ sub make_paragraphs ($%)
       $_ = $fc_file->getline;
       $/ = $saved_dollar_slash;
       $fc_file->close;
-
       # Remove ##-comments.
       # Besides we don't need more than two consecutive new-lines.
       s/(?:$IGNORE_PATTERN|(?<=\n\n)\n+)//gom;
-
+      # Remember the contents of the just-read file.
       $am_file_cache{$file} = $_;
     }
 
@@ -7011,8 +7006,22 @@ sub make_paragraphs ($%)
   # ####### and do not remove the latter.)
   s/^[ \t]*(?:##%)+.*\n//gm;
 
-  # Split at unescaped new lines.
-  my @lines = split (/(?<!\\)\n/, $_);
+  return $_;
+}
+
+
+# @PARAGRAPHS
+# &make_paragraphs ($MAKEFILE, [%TRANSFORM])
+# ------------------------------------------
+# Load a $MAKEFILE, apply the %TRANSFORM, and return it as a list of
+# paragraphs.
+sub make_paragraphs ($%)
+{
+  my ($file, %transform) = @_;
+  $transform{FIRST} = !$transformed_files{$file};
+  $transformed_files{$file} = 1;
+
+  my @lines = split /(?<!\\)\n/, preprocess_file ($file, %transform);
   my @res;
 
   while (defined ($_ = shift @lines))