[svn:parrot] r43495 - in branches/one_make: config/gen/makefiles tools/build

cotto at svn.parrot.org cotto at svn.parrot.org
Wed Jan 20 07:09:52 UTC 2010


Author: cotto
Date: Wed Jan 20 07:09:52 2010
New Revision: 43495
URL: https://trac.parrot.org/parrot/changeset/43495

Log:
[h2pasm] make h2pasm smart enough to write exactly one output file per invocation and start using it that way

Modified:
   branches/one_make/config/gen/makefiles/root.in
   branches/one_make/tools/build/h2pasm.pl

Modified: branches/one_make/config/gen/makefiles/root.in
==============================================================================
--- branches/one_make/config/gen/makefiles/root.in	Wed Jan 20 07:00:41 2010	(r43494)
+++ branches/one_make/config/gen/makefiles/root.in	Wed Jan 20 07:09:52 2010	(r43495)
@@ -1010,17 +1010,17 @@
 ###############################################################################
 
 runtime/parrot/include/cclass.pasm : $(INC_DIR)/cclass.h tools/build/h2pasm.pl
-	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/cclass.h > $@
+	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/cclass.h $@
 runtime/parrot/include/datatypes.pasm : $(INC_DIR)/datatypes.h tools/build/h2pasm.pl
-	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/datatypes.h > $@
+	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/datatypes.h $@
 runtime/parrot/include/enums.pasm : $(INC_DIR)/enums.h tools/build/h2pasm.pl
-	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/enums.h > $@
+	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/enums.h $@
 runtime/parrot/include/events.pasm : $(INC_DIR)/events.h tools/build/h2pasm.pl
-	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/events.h > $@
+	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/events.h $@
 runtime/parrot/include/hash_key_type.pasm : $(INC_DIR)/hash.h tools/build/h2pasm.pl
-	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/hash.h > $@
+	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/hash.h $@
 runtime/parrot/include/io.pasm : $(INC_DIR)/io.h tools/build/h2pasm.pl
-	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/io.h > $@
+	$(PERL) tools/build/h2pasm.pl $(INC_DIR)/io.h $@
 
 $(INC_DIR)/oplib/ops.h lib/Parrot/OpLib/core.pm : $(OPS_FILES) $(BUILD_TOOLS_DIR)/ops2pm.pl \
     lib/Parrot/OpsFile.pm lib/Parrot/Op.pm src/ops/ops.num src/ops/ops.skip

Modified: branches/one_make/tools/build/h2pasm.pl
==============================================================================
--- branches/one_make/tools/build/h2pasm.pl	Wed Jan 20 07:00:41 2010	(r43494)
+++ branches/one_make/tools/build/h2pasm.pl	Wed Jan 20 07:09:52 2010	(r43495)
@@ -14,19 +14,30 @@
 use strict;
 use warnings;
 
-my $usage = "Usage: $0 <input_file> > <output file>\n";
+my $usage = "Usage: $0 <input_file> <output_file>\n";
 
 my $in_file  = shift or die $usage;
+my $out_file = shift or die $usage;
 die $usage if @ARGV;
 
-open my $fh, '<', $in_file or die "Can't open $in_file: $!\n";
-my @directives = parse_file($in_file, $fh);
-close $fh;
-for my $d (@directives) {
-    my @defs = perform_directive($d);
-    for my $target ( @{ $d->{files} } ) {
-        my $gen = join "\n", &const_to_parrot(@defs);
-        print <<"EOF";
+open my $in_fh, '<', $in_file or die "Can't open $in_file: $!\n";
+my $directive = parse_file($in_file, $in_fh, $out_file);
+close $in_fh;
+die "invalid output file: '$out_file' for input '$in_file'" unless $directive;
+
+my @defs = perform_directive($directive);
+my $target  = $directive->{file};
+my $gen;
+if ($target =~ /\.pm$/) {
+    $gen = join "\n", &const_to_perl(@defs);
+    $gen .= "1;\n";
+}
+else {
+    $gen = join "\n", &const_to_parrot(@defs);
+}
+
+open my $out_fh, '>', $out_file or die "Can't open $out_file: $!\n";
+print $out_fh <<"EOF";
 # DO NOT EDIT THIS FILE.
 #
 # This file is generated automatically from
@@ -36,26 +47,21 @@
 #
 $gen
 EOF
-    }
-}
+close $out_fh;
 
 sub const_to_parrot {
-    my $keylen = 0;
-    foreach my $element (@_) {
-        my $size = length($element->[0]);
-        if ($size > $keylen) {
-            $keylen = $size;
-        }
-    }
-    my $vallen = 0;
-    foreach my $element (@_) {
-        my $size = length($element->[1]);
-        if ($size > $vallen) {
-            $vallen = $size;
-        }
-    }
 
-    return map {sprintf ".macro_const %-${keylen}s %${vallen}s", $_->[0], $_->[1]} @_;
+    my $keylen = (sort { $a <=> $b } map { length($_->[0]) } @_ )[-1] ;
+    my $vallen = (sort { $a <=> $b } map { length($_->[1]) } @_ )[-1] ;
+
+    map {sprintf ".macro_const %-${keylen}s %${vallen}s", $_->[0], $_->[1]} @_;
+}
+
+sub const_to_perl {
+
+    my $keylen = (sort { $a <=> $b } map { length($_->[0]) } @_ )[-1] ;
+
+    map {sprintf "use constant %-${keylen}s => %s;", $_->[0], $_->[1]} @_;
 }
 
 sub transform_name {
@@ -81,9 +87,9 @@
 }
 
 sub parse_file {
-    my ( $file, $fh ) = @_;
+    my ( $in_file, $fh, $out_file) = @_;
 
-    my ( @d, %values, $last_val, $cur, $or_continues );
+    my ( @directives, %values, $last_val, $cur, $or_continues );
     while ( my $line = <$fh> ) {
         if (
             $line =~ m!
@@ -93,18 +99,22 @@
             !x
             )
         {
-            $cur and die "Missing '&end_gen' in $file\n";
+            $cur and die "Missing '&end_gen' in $in_file\n";
+            my $file;
+            foreach (split ' ', $2) {
+                $file = $_ if $out_file =~ /$_$/;
+            }
             $cur = {
                 type   => $1,
-                files  => [ split ' ', $2 ],
+                file   => $file,
                 prefix => defined $3 ? $3 : '',
                 defined $4 ? ( subst => $4 ) : (),
             };
             $last_val = -1;
         }
         elsif ( $line =~ /&end_gen\b/ ) {
-            $cur or die "Missing &gen_from_(enum|def) in $file\n";
-            push @d, $cur;
+            $cur or die "Missing &gen_from_(enum|def) in $in_file\n";
+            return $cur if defined $cur->{file};
             $cur = undef;
         }
 
@@ -170,9 +180,9 @@
             }
         }
     }
-    $cur and die "Missing '&end_gen' in $file\n";
+    $cur and die "Missing '&end_gen' in $in_file\n";
 
-    return @d;
+    return undef;
 }
 
 1;


More information about the parrot-commits mailing list