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