This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Sv_*set() doc's and extra const's for the SvPVX_const() tasks
[perl5.git] / symbian / xsbuild.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4
5 use Getopt::Long;
6 use File::Basename;
7 use Cwd;
8
9 do "sanity.pl";
10
11 my $CoreBuild = -d "ext" && -f "perl.h" && -d "symbian" && -f "perl.c";
12
13 my $SymbianVersion = $ENV{XSBUILD_SYMBIAN_VERSION};
14 my $PerlVersion    = $ENV{XSBUILD_PERL_VERSION};
15 my $CSuffix        = '.c';
16 my $CPlusPlus;
17 my $Config;
18 my $Build;
19 my $Clean;
20 my $DistClean;
21 my $Sis;
22
23 sub usage {
24     die <<__EOF__;
25 $0: Usage: $0 [--symbian=version] [--perl=version]
26               [--csuffix=csuffix] [--cplusplus]
27               [--win=win] [--arm=arm]
28               [--config|--build|--clean|--distclean|--sis] ext
29 __EOF__
30 }
31
32 my $CWD;
33 my $SDK;
34 my $VERSION;
35 my $R_V_SV;
36 my $PERLSDK;
37 my $WIN;
38 my $ARM;
39 my $HOME = getcwd();
40
41 if ( !defined $PerlVersion && $0 =~ m:\\symbian\\perl\\(.+)\\bin\\xsbuild.pl:i )
42 {
43     $PerlVersion = $1;
44 }
45
46 if ( !defined $SymbianVersion) {
47     ($SymbianVersion) = ($ENV{PATH} =~ m!C:\\Symbian\\(.+?)\\!i);
48 }
49
50 my $S60SDK;
51
52 if ($CoreBuild) {
53     unshift @INC, "symbian";
54     do "sanity.pl";
55     my %VERSION = %{ do "version.pl" };
56     $SDK     = do "sdk.pl";
57     $VERSION = "$VERSION{REVISION}$VERSION{VERSION}$VERSION{SUBVERSION}";
58     $R_V_SV  = "$VERSION{REVISION}.$VERSION{VERSION}.$VERSION{SUBVERSION}";
59     $HOME    = do "cwd.pl";
60     $SymbianVersion = $1 if $SDK =~ m:\\Symbian\\([^\\]+):;
61     $PerlVersion    = $R_V_SV;
62     $S60SDK = $ENV{S60SDK}; # from sdk.pl
63 }
64
65 usage()
66   unless GetOptions(
67     'symbian=s' => \$SymbianVersion,
68     'perl=s'    => \$PerlVersion,
69     'csuffix=s' => \$CSuffix,
70     'cplusplus' => \$CPlusPlus,
71     'win=s'     => \$WIN,
72     'arm=s'     => \$ARM,
73     'config'    => \$Config,
74     'build'     => \$Build,
75     'clean'     => \$Clean,
76     'distclean' => \$DistClean,
77     'sis'       => \$Sis
78   );
79
80 usage() unless @ARGV;
81
82 $CSuffix = '.cpp' if $CPlusPlus;
83 $Build = !( $Config || $Clean || $DistClean ) || $Sis unless defined $Build;
84
85 die "$0: Symbian version undefined\n" unless defined $SymbianVersion;
86
87 $SymbianVersion =~ s:/:\\:g;
88
89 die "$0: Symbian version '$SymbianVersion' not found\n"
90   unless -d "\\Symbian\\$SymbianVersion";
91
92 die "$0: Perl version undefined\n" unless defined $PerlVersion;
93
94 die "$0: Perl version '$PerlVersion' not found\n"
95   if !$CoreBuild && !-d "\\Symbian\\Perl\\$PerlVersion";
96
97 print "Configuring with Symbian $SymbianVersion and Perl $PerlVersion...\n";
98
99 $SDK     = "\\Symbian\\$SymbianVersion" unless defined $SDK;
100 $PERLSDK = "\\Symbian\\Perl\\$PerlVersion";
101
102 $R_V_SV = $PerlVersion;
103
104 $VERSION =~ tr/.//d;
105
106 $ENV{SDK}   = $SDK;    # For the Errno extension
107 $ENV{CROSS} = 1;       # For the Encode extension
108
109 my $UREL = $ENV{UREL}; # from sdk.pl
110 $UREL =~ s/-ARM-/$ARM/;
111 my $UARM = $ENV{UARM}; # from sdk.pl
112 my $SRCDBG = $UARM eq 'udeb' ? "SRCDBG" : "";
113
114 my %CONF;
115 my %EXTCFG;
116
117 sub write_bld_inf {
118     my ($base) = @_;
119     print "\tbld.inf\n";
120     open( BLD_INF, ">bld.inf" ) or die "$0: bld.inf: $!\n";
121     print BLD_INF <<__EOF__;
122 PRJ_MMPFILES
123 $base.mmp
124 PRJ_PLATFORMS
125 $WIN $ARM
126 __EOF__
127     close(BLD_INF);
128 }
129
130 sub system_echo {
131     my $cmd = shift;
132     print "xsbuild: ", $cmd, "\n";
133     return system($cmd);
134 }
135
136 sub run_PL {
137     my ( $PL, $dir, $file ) = @_;
138     if ( defined $file ) {
139         print "\t(Running $dir\\$PL to create $file)\n";
140         unlink($file);
141     }
142     else {
143         print "\t(Running $dir\\$PL)\n";
144     }
145     my $cmd;
146     if ($CoreBuild) {
147         # Problem: the Config.pm we have in $HOME\\lib carries the
148         # version number of the Perl we are building, while the Perl
149         # we are running might have some other version.  Solution:
150         # temporarily replace the Config.pm with a patched version.
151         my $V = sprintf "%vd", $^V;
152         unlink("$HOME\\lib\\Config.pm.bak");
153         system_echo("perl -pi.bak -e \"s:\\Q$R_V_SV:$V:\" $HOME\\lib\\Config.pm");
154     }
155     system_echo("perl -I$HOME\\lib -I$HOME\\xlib\\symbian $PL") == 0
156       or warn "$0: $PL failed.\n";
157     if ($CoreBuild) {
158         system_echo("copy $HOME\\lib\\Config.pm.bak $HOME\\lib\\Config.pm");
159     }
160     if ( defined $file ) { -s $file or die "$0: No $file created.\n" }
161 }
162
163 sub read_old_multi {
164     my ( $conf, $k ) = @_;
165     push @{ $conf->{$k} }, split( ' ', $1 ) if /^$k\s(.+)$/;
166 }
167
168 sub uniquefy_filenames {
169     my $b = [];
170     my %c = ();
171     for my $i (@{$_[0]}) {
172         $i =~ s!/!\\!g;
173         $i = lc $i if $i =~ m!\\!;
174         $i =~ s!^c:!!;
175         push @$b, $i unless $c{$i}++;
176     }
177     return $b;
178 }
179
180 sub read_mmp {
181     my ( $conf, $mmp ) = @_;
182     if ( -r $mmp && open( MMP, "<$mmp" ) ) {
183         print "\tReading $mmp...\n";
184         while (<MMP>) {
185             chomp;
186             $conf->{TARGET}     = $1 if /^TARGET\s+(.+)$/;
187             $conf->{TARGETPATH} = $1 if /^TARGETPATH\s+(.+)$/;
188             $conf->{EXTVERSION} = $1 if /^EXTVERSION\s+(.+)$/;
189             read_old_multi( $conf, "SOURCE" );
190             read_old_multi( $conf, "SOURCEPATH" );
191             read_old_multi( $conf, "USERINCLUDE" );
192             read_old_multi( $conf, "SYSTEMINCLUDE" );
193             read_old_multi( $conf, "LIBRARY" );
194             read_old_multi( $conf, "MACRO" );
195         }
196         close(MMP);
197     }
198 }
199
200 sub write_mmp {
201     my ( $base, $userinclude, @src ) = @_;
202
203     print "\t$base.mmp\n";
204     $CONF{TARGET}        = "$base.dll";
205     $CONF{TARGETPATH}    = "\\System\\Libs\\Perl\\$R_V_SV";
206     $CONF{SOURCE}        = [@src];
207     $CONF{SOURCEPATH}    = [ $CWD, $HOME ];
208     $CONF{USERINCLUDE}   = [ $CWD, $HOME ];
209     $CONF{SYSTEMINCLUDE} = ["$PERLSDK\\include"] unless $CoreBuild;
210     $CONF{SYSTEMINCLUDE} = [ $HOME ] if $CoreBuild;
211     $CONF{LIBRARY}       = [];
212     $CONF{MACRO}         = [];
213     read_mmp( \%CONF, "_init.mmp" );
214     read_mmp( \%CONF, "$base.mmp" );
215
216     for my $ui ( @{$userinclude} ) {
217         $ui =~ s!/!\\!g;
218         if ( $ui =~ m!^(?:[CD]:)?\\! ) {
219             push @{ $CONF{USERINCLUDE} }, $ui;
220         }
221         else {
222             push @{ $CONF{USERINCLUDE} }, "$HOME\\$ui";
223         }
224     }
225     push @{ $CONF{SYSTEMINCLUDE} }, "\\epoc32\\include";
226     push @{ $CONF{SYSTEMINCLUDE} }, "\\epoc32\\include\\libc";
227     push @{ $CONF{LIBRARY} },       "euser.lib";
228     push @{ $CONF{LIBRARY} },       "estlib.lib";
229     push @{ $CONF{LIBRARY} },       "perl$VERSION.lib";
230     push @{ $CONF{MACRO} },         "SYMBIAN" unless $CoreBuild;
231     push @{ $CONF{MACRO} },         "PERL_EXT" if $CoreBuild;
232     push @{ $CONF{MACRO} },         "MULTIPLICITY";
233     push @{ $CONF{MACRO} },         "PERL_IMPLICIT_CONTEXT";
234     push @{ $CONF{MACRO} },         "PERL_GLOBAL_STRUCT";
235     push @{ $CONF{MACRO} },         "PERL_GLOBAL_STRUCT_PRIVATE";
236
237     for my $u (qw(SOURCE SOURCEPATH SYSTEMINCLUDE USERINCLUDE LIBRARY MACRO)) {
238         $CONF{$u} = uniquefy_filenames( $CONF{$u} );
239     }
240     open( BASE_MMP, ">$base.mmp" ) or die "$0: $base.mmp: $!\n";
241
242     print BASE_MMP <<__EOF__;
243 TARGET          $CONF{TARGET}
244 TARGETTYPE      dll
245 TARGETPATH      $CONF{TARGETPATH}
246 SOURCE          @{$CONF{SOURCE}}
247 $SRCDBG
248 __EOF__
249     for my $u (qw(SOURCEPATH SYSTEMINCLUDE USERINCLUDE)) {
250         for my $v ( @{ $CONF{$u} } ) {
251             print BASE_MMP "$u\t$v\n";
252         }
253     }
254     # OPTION does not work in MMPs for pre-2.0 SDKs?
255     print BASE_MMP <<__EOF__;
256 LIBRARY         @{$CONF{LIBRARY}}
257 MACRO           @{$CONF{MACRO}}
258 // OPTION       MSVC /P
259 // OPTION       GCC -E
260 __EOF__
261     close(BASE_MMP);
262
263 }
264
265 sub write_makefile {
266     my ( $base, $build ) = @_;
267
268     print "\tMakefile\n";
269
270     my $windef1 = "$SDK\\Epoc32\\Build$CWD\\$base\\$WIN\\$base.def";
271     my $windef2 = "..\\BWINS\\${base}u.def";
272     my $armdef1 = "$SDK\\Epoc32\\Build$CWD\\$base\\$ARM\\$base.def";
273     my $armdef2 = "..\\BMARM\\${base}u.def";
274
275     my $wrap = $SDK && $S60SDK eq '1.2' && $SDK !~ /_CW$/;
276     my $ABLD = $wrap ? 'perl b.pl' : 'abld';
277
278     open( MAKEFILE, ">Makefile" ) or die "$0: Makefile: $!\n";
279     print MAKEFILE <<__EOF__;
280 WIN = $WIN
281 ARM = $ARM
282 ABLD = $ABLD
283
284 all:    build freeze
285
286 sis:    build_arm freeze_arm
287
288 build:  abld.bat build_win build_arm
289
290 abld.bat:
291         bldmake bldfiles
292
293 build_win: abld.bat
294         bldmake bldfiles
295         \$(ABLD) build \$(WIN) udeb
296
297 build_arm: abld.bat
298         bldmake bldfiles
299         \$(ABLD) build \$(ARM) $UARM
300
301 win:    build_win freeze_win
302
303 arm:    build_arm freeze_arm
304
305 freeze: freeze_win freeze_arm
306
307 freeze_win:
308         bldmake bldfiles
309         \$(ABLD) freeze \$(WIN) $base
310
311 freeze_arm:
312         bldmake bldfiles
313         \$(ABLD) freeze \$(ARM) $base
314
315 defrost:        defrost_win defrost_arm
316
317 defrost_win:
318         -del /f $windef1
319         -del /f $windef2
320
321 defrost_arm:
322         -del /f $armdef1
323         -del /f $armdef2
324
325 clean:  clean_win clean_arm
326
327 clean_win:
328         \$(ABLD) clean \$(WIN)
329
330 clean_arm:
331         \$(ABLD) clean \$(ARM)
332
333 realclean:      clean realclean_win realclean_arm
334         -del /f _init.c b.pl
335         -del /f $base.c $base.mmp
336
337 realclean_win:
338         \$(ABLD) reallyclean \$(WIN)
339
340 realclean_arm:
341         \$(ABLD) reallyclean \$(ARM)
342
343 distclean:      defrost realclean
344         -rmdir ..\\BWINS ..\\BMARM
345         -del /f const-c.inc const-xs.inc
346         -del /f Makefile abld.bat bld.inf
347 __EOF__
348     close(MAKEFILE);
349     if ($wrap) {
350         if(open(B,">b.pl")) {
351             print B <<'__EOF__';
352 # abld.pl wrapper.
353
354 # nmake doesn't like MFLAGS and MAKEFLAGS being set to -w and w.
355 delete $ENV{MFLAGS};
356 delete $ENV{MAKEFLAGS};
357
358 print "abld @ARGV\n";
359 system("abld @ARGV");
360 __EOF__
361             close(B);
362         } else {
363             warn "$0: failed to create b.pl: $!\n";
364         }
365     }
366 }
367
368 sub update_dir {
369     print "[chdir from ", getcwd(), " to ";
370     chdir(shift) or return;
371     update_cwd();
372     print getcwd(), "]\n";
373 }
374
375 sub xsconfig {
376     my ( $ext, $dir ) = @_;
377     print "Configuring for $ext, directory $dir...\n";
378     my $extu = $CoreBuild ? "$HOME\\lib\\ExtUtils" : "$PERLSDK\\lib\\ExtUtils";
379     update_dir($dir) or die "$0: chdir '$dir': $!\n";
380     my $build  = dirname($ext);
381     my $base   = basename($ext);
382     my $basexs = "$base.xs";
383     my $basepm = "$base.pm";
384     my $basec  = "$base$CSuffix";
385     my $extdir = ".";
386     if ( $dir =~ m:^ext\\(.+): ) {
387         $extdir = $1;
388     }
389     elsif ( $dir ne "." ) {
390         $extdir = $dir;
391     }
392     my $extdirdir  = dirname($extdir);
393     my $targetroot = "\\System\\Libs\\Perl\\$R_V_SV";
394     write_bld_inf($base) if -f $basexs;
395
396     my %src;
397     $src{$basec}++;
398
399     $extdirdir = $extdirdir eq "." ? "" : "$extdirdir\\";
400
401     my %lst;
402     $lst{"$UREL\\$base.dll"} =
403       "$targetroot\\$ARM-symbian\\$base.dll"
404       if -f $basexs;
405     $lst{"$dir\\$base.pm"} = "$targetroot\\$extdirdir$base.pm"
406       if -f $basepm && $base ne 'XSLoader';
407
408     my %incdir;
409     my $ran_PL;
410     if ( -d 'lib' ) {
411         use File::Find;
412         my @found;
413         find( sub { push @found, $File::Find::name if -f $_ }, 'lib' );
414         for my $found (@found) {
415             my ($short) = ( $found =~ m/^lib.(.+)/ );
416             $short =~ s!/!\\!g;
417             $found =~ s!/!\\!g;
418             $lst{"$dir\\$found"} = "$targetroot\\$short";
419         }
420     }
421     if ( my @pm = glob("*.pm */*.pm") ) {
422         for my $pm (@pm) {
423             next if $pm =~ m:^t/:;
424             $pm =~ s:/:\\:g;
425             $lst{"$dir\\$pm"} = "$targetroot\\$extdirdir$pm";
426         }
427     }
428     if ( my @c = glob("*.c *.cpp */*.c */*.cpp") ) {
429         for my $c (@c) {
430             $c =~ s:/:\\:g;
431             $src{$c}++;
432         }
433     }
434     if ( my @h = glob("*.h */*.h") ) {
435         for my $h (@h) {
436             $h =~ s:/:\\:g;
437             $h = dirname($h);
438             $incdir{"$dir\\$h"}++ unless $h eq ".";
439         }
440     }
441     if ( exists $EXTCFG{$ext} ) {
442         for my $cfg ( @{ $EXTCFG{$ext} } ) {
443             if ( $cfg =~ /^([-+])?(.+\.(c|cpp|h))$/ ) {
444                 my $o = defined $1 ? $1 : '+';
445                 my $f = $2;
446                 $f =~ s:/:\\:g;
447                 for my $f ( glob($f) ) {
448                     if ( $o eq '+' ) {
449                         warn "$0: no source file $dir\\$f\n" unless -f $f;
450                         $src{$f}++ unless $cfg =~ /\.h$/;
451                         if ( $f =~ m:^(.+)\\[^\\]+$: ) {
452                             $incdir{$1}++;
453                         }
454                     }
455                     elsif ( $o eq '-' ) {
456                         delete $src{$f};
457                     }
458                 }
459             }
460             if ( $cfg =~ /^([-+])?(.+\.(pm|pl|inc))$/ ) {
461                 my $o = defined $1 ? $1 : '+';
462                 my $f = $2;
463                 $f =~ s:/:\\:g;
464                 for my $f ( glob($f) ) {
465                     if ( $o eq '+' ) {
466                         warn "$0: no Perl file $dir\\$f\n" unless -f $f;
467                         $lst{"$dir\\$f"} = "$targetroot\\$extdir\\$f";
468                     }
469                     elsif ( $o eq '-' ) {
470                         delete $lst{"$dir\\$f"};
471                     }
472                 }
473             }
474             if ( $cfg eq 'CONST' && !$ran_PL++ ) {
475                 run_PL( "Makefile.PL", $dir, "const-xs.inc" );
476             }
477         }
478     }
479     unless ( $ran_PL++ ) {
480         run_PL( "Makefile.PL", $dir ) if -f "Makefile.PL";
481     }
482     if ( $dir eq "ext\\Errno" ) {
483         run_PL( "Errno_pm.PL", $dir, "Errno.pm" );
484         $lst{"$dir\\Errno.pm"} = "$targetroot\\Errno.pm";
485     }
486     elsif ( $dir eq "ext\\Devel\\PPPort" ) {
487         run_PL( "ppport_h.PL", $dir, "ppport.h" );
488     }
489     elsif ( $dir eq "ext\\DynaLoader" ) {
490         run_PL( "XSLoader_pm.PL", $dir, "XSLoader.pm" );
491         $lst{"ext\\DynaLoader\\XSLoader.pm"} = "$targetroot\\XSLoader.pm";
492     }
493     elsif ( $dir eq "ext\\Encode" ) {
494         system_echo("perl bin\\enc2xs -Q -O -o def_t.c -f def_t.fnm") == 0
495           or die "$0: running enc2xs failed: $!\n";
496     }
497
498     my @lst = sort keys %lst;
499
500     read_mmp( \%CONF, "_init.mmp" );
501     read_mmp( \%CONF, "$base.mmp" );
502
503     if ( -f $basexs ) {
504         my %MM;    # MakeMaker results
505         my @MM = qw(VERSION XS_VERSION);
506         if ( -f "Makefile" ) {
507             print "\tReading MakeMaker Makefile...\n";
508             if ( open( MAKEFILE, "Makefile" ) ) {
509                 while (<MAKEFILE>) {
510                     for my $m (@MM) {
511                         if (m!^$m = (.+)!) {
512                             $MM{$m} = $1;
513                             print "\t$m = $1\n";
514                         }
515                     }
516                 }
517                 close(MAKEFILE);
518             }
519             else {
520                 warn "$0: Makefile: $!";
521             }
522             print "\tDeleting MakeMaker Makefile.\n";
523             unlink("Makefile");
524         }
525
526         unlink($basec);
527         print "\t$basec\n";
528         if ( defined $CONF{EXTVERSION} ) {
529             my $EXTVERSION = $CONF{EXTVERSION};
530             print "\tUsing $EXTVERSION for version...\n";
531             $MM{VERSION} = $MM{XS_VERSION} = $EXTVERSION;
532         }
533         die "VERSION or XS_VERSION undefined\n"
534           unless defined $MM{VERSION} && defined $MM{XS_VERSION};
535         if ( open( BASE_C, ">$basec" ) ) {
536             print BASE_C <<__EOF__;
537 #ifndef VERSION
538 #define VERSION "$MM{VERSION}"
539 #endif
540 #ifndef XS_VERSION
541 #define XS_VERSION "$MM{XS_VERSION}"
542 #endif
543 __EOF__
544             close(BASE_C);
545         }
546         else {
547             warn "$0: $basec: $!";
548         }
549         unless (
550             system(
551 "perl -I$PERLSDK\\lib $extu\\xsubpp -C++ -csuffix .cpp -typemap $extu\\typemap -noprototypes $basexs >> $basec"
552             ) == 0
553             && -s $basec
554           )
555         {
556             die "$0: perl xsubpp failed: $!\n";
557         }
558
559         print "\t_init.c\n";
560         open( _INIT_C, ">_init.c" ) or die "$!: _init.c: $!\n";
561         print _INIT_C <<__EOF__;
562     #include "EXTERN.h"
563     #include "perl.h"
564     EXPORT_C void _init(void *handle) {
565     }
566 __EOF__
567         close(_INIT_C);
568
569         my @src = ( "_init.c", sort keys %src );
570
571         if ( $base eq "Encode" ) {    # Currently unused.
572             for my $submf ( glob("*/Makefile") ) {
573                 my $d = dirname($submf);
574                 print "Configuring Encode::$d...\n";
575                 if ( open( SUBMF, $submf ) ) {
576                     if ( update_dir($d) ) {
577                         my @subsrc;
578                         while (<SUBMF>) {
579                             next if 1 .. /postamble/;
580                             if (m!^(\w+_t)\.c : !) {
581                                 system(
582                                     "perl ..\\bin\\enc2xs -Q -o $1.c -f $1.fnm")
583                                   == 0
584                                   or warn "$0: enc2xs: $!\n";
585                                 push @subsrc, "$1.c";
586                             }
587                         }
588                         close(SUBMF);
589                         unlink($submf);
590                         my $subbase = $d;
591                         $subbase =~ s!/!::!g;
592                         write_mmp( $subbase, ["..\\Encode"], "$subbase.c",
593                             @subsrc );
594                         write_makefile( $subbase, $build );
595                         write_bld_inf($subbase);
596
597                         unless (
598                             system(
599 "perl -I$HOME\\lib ..\\$extu\\xsubpp -C++ -csuffix .cpp -typemap ..\\$extu\\typemap -noprototypes $subbase.xs > $subbase.c"
600                             ) == 0
601                             && -s "$subbase.c"
602                           )
603                         {
604                             die "$0: perl xsubpp failed: $!\n";
605                         }
606                         update_dir("..");
607                     }
608                     else {
609                         warn "$0: chdir $d: $!\n";
610                     }
611                 }
612                 else {
613                     warn "$0: $submf: $!";
614                 }
615             }
616             print "Configuring Encode...\n";
617         }
618
619         write_mmp( $base, [ keys %incdir ], @src );
620         write_makefile( $base, $build );
621     }
622     my $lstname = $ext;
623     $lstname =~ s:^ext\\::;
624     $lstname =~ s:\\:-:g;
625     print "\t$lstname.lst\n";
626     my $lstout =
627       $CoreBuild ? "$HOME/symbian/$lstname.lst" : "$HOME/$lstname.lst";
628     if ( open( my $lst, ">$lstout" ) ) {
629         for my $f (@lst) { print $lst qq["$f"-"!:$lst{$f}"\n] }
630         close($lst);
631     }
632     else {
633         die "$0: $lstout: $!\n";
634     }
635     update_dir($HOME);
636 }
637
638 sub update_cwd {
639     $CWD = getcwd();
640     $CWD =~ s!^[CD]:!!i;
641     $CWD =~ s!/!\\!g;
642 }
643
644 for my $ext (@ARGV) {
645
646     $ext =~ s!::!\\!g;
647     $ext =~ s!/!\\!g;
648
649     my $cfg;
650
651     $cfg = $2 if $ext =~ s/(.+?),(.+)/$1/;
652
653     my $dir;
654
655     unless ( -e $ext ) {
656         if ( $ext =~ /\.xs$/ && !-f $ext ) {
657             if ( -f "ext\\$ext" ) {
658                 $ext = "ext\\$ext";
659                 $dir = dirname($ext);
660             }
661         }
662         elsif ( !-d $ext ) {
663             if ( -d "ext\\$ext" ) {
664                 $ext = "ext\\$ext";
665                 $dir = $ext;
666             }
667         }
668         $dir = "." unless defined $dir;
669     }
670     else {
671         if ( $ext =~ /\.xs$/ && -f $ext ) {
672             $ext = dirname($ext);
673             $dir = $ext;
674         }
675         elsif ( -d $ext ) {
676             $dir = $ext;
677         }
678     }
679
680     if ( $ext eq "XSLoader" ) {
681         $ext = "ext\\XSLoader";
682     }
683     if ( $ext eq "ext\\XSLoader" ) {
684         $dir = "ext\\DynaLoader";
685     }
686
687     $EXTCFG{$ext} = [ split( /,/, $cfg ) ] if defined $cfg;
688
689     die "$0: no lib\\Config.pm\n"
690       if $CoreBuild && $Build && !-f "lib\\Config.pm";
691
692     if ($CoreBuild) {
693         open( my $cfg, "symbian/install.cfg" )
694           or die "$0: symbian/install.cfg: $!\n";
695         my $extdir = $dir;
696         $extdir =~ s:^ext\\::;
697         while (<$cfg>) {
698             next unless /^ext\s+(.+)/;
699             chomp;
700             my $ext = $1;
701             my @ext = split( ' ', $ext );
702             $EXTCFG{"ext\\$ext[0]"} = [@ext];
703         }
704         close($cfg);
705     }
706
707     if ( $Config || $Build ) {
708         xsconfig( $ext, $dir ) or die "$0: xsconfig '$ext' failed\n";
709         next if $Config;
710     }
711
712     my $chdir = $ext eq "ext\\XSLoader" ? "ext\\DynaLoader" : $dir;
713     die "$0: no directory '$chdir'\n" unless -d $chdir;
714     update_dir($chdir) or die "$0: chdir '$chdir' failed: $!\n";
715
716     my %CONF;
717
718     my @ext   = split( /\\/, $ext );
719     my $base  = $ext[-1];
720
721     if ( $Clean || $DistClean ) {
722         print "Cleaning $ext...\n";
723         unlink("bld.inf");
724         unlink("$base.mmp");
725         unlink("_init.c");
726         unlink("const-c.inc");
727         unlink("const-xs.inc");
728         rmdir("..\\bmarm");
729     }
730
731     if ( $Build && $ext ne "ext\\XSLoader" && $ext ne "ext\\Errno" ) {
732
733      # We compile the extension three (3) times.
734      # (1) Only the _init.c to get _init() as the ordinal 1 function in the DLL.
735      # (2) With the rest and the _init.c to get ordinals for the rest.
736      # (3) With an updated _init.c that carries the symbols from step (2).
737
738         system("make clean");
739         system("make defrost") == 0 or die "$0: make defrost failed\n";
740
741         my @TARGET;
742
743         push @TARGET, 'sis' if $Sis;
744
745         # Compile #1.
746         # Hide all but the _init.c.
747         print "\n*** $ext - Compile 1 of 3.\n\n";
748         system(
749 "perl -pi.bak -e \"s:^SOURCE\\s+_init.c:SOURCE\\t_init.c // :\" $base.mmp"
750         );
751         system("bldmake bldfiles");
752         system("make @TARGET") == 0 or die "$0: make #1 failed\n";
753
754         # Compile #2.
755         # Reveal the rest again.
756         print "\n*** $ext - Compile 2 of 3.\n\n";
757         system(
758 "perl -pi.bak -e \"s:^SOURCE\\t_init.c // :SOURCE\\t_init.c :\" $base.mmp"
759         );
760         system("make @TARGET") == 0 or die "$0: make #2 failed\n";
761         unlink("$base.mmp.bak");
762
763         open( _INIT_C, ">_init.c" ) or die "$0: _init.c: $!\n";
764         print _INIT_C <<'__EOF__';
765 #include "EXTERN.h"
766 #include "perl.h"
767
768 /* This is a different but matching definition from in dl_symbian.xs. */
769 typedef struct {
770     void*       handle;
771     int         error;
772     HV*         symbols;
773 } PerlSymbianLibHandle;
774
775 EXPORT_C void _init(void* handle) {
776 __EOF__
777
778         my %symbol;
779         my $def;
780         my $basef;
781         for my $f ("$SDK\\Epoc32\\Build$CWD\\$base\\WINS\\$base.def",
782                    "..\\BMARM\\${base}u.def") {
783             print "\t($f - ";
784             if ( open( $def, $f ) ) {
785                 print "OK)\n";
786                 $basef = $f;
787                 last;
788             } else {
789                 print "no)\n";
790             }
791         }
792         unless (defined $basef) {
793             die "$0: failed to find .def for $base\n";
794         }
795         while (<$def>) {
796             next while 1 .. /^EXPORTS/;
797             if (/^\s*(\w+) \@ (\d+) /) {
798                 $symbol{$1} = $2;
799             }
800         }
801         close($def);
802  
803         my @symbol = sort keys %symbol;
804         if (@symbol) {
805             print _INIT_C <<'__EOF__';
806     dTHX;
807     PerlSymbianLibHandle* h = (PerlSymbianLibHandle*)handle;
808     if (!h->symbols)
809         h->symbols = newHV();
810     if (h->symbols) {
811 __EOF__
812             for my $sym (@symbol) {
813                 my $len = length($sym);
814                 print _INIT_C <<__EOF__;
815         hv_store(h->symbols, "$sym", $len, newSViv($symbol{$sym}), 0);
816 __EOF__
817             }
818         }
819         else {
820             die "$0: $basef: no exports found\n";
821         }
822
823         print _INIT_C <<'__EOF__';
824     }
825 }
826 __EOF__
827         close(_INIT_C);
828
829         # Compile #3.  This is for real.
830         print "\n*** $ext - Compile 3 of 3.\n\n";
831         system("make @TARGET") == 0 or die "$0: make #3 failed\n";
832
833     }
834     elsif ( $Clean || $DistClean ) {
835         if ( $ext eq "ext\\Errno" ) {
836             unlink( "Errno.pm", "Makefile" );
837         }
838         else {
839             if ( -f "Makefile" ) {
840                 if ($Clean) {
841                     system("make clean") == 0 or die "$0: make clean failed\n";
842                 }
843                 elsif ($DistClean) {
844                     system("make distclean") == 0
845                       or die "$0: make distclean failed\n";
846                 }
847             }
848             if ( $ext eq "ext\\Devel\\PPPort" ) {
849                 unlink("ppport.h");
850             }
851         }
852         my @B = glob("ext/BWINS ext/BMARM ext/*/BWINS ext/*/BMARM Makefile");
853         rmdir(@B) if @B;
854     }
855
856     update_dir($HOME);
857
858 }    # for my $ext
859
860 exit(0);
861