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 for my $ui ( @{$userinclude} ) {
219 if ( $ui =~ m!^(?:[CD]:)?\\! ) {
220 push @{ $CONF{USERINCLUDE} }, $ui;
223 push @{ $CONF{USERINCLUDE} }, "$BUILDROOT\\$ui";
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";
238 for my $u (qw(SOURCE SOURCEPATH SYSTEMINCLUDE USERINCLUDE LIBRARY MACRO)) {
239 $CONF{$u} = uniquefy_filenames( $CONF{$u} );
241 open( BASE_MMP, ">$base.mmp" ) or die "$0: $base.mmp: $!\n";
243 print BASE_MMP <<__EOF__;
246 TARGETPATH $CONF{TARGETPATH}
247 SOURCE @{$CONF{SOURCE}}
250 for my $u (qw(SOURCEPATH SYSTEMINCLUDE USERINCLUDE)) {
251 for my $v ( @{ $CONF{$u} } ) {
252 print BASE_MMP "$u\t$v\n";
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}}
267 my ( $base, $build ) = @_;
269 print "\tMakefile\n";
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";
276 my $wrap = $SDK && $S60SDK eq '1.2' && $SDK !~ /_CW$/;
277 my $ABLD = $wrap ? 'perl b.pl' : 'abld';
279 open( MAKEFILE, ">Makefile" ) or die "$0: Makefile: $!\n";
280 print MAKEFILE <<__EOF__;
287 sis: build_arm freeze_arm
289 build: abld.bat build_win build_arm
296 \$(ABLD) build \$(WIN) udeb
300 \$(ABLD) build \$(ARM) $UARM
302 win: build_win freeze_win
304 arm: build_arm freeze_arm
306 freeze: freeze_win freeze_arm
310 \$(ABLD) freeze \$(WIN) $base
314 \$(ABLD) freeze \$(ARM) $base
316 defrost: defrost_win defrost_arm
326 clean: clean_win clean_arm
329 \$(ABLD) clean \$(WIN)
332 \$(ABLD) clean \$(ARM)
334 realclean: clean realclean_win realclean_arm
336 -del /f $base.c $base.mmp
339 \$(ABLD) reallyclean \$(WIN)
342 \$(ABLD) reallyclean \$(ARM)
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
351 if(open(B,">b.pl")) {
355 # nmake doesn't like MFLAGS and MAKEFLAGS being set to -w and w.
357 delete $ENV{MAKEFLAGS};
359 print "abld @ARGV\n";
360 system_echo("abld @ARGV");
364 warn "$0: failed to create b.pl: $!\n";
370 print "[chdir from ", getcwd(), " to ";
371 chdir(shift) or return;
373 print getcwd(), "]\n";
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";
387 if ( $dir =~ m:^ext\\(.+): ) {
390 elsif ( $dir ne "." ) {
393 my $extdirdir = dirname($extdir);
394 my $targetroot = "\\System\\Libs\\Perl\\$R_V_SV";
395 write_bld_inf($base) if -f $basexs;
400 $extdirdir = $extdirdir eq "." ? "" : "$extdirdir\\";
403 $lst{"$UREL\\$base.dll"} =
404 "$targetroot\\$ARM-symbian\\$base.dll"
406 $lst{"$dir\\$base.pm"} = "$targetroot\\$extdirdir$base.pm"
407 if -f $basepm && $base ne 'XSLoader';
414 find( sub { push @found, $File::Find::name if -f $_ }, 'lib' );
415 for my $found (@found) {
416 my ($short) = ( $found =~ m/^lib.(.+)/ );
419 $lst{"$dir\\$found"} = "$targetroot\\$short";
422 if ( my @pm = glob("*.pm */*.pm") ) {
424 next if $pm =~ m:^t/:;
426 $lst{"$dir\\$pm"} = "$targetroot\\$extdirdir$pm";
429 if ( my @c = glob("*.c *.cpp */*.c */*.cpp") ) {
435 if ( my @h = glob("*.h */*.h") ) {
439 $incdir{"$dir\\$h"}++ unless $h eq ".";
442 if ( exists $EXTCFG{$ext} ) {
443 for my $cfg ( @{ $EXTCFG{$ext} } ) {
444 if ( $cfg =~ /^([-+])?(.+\.(c|cpp|h))$/ ) {
445 my $o = defined $1 ? $1 : '+';
448 for my $f ( glob($f) ) {
450 warn "$0: no source file $dir\\$f\n" unless -f $f;
451 $src{$f}++ unless $cfg =~ /\.h$/;
452 if ( $f =~ m:^(.+)\\[^\\]+$: ) {
456 elsif ( $o eq '-' ) {
461 if ( $cfg =~ /^([-+])?(.+\.(pm|pl|inc))$/ ) {
462 my $o = defined $1 ? $1 : '+';
465 for my $f ( glob($f) ) {
467 warn "$0: no Perl file $dir\\$f\n" unless -f $f;
468 $lst{"$dir\\$f"} = "$targetroot\\$extdir\\$f";
470 elsif ( $o eq '-' ) {
471 delete $lst{"$dir\\$f"};
475 if ( $cfg eq 'CONST' && !$ran_PL++ ) {
476 run_PL( "Makefile.PL", $dir, "const-xs.inc" );
480 unless ( $ran_PL++ ) {
481 run_PL( "Makefile.PL", $dir ) if -f "Makefile.PL";
483 if ( $dir eq "ext\\Errno" ) {
484 run_PL( "Errno_pm.PL", $dir, "Errno.pm" );
485 $lst{"$dir\\Errno.pm"} = "$targetroot\\Errno.pm";
487 elsif ( $dir eq "ext\\Devel\\PPPort" ) {
488 run_PL( "ppport_h.PL", $dir, "ppport.h" );
490 elsif ( $dir eq "ext\\DynaLoader" ) {
491 run_PL( "XSLoader_pm.PL", $dir, "XSLoader.pm" );
492 $lst{"ext\\DynaLoader\\XSLoader.pm"} = "$targetroot\\XSLoader.pm";
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";
499 my @lst = sort keys %lst;
501 read_mmp( \%CONF, "_init.mmp" );
502 read_mmp( \%CONF, "$base.mmp" );
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" ) ) {
521 warn "$0: Makefile: $!";
523 print "\tDeleting MakeMaker Makefile.\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;
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__;
539 #define VERSION "$MM{VERSION}"
542 #define XS_VERSION "$MM{XS_VERSION}"
548 warn "$0: $basec: $!";
552 "perl -I$BUILDROOT\\lib -I$PERLSDK\\lib $extu\\xsubpp -csuffix .cpp -typemap $extu\\typemap -noprototypes $basexs >> $basec"
557 die "$0: perl xsubpp failed: $!\n";
561 open( _INIT_C, ">_init.c" ) or die "$!: _init.c: $!\n";
562 print _INIT_C <<__EOF__;
565 EXPORT_C void _init(void *handle) {
570 my @src = ( "_init.c", sort keys %src );
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) ) {
580 next if 1 .. /postamble/;
581 if (m!^(\w+_t)\.c : !) {
583 "perl ..\\bin\\enc2xs -Q -o $1.c -f $1.fnm")
585 or warn "$0: enc2xs: $!\n";
586 push @subsrc, "$1.c";
592 $subbase =~ s!/!::!g;
593 write_mmp( $subbase, ["..\\Encode"], "$subbase.c",
595 write_makefile( $subbase, $build );
596 write_bld_inf($subbase);
600 "perl -I$BUILDROOT\\lib ..\\$extu\\xsubpp -csuffix .cpp -typemap ..\\$extu\\typemap -noprototypes $subbase.xs > $subbase.c"
605 die "$0: perl xsubpp failed: $!\n";
610 warn "$0: chdir $d: $!\n";
614 warn "$0: $submf: $!";
617 print "Configuring Encode...\n";
620 write_mmp( $base, [ keys %incdir ], @src );
621 write_makefile( $base, $build );
624 $lstname =~ s:^ext\\::;
625 $lstname =~ s:\\:-:g;
626 print "\t$lstname.lst\n";
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] }
634 die "$0: $lstout: $!\n";
636 update_dir($BUILDROOT);
645 for my $ext (@ARGV) {
652 $cfg = $2 if $ext =~ s/(.+?),(.+)/$1/;
657 if ( $ext =~ /\.xs$/ && !-f $ext ) {
658 if ( -f "ext\\$ext" ) {
660 $dir = dirname($ext);
664 if ( -d "ext\\$ext" ) {
669 $dir = "." unless defined $dir;
672 if ( $ext =~ /\.xs$/ && -f $ext ) {
673 $ext = dirname($ext);
681 if ( $ext eq "XSLoader" ) {
682 $ext = "ext\\XSLoader";
684 if ( $ext eq "ext\\XSLoader" ) {
685 $dir = "ext\\DynaLoader";
688 $EXTCFG{$ext} = [ split( /,/, $cfg ) ] if defined $cfg;
690 die "$0: no lib\\Config.pm\n"
691 if $CoreBuild && $Build && !-f "lib\\Config.pm";
694 open( my $cfg, "symbian/install.cfg" )
695 or die "$0: symbian/install.cfg: $!\n";
697 $extdir =~ s:^ext\\::;
699 next unless /^ext\s+(.+)/;
702 my @ext = split( ' ', $ext );
703 $EXTCFG{"ext\\$ext[0]"} = [@ext];
708 if ( $Config || $Build ) {
709 xsconfig( $ext, $dir ) or die "$0: xsconfig '$ext' failed\n";
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";
719 my @ext = split( /\\/, $ext );
722 if ( $Clean || $DistClean ) {
723 print "Cleaning $ext...\n";
727 unlink("const-c.inc");
728 unlink("const-xs.inc");
732 if ( $Build && $ext ne "ext\\XSLoader" && $ext ne "ext\\Errno" ) {
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).
739 system_echo("make clean");
740 system_echo("make defrost") == 0 or die "$0: make defrost failed\n";
744 push @TARGET, 'sis' if $Sis;
747 # Hide all but the _init.c.
748 print "\n*** $ext - Compile 1 of 3.\n\n";
749 print "(patching $base.mmp)\n";
751 "perl -pi.bak -e \"s:^SOURCE\\s+_init.c:SOURCE\\t_init.c // :\" $base.mmp"
753 system_echo("bldmake bldfiles");
754 system_echo("make @TARGET") == 0 or die "$0: make #1 failed\n";
757 # Reveal the rest again.
758 print "\n*** $ext - Compile 2 of 3.\n\n";
759 print "(patching $base.mmp)\n";
761 "perl -pi.bak -e \"s:^SOURCE\\t_init.c // :SOURCE\\t_init.c :\" $base.mmp"
763 system_echo("make @TARGET") == 0 or die "$0: make #2 failed\n";
764 unlink("$base.mmp.bak");
766 open( _INIT_C, ">_init.c" ) or die "$0: _init.c: $!\n";
767 print _INIT_C <<'__EOF__';
771 /* This is a different but matching definition from in dl_symbian.xs. */
776 } PerlSymbianLibHandle;
778 EXPORT_C void _init(void* handle) {
784 for my $f ("$SDK\\Epoc32\\Build$CWD\\$base\\WINS\\$base.def",
785 "..\\BMARM\\${base}u.def") {
787 if ( open( $def, $f ) ) {
795 unless (defined $basef) {
796 die "$0: failed to find .def for $base\n";
799 next while 1 .. /^EXPORTS/;
800 if (/^\s*(\w+) \@ (\d+) /) {
806 my @symbol = sort keys %symbol;
808 print _INIT_C <<'__EOF__';
810 PerlSymbianLibHandle* h = (PerlSymbianLibHandle*)handle;
812 h->symbols = newHV();
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);
823 die "$0: $basef: no exports found\n";
826 print _INIT_C <<'__EOF__';
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";
837 elsif ( $Clean || $DistClean ) {
838 if ( $ext eq "ext\\Errno" ) {
839 unlink( "Errno.pm", "Makefile" );
842 if ( -f "Makefile" ) {
844 system_echo("make clean") == 0 or die "$0: make clean failed\n";
847 system_echo("make distclean") == 0
848 or die "$0: make distclean failed\n";
851 if ( $ext eq "ext\\Devel\\PPPort" ) {
855 my @B = glob("ext/BWINS ext/BMARM ext/*/BWINS ext/*/BMARM Makefile");
859 update_dir($BUILDROOT);