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 2008/09/06 08:52:05 UTC

svn commit: r692618 - in /lucene/lucy/trunk: charmonizer/bin/ charmonizer/bin/metaquote charmonizer/metaquote_src/ perl/buildlib/Lucy/Build.pm

Author: marvin
Date: Fri Sep  5 23:52:04 2008
New Revision: 692618

URL: http://svn.apache.org/viewvc?rev=692618&view=rev
Log:
Replace C metaquote utility with more useful Perl script.

Added:
    lucene/lucy/trunk/charmonizer/bin/
    lucene/lucy/trunk/charmonizer/bin/metaquote   (with props)
Removed:
    lucene/lucy/trunk/charmonizer/metaquote_src/
Modified:
    lucene/lucy/trunk/perl/buildlib/Lucy/Build.pm

Added: lucene/lucy/trunk/charmonizer/bin/metaquote
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/charmonizer/bin/metaquote?rev=692618&view=auto
==============================================================================
--- lucene/lucy/trunk/charmonizer/bin/metaquote (added)
+++ lucene/lucy/trunk/charmonizer/bin/metaquote Fri Sep  5 23:52:04 2008
@@ -0,0 +1,154 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+use Getopt::Long;
+use File::Spec;
+use File::Find qw( find );
+use File::Path qw( mkpath );
+use File::stat;
+
+my %done;
+my ( $outdir, $charm_src );
+
+GetOptions(
+    'out=s' => \$outdir,
+    'src=s' => \$charm_src,
+);
+$charm_src ||= File::Spec->rel2abs('src');
+
+# Validate Charmonizer source dir.
+my $probe_path
+    = File::Spec->catfile( $charm_src, 'Charmonizer', 'Probe.harm' );
+die("Can't find Charmonizer source tree -- was 'src' specified correctly?")
+    unless -e $probe_path;
+
+# Verify/create output directory.
+die "Must specify output directory" unless defined $outdir;
+mkpath($outdir)                     unless -d $outdir;
+
+# Accumulate list of .harm/.charm files.
+my @charm_files;
+find(
+    {   wanted => sub {
+            my $name = $File::Find::name;
+            return unless $name =~ s/.*?(Charmonizer.+\.c?harm$)/$1/;
+            push @charm_files, $name;
+        },
+        no_chdir => 1,
+    },
+    $charm_src,
+);
+
+# Process all source files.
+process_file($_) for @charm_files;
+exit;
+
+# Transfer a Charmonizer source file to the destination, transforming it from
+# a .charm/.harm to a .c/.h and replacing all metaquotes.
+sub process_file {
+    my $rel_path = shift;
+    return if $done{$rel_path};
+
+    my $src_path = File::Spec->catfile( $charm_src, $rel_path );
+    my $content  = slurp_file($src_path);
+    $content = replace_metaquotes($content);
+    my $dest_path = File::Spec->catfile( $outdir, $rel_path );
+    $dest_path =~ s/(\.[ch])[^.]+$/$1/ or die "No match";
+
+    # Don't generate new file if current.
+    my $stat_orig = stat($src_path);
+    my $stat_dest = stat($dest_path);
+    return
+        if ($stat_orig
+        and $stat_dest
+        and $stat_dest->mtime > $stat_orig->mtime );
+
+    print "Writing $dest_path\n";
+    spit_file( $dest_path, $content );
+    $done{$rel_path} = 1;
+}
+
+sub replace_metaquotes {
+    my $content = shift;
+    while ( $content =~ m/METAQUOTE(.+?)METAQUOTE/s ) {
+        my $metaquoted = $1;
+        $metaquoted =~ s/\\/\\\\/g;
+        $metaquoted =~ s/"/\\"/g;
+        $metaquoted =~ s/\n/\\n"\n    "/mg;
+        $content    =~ s/METAQUOTE(.+?)METAQUOTE/"$metaquoted"/s
+            or die "no match";
+    }
+    return $content;
+}
+
+sub slurp_file {
+    my $path = shift;
+    open( my $fh, '<', $path ) or die "Can't open '$path': $!";
+    my $content = do { local $/; <$fh> };
+    close $fh or die "Can't close '$path': $!";
+    return $content;
+}
+
+sub spit_file {
+    my ( $path, $content ) = @_;
+    my ( undef, $dest_dir, undef ) = File::Spec->splitpath($path);
+    mkpath($dest_dir);
+    open( my $fh, '>', $path ) or die "Can't open '$path': $!";
+    binmode $fh;
+    print $fh $content;
+    close $fh or die "Can't close '$path': $!";
+}
+
+
+__END__
+
+=head1 NAME
+
+metaquote - Translate Charmonizer modules to C.
+
+=head1 SYNOPSIS
+
+   $ ./bin/metaquote --out=/path/to/output_charm_dir \
+   > --src=/path/to/charmonizer/src
+
+=head1 DESCRIPTION
+
+The C<metaquote> utility processes a directory of Charmonizer .harm/.charm
+source files, replacing METAQUOTE-delimited text with legal C double-quoted
+text and generating .h/.c files.
+
+=head1 ARGUMENTS
+
+=over
+
+=item --out=DEST_DIR
+
+The directory where the extracted .c/.h Charmonizer module files will be
+written.
+
+=item --src=SOURCE_DIR
+
+The Charmonizer source directory.
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+    /**
+     * Copyright 2008 The Apache Software Foundation
+     *
+     * Licensed 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.
+     */
+
+=cut

Propchange: lucene/lucy/trunk/charmonizer/bin/metaquote
------------------------------------------------------------------------------
    svn:executable = *

Modified: lucene/lucy/trunk/perl/buildlib/Lucy/Build.pm
URL: http://svn.apache.org/viewvc/lucene/lucy/trunk/perl/buildlib/Lucy/Build.pm?rev=692618&r1=692617&r2=692618&view=diff
==============================================================================
--- lucene/lucy/trunk/perl/buildlib/Lucy/Build.pm (original)
+++ lucene/lucy/trunk/perl/buildlib/Lucy/Build.pm Fri Sep  5 23:52:04 2008
@@ -38,15 +38,28 @@
 
 unshift @PATH, curdir();
 
-my $is_distro_not_devel = -e 'charmonizer';
-my $base_dir            = $is_distro_not_devel ? curdir() : updir();
+=for Rationale
 
-my $METAQUOTE_EXE_PATH     = 'metaquote' . $Config{_exe};
-my $CHARMONIZE_EXE_PATH    = 'charmonize' . $Config{_exe};
-my $CHARMONIZER_SOURCE_DIR = catdir( $base_dir, 'charmonizer', 'src' );
-my $FILTERED_DIR = catdir( $base_dir, qw( charmonizer filtered_src ) );
-my $C_SOURCE_DIR = catdir( $base_dir, 'c_src' );
-my $R_SOURCE_DIR = catdir( $C_SOURCE_DIR, 'r' );
+When the distribution tarball for the Perl binding of Lucy is built, c_src/,
+c_test/, and any other needed files/directories are copied into the perl/
+directory within the main Lucy directory.  Then the distro is built from the
+contents of the perl/ directory, leaving out all the files in ruby/, etc.
+However, during development, the files are accessed from their original
+locations.
+
+=cut
+
+my $is_distro_not_devel = -e 'c_src';
+my $base_dir = $is_distro_not_devel ? curdir() : updir();
+
+my $CHARMONIZE_EXE_PATH  = 'charmonize' . $Config{_exe};
+my $CHARMONIZER_ORIG_DIR = catdir( $base_dir, 'charmonizer' );
+my $CHARMONIZER_GEN_DIR  = catdir( $CHARMONIZER_ORIG_DIR, 'gen' );
+my $C_SOURCE_DIR         = catdir( $base_dir, 'c_src' );
+my $H_SOURCE_DIR         = catdir( $C_SOURCE_DIR, 'h' );
+my $XS_SOURCE_DIR        = 'xs';
+my $AUTOBIND_PM_PATH     = catfile(qw( lib KinoSearch Autobinding.pm ));
+my $AUTOBIND_XS_PATH     = catfile(qw( lib KinoSearch Autobinding.xs ));
 
 my $EXTRA_CCFLAGS = '';
 if ( $ENV{LUCY_DEBUG} ) {
@@ -55,46 +68,33 @@
 }
 my $VALGRIND = $ENV{CHARM_VALGRIND} ? "valgrind --leak-check=full " : "";
 
-# Compile the metaquote source filter utility.
+# Collect all relevant Charmonizer files.
 sub ACTION_metaquote {
-    my $self = shift;
-    my $source_path
-        = catfile( $base_dir, qw( charmonizer metaquote_src metaquote.c ) );
-
-    # don't compile if we're up to date
-    return if $self->up_to_date( [$source_path], $METAQUOTE_EXE_PATH );
-
-    # compile
-    print "\nBuilding $METAQUOTE_EXE_PATH...\n\n";
-    my $cbuilder = Lucy::Build::CBuilder->new;
-    my $o_file   = $cbuilder->compile(
-        source               => $source_path,
-        extra_compiler_flags => $EXTRA_CCFLAGS,
-    );
-    $cbuilder->link_executable(
-        objects  => [$o_file],
-        exe_file => $METAQUOTE_EXE_PATH,
-    );
-
-    # clean both the object file and the executable
-    $self->add_to_cleanup( $o_file, $METAQUOTE_EXE_PATH );
+    my $self          = shift;
+    my $charm_src_dir = catdir( $CHARMONIZER_ORIG_DIR, 'src' );
+    my $orig_files    = $self->rscan_dir( $charm_src_dir, qr/\.c?harm$/ );
+    my $dest_files    = $self->rscan_dir( $CHARMONIZER_GEN_DIR, qr/\.[ch]$/ );
+    push @$dest_files, $CHARMONIZER_GEN_DIR;
+    if ( !$self->up_to_date( $orig_files, $dest_files ) ) {
+        mkpath $CHARMONIZER_GEN_DIR unless -d $CHARMONIZER_GEN_DIR;
+        $self->add_to_cleanup($CHARMONIZER_GEN_DIR);
+        my $metaquote = catfile( $CHARMONIZER_ORIG_DIR, qw( bin metaquote ) );
+        my $command = "$^X $metaquote --src=$charm_src_dir "
+            . "--out=$CHARMONIZER_GEN_DIR";
+        system($command);
+    }
 }
 
 # Build the charmonize executable.
 sub ACTION_charmonizer {
     my $self = shift;
-
     $self->dispatch('metaquote');
 
-    # gather .charm and .harm files and run them through metaquote
-    if ( !-d $FILTERED_DIR ) {
-        mkpath($FILTERED_DIR) or die "can't mkpath '$FILTERED_DIR': $!";
-    }
-    my $charm_source_files = $self->rscan_dir( $CHARMONIZER_SOURCE_DIR,
-        sub { $File::Find::name =~ /\.c?harm$/ } );
-    my $filtered_files = $self->_metaquote_charm_files($charm_source_files);
-    my $charmonize_c   = catfile( $base_dir, qw( charmonizer charmonize.c ) );
-    my @all_source     = ( $charmonize_c, @$filtered_files );
+    # Gather .c and .h Charmonizer files.
+    my $charm_source_files
+        = $self->rscan_dir( $CHARMONIZER_GEN_DIR, qr/Charmonizer.+\.[ch]$/ );
+    my $charmonize_c = catfile( $CHARMONIZER_ORIG_DIR, 'charmonize.c' );
+    my @all_source = ( $charmonize_c, @$charm_source_files );
 
     # don't compile if we're up to date
     return if $self->up_to_date( \@all_source, $CHARMONIZE_EXE_PATH );
@@ -108,52 +108,23 @@
         next unless /\.c$/;
         next if m#Charmonizer/Test#;
         my $o_file = $cbuilder->object_file($_);
+        $self->add_to_cleanup($o_file);
         push @o_files, $o_file;
 
         next if $self->up_to_date( $_, $o_file );
 
         $cbuilder->compile(
             source               => $_,
-            include_dirs         => [$FILTERED_DIR],
+            include_dirs         => [$CHARMONIZER_GEN_DIR],
             extra_compiler_flags => $EXTRA_CCFLAGS,
         );
     }
 
+    $self->add_to_cleanup($CHARMONIZE_EXE_PATH);
     my $exe_path = $cbuilder->link_executable(
         objects  => \@o_files,
         exe_file => $CHARMONIZE_EXE_PATH,
     );
-
-    $self->add_to_cleanup( $FILTERED_DIR, @$filtered_files, @o_files,
-        $CHARMONIZE_EXE_PATH, );
-}
-
-sub _metaquote_charm_files {
-    my ( $self, $charm_files ) = @_;
-    my @filtered_files;
-
-    for my $source_path (@$charm_files) {
-        my $dest_path = $source_path;
-        $dest_path =~ s#(.*)src#$1filtered_src#;
-        $dest_path =~ s#\.charm#.c#;
-        $dest_path =~ s#\.harm#.h#;
-
-        push @filtered_files, $dest_path;
-
-        next if ( $self->up_to_date( $source_path, $dest_path ) );
-
-        # create directories if need be
-        my ( undef, $dir, undef ) = splitpath($dest_path);
-        if ( !-d $dir ) {
-            $self->add_to_cleanup($dir);
-            mkpath $dir or die "Couldn't mkpath $dir";
-        }
-
-        # run the metaquote filter
-        system( $METAQUOTE_EXE_PATH, $source_path, $dest_path );
-    }
-
-    return \@filtered_files;
 }
 
 # Run the charmonizer executable, creating the charmony.h file.
@@ -198,15 +169,16 @@
     $self->dispatch('charmony');
 
     # collect source files
-    my $source_path     = catfile( $base_dir, 'charmonizer', 'charm_test.c' );
-    my $exe_path        = "charm_test$Config{_exe}";
-    my $test_source_dir = catdir( $FILTERED_DIR, qw( Charmonizer Test ) );
-    my $source_files    = $self->rscan_dir( $FILTERED_DIR,
+    my $source_path = catfile( $base_dir, 'charmonizer', 'charm_test.c' );
+    my $exe_path = "charm_test$Config{_exe}";
+    my $test_source_dir
+        = catdir( $CHARMONIZER_GEN_DIR, qw( Charmonizer Test ) );
+    my $source_files = $self->rscan_dir( $CHARMONIZER_GEN_DIR,
         sub { $File::Find::name =~ m#Charmonizer/Test.*?\.c$# } );
     push @$source_files, $source_path;
 
     # collect include dirs
-    my @include_dirs = ( $FILTERED_DIR, curdir() );
+    my @include_dirs = ( $CHARMONIZER_GEN_DIR, curdir() );
 
     # add Windows supplements
     if ( $Config{osname} =~ /mswin/i ) {