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