11 my $CoreBuild = -d "ext" && -f "perl.h" && -d "symbian" && -f "perl.c";
13 my $SymbianVersion = $ENV{XSBUILD_SYMBIAN_VERSION};
14 my $PerlVersion = $ENV{XSBUILD_PERL_VERSION};
25 $0: Usage: $0 [--symbian=version] [--perl=version]
26 [--csuffix=csuffix] [--cplusplus]
27 [--win=win] [--arm=arm]
28 [--config|--build|--clean|--distclean|--sis] ext
39 my $BUILDROOT = getcwd();
41 if ( !defined $PerlVersion && $0 =~ m:\\symbian\\perl\\(.+)\\bin\\xsbuild.pl:i )
46 if ( !defined $SymbianVersion) {
47 ($SymbianVersion) = ($ENV{PATH} =~ m!C:\\Symbian\\(.+?)\\!i);
53 unshift @INC, "symbian";
55 my %VERSION = %{ do "version.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
67 'symbian=s' => \$SymbianVersion,
68 'perl=s' => \$PerlVersion,
69 'csuffix=s' => \$CSuffix,
70 'cplusplus' => \$CPlusPlus,
76 'distclean' => \$DistClean,
82 $CSuffix = '.cpp' if $CPlusPlus;
83 $Build = !( $Config || $Clean || $DistClean ) || $Sis unless defined $Build;
85 die "$0: Symbian version undefined\n" unless defined $SymbianVersion;
87 $SymbianVersion =~ s:/:\\:g;
89 die "$0: Symbian version '$SymbianVersion' not found\n"
90 unless -d "\\Symbian\\$SymbianVersion";
92 die "$0: Perl version undefined\n" unless defined $PerlVersion;
94 die "$0: Perl version '$PerlVersion' not found\n"
95 if !$CoreBuild && !-d "\\Symbian\\Perl\\$PerlVersion";
97 print "Configuring with Symbian $SymbianVersion and Perl $PerlVersion...\n";
99 $SDK = "\\Symbian\\$SymbianVersion" unless defined $SDK;
100 $PERLSDK = "\\Symbian\\Perl\\$PerlVersion";
102 $R_V_SV = $PerlVersion;
106 $ENV{SDK} = $SDK; # For the Errno extension
107 $ENV{CROSS} = 1; # For the Encode extension
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" : "";
120 open( BLD_INF, ">bld.inf" ) or die "$0: bld.inf: $!\n";
121 print BLD_INF <<__EOF__;
132 print "xsbuild: ", $cmd, "\n";
137 my ( $PL, $dir, $file ) = @_;
138 if ( defined $file ) {
139 print "\t(Running $dir\\$PL to create $file)\n";
143 print "\t(Running $dir\\$PL)\n";
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");
156 system_echo("perl -I$BUILDROOT\\lib -I$BUILDROOT\\xlib\\symbian $PL") == 0
157 or warn "$0: $PL failed.\n";
159 system_echo("copy $BUILDROOT\\lib\\Config.pm.bak $BUILDROOT\\lib\\Config.pm");
161 if ( defined $file ) { -s $file or die "$0: No $file created.\n" }
165 my ( $conf, $k ) = @_;
166 push @{ $conf->{$k} }, split( ' ', $1 ) if /^$k\s(.+)$/;
169 sub uniquefy_filenames {
172 for my $i (@{$_[0]}) {
174 $i = lc $i if $i =~ m!\\!;
176 push @$b, $i unless $c{$i}++;
182 my ( $conf, $mmp ) = @_;
183 if ( -r $mmp && open( MMP, "<$mmp" ) ) {
184 print "\tReading $mmp...\n";
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" );
202 my ( $base, $userinclude, @src ) = @_;
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;
214 read_mmp( \%CONF, "_init.mmp" );
215 read_mmp( \%CONF, "$base.mmp" );
217 if ($base eq 'Zlib') {
218 push @{$CONF{USERINCLUDE}}, "$CWD\\zlib-src";
221 for my $ui ( @{$userinclude} ) {
223 if ( $ui =~ m!^(?:[CD]:)?\\! ) {
224 push @{ $CONF{USERINCLUDE} }, $ui;
227 push @{ $CONF{USERINCLUDE} }, "$BUILDROOT\\$ui";
230 push @{ $CONF{SYSTEMINCLUDE} }, "\\epoc32\\include";
231 push @{ $CONF{SYSTEMINCLUDE} }, "\\epoc32\\include\\libc";
232 push @{ $CONF{LIBRARY} }, "euser.lib";
233 push @{ $CONF{LIBRARY} }, "estlib.lib";
234 push @{ $CONF{LIBRARY} }, "perl$VERSION.lib";
235 push @{ $CONF{MACRO} }, "SYMBIAN" unless $CoreBuild;
236 push @{ $CONF{MACRO} }, "PERL_EXT" if $CoreBuild;
237 push @{ $CONF{MACRO} }, "MULTIPLICITY";
238 push @{ $CONF{MACRO} }, "PERL_IMPLICIT_CONTEXT";
239 push @{ $CONF{MACRO} }, "PERL_GLOBAL_STRUCT";
240 push @{ $CONF{MACRO} }, "PERL_GLOBAL_STRUCT_PRIVATE";
242 for my $u (qw(SOURCE SOURCEPATH SYSTEMINCLUDE USERINCLUDE LIBRARY MACRO)) {
243 $CONF{$u} = uniquefy_filenames( $CONF{$u} );
245 open( BASE_MMP, ">$base.mmp" ) or die "$0: $base.mmp: $!\n";
247 print BASE_MMP <<__EOF__;
250 TARGETPATH $CONF{TARGETPATH}
251 SOURCE @{$CONF{SOURCE}}
254 for my $u (qw(SOURCEPATH SYSTEMINCLUDE USERINCLUDE)) {
255 for my $v ( @{ $CONF{$u} } ) {
256 print BASE_MMP "$u\t$v\n";
259 # OPTION does not work in MMPs for pre-2.0 SDKs?
260 print BASE_MMP <<__EOF__;
261 LIBRARY @{$CONF{LIBRARY}}
262 MACRO @{$CONF{MACRO}}
271 my ( $base, $build ) = @_;
273 print "\tMakefile\n";
275 my $windef1 = "$SDK\\Epoc32\\Build$CWD\\$base\\$WIN\\$base.def";
276 my $windef2 = "..\\BWINS\\${base}u.def";
277 my $armdef1 = "$SDK\\Epoc32\\Build$CWD\\$base\\$ARM\\$base.def";
278 my $armdef2 = "..\\BMARM\\${base}u.def";
280 my $wrap = $SDK && $S60SDK eq '1.2' && $SDK !~ /_CW$/;
281 my $ABLD = $wrap ? 'perl b.pl' : 'abld';
283 open( MAKEFILE, ">Makefile" ) or die "$0: Makefile: $!\n";
284 print MAKEFILE <<__EOF__;
291 sis: build_arm freeze_arm
293 build: abld.bat build_win build_arm
300 \$(ABLD) build \$(WIN) udeb
304 \$(ABLD) build \$(ARM) $UARM
306 win: build_win freeze_win
308 arm: build_arm freeze_arm
310 freeze: freeze_win freeze_arm
314 \$(ABLD) freeze \$(WIN) $base
318 \$(ABLD) freeze \$(ARM) $base
320 defrost: defrost_win defrost_arm
330 clean: clean_win clean_arm
333 \$(ABLD) clean \$(WIN)
336 \$(ABLD) clean \$(ARM)
338 realclean: clean realclean_win realclean_arm
340 -del /f $base.c $base.mmp
343 \$(ABLD) reallyclean \$(WIN)
346 \$(ABLD) reallyclean \$(ARM)
348 distclean: defrost realclean
349 -rmdir ..\\BWINS ..\\BMARM
350 -del /f const-c.inc const-xs.inc
351 -del /f Makefile abld.bat bld.inf
355 if(open(B,">b.pl")) {
359 # nmake doesn't like MFLAGS and MAKEFLAGS being set to -w and w.
361 delete $ENV{MAKEFLAGS};
363 print "abld @ARGV\n";
364 system_echo("abld @ARGV");
368 warn "$0: failed to create b.pl: $!\n";
374 print "[chdir from ", getcwd(), " to ";
375 chdir(shift) or return;
377 print getcwd(), "]\n";
381 my ( $ext, $dir ) = @_;
382 print "Configuring for $ext, directory $dir...\n";
383 my $extu = $CoreBuild ? "$BUILDROOT\\lib\\ExtUtils" : "$PERLSDK\\lib\\ExtUtils";
384 update_dir($dir) or die "$0: chdir '$dir': $!\n";
385 my $build = dirname($ext);
386 my $base = basename($ext);
387 my $basexs = "$base.xs";
388 my $basepm = "$base.pm";
389 my $basec = "$base$CSuffix";
391 if ( $dir =~ m:^ext\\(.+): ) {
394 elsif ( $dir ne "." ) {
397 my $extdirdir = dirname($extdir);
398 my $targetroot = "\\System\\Libs\\Perl\\$R_V_SV";
399 write_bld_inf($base) if -f $basexs;
404 $extdirdir = $extdirdir eq "." ? "" : "$extdirdir\\";
407 $lst{"$UREL\\$base.dll"} =
408 "$targetroot\\$ARM-symbian\\$base.dll"
410 $lst{"$dir\\$base.pm"} = "$targetroot\\$extdirdir$base.pm"
411 if -f $basepm && $base ne 'XSLoader';
418 find( sub { push @found, $File::Find::name if -f $_ }, 'lib' );
419 for my $found (@found) {
420 my ($short) = ( $found =~ m/^lib.(.+)/ );
423 $lst{"$dir\\$found"} = "$targetroot\\$short";
426 if ( my @pm = glob("*.pm */*.pm") ) {
428 next if $pm =~ m:^t/:;
430 $lst{"$dir\\$pm"} = "$targetroot\\$extdirdir$pm";
433 if ( my @c = glob("*.c *.cpp */*.c */*.cpp") ) {
434 @c = grep { ! m:^zlib-src/: } @c if $ext eq 'ext\Compress\Zlib';
440 if ( my @h = glob("*.h */*.h") ) {
441 @h = grep { ! m:^zlib-src/: } @h if $ext eq 'ext\Compress\Zlib';
445 $incdir{"$dir\\$h"}++ unless $h eq ".";
448 if ( exists $EXTCFG{$ext} ) {
449 for my $cfg ( @{ $EXTCFG{$ext} } ) {
450 if ( $cfg =~ /^([-+])?(.+\.(c|cpp|h))$/ ) {
451 my $o = defined $1 ? $1 : '+';
454 for my $f ( glob($f) ) {
456 warn "$0: no source file $dir\\$f\n" unless -f $f;
457 $src{$f}++ unless $cfg =~ /\.h$/;
458 if ( $f =~ m:^(.+)\\[^\\]+$: ) {
462 elsif ( $o eq '-' ) {
467 if ( $cfg =~ /^([-+])?(.+\.(pm|pl|inc))$/ ) {
468 my $o = defined $1 ? $1 : '+';
471 for my $f ( glob($f) ) {
473 warn "$0: no Perl file $dir\\$f\n" unless -f $f;
474 $lst{"$dir\\$f"} = "$targetroot\\$extdir\\$f";
476 elsif ( $o eq '-' ) {
477 delete $lst{"$dir\\$f"};
481 if ( $cfg eq 'CONST' && !$ran_PL++ ) {
482 run_PL( "Makefile.PL", $dir, "const-xs.inc" );
486 unless ( $ran_PL++ ) {
487 run_PL( "Makefile.PL", $dir ) if -f "Makefile.PL";
489 if ( $dir eq "ext\\Errno" ) {
490 run_PL( "Errno_pm.PL", $dir, "Errno.pm" );
491 $lst{"$dir\\Errno.pm"} = "$targetroot\\Errno.pm";
493 elsif ( $dir eq "ext\\Devel\\PPPort" ) {
494 run_PL( "ppport_h.PL", $dir, "ppport.h" );
496 elsif ( $dir eq "ext\\DynaLoader" ) {
497 run_PL( "XSLoader_pm.PL", $dir, "XSLoader.pm" );
498 $lst{"ext\\DynaLoader\\XSLoader.pm"} = "$targetroot\\XSLoader.pm";
500 elsif ( $dir eq "ext\\Encode" ) {
501 system_echo("perl bin\\enc2xs -Q -O -o def_t.c -f def_t.fnm") == 0
502 or die "$0: running enc2xs failed: $!\n";
505 my @lst = sort keys %lst;
507 read_mmp( \%CONF, "_init.mmp" );
508 read_mmp( \%CONF, "$base.mmp" );
511 my %MM; # MakeMaker results
512 my @MM = qw(VERSION XS_VERSION);
513 if ( -f "Makefile" ) {
514 print "\tReading MakeMaker Makefile...\n";
515 if ( open( MAKEFILE, "Makefile" ) ) {
527 warn "$0: Makefile: $!";
529 print "\tDeleting MakeMaker Makefile.\n";
535 if ( defined $CONF{EXTVERSION} ) {
536 my $EXTVERSION = $CONF{EXTVERSION};
537 print "\tUsing $EXTVERSION for version...\n";
538 $MM{VERSION} = $MM{XS_VERSION} = $EXTVERSION;
540 die "VERSION or XS_VERSION undefined\n"
541 unless defined $MM{VERSION} && defined $MM{XS_VERSION};
542 if ( open( BASE_C, ">$basec" ) ) {
543 print BASE_C <<__EOF__;
545 #define VERSION "$MM{VERSION}"
548 #define XS_VERSION "$MM{XS_VERSION}"
554 warn "$0: $basec: $!";
558 "perl -I$BUILDROOT\\lib -I$PERLSDK\\lib $extu\\xsubpp -csuffix .cpp -typemap $extu\\typemap -noprototypes $basexs >> $basec"
563 die "$0: perl xsubpp failed: $!\n";
567 open( _INIT_C, ">_init.c" ) or die "$!: _init.c: $!\n";
568 print _INIT_C <<__EOF__;
571 EXPORT_C void _init(void *handle) {
576 my @src = ( "_init.c", sort keys %src );
578 if ( $base eq "Encode" ) { # Currently unused.
579 for my $submf ( glob("*/Makefile") ) {
580 my $d = dirname($submf);
581 print "Configuring Encode::$d...\n";
582 if ( open( SUBMF, $submf ) ) {
583 if ( update_dir($d) ) {
586 next if 1 .. /postamble/;
587 if (m!^(\w+_t)\.c : !) {
589 "perl ..\\bin\\enc2xs -Q -o $1.c -f $1.fnm")
591 or warn "$0: enc2xs: $!\n";
592 push @subsrc, "$1.c";
598 $subbase =~ s!/!::!g;
599 write_mmp( $subbase, ["..\\Encode"], "$subbase.c",
601 write_makefile( $subbase, $build );
602 write_bld_inf($subbase);
606 "perl -I$BUILDROOT\\lib ..\\$extu\\xsubpp -csuffix .cpp -typemap ..\\$extu\\typemap -noprototypes $subbase.xs > $subbase.c"
611 die "$0: perl xsubpp failed: $!\n";
616 warn "$0: chdir $d: $!\n";
620 warn "$0: $submf: $!";
623 print "Configuring Encode...\n";
626 write_mmp( $base, [ keys %incdir ], @src );
627 write_makefile( $base, $build );
630 $lstname =~ s:^ext\\::;
631 $lstname =~ s:\\:-:g;
632 print "\t$lstname.lst\n";
634 $CoreBuild ? "$BUILDROOT/symbian/$lstname.lst" : "$BUILDROOT/$lstname.lst";
635 if ( open( my $lst, ">$lstout" ) ) {
636 for my $f (@lst) { print $lst qq["$f"-"!:$lst{$f}"\n] }
640 die "$0: $lstout: $!\n";
642 update_dir($BUILDROOT);
651 for my $ext (@ARGV) {
658 $cfg = $2 if $ext =~ s/(.+?),(.+)/$1/;
663 if ( $ext =~ /\.xs$/ && !-f $ext ) {
664 if ( -f "ext\\$ext" ) {
666 $dir = dirname($ext);
670 if ( -d "ext\\$ext" ) {
675 $dir = "." unless defined $dir;
678 if ( $ext =~ /\.xs$/ && -f $ext ) {
679 $ext = dirname($ext);
687 if ( $ext eq "XSLoader" ) {
688 $ext = "ext\\XSLoader";
690 if ( $ext eq "ext\\XSLoader" ) {
691 $dir = "ext\\DynaLoader";
694 $EXTCFG{$ext} = [ split( /,/, $cfg ) ] if defined $cfg;
696 die "$0: no lib\\Config.pm\n"
697 if $CoreBuild && $Build && !-f "lib\\Config.pm";
700 open( my $cfg, "symbian/install.cfg" )
701 or die "$0: symbian/install.cfg: $!\n";
703 $extdir =~ s:^ext\\::;
705 next unless /^ext\s+(.+)/;
708 my @ext = split( ' ', $ext );
709 $EXTCFG{"ext\\$ext[0]"} = [@ext];
714 if ( $Config || $Build ) {
715 xsconfig( $ext, $dir ) or die "$0: xsconfig '$ext' failed\n";
719 my $chdir = $ext eq "ext\\XSLoader" ? "ext\\DynaLoader" : $dir;
720 die "$0: no directory '$chdir'\n" unless -d $chdir;
721 update_dir($chdir) or die "$0: chdir '$chdir' failed: $!\n";
725 my @ext = split( /\\/, $ext );
728 if ( $Clean || $DistClean ) {
729 print "Cleaning $ext...\n";
733 unlink("const-c.inc");
734 unlink("const-xs.inc");
738 if ( $Build && $ext ne "ext\\XSLoader" && $ext ne "ext\\Errno" ) {
740 # We compile the extension three (3) times.
741 # (1) Only the _init.c to get _init() as the ordinal 1 function in the DLL.
742 # (2) With the rest and the _init.c to get ordinals for the rest.
743 # (3) With an updated _init.c that carries the symbols from step (2).
745 system_echo("make clean");
746 system_echo("make defrost") == 0 or die "$0: make defrost failed\n";
750 push @TARGET, 'sis' if $Sis;
753 # Hide all but the _init.c.
754 print "\n*** $ext - Compile 1 of 3.\n\n";
755 print "(patching $base.mmp)\n";
757 "perl -pi.bak -e \"s:^SOURCE\\s+_init.c:SOURCE\\t_init.c // :\" $base.mmp"
759 system_echo("bldmake bldfiles");
760 system_echo("make @TARGET") == 0 or die "$0: make #1 failed\n";
763 # Reveal the rest again.
764 print "\n*** $ext - Compile 2 of 3.\n\n";
765 print "(patching $base.mmp)\n";
767 "perl -pi.bak -e \"s:^SOURCE\\t_init.c // :SOURCE\\t_init.c :\" $base.mmp"
769 system_echo("make @TARGET") == 0 or die "$0: make #2 failed\n";
770 unlink("$base.mmp.bak");
772 open( _INIT_C, ">_init.c" ) or die "$0: _init.c: $!\n";
773 print _INIT_C <<'__EOF__';
777 /* This is a different but matching definition from in dl_symbian.xs. */
782 } PerlSymbianLibHandle;
784 EXPORT_C void _init(void* handle) {
790 for my $f ("$SDK\\Epoc32\\Build$CWD\\$base\\WINS\\$base.def",
791 "..\\BMARM\\${base}u.def") {
793 if ( open( $def, $f ) ) {
801 unless (defined $basef) {
802 die "$0: failed to find .def for $base\n";
805 next while 1 .. /^EXPORTS/;
806 if (/^\s*(\w+) \@ (\d+) /) {
812 my @symbol = sort keys %symbol;
814 print _INIT_C <<'__EOF__';
816 PerlSymbianLibHandle* h = (PerlSymbianLibHandle*)handle;
818 h->symbols = newHV();
821 for my $sym (@symbol) {
822 my $len = length($sym);
823 print _INIT_C <<__EOF__;
824 hv_store(h->symbols, "$sym", $len, newSViv($symbol{$sym}), 0);
829 die "$0: $basef: no exports found\n";
832 print _INIT_C <<'__EOF__';
838 # Compile #3. This is for real.
839 print "\n*** $ext - Compile 3 of 3.\n\n";
840 system_echo("make @TARGET") == 0 or die "$0: make #3 failed\n";
843 elsif ( $Clean || $DistClean ) {
844 if ( $ext eq "ext\\Errno" ) {
845 unlink( "Errno.pm", "Makefile" );
848 if ( -f "Makefile" ) {
850 system_echo("make clean") == 0 or die "$0: make clean failed\n";
853 system_echo("make distclean") == 0
854 or die "$0: make distclean failed\n";
857 if ( $ext eq "ext\\Devel\\PPPort" ) {
861 my @B = glob("ext/BWINS ext/BMARM ext/*/BWINS ext/*/BMARM Makefile");
865 update_dir($BUILDROOT);