You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/07/03 03:20:43 UTC

[lucy-commits] svn commit: r1142355 - /incubator/lucy/trunk/devel/bin/gen_charmonizer_makefiles.pl

Author: marvin
Date: Sun Jul  3 01:20:43 2011
New Revision: 1142355

URL: http://svn.apache.org/viewvc?rev=1142355&view=rev
Log:
Refactor gen_charmonizer_makefiles.pl using OO programming.  The output is
unchanged, for now.

Modified:
    incubator/lucy/trunk/devel/bin/gen_charmonizer_makefiles.pl

Modified: incubator/lucy/trunk/devel/bin/gen_charmonizer_makefiles.pl
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/devel/bin/gen_charmonizer_makefiles.pl?rev=1142355&r1=1142354&r2=1142355&view=diff
==============================================================================
--- incubator/lucy/trunk/devel/bin/gen_charmonizer_makefiles.pl (original)
+++ incubator/lucy/trunk/devel/bin/gen_charmonizer_makefiles.pl Sun Jul  3 01:20:43 2011
@@ -17,38 +17,69 @@
 
 use strict;
 use warnings;
-use File::Find qw( find );
-use FindBin;
 
--d "src" or die "Switch to the directory containg the charmonizer src/.\n";
+package Charmonizer::Build::Makefile;
+use File::Find qw();
+use FindBin;
+use Carp qw( confess );
+use Cwd qw( getcwd );
 
-my (@srcs, @tests, @hdrs);
-my $license = "";
+sub new {
+    my ( $class, %args ) = @_;
 
-sub wanted {
-    if (/\.c$/) {
-        if (/^Test/) {
-            push @tests, $File::Find::name;
-        }
-        else {
-            push @srcs, $File::Find::name;
-        }
-    }
-    elsif (/\.h$/) {
-        push @hdrs, $File::Find::name;
+    # Validate args, create object.
+    for (qw( dir filename )) {
+        $args{$_} or confess("Missing required param '$_'");
     }
+    my $dir = $args{dir};
+    my $self = bless { 
+        dir      => $dir,  
+        filename => $args{filename},
+    }, $class;
+
+    # Gather source paths, normalized for the target OS.
+    my $orig_dir = getcwd();
+    chdir($dir);
+    -d 'src' or confess("Can't find 'src' directory within '$dir'");
+    my ( @h_files, @c_files, @c_tests );
+    push @c_files, "charmonize.c";
+    File::Find::find( {
+        wanted => sub {
+            if (/\.c$/) {
+                if (/^Test/) {
+                    push @c_tests, $File::Find::name;
+                }
+                else {
+                    push @c_files, $File::Find::name;
+                }
+            }
+            elsif (/\.h$/) {
+                push @h_files, $File::Find::name;
+            }
+        },
+    }, 'src', );
+    chdir($orig_dir);
+    $self->{c_files} = [ sort $self->pathify(@c_files) ];
+    $self->{h_files} = [ sort $self->pathify(@h_files) ];
+    $self->{c_tests} = [ sort $self->pathify(@c_tests) ];
+
+    return $self;
 }
 
+sub pathify { confess "abstract method" }
+
 sub unixify {
+    my $self = shift;
     map { my $copy = $_; $copy =~ tr{\\}{/}; $copy } @_;
 }
 
 sub winnify {
+    my $self = shift;
     map { my $copy = $_; $copy =~ tr{/}{\\}; $copy } @_;
 }
 
 sub objectify {
-    my @objects = @_;
+    my ( $self, @objects ) = @_;
     for (@objects) {
         s/\.c$/\$(OBJEXT)/ or die "No match: $_";
     }
@@ -56,6 +87,7 @@ sub objectify {
 }
 
 sub test_execs {
+    my $self = shift;
     my @test_execs = grep { $_ !~ /Test\.c/ } @_; # skip Test.c entry
     for (@test_execs) {
         s/.*(Test\w+)\.c$/$1\$(EXEEXT)/ or die "no match: $_";
@@ -64,6 +96,7 @@ sub test_execs {
 }
 
 sub test_blocks {
+    my $self = shift;
     my @c_files = grep { $_ !~ /Test\.c/ } @_; # skip Test.c entry
     my @blocks;
     for my $c_file (@c_files) {
@@ -79,6 +112,8 @@ END_BLOCK
     return @blocks;
 }
 
+sub clean_target { confess "abstract method" }
+
 sub clean_target_posix {
     qq|clean:\n\trm -f \$(CLEANABLE)|;
 }
@@ -87,7 +122,87 @@ sub clean_target_win {
     qq|clean:\n\tCMD /c FOR %i IN (\$(CLEANABLE)) DO IF EXIST %i DEL /F %i|;
 }
 
-sub makefile_top_posix {
+sub gen_makefile {
+    my ( $self, %args ) = @_;
+    open my $fh, ">", $args{file} or die "open '$args{file}' failed: $!\n";
+    my $license = $self->license;
+    my $content = <<EOT;
+# GENERATED BY $FindBin::Script: do not hand-edit!!!
+#
+$license
+$args{top}
+PROGNAME= charmonize\$(EXEEXT)
+
+TESTS= $args{test_execs}
+
+OBJS= $args{objs}
+
+TEST_OBJS= $args{test_objs}
+
+HEADERS= $args{headers}
+
+CLEANABLE= \$(OBJS) \$(PROGNAME) \$(TEST_OBJS) \$(TESTS) *.pdb
+
+all: \$(PROGNAME)
+
+\$(PROGNAME): \$(OBJS)
+	\$(LINKER) \$(LINKFLAGS) \$(OBJS) \$(LINKOUT)"\$(PROGNAME)"
+
+\$(OBJS) \$(TEST_OBJS): \$(HEADERS)
+
+tests: \$(TESTS)
+
+$args{test_blocks}
+$args{clean_target}
+
+EOT
+    print $fh $content;
+}
+
+sub write_makefile {
+    my $self = shift;
+    my @objects      = $self->objectify( @{ $self->{c_files} } );
+    my @test_objects = $self->objectify( @{ $self->{c_tests} } );
+    my @test_execs   = $self->test_execs( @{ $self->{c_tests} } );
+    my @test_blocks  = $self->test_blocks( @{ $self->{c_tests} } );
+
+    $self->gen_makefile(
+        test_execs   => join(" ", $self->pathify(@test_execs)),
+        objs         => join(" ", $self->pathify(@objects)),
+        test_objs    => join(" ", $self->pathify(@test_objects)),
+        headers      => join(" ", $self->pathify( @{ $self->{h_files} } ) ),
+        test_blocks  => join("\n", $self->pathify(@test_blocks)),
+        top          => $self->top,
+        clean_target => $self->clean_target,
+        file         => $self->{filename},
+    );
+}
+
+sub license {
+    return <<'END_LICENSE';
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+END_LICENSE
+}
+
+package Charmonizer::Build::Makefile::Posix;
+BEGIN { our @ISA = qw( Charmonizer::Build::Makefile ) }
+
+sub new { shift->SUPER::new( filename => 'Makefile', @_ ) }
+
+sub top {
     return <<END_STUFF;
 CC= cc
 DEFS=
@@ -103,7 +218,15 @@ LINKOUT= -o
 END_STUFF
 }
 
-sub makefile_top_msvc {
+sub clean_target { shift->clean_target_posix }
+sub pathify      { shift->unixify(@_) }
+
+package Charmonizer::Build::Makefile::MSVC;
+BEGIN { our @ISA = qw( Charmonizer::Build::Makefile ) }
+
+sub new { shift->SUPER::new( filename => 'Makefile.MSVC', @_ ) }
+
+sub top {
     return <<END_STUFF;
 CC= cl
 DEFS=
@@ -119,7 +242,15 @@ LINKOUT= /OUT:
 END_STUFF
 }
 
-sub makefile_top_mingw {
+sub pathify      { shift->winnify(@_) }
+sub clean_target { shift->clean_target_win }
+
+package Charmonizer::Build::Makefile::MinGW;
+BEGIN { our @ISA = qw( Charmonizer::Build::Makefile ) }
+
+sub new { shift->SUPER::new( filename => 'Makefile.MinGW', @_ ) }
+
+sub top {
     return <<END_STUFF;
 CC= gcc
 DEFS=
@@ -135,90 +266,18 @@ LINKOUT= -o
 END_STUFF
 }
 
-sub gen_makefile {
-    my %args = @_;
-    open my $fh, ">", $args{file} or die "open '$args{file}' failed: $!\n";
-    my $content = <<EOT;
-# GENERATED BY $FindBin::Script: do not hand-edit!!!
-#
-$license
-$args{top}
-PROGNAME= charmonize\$(EXEEXT)
-
-TESTS= $args{test_execs}
-
-OBJS= $args{objs}
-
-TEST_OBJS= $args{test_objs}
-
-HEADERS= $args{headers}
-
-CLEANABLE= \$(OBJS) \$(PROGNAME) \$(TEST_OBJS) \$(TESTS) *.pdb
-
-all: \$(PROGNAME)
-
-\$(PROGNAME): \$(OBJS)
-	\$(LINKER) \$(LINKFLAGS) \$(OBJS) \$(LINKOUT)"\$(PROGNAME)"
-
-\$(OBJS) \$(TEST_OBJS): \$(HEADERS)
-
-tests: \$(TESTS)
-
-$args{test_blocks}
-$args{clean_target}
-
-EOT
-    print $fh $content;
-}
+sub pathify      { shift->winnify(@_) }
+sub clean_target { shift->clean_target_win }
 
 ### actual script follows
+package main;
 
-open my $fh, $0 or die "Can't open $0: $!\n";
-scalar <$fh>, scalar <$fh>; # skip first 2 lines
-while (<$fh>) {
-    /^#/ or last;
-    $license .= $_;
-}
-
-push @srcs, "charmonize.c";
-find \&wanted, "src";
-@srcs  = sort @srcs;
-@hdrs  = sort @hdrs;
-@tests = sort @tests;
-my @objects      = objectify(@srcs);
-my @test_objects = objectify(@tests);
-my @test_execs   = test_execs(@tests);
-my @test_blocks  = test_blocks(@tests);
-
-gen_makefile
-    test_execs   => join(" ", unixify(@test_execs)),
-    objs         => join(" ", unixify(@objects)),
-    test_objs    => join(" ", unixify(@test_objects)),
-    headers      => join(" ", unixify(@hdrs)),
-    test_blocks  => join("\n", unixify(@test_blocks)),
-    top          => makefile_top_posix(),
-    clean_target => clean_target_posix(),
-    file         => 'Makefile';
-
-gen_makefile
-    test_execs   => join(" ", winnify(@test_execs)),
-    objs         => join(" ", winnify(@objects)),
-    test_objs    => join(" ", winnify(@test_objects)),
-    headers      => join(" ", winnify(@hdrs)),
-    test_blocks  => join("\n", winnify(@test_blocks)),
-    top          => makefile_top_msvc(),
-    clean_target => clean_target_win(),
-    file         => 'Makefile.MSVC';
-
-gen_makefile
-    test_execs   => join(" ", winnify(@test_execs)),
-    objs         => join(" ", winnify(@objects)),
-    test_objs    => join(" ", winnify(@test_objects)),
-    headers      => join(" ", winnify(@hdrs)),
-    test_blocks  => join("\n", winnify(@test_blocks)),
-    top          => makefile_top_mingw(),
-    clean_target => clean_target_win(),
-    file         => 'Makefile.MinGW';
+my $makefile_posix = Charmonizer::Build::Makefile::Posix->new( dir => '.' );
+my $makefile_msvc  = Charmonizer::Build::Makefile::MSVC->new( dir => '.' );
+my $makefile_mingw = Charmonizer::Build::Makefile::MinGW->new( dir => '.' );
+$makefile_posix->write_makefile;
+$makefile_msvc->write_makefile;
+$makefile_mingw->write_makefile;
 
 __END__