More Han tweaks from Autrjius Tang: most importantly,
[p5sagit/p5-mst-13.2.git] / ext / Encode / CN / Makefile.PL
1 use 5.7.2;
2 use strict;
3 use ExtUtils::MakeMaker;
4
5 my %tables = (EUC_CN   => ['euc-cn.enc'],
6               GB2312   => ['gb2312.enc'],
7               GB12345  => ['gb12345.enc'],
8               CP936    => ['cp936.enc'],
9               'ISO_IR_165' => ['iso-ir-165.enc'],
10              );
11
12 my $name = 'CN';
13
14 WriteMakefile(
15               INC               => "-I..",
16               NAME              => 'Encode::'.$name,
17               VERSION_FROM      => "$name.pm",
18               OBJECT            => '$(O_FILES)',
19               'dist'            => {
20                   COMPRESS      => 'gzip -9f',
21                   SUFFIX        => 'gz',
22                   DIST_DEFAULT => 'all tardist',
23               },
24               MAN3PODS  => {},
25               # OS 390 winges about line numbers > 64K ???
26               XSOPT => '-nolinenumbers',
27               );
28
29 package MY;
30
31 sub post_initialize
32 {
33     my ($self) = @_;
34     my %o;
35     my $x = $self->{'OBJ_EXT'};
36     # Add the table O_FILES
37     foreach my $e (keys %tables)
38     {
39         $o{$e.$x} = 1;
40     }
41     $o{"$name$x"} = 1;
42     $self->{'O_FILES'} = [sort keys %o];
43     my @files = ("$name.xs");
44     $self->{'C'} = ["$name.c"];
45     $self->{'H'} = [$self->catfile($self->updir,'encode.h')];
46     my %xs;
47     foreach my $table (keys %tables) {
48         push (@{$self->{'C'}},"$table.c");
49         # Do NOT add $table.h etc. to H_FILES unless we own up as to how they
50         # get built.
51         foreach my $ext (qw($(OBJ_EXT) .c .h _def.h .fnm)) {
52             push (@files,$table.$ext);
53         }
54     }
55     $self->{'XS'} = { "$name.xs" => "$name.c" };
56     $self->{'clean'}{'FILES'} .= join(' ',@files);
57     open(XS,">$name.xs") || die "Cannot open $name.xs:$!";
58     print XS <<'END';
59 #include <EXTERN.h>
60 #include <perl.h>
61 #include <XSUB.h>
62 #define U8 U8
63 #include "../encode.h"
64 END
65     foreach my $table (keys %tables) {
66         print XS qq[#include "${table}.h"\n];
67     }
68     print XS <<"END";
69
70 static void
71 Encode_XSEncoding(pTHX_ encode_t *enc)
72 {
73  dSP;
74  HV *stash = gv_stashpv("Encode::XS", TRUE);
75  SV *sv    = sv_bless(newRV_noinc(newSViv(PTR2IV(enc))),stash);
76  int i = 0;
77  PUSHMARK(sp);
78  XPUSHs(sv);
79  while (enc->name[i])
80   {
81    const char *name = enc->name[i++];
82    XPUSHs(sv_2mortal(newSVpvn(name,strlen(name))));
83   }
84  PUTBACK;
85  call_pv("Encode::define_encoding",G_DISCARD);
86  SvREFCNT_dec(sv);
87 }
88
89 MODULE = Encode::$name  PACKAGE = Encode::$name
90 BOOT:
91 {
92 END
93     foreach my $table (keys %tables) {
94         print XS qq[#include "${table}_def.h"\n];
95     }
96     print XS "}\n";
97     close(XS);
98     return "# Built $name.xs\n\n";
99 }
100
101 sub postamble
102 {
103     my $self = shift;
104     my $dir  = $self->catdir($self->updir,'Encode');
105     my $str  = "# $name\$(OBJ_EXT) depends on .h and _def.h files not .c files - but all written by compile\n";
106     $str    .= "$name.c : $name.xs ";
107     foreach my $table (keys %tables)
108     {
109         $str .= " $table.c";
110     }
111     $str .= "\n\n";
112     $str .= "$name\$(OBJ_EXT) : $name.c\n\n";
113
114     my $compile = $self->catfile($self->updir,'compile');
115     foreach my $table (keys %tables)
116     {
117         my $numlines = 1;
118         my $lengthsofar = length($str);
119         my $continuator = '';
120         $str .= "$table.c : $compile Makefile.PL";
121         foreach my $file (@{$tables{$table}})
122         {
123             $str .= $continuator.' '.$self->catfile($dir,$file);
124             if ( length($str)-$lengthsofar > 128*$numlines )
125             {
126                 $continuator .= " \\\n\t";
127                 $numlines++;
128             } else {
129                 $continuator = '';
130             }
131         }
132         $str .= $^O eq 'VMS' # In VMS quote to preserve case
133             ? qq{\n\t\$(PERL) $compile -"Q" -o \$\@ -f $table.fnm\n\n}
134             : qq{\n\t\$(PERL) $compile -Q -o \$\@ -f $table.fnm\n\n};
135         open (FILELIST, ">$table.fnm")
136             || die "Could not open $table.fnm: $!";
137         foreach my $file (@{$tables{$table}})
138         {
139             print FILELIST $self->catfile($dir,$file) . "\n";
140         }
141         close(FILELIST);
142     }
143     return $str;
144 }
145