1use 5.7.2;
2use strict;
3use ExtUtils::MakeMaker;
4use strict;
5
6my %tables = (
7          euc_jp_t   => ['euc-jp.ucm'],
8              sjis_t     => ['shiftjis.ucm',
9                 'macJapanese.ucm',
10                 'cp932.ucm'],
11          raw_t      => [
12                 qw(jis0201.ucm jis0208.ucm jis0212.ucm)
13                 ],
14             );
15
16unless ($ENV{AGGREGATE_TABLES}){
17    my @ucm;
18    for my $k (keys %tables){
19    push @ucm, @{$tables{$k}};
20    }
21    %tables = ();
22    my $seq = 0;
23    for my $ucm (sort @ucm){
24    # 8.3 compliance !
25    my $t = sprintf ("%s_%02d_t", substr($ucm, 0, 2), $seq++);
26    $tables{$t} = [ $ucm ];
27    }
28}
29
30my $name = 'JP';
31
32WriteMakefile(
33              INC		=> "-I../Encode",
34          NAME		=> 'Encode::'.$name,
35          VERSION_FROM	=> "$name.pm",
36          OBJECT		=> '$(O_FILES)',
37          'dist'		=> {
38          COMPRESS	=> 'gzip -9f',
39          SUFFIX	=> 'gz',
40          DIST_DEFAULT => 'all tardist',
41          },
42          MAN3PODS	=> {},
43          # OS 390 winges about line numbers > 64K ???
44          XSOPT => '-nolinenumbers',
45          );
46
47package MY;
48
49sub post_initialize
50{
51    my ($self) = @_;
52    my %o;
53    my $x = $self->{'OBJ_EXT'};
54    # Add the table O_FILES
55    foreach my $e (keys %tables)
56    {
57    $o{$e.$x} = 1;
58    }
59    $o{"$name$x"} = 1;
60    $self->{'O_FILES'} = [sort keys %o];
61    my @files = ("$name.xs");
62    $self->{'C'} = ["$name.c"];
63    $self->{SOURCE} .= " $name.c"
64        if $^O eq 'MacOS' && $self->{SOURCE} !~ /\b$name\.c\b/;
65    $self->{'H'} = [$self->catfile($self->updir,'Encode', 'encode.h')];
66    my %xs;
67    foreach my $table (sort keys %tables) {
68    push (@{$self->{'C'}},"$table.c");
69    # Do NOT add $table.h etc. to H_FILES unless we own up as to how they
70    # get built.
71    foreach my $ext (qw($(OBJ_EXT) .c .h .exh .fnm)) {
72        push (@files,$table.$ext);
73    }
74    $self->{SOURCE} .= " $table.c"
75        if $^O eq 'MacOS' && $self->{SOURCE} !~ /\b$table\.c\b/;
76    }
77    $self->{'XS'} = { "$name.xs" => "$name.c" };
78    $self->{'clean'}{'FILES'} .= join(' ',@files);
79    open(XS,">$name.xs") || die "Cannot open $name.xs:$!";
80    print XS <<'END';
81#define PERL_NO_GET_CONTEXT
82#include <EXTERN.h>
83#include <perl.h>
84#include <XSUB.h>
85#include "encode.h"
86END
87    foreach my $table (sort keys %tables) {
88    print XS qq[#include "${table}.h"\n];
89    }
90    print XS <<"END";
91
92static void
93Encode_XSEncoding(pTHX_ encode_t *enc)
94{
95 dSP;
96 HV *stash = gv_stashpv("Encode::XS", TRUE);
97 SV *iv    = newSViv(PTR2IV(enc));
98 SV *sv    = sv_bless(newRV_noinc(iv),stash);
99 int i = 0;
100 /* with the SvLEN() == 0 hack, PVX won't be freed. We cast away name's
101 constness, in the hope that perl won't mess with it. */
102 assert(SvTYPE(iv) >= SVt_PV); assert(SvLEN(iv) == 0);
103 SvFLAGS(iv) |= SVp_POK;
104 SvPVX(iv) = (char*) enc->name[0];
105 PUSHMARK(sp);
106 XPUSHs(sv);
107 while (enc->name[i])
108  {
109   const char *name = enc->name[i++];
110   XPUSHs(sv_2mortal(newSVpvn(name,strlen(name))));
111  }
112 PUTBACK;
113 call_pv("Encode::define_encoding",G_DISCARD);
114 SvREFCNT_dec(sv);
115}
116
117MODULE = Encode::$name	PACKAGE = Encode::$name
118PROTOTYPES: DISABLE
119BOOT:
120{
121END
122    foreach my $table (sort keys %tables) {
123    print XS qq[#include "${table}.exh"\n];
124    }
125    print XS "}\n";
126    close(XS);
127    return "# Built $name.xs\n\n";
128}
129
130sub postamble
131{
132    my $self = shift;
133    my $dir  = $self->catdir($self->updir,'ucm');
134    my $str  = "# $name\$(OBJ_EXT) depends on .h and .exh files not .c files - but all written by enc2xs\n";
135    $str    .= "$name.c : $name.xs ";
136    foreach my $table (sort keys %tables)
137    {
138    $str .= " $table.c";
139    }
140    $str .= "\n\n";
141    $str .= "$name\$(OBJ_EXT) : $name.c\n\n";
142
143    my $enc2xs = $self->catfile($self->updir,'bin', 'enc2xs');
144    foreach my $table (sort keys %tables)
145    {
146    my $numlines = 1;
147    my $lengthsofar = length($str);
148    my $continuator = '';
149    $str .= "$table.c : $enc2xs Makefile.PL";
150    foreach my $file (@{$tables{$table}})
151    {
152        $str .= $continuator.' '.$self->catfile($dir,$file);
153        if ( length($str)-$lengthsofar > 128*$numlines )
154        {
155        $continuator .= " \\\n\t";
156        $numlines++;
157        } else {
158        $continuator = '';
159        }
160    }
161    my $plib   = $self->{PERL_CORE} ? '"-I$(PERL_LIB)"' : '';
162    $plib .= " -MCross=$::Cross::platform" if defined $::Cross::platform;
163    my $ucopts = '-"Q"';
164    $str .=
165        qq{\n\t\$(PERL) $plib $enc2xs $ucopts -o \$\@ -f $table.fnm\n\n};
166    open (FILELIST, ">$table.fnm")
167        || die "Could not open $table.fnm: $!";
168    foreach my $file (@{$tables{$table}})
169    {
170        print FILELIST $self->catfile($dir,$file) . "\n";
171    }
172    close(FILELIST);
173    }
174    return $str;
175}
176
177