root/lang/perl/Alien-MeCab/trunk/Makefile.PL

Revision 9850, 6.9 kB (checked in by daisuke, 9 months ago)

Optionally depend on Alien::Iconv

  • Property svn:keywords set to Id
Line 
1#!perl
2use strict;
3use Cwd ();
4use inc::Devel::CheckLib;
5use ExtUtils::MakeMaker;
6use File::Spec;
7
8my $CURDIR = Cwd::cwd();
9
10# Here's the file that we're going to use to extract some data
11my $SPEC_FILE       = 'lib/Alien/MeCab.pm';
12
13# Check for hellish-ness
14my $RUNNING_IN_HELL = $^O eq 'MSWin32';
15
16# Get the version
17my $DIST_VERSION  = do {
18    require ExtUtils::MM_Unix;
19    ExtUtils::MM_Unix->parse_version($SPEC_FILE);
20};
21
22# Actual mecab version. This is the number up to the second fractional digit
23# of the dist version
24my $MECAB_VERSION    = substr($DIST_VERSION, 0, 4);
25
26# Filenames
27my $MECAB_SOURCE_DIR = File::Spec->catfile("src", "mecab-$MECAB_VERSION");
28my $MECAB_BASENAME   = "mecab-$MECAB_VERSION.tar.gz";
29my $MECAB_SOURCE     = File::Spec->catfile("src", $MECAB_BASENAME);
30my $MECAB_EXE        = File::Spec->catfile("src", "mecab-$MECAB_VERSION.exe");
31
32# Absolut-ize all paths
33$MECAB_SOURCE_DIR    = File::Spec->rel2abs($MECAB_SOURCE_DIR);
34$MECAB_SOURCE        = File::Spec->rel2abs($MECAB_SOURCE);
35$MECAB_EXE           = File::Spec->rel2abs($MECAB_EXE);
36
37my %REQUIRES;
38
39# Construct the necessary flags
40my $CCFLAGS = $ENV{CCFLAGS};
41my $LDFLAGS = $ENV{LDFLAGS};
42if (! $RUNNING_IN_HELL) {
43    $CCFLAGS ||= '-I/usr/local/include';
44    $LDFLAGS ||= '-L/usr/local/lib';
45}
46
47if (! $RUNNING_IN_HELL) {
48    eval {
49        Devel::CheckLib::assert_lib(lib => "iconv", LIBS => $LDFLAGS);
50    };
51    if ($@) {
52        print <<EOM;
53
54*** Whoa! libiconv was not detected! ***
55
56MeCab requires libiconv to work properly. You can either
57    (1) install it yourself by hand, or
58    (2) install it via Alien::Iconv
59EOM
60
61        my $yn = prompt("Would you like to add Alien::Iconv to prerequisite modules?", "y");
62        if ($yn !~ /^y(?:es)?$/) {
63            print <<EOM;
64
65You chose not to install libiconv. Please install it manually and
66re-run perl Makefile.PL for Alien::MeCab.
67
68EOM
69            exit 0;
70        }
71
72        $REQUIRES{ 'Alien::MeCab' } = '1.12001';
73    }
74}
75
76eval {
77    Devel::CheckLib::assert_lib(lib => "mecab", LIBS => $LDFLAGS )
78};
79if (! $@) {
80    print <<EOM;
81
82*** Whoa!  We've detected a previous installtion of libmecab ***
83
84Because Alien::MeCab may have been called to be installed from a dependency
85of another module, we want to make sure that you *really* want to install
86this version of Alien::MeCab (and therefore libmecab).
87
88If you answer "y", then We're going to install
89    libmecab: $MECAB_VERSION
90
91This operation may OVERWRITE your previous installation
92EOM
93    my $yn = prompt("Really install?", "n");
94    if ($yn !~ /^y(?:es)?$/) {
95        exit 0;
96    }
97}
98
99# Ask if we want to download the source.
100my $sourcefile = $RUNNING_IN_HELL ? $MECAB_EXE : $MECAB_SOURCE;
101if (! -f $sourcefile) {
102    my $yn = prompt("mecab source file $sourcefile does not exist. Download it now?", "y");
103    if ($yn =~ /^y(?:es)?$/i) {
104        my @cmd = ($^X, File::Spec->catfile("src", "fetchsrc.pl"), "--version", $MECAB_VERSION);
105        system(@cmd);
106    }
107}
108
109# If the source hasn't been expanded, then unpack it
110if ($^O ne 'MSWin32') {
111    if (! -d $MECAB_SOURCE_DIR) {
112        my $yn = prompt("Mecab source directory has not been unpacked yet. Unpack it now?", "y");
113        if ($yn =~ /^y(?:es)?$/i) {
114            eval {
115                require Archive::Tar;
116                Archive::Tar->can_handle_compressed_files or die "No compression support :(";
117            };
118            if ($@) {
119                print STDERR "Archive extraction requires Archive::Tar (with IO::Zlib)\n";
120                exit 0;
121            }
122
123            eval {
124                chdir File::Spec->catfile($CURDIR, 'src');
125
126                print "Unpacking... (please be patient)\n";
127                Archive::Tar->extract_archive( $MECAB_BASENAME, 1 );
128            };
129            if ($@) {
130                print STDERR "Failed to gunzip file $MECAB_SOURCE $IO::Compress::Gunzip::GunzipError\n";
131                chdir $CURDIR;
132                exit 0;
133            }
134            chdir $CURDIR;
135        }
136    }
137
138    {
139        print "\n";
140        my $run_configure;
141        if( -e File::Spec->catfile($MECAB_SOURCE_DIR, 'config.status')) {
142            $run_configure = prompt(
143                "Looks like MeCab has already been configured.\n".
144                "Do you want to re-run configure?",
145                "n"
146            );
147        } else {
148            $run_configure = prompt(
149                "No config.status found. Run MeCab's configure now?", 'y'
150            );
151        }
152
153        if( $run_configure =~ /^y/i ) {
154            print "\nWe're going to run configure for mecab.\n",
155                "First, we'll ask you a few questions about common options\n\n";
156
157            my $prefix = prompt( "Where would you like to install libmecab?", "/usr/local" );
158            my $charset = prompt( "What charset would you like to use?", "utf8" );
159            my $configure_args = '';
160            $configure_args .= "--prefix=$prefix " if $prefix;
161            $configure_args .= "--with-charset=$charset " if $charset;
162
163            $configure_args .= prompt("Are there any other arguments you would like to pass to configure?" );
164
165            print "\nMeCab will be configured with the following arguments:\n",
166                "  $configure_args\n";
167
168            chdir $MECAB_SOURCE_DIR;
169
170            local $ENV{CFLAGS}  = $CCFLAGS;
171            local $ENV{LDFLAGS} = $LDFLAGS;
172            my @cmd = (File::Spec->catfile($MECAB_SOURCE_DIR, "configure"),
173                split(/\s+/, $configure_args));
174            if (system(@cmd) != 0) {
175                print <<"END";
176configure $configure_args failed: $!
177    Something went wrong with the MeCab configuration.
178    You should correct it and re-run Makefile.PL.
179END
180                chdir $CURDIR;
181                exit 0;
182            }
183            chdir $CURDIR;
184        }
185    }
186}
187
188print <<EOM;
189Going to use the following information:
190    DIST_VERSION: $DIST_VERSION
191    MECAB_VERSION: $MECAB_VERSION
192    CCFLAGS: $CCFLAGS
193    LDFLAGS: $LDFLAGS
194EOM
195
196WriteMakefile(
197    CCFLAGS => $CCFLAGS,
198    LDFLAGS => $LDFLAGS,
199    NAME => 'Alien-MeCab',
200    VERSION => $DIST_VERSION,
201    PREREQ_PM => \%REQUIRES,
202);
203
204print "Now you should type 'make'\n";
205
206package MY;
207sub top_targets
208{
209    my $inherited = shift->SUPER::top_targets(@_);
210    $inherited =~ s/^all :: /all :: libmecab /;
211    return $inherited;
212}
213
214sub constants
215{
216    my $inherited = shift->SUPER::constants(@_);
217    $inherited .= "MEACB_VERSION=$MECAB_VERSION\nMECAB_SRC=src/mecab-$MECAB_VERSION\n";
218    return $inherited;
219}
220
221sub postamble {
222    my $make_str;
223
224        if ($RUNNING_IN_HELL) {
225        $make_str = <<MAKE_FRAG;
226libmecab:
227        $MECAB_EXE
228
229MAKE_FRAG
230    } else {
231        $make_str = <<'MAKE_FRAG';
232libmecab:
233        cd $(MECAB_SRC) && $(MAKE) all
234
235MAKE_FRAG
236    }
237
238    $make_str .= <<'MAKE_FRAG';
239fetchsrc:
240        $(PERL) fetchsrc.pl
241
242MAKE_FRAG
243
244    return $make_str;
245}
246
247
248#sub metafile_target {
249#    my $inherited = shift->SUPER::metafile_target(@_);
250#    my $build_requires = <<EOM;
251#build_requires:
252#    Cwd: 0
253#    File::Spec: 0
254#EOM
255#    $inherited =~ s/meta-spec:/${build_requires}meta-spec/;
256#    return $inherited;
257#}
258
259__END__
Note: See TracBrowser for help on using the browser.