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;
104 $VERSION = $PerlVersion unless defined $VERSION;
106 $VERSION =~ tr/.//d if defined $VERSION;
108 $ENV{SDK} = $SDK; # For the Errno extension
109 $ENV{CROSS} = 1; # For the Encode extension
112 my $UREL = "$SDK\\epoc32\\release\\-ARM-\\$UARM";
114 if (exists $ENV{UREL}) {
115 $UREL = $ENV{UREL}; # from sdk.pl
116 $UREL =~ s/-ARM-/$ARM/;
117 $UARM = $ENV{UARM}; # from sdk.pl
118 $SRCDBG = $UARM eq 'udeb' ? "SRCDBG" : "";
127 open( BLD_INF, ">bld.inf" ) or die "$0: bld.inf: $!\n";
128 print BLD_INF <<__EOF__;
139 print "xsbuild: ", $cmd, "\n";
144 my ( $PL, $dir, $file ) = @_;
145 if ( defined $file ) {
146 print "\t(Running $dir\\$PL to create $file)\n";
150 print "\t(Running $dir\\$PL)\n";
154 # Problem: the Config.pm we have in $BUILDROOT\\lib carries the
155 # version number of the Perl we are building, while the Perl
156 # we are running might have some other version. Solution:
157 # temporarily replace the Config.pm with a patched version.
158 my $V = sprintf "%vd", $^V;
159 unlink("$BUILDROOT\\lib\\Config.pm.bak");
160 print "(patching $BUILDROOT\\lib\\Config.pm)\n";
161 system_echo("perl -pi.bak -e \"s:\\Q$R_V_SV:$V:\" $BUILDROOT\\lib\\Config.pm");
163 system_echo("perl -I$BUILDROOT\\lib -I$BUILDROOT\\xlib\\symbian $PL") == 0
164 or warn "$0: $PL failed.\n";
166 system_echo("copy $BUILDROOT\\lib\\Config.pm.bak $BUILDROOT\\lib\\Config.pm");
168 if ( defined $file ) { -s $file or die "$0: No $file created.\n" }
172 my ( $conf, $k ) = @_;
173 push @{ $conf->{$k} }, split( ' ', $1 ) if /^$k\s(.+)$/;
176 sub uniquefy_filenames {
179 for my $i (@{$_[0]}) {
181 $i = lc $i if $i =~ m!\\!;
183 push @$b, $i unless $c{$i}++;
189 my ( $conf, $mmp ) = @_;
190 if ( -r $mmp && open( MMP, "<$mmp" ) ) {
191 print "\tReading $mmp...\n";
194 $conf->{TARGET} = $1 if /^TARGET\s+(.+)$/;
195 $conf->{TARGETPATH} = $1 if /^TARGETPATH\s+(.+)$/;
196 $conf->{EXTVERSION} = $1 if /^EXTVERSION\s+(.+)$/;
197 read_old_multi( $conf, "SOURCE" );
198 read_old_multi( $conf, "SOURCEPATH" );
199 read_old_multi( $conf, "USERINCLUDE" );
200 read_old_multi( $conf, "SYSTEMINCLUDE" );
201 read_old_multi( $conf, "LIBRARY" );
202 read_old_multi( $conf, "MACRO" );
209 my ( $base, $userinclude, @src ) = @_;
211 print "\t$base.mmp\n";
212 $CONF{TARGET} = "$base.dll";
213 $CONF{TARGETPATH} = "\\System\\Libs\\Perl\\$R_V_SV";
214 $CONF{SOURCE} = [@src];
215 $CONF{SOURCEPATH} = [ $CWD, $BUILDROOT ];
216 $CONF{USERINCLUDE} = [ $CWD, $BUILDROOT ];
217 $CONF{SYSTEMINCLUDE} = ["$PERLSDK\\include"] unless $CoreBuild;
218 $CONF{SYSTEMINCLUDE} = [ $BUILDROOT ] if $CoreBuild;
221 read_mmp( \%CONF, "_init.mmp" );
222 read_mmp( \%CONF, "$base.mmp" );
224 if ($base eq 'Zlib') {
225 push @{$CONF{USERINCLUDE}}, "$CWD\\zlib-src";
228 for my $ui ( @{$userinclude} ) {
230 if ( $ui =~ m!^(?:[CD]:)?\\! ) {
231 push @{ $CONF{USERINCLUDE} }, $ui;
234 push @{ $CONF{USERINCLUDE} }, "$BUILDROOT\\$ui";
237 push @{ $CONF{SYSTEMINCLUDE} }, "\\epoc32\\include";
238 push @{ $CONF{SYSTEMINCLUDE} }, "\\epoc32\\include\\libc";
239 push @{ $CONF{LIBRARY} }, "euser.lib";
240 push @{ $CONF{LIBRARY} }, "estlib.lib";
241 push @{ $CONF{LIBRARY} }, "perl$VERSION.lib";
242 push @{ $CONF{MACRO} }, "SYMBIAN" unless $CoreBuild;
243 push @{ $CONF{MACRO} }, "PERL_EXT" if $CoreBuild;
244 push @{ $CONF{MACRO} }, "MULTIPLICITY";
245 push @{ $CONF{MACRO} }, "PERL_IMPLICIT_CONTEXT";
246 push @{ $CONF{MACRO} }, "PERL_GLOBAL_STRUCT";
247 push @{ $CONF{MACRO} }, "PERL_GLOBAL_STRUCT_PRIVATE";
249 for my $u (qw(SOURCE SOURCEPATH SYSTEMINCLUDE USERINCLUDE LIBRARY MACRO)) {
250 $CONF{$u} = uniquefy_filenames( $CONF{$u} );
252 open( BASE_MMP, ">$base.mmp" ) or die "$0: $base.mmp: $!\n";
254 print BASE_MMP <<__EOF__;
257 TARGETPATH $CONF{TARGETPATH}
258 SOURCE @{$CONF{SOURCE}}
261 for my $u (qw(SOURCEPATH SYSTEMINCLUDE USERINCLUDE)) {
262 for my $v ( @{ $CONF{$u} } ) {
263 print BASE_MMP "$u\t$v\n";
266 # OPTION does not work in MMPs for pre-2.0 SDKs?
267 print BASE_MMP <<__EOF__;
268 LIBRARY @{$CONF{LIBRARY}}
269 MACRO @{$CONF{MACRO}}
278 my ( $base, $build ) = @_;
280 print "\tMakefile\n";
282 my $windef1 = "$SDK\\Epoc32\\Build$CWD\\$base\\$WIN\\$base.def";
283 my $windef2 = "..\\BWINS\\${base}u.def";
284 my $armdef1 = "$SDK\\Epoc32\\Build$CWD\\$base\\$ARM\\$base.def";
285 my $armdef2 = "..\\BMARM\\${base}u.def";
287 my $wrap = $SDK && $S60SDK eq '1.2' && $SDK !~ /_CW$/;
288 my $ABLD = $wrap ? 'perl b.pl' : 'abld';
290 open( MAKEFILE, ">Makefile" ) or die "$0: Makefile: $!\n";
291 print MAKEFILE <<__EOF__;
298 sis: build_arm freeze_arm
300 build: abld.bat build_win build_arm
307 \$(ABLD) build \$(WIN) udeb
311 \$(ABLD) build \$(ARM) $UARM
313 win: build_win freeze_win
315 arm: build_arm freeze_arm
317 freeze: freeze_win freeze_arm
321 \$(ABLD) freeze \$(WIN) $base
325 \$(ABLD) freeze \$(ARM) $base
327 defrost: defrost_win defrost_arm
337 clean: clean_win clean_arm
340 \$(ABLD) clean \$(WIN)
343 \$(ABLD) clean \$(ARM)
345 realclean: clean realclean_win realclean_arm
347 -del /f $base.c $base.mmp
350 \$(ABLD) reallyclean \$(WIN)
353 \$(ABLD) reallyclean \$(ARM)
355 distclean: defrost realclean
356 -rmdir ..\\BWINS ..\\BMARM
357 -del /f const-c.inc const-xs.inc
358 -del /f Makefile abld.bat bld.inf
362 if(open(B,">b.pl")) {
366 # nmake doesn't like MFLAGS and MAKEFLAGS being set to -w and w.
368 delete $ENV{MAKEFLAGS};
370 print "abld @ARGV\n";
371 system_echo("abld @ARGV");
375 warn "$0: failed to create b.pl: $!\n";
381 print "[chdir from ", getcwd(), " to ";
382 chdir(shift) or return;
384 print getcwd(), "]\n";
388 my ( $ext, $dir ) = @_;
389 print "Configuring for $ext, directory '$dir'...\n";
390 my $extu = $CoreBuild ? "$BUILDROOT\\lib\\ExtUtils" : "$PERLSDK\\lib\\ExtUtils";
391 update_dir($dir) or die "$0: chdir '$dir': $!\n";
392 my $build = dirname($ext);
393 my $base = basename($ext);
394 my $basexs = "$base.xs";
395 my $basepm = "$base.pm";
396 my $basec = "$base$CSuffix";
398 if ( $dir =~ m:^ext\\(.+): ) {
401 elsif ( $dir ne "." ) {
404 my $extdirdir = dirname($extdir);
405 my $targetroot = "\\System\\Libs\\Perl\\$R_V_SV";
406 write_bld_inf($base) if -f $basexs;
411 $extdirdir = $extdirdir eq "." ? "" : "$extdirdir\\";
414 $lst{"$UREL\\$base.dll"} =
415 "$targetroot\\$ARM-symbian\\$base.dll"
417 $lst{"$dir\\$base.pm"} = "$targetroot\\$extdirdir$base.pm"
418 if -f $basepm && $base ne 'XSLoader';
425 find( sub { push @found, $File::Find::name if -f $_ }, 'lib' );
426 for my $found (@found) {
427 my ($short) = ( $found =~ m/^lib.(.+)/ );
430 $lst{"$dir\\$found"} = "$targetroot\\$short";
433 if ( my @pm = glob("*.pm */*.pm") ) {
435 next if $pm =~ m:^t/:;
437 $lst{"$dir\\$pm"} = "$targetroot\\$extdirdir$pm";
440 if ( my @c = glob("*.c *.cpp */*.c */*.cpp") ) {
441 @c = grep { ! m:^zlib-src/: } @c if $ext eq 'ext\Compress\Zlib';
447 if ( my @h = glob("*.h */*.h") ) {
448 @h = grep { ! m:^zlib-src/: } @h if $ext eq 'ext\Compress\Zlib';
452 $incdir{"$dir\\$h"}++ unless $h eq ".";
455 if ( exists $EXTCFG{$ext} ) {
456 for my $cfg ( @{ $EXTCFG{$ext} } ) {
457 if ( $cfg =~ /^([-+])?(.+\.(c|cpp|h))$/ ) {
458 my $o = defined $1 ? $1 : '+';
461 for my $f ( glob($f) ) {
463 warn "$0: no source file $dir\\$f\n" unless -f $f;
464 $src{$f}++ unless $cfg =~ /\.h$/;
465 if ( $f =~ m:^(.+)\\[^\\]+$: ) {
469 elsif ( $o eq '-' ) {
474 if ( $cfg =~ /^([-+])?(.+\.(pm|pl|inc))$/ ) {
475 my $o = defined $1 ? $1 : '+';
478 for my $f ( glob($f) ) {
480 warn "$0: no Perl file $dir\\$f\n" unless -f $f;
481 $lst{"$dir\\$f"} = "$targetroot\\$extdir\\$f";
483 elsif ( $o eq '-' ) {
484 delete $lst{"$dir\\$f"};
488 if ( $cfg eq 'CONST' && !$ran_PL++ ) {
489 run_PL( "Makefile.PL", $dir, "const-xs.inc" );
493 unless ( $ran_PL++ ) {
494 run_PL( "Makefile.PL", $dir ) if -f "Makefile.PL";
496 if ( $dir eq "ext\\Errno" ) {
497 run_PL( "Errno_pm.PL", $dir, "Errno.pm" );
498 $lst{"$dir\\Errno.pm"} = "$targetroot\\Errno.pm";
500 elsif ( $dir eq "ext\\Devel\\PPPort" ) {
501 run_PL( "ppport_h.PL", $dir, "ppport.h" );
503 elsif ( $dir eq "ext\\DynaLoader" ) {
504 run_PL( "XSLoader_pm.PL", $dir, "XSLoader.pm" );
505 $lst{"ext\\DynaLoader\\XSLoader.pm"} = "$targetroot\\XSLoader.pm";
507 elsif ( $dir eq "ext\\Encode" ) {
508 system_echo("perl bin\\enc2xs -Q -O -o def_t.c -f def_t.fnm") == 0
509 or die "$0: running enc2xs failed: $!\n";
512 my @lst = sort keys %lst;
514 read_mmp( \%CONF, "_init.mmp" );
515 read_mmp( \%CONF, "$base.mmp" );
518 my %MM; # MakeMaker results
519 my @MM = qw(VERSION XS_VERSION);
520 if ( -f "Makefile" ) {
521 print "\tReading MakeMaker Makefile...\n";
522 if ( open( MAKEFILE, "Makefile" ) ) {
534 warn "$0: Makefile: $!";
536 print "\tDeleting MakeMaker Makefile.\n";
542 if ( defined $CONF{EXTVERSION} ) {
543 my $EXTVERSION = $CONF{EXTVERSION};
544 print "\tUsing $EXTVERSION for version...\n";
545 $MM{VERSION} = $MM{XS_VERSION} = $EXTVERSION;
547 die "VERSION or XS_VERSION undefined\n"
548 unless defined $MM{VERSION} && defined $MM{XS_VERSION};
549 if ( open( BASE_C, ">$basec" ) ) {
550 print BASE_C <<__EOF__;
552 #define VERSION "$MM{VERSION}"
555 #define XS_VERSION "$MM{XS_VERSION}"
561 warn "$0: $basec: $!";
565 "perl -I$BUILDROOT\\lib -I$PERLSDK\\lib $extu\\xsubpp -csuffix .cpp -typemap $extu\\typemap -noprototypes $basexs >> $basec"
570 die "$0: perl xsubpp failed: $!\n";
574 open( _INIT_C, ">_init.c" ) or die "$!: _init.c: $!\n";
575 print _INIT_C <<__EOF__;
578 EXPORT_C void _init(void *handle) {
583 my @src = ( "_init.c", sort keys %src );
585 if ( $base eq "Encode" ) { # Currently unused.
586 for my $submf ( glob("*/Makefile") ) {
587 my $d = dirname($submf);
588 print "Configuring Encode::$d...\n";
589 if ( open( SUBMF, $submf ) ) {
590 if ( update_dir($d) ) {
593 next if 1 .. /postamble/;
594 if (m!^(\w+_t)\.c : !) {
596 "perl ..\\bin\\enc2xs -Q -o $1.c -f $1.fnm")
598 or warn "$0: enc2xs: $!\n";
599 push @subsrc, "$1.c";
605 $subbase =~ s!/!::!g;
606 write_mmp( $subbase, ["..\\Encode"], "$subbase.c",
608 write_makefile( $subbase, $build );
609 write_bld_inf($subbase);
613 "perl -I$BUILDROOT\\lib ..\\$extu\\xsubpp -csuffix .cpp -typemap ..\\$extu\\typemap -noprototypes $subbase.xs > $subbase.c"
618 die "$0: perl xsubpp failed: $!\n";
623 warn "$0: chdir $d: $!\n";
627 warn "$0: $submf: $!";
630 print "Configuring Encode...\n";
633 write_mmp( $base, [ keys %incdir ], @src );
634 write_makefile( $base, $build );
637 $lstname =~ s:^ext\\::;
638 $lstname =~ s:\\:-:g;
639 print "\t$lstname.lst\n";
641 $CoreBuild ? "$BUILDROOT/symbian/$lstname.lst" : "$BUILDROOT/$lstname.lst";
642 if ( open( my $lst, ">$lstout" ) ) {
643 for my $f (@lst) { print $lst qq["$f"-"!:$lst{$f}"\n] }
647 die "$0: $lstout: $!\n";
649 update_dir($BUILDROOT);
658 for my $ext (@ARGV) {
665 $cfg = $2 if $ext =~ s/(.+?),(.+)/$1/;
670 if ( $ext =~ /\.xs$/ && !-f $ext ) {
671 if ( -f "ext\\$ext" ) {
673 $dir = dirname($ext);
677 if ( -d "ext\\$ext" ) {
682 $dir = "." unless defined $dir;
685 if ( $ext =~ /\.xs$/ && -f $ext ) {
686 $ext = dirname($ext);
694 if ( $ext eq "XSLoader" ) {
695 $ext = "ext\\XSLoader";
697 if ( $ext eq "ext\\XSLoader" ) {
698 $dir = "ext\\DynaLoader";
701 $EXTCFG{$ext} = [ split( /,/, $cfg ) ] if defined $cfg;
703 die "$0: no lib\\Config.pm\n"
704 if $CoreBuild && $Build && !-f "lib\\Config.pm";
707 open( my $cfg, "symbian/install.cfg" )
708 or die "$0: symbian/install.cfg: $!\n";
710 $extdir =~ s:^ext\\::;
712 next unless /^ext\s+(.+)/;
715 my @ext = split( ' ', $ext );
716 $EXTCFG{"ext\\$ext[0]"} = [@ext];
721 if ( $Config || $Build ) {
722 xsconfig( $ext, $dir ) or die "$0: xsconfig '$ext' failed\n";
726 my $chdir = $ext eq "ext\\XSLoader" ? "ext\\DynaLoader" : $dir;
727 die "$0: no directory '$chdir'\n" unless -d $chdir;
728 update_dir($chdir) or die "$0: chdir '$chdir' failed: $!\n";
732 my @ext = split( /\\/, $ext );
735 if ( $Clean || $DistClean ) {
736 print "Cleaning $ext...\n";
740 unlink("const-c.inc");
741 unlink("const-xs.inc");
745 if ( $Build && $ext ne "ext\\XSLoader" && $ext ne "ext\\Errno" ) {
747 # We compile the extension three (3) times.
748 # (1) Only the _init.c to get _init() as the ordinal 1 function in the DLL.
749 # (2) With the rest and the _init.c to get ordinals for the rest.
750 # (3) With an updated _init.c that carries the symbols from step (2).
752 system_echo("make clean");
753 system_echo("make defrost") == 0 or warn "$0: make defrost failed\n";
757 push @TARGET, 'sis' if $Sis;
760 # Hide all but the _init.c.
761 print "\n*** $ext - Compile 1 of 3.\n\n";
762 print "(patching $base.mmp)\n";
764 "perl -pi.bak -e \"s:^SOURCE\\s+_init.c:SOURCE\\t_init.c // :\" $base.mmp"
766 system_echo("bldmake bldfiles");
767 system_echo("make @TARGET") == 0 or die "$0: make #1 failed\n";
770 # Reveal the rest again.
771 print "\n*** $ext - Compile 2 of 3.\n\n";
772 print "(patching $base.mmp)\n";
774 "perl -pi.bak -e \"s:^SOURCE\\t_init.c // :SOURCE\\t_init.c :\" $base.mmp"
776 system_echo("make @TARGET") == 0 or die "$0: make #2 failed\n";
777 unlink("$base.mmp.bak");
779 open( _INIT_C, ">_init.c" ) or die "$0: _init.c: $!\n";
780 print _INIT_C <<'__EOF__';
784 /* This is a different but matching definition from in dl_symbian.xs. */
789 } PerlSymbianLibHandle;
791 EXPORT_C void _init(void* handle) {
797 for my $f ("$SDK\\Epoc32\\Build$CWD\\$base\\WINS\\$base.def",
798 "..\\BMARM\\${base}u.def") {
800 if ( open( $def, $f ) ) {
808 unless (defined $basef) {
809 die "$0: failed to find .def for $base\n";
812 next while 1 .. /^EXPORTS/;
813 if (/^\s*(\w+) \@ (\d+) /) {
819 my @symbol = sort keys %symbol;
821 print _INIT_C <<'__EOF__';
823 PerlSymbianLibHandle* h = (PerlSymbianLibHandle*)handle;
825 h->symbols = newHV();
828 for my $sym (@symbol) {
829 my $len = length($sym);
830 print _INIT_C <<__EOF__;
831 hv_store(h->symbols, "$sym", $len, newSViv($symbol{$sym}), 0);
836 die "$0: $basef: no exports found\n";
839 print _INIT_C <<'__EOF__';
845 # Compile #3. This is for real.
846 print "\n*** $ext - Compile 3 of 3.\n\n";
847 system_echo("make @TARGET") == 0 or die "$0: make #3 failed\n";
850 elsif ( $Clean || $DistClean ) {
851 if ( $ext eq "ext\\Errno" ) {
852 unlink( "Errno.pm", "Makefile" );
855 if ( -f "Makefile" ) {
857 system_echo("make clean") == 0 or die "$0: make clean failed\n";
860 system_echo("make distclean") == 0
861 or die "$0: make distclean failed\n";
864 if ( $ext eq "ext\\Devel\\PPPort" ) {
868 my @B = glob("ext/BWINS ext/BMARM ext/*/BWINS ext/*/BMARM Makefile");
872 update_dir($BUILDROOT);