This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In buildtoc, remove whitespace only lines just before output.
[perl5.git] / vms / gen_shrfls.pl
1 # Create global symbol declarations, transfer vector, and
2 # linker options files for PerlShr.
3 #
4 # Input:
5 #    $cflags - command line qualifiers passed to cc when preprocesing perl.h
6 #        Note: A rather simple-minded attempt is made to restore quotes to
7 #        a /Define clause - use with care.
8 #    $objsuffix - file type (including '.') used for object files.
9 #    $libperl - Perl object library.
10 #    $extnames - package names for static extensions (used to generate
11 #        linker options file entries for boot functions)
12 #    $rtlopt - name of options file specifying RTLs to which PerlShr.Exe
13 #        must be linked
14 #
15 # Output:
16 #    PerlShr_Attr.Opt - linker options file which specifies that global vars
17 #        be placed in NOSHR,WRT psects.  Use when linking any object files
18 #        against PerlShr.Exe, since cc places global vars in SHR,WRT psects
19 #        by default.
20 #    PerlShr_Bld.Opt - declares universal symbols for PerlShr.Exe
21 #    Perlshr_Gbl*.Mar, Perlshr_Gbl*.Obj (VAX  only) - declares global symbols
22 #        for global vars (done here because gcc can't globaldef) and creates
23 #        transfer vectors for routines on a VAX.
24 #    PerlShr_Gbl.Opt (VAX only) - list of PerlShr_Gbl*.Obj, used for input
25 #        to the linker when building PerlShr.Exe.
26 #
27 # To do:
28 #   - figure out a good way to collect global vars in one psect, given that
29 #     we can't use globaldef because of gcc.
30 #   - then, check for existing files and preserve symbol and transfer vector
31 #     order for upward compatibility
32 #   - then, add GSMATCH to options file - but how do we insure that new
33 #     library has everything old one did
34 #     (i.e. /Define=DEBUGGING,EMBED,MULTIPLICITY)?
35 #
36 # Author: Charles Bailey  bailey@newman.upenn.edu
37
38 use strict;
39 require 5.000;
40
41 my $debug = $ENV{'GEN_SHRFLS_DEBUG'};
42
43 print "gen_shrfls.pl Rev. 30-Sep-2010\n" if $debug;
44
45 if ($ARGV[0] eq '-f') {
46   open(INP,$ARGV[1]) or die "Can't read input file $ARGV[1]: $!\n";
47   print "Input taken from file $ARGV[1]\n" if $debug;
48   @ARGV = ();
49   while (<INP>) {
50     chomp;
51     push(@ARGV,split(/\|/,$_));
52   }
53   close INP;
54   print "Read input data | ",join(' | ',@ARGV)," |\n" if $debug > 1;
55 }
56
57 my $cc_cmd = shift @ARGV;
58 my $cpp_file;
59
60 # Someday, we'll have $GetSyI built into perl . . .
61 my $isvax = `\$ Write Sys\$Output \(F\$GetSyI(\"HW_MODEL\") .LE. 1024 .AND. F\$GetSyI(\"HW_MODEL\") .GT. 0\)`;
62 chomp $isvax;
63 print "\$isvax: \\$isvax\\\n" if $debug;
64
65 my $isi64 = `\$ Write Sys\$Output \(F\$GetSyI(\"HW_MODEL\") .GE. 4096)`;
66 chomp $isi64;
67 print "\$isi64: \\$isi64\\\n" if $debug;
68
69 print "Input \$cc_cmd: \\$cc_cmd\\\n" if $debug;
70 my $docc = ($cc_cmd !~ /^~~/);
71 print "\$docc = $docc\n" if $debug;
72
73 my ( $use_threads, $use_mymalloc, $care_about_case, $shorten_symbols,
74      $debugging_enabled, $hide_mymalloc, $isgcc, $use_perlio, $dir )
75    = ( 0, 0, 0, 0, 0, 0, 0, 0 );
76
77 if ($docc) {
78   if (-f 'perl.h') { $dir = '[]'; }
79   elsif (-f '[-]perl.h') { $dir = '[-]'; }
80   else { die "$0: Can't find perl.h\n"; }
81
82   # Go see what is enabled in config.sh
83   my $config = $dir . "config.sh";
84   open CONFIG, '<', $config;
85   while(<CONFIG>) {
86     $use_threads++ if /usethreads='(define|yes|true|t|y|1)'/i;
87     $use_mymalloc++ if /usemymalloc='(define|yes|true|t|y|1)'/i;
88     $care_about_case++ if /d_vms_case_sensitive_symbols='(define|yes|true|t|y|1)'/i;
89     $shorten_symbols++ if /d_vms_shorten_long_symbols='(define|yes|true|t|y|1)'/i;
90     $debugging_enabled++ if /usedebugging_perl='(define|yes|true|t|y|1)'/i;
91     $hide_mymalloc++ if /embedmymalloc='(define|yes|true|t|y|1)'/i;
92     $isgcc++ if /gccversion='[^']/;
93     $use_perlio++ if /useperlio='(define|yes|true|t|y|1)'/i;
94   }
95   close CONFIG;
96   
97   # put quotes back onto defines - they were removed by DCL on the way in
98   if (my ($prefix,$defines,$suffix) =
99          ($cc_cmd =~ m#(.*)/Define=(.*?)([/\s].*)#i)) {
100     $defines =~ s/^\((.*)\)$/$1/;
101     $debugging_enabled ||= $defines =~ /\bDEBUGGING\b/;
102     my @defines = split(/,/,$defines);
103     $cc_cmd = "$prefix/Define=(" . join(',',grep($_ = "\"$_\"",@defines)) 
104               . ')' . $suffix;
105   }
106   print "Filtered \$cc_cmd: \\$cc_cmd\\\n" if $debug;
107
108   # check for gcc - if present, we'll need to use MACRO hack to
109   # define global symbols for shared variables
110
111   print "\$isgcc: $isgcc\n" if $debug;
112   print "\$debugging_enabled: $debugging_enabled\n" if $debug;
113
114 }
115 else { 
116   (undef,undef,$cpp_file,$cc_cmd) = split(/~~/,$cc_cmd,4);
117   $isgcc = $cc_cmd =~ /case_hack/i
118            or 0;  # for nice debug output
119   $debugging_enabled = $cc_cmd =~ /\bdebugging\b/i;
120   print "\$isgcc: \\$isgcc\\\n" if $debug;
121   print "\$debugging_enabled: \\$debugging_enabled\\\n" if $debug;
122   print "Not running cc, preprocesor output in \\$cpp_file\\\n" if $debug;
123 }
124
125 my $objsuffix = shift @ARGV;
126 print "\$objsuffix: \\$objsuffix\\\n" if $debug;
127 my $dbgprefix = shift @ARGV;
128 print "\$dbgprefix: \\$dbgprefix\\\n" if $debug;
129 my $olbsuffix = shift @ARGV;
130 print "\$olbsuffix: \\$olbsuffix\\\n" if $debug;
131 my $libperl = "${dbgprefix}libperl$olbsuffix";
132 my $extnames = shift @ARGV;
133 print "\$extnames: \\$extnames\\\n" if $debug;
134 my $rtlopt = shift @ARGV;
135 print "\$rtlopt: \\$rtlopt\\\n" if $debug;
136
137 my (%vars, %cvars, %fcns);
138
139 # These are symbols that we should not export.  They may merely
140 # look like exportable symbols but aren't, or they may be declared
141 # as exportable symbols but there is no function implementing them
142 # (possibly due to an alias).
143
144 my %symbols_to_exclude = (
145   '__attribute__format__'  => 1,
146   'main'                   => 1,
147   'Perl_pp_avalues'        => 1,
148   'Perl_pp_reach'          => 1,
149   'Perl_pp_rvalues'        => 1,
150   'Perl_pp_say'            => 1,
151   'Perl_pp_transr'         => 1,
152   'sizeof'                 => 1,
153 );
154
155 sub scan_var {
156   my($line) = @_;
157   my($const) = $line =~ /^EXTCONST/;
158
159   print "\tchecking for global variable\n" if $debug > 1;
160   $line =~ s/\s*EXT/EXT/;
161   $line =~ s/INIT\s*\(.*\)//;
162   $line =~ s/\[.*//;
163   $line =~ s/=.*//;
164   $line =~ s/\W*;?\s*$//;
165   $line =~ s/\W*\)\s*\(.*$//; # closing paren for args stripped in previous stmt
166   print "\tfiltered to \\$line\\\n" if $debug > 1;
167   if ($line =~ /(\w+)$/) {
168     print "\tvar name is \\$1\\" . ($const ? ' (const)' : '') . "\n" if $debug > 1;
169    if ($const) { $cvars{$1}++; }
170    else        { $vars{$1}++;  }
171   }
172 }
173
174 sub scan_func {
175   my @lines = split /;/, $_[0];
176
177   for my $line (@lines) {
178     print "\tchecking for global routine\n" if $debug > 1;
179     $line =~ s/\b(IV|Off_t|Size_t|SSize_t|void|int)\b//i;
180     if ( $line =~ /(\w+)\s*\(/ ) {
181       print "\troutine name is \\$1\\\n" if $debug > 1;
182       if (exists($symbols_to_exclude{$1})
183           || ($1 eq 'Perl_stashpv_hvname_match' && ! $use_threads)) {
184         print "\tskipped\n" if $debug > 1;
185       }
186       else { $fcns{$1}++ }
187     }
188   }
189 }
190
191 # Go add some right up front if we need 'em
192 if ($use_mymalloc) {
193   $fcns{'Perl_malloc'}++;
194   $fcns{'Perl_calloc'}++;
195   $fcns{'Perl_realloc'}++;
196   $fcns{'Perl_mfree'}++;
197 }
198
199 my ($used_expectation_enum, $used_opcode_enum) = (0, 0); # avoid warnings
200 if ($docc) {
201   1 while unlink 'perlincludes.tmp';
202   END { 1 while unlink 'perlincludes.tmp'; }  # and clean up after
203
204   open(PERLINC, '>', 'perlincludes.tmp') or die "Couldn't open 'perlincludes.tmp' $!";
205
206   print PERLINC qq/#include "${dir}perl.h"\n/;
207   print PERLINC qq/#include "${dir}perlapi.h"\n/; 
208   print PERLINC qq/#include "${dir}perliol.h"\n/ if $use_perlio;
209   print PERLINC qq/#include "${dir}regcomp.h"\n/;
210
211   close PERLINC;
212   my $preprocess_list = 'perlincludes.tmp';
213
214   open(CPP,"${cc_cmd}/NoObj/PreProc=Sys\$Output $preprocess_list|")
215     or die "$0: Can't preprocess $preprocess_list: $!\n";
216 }
217 else {
218   open(CPP,"$cpp_file") or die "$0: Can't read preprocessed file $cpp_file: $!\n";
219 }
220 my %checkh = map { $_,1 } qw( bytecode byterun intrpvar perlapi perlio perliol 
221                            perlvars proto regcomp thrdvar thread );
222 my $ckfunc = 0;
223 LINE: while (<CPP>) {
224   while (/^#.*vmsish\.h/i .. /^#.*perl\.h/i) {
225     while (/__VMS_PROTOTYPES__/i .. /__VMS_SEPYTOTORP__/i) {
226       print "vms_proto>> $_" if $debug > 2;
227       if (/^\s*EXT(CONST|\s+)/) { &scan_var($_);  }
228       else        { &scan_func($_); }
229       last LINE unless defined($_ = <CPP>);
230     }
231     print "vmsish.h>> $_" if $debug > 2;
232     if (/^\s*EXT(CONST|\s+)/) { &scan_var($_); }
233     last LINE unless defined($_ = <CPP>);
234   }    
235   while (/^#.*opcode\.h/i .. /^#.*perl\.h/i) {
236     print "opcode.h>> $_" if $debug > 2;
237     if (/^OP \*\s/) { &scan_func($_); }
238     if (/^\s*EXT(CONST|\s+)/) { &scan_var($_); }
239     last LINE unless defined($_ = <CPP>);
240   }
241   # Check for transition to new header file
242   my $scanname;
243   if (/^# \d+ "(\S+)"/) {
244     my $spec = $1;
245     # Pull name from library module or header filespec
246     $spec =~ /^(\w+)$/ or $spec =~ /(\w+)\.h/i;
247     my $name = lc $1;
248     $ckfunc = exists $checkh{$name} ? 1 : 0;
249     $scanname = $name if $ckfunc;
250     print "Header file transition: ckfunc = $ckfunc for $name.h\n" if $debug > 1;
251   }
252   if ($ckfunc) {
253     print "$scanname>> $_" if $debug > 2;
254     if (/^\s*EXT(CONST|\s+)/) { &scan_var($_);  }
255     else           { &scan_func($_); }
256   }
257   else {
258     print $_ if $debug > 3 && ($debug > 5 || length($_));
259     if (/^\s*EXT(CONST|\s+)/) { &scan_var($_); }
260   }
261 }
262 close CPP;
263
264 while (<DATA>) {
265   next if /^#/;
266   s/\s+#.*\n//;
267   next if /^\s*$/;
268   my ($key,$array) = split('=',$_);
269   if ($array eq 'vars') { $key = "PL_$key";   }
270   else                  { $key = "Perl_$key"; }
271   print "Adding $key to \%$array list\n" if $debug > 1;
272   ${$array}{$key}++;
273 }
274 if ($debugging_enabled and $isgcc) { $vars{'colors'}++ }
275 foreach (split /\s+/, $extnames) {
276   my($pkgname) = $_;
277   $pkgname =~ s/::/__/g;
278   $fcns{"boot_$pkgname"}++;
279   print "Adding boot_$pkgname to \%fcns (for extension $_)\n" if $debug;
280 }
281
282 # For symbols over 31 characters, export the shortened name.
283 # TODO: Make this general purpose so we can predict the shortened name the
284 # compiler will generate for any symbol over 31 characters in length.  The
285 # docs to CC/NAMES=SHORTENED describe the CRC used to shorten the name, but
286 # don't describe its use fully enough to actually mimic what the compiler
287 # does.
288
289 if ($shorten_symbols) {
290   if (exists $fcns{'Perl_ck_entersub_args_proto_or_list'}) {
291     delete $fcns{'Perl_ck_entersub_args_proto_or_list'};
292     if ($care_about_case) {
293       $fcns{'Perl_ck_entersub_args_p11c2bjj$'}++;
294     }
295     else {
296       $fcns{'PERL_CK_ENTERSUB_ARGS_P3IAT616$'}++;
297     }
298   }
299 }
300
301 # Eventually, we'll check against existing copies here, so we can add new
302 # symbols to an existing options file in an upwardly-compatible manner.
303
304 my $marord = 1;
305 open(OPTBLD,'>', "${dir}${dbgprefix}perlshr_bld.opt")
306   or die "$0: Can't write to ${dir}${dbgprefix}perlshr_bld.opt: $!\n";
307 if ($isvax) {
308   open(MAR, '>', "${dir}perlshr_gbl${marord}.mar")
309     or die "$0: Can't write to ${dir}perlshr_gbl${marord}.mar: $!\n";
310   print MAR "\t.title perlshr_gbl$marord\n";
311 }
312
313 unless ($isgcc) {
314   if ($isi64) {
315     print OPTBLD "PSECT_ATTR=\$GLOBAL_RO_VARS,NOEXE,RD,NOWRT,SHR\n";
316     print OPTBLD "PSECT_ATTR=\$GLOBAL_RW_VARS,NOEXE,RD,WRT,NOSHR\n";
317   }
318   else {
319     print OPTBLD "PSECT_ATTR=\$GLOBAL_RO_VARS,PIC,NOEXE,RD,NOWRT,SHR\n";
320     print OPTBLD "PSECT_ATTR=\$GLOBAL_RW_VARS,PIC,NOEXE,RD,WRT,NOSHR\n";
321   }
322 }
323 print OPTBLD "case_sensitive=yes\n" if $care_about_case;
324 my $count = 0;
325 foreach my $var (sort (keys %vars,keys %cvars)) {
326   if ($isvax) { print OPTBLD "UNIVERSAL=$var\n"; }
327   else { print OPTBLD "SYMBOL_VECTOR=($var=DATA)\n"; }
328   # This hack brought to you by the lack of a globaldef in gcc.
329   if ($isgcc) {
330     if ($count++ > 200) {  # max 254 psects/file
331       print MAR "\t.end\n";
332       close MAR;
333       $marord++;
334       open(MAR, '>', "${dir}perlshr_gbl${marord}.mar")
335         or die "$0: Can't write to ${dir}perlshr_gbl${marord}.mar: $!\n";
336       print MAR "\t.title perlshr_gbl$marord\n";
337       $count = 0;
338     }
339     print MAR "\t.psect ${var},long,pic,ovr,rd,wrt,noexe,noshr\n";
340     print MAR "\t${var}::       .blkl 1\n";
341   }
342 }
343
344 print MAR "\t.psect \$transfer_vec,pic,rd,nowrt,exe,shr\n" if ($isvax);
345 foreach my $func (sort keys %fcns) {
346   if ($isvax) {
347     print MAR "\t.transfer $func\n";
348     print MAR "\t.mask $func\n";
349     print MAR "\tjmp G\^${func}+2\n";
350   }
351   else { print OPTBLD "SYMBOL_VECTOR=($func=PROCEDURE)\n"; }
352 }
353 if ($isvax) {
354   print MAR "\t.end\n";
355   close MAR;
356 }
357
358 open(OPTATTR, '>', "${dir}perlshr_attr.opt")
359   or die "$0: Can't write to ${dir}perlshr_attr.opt: $!\n";
360 if ($isgcc) {
361   foreach my $var (sort keys %cvars) {
362     print OPTATTR "PSECT_ATTR=${var},PIC,OVR,RD,NOEXE,NOWRT,SHR\n";
363   }
364   foreach my $var (sort keys %vars) {
365     print OPTATTR "PSECT_ATTR=${var},PIC,OVR,RD,NOEXE,WRT,NOSHR\n";
366   }
367 }
368 else {
369   print OPTATTR "! No additional linker directives are needed when using DECC\n";
370 }
371 close OPTATTR;
372
373 my $incstr = 'PERL,GLOBALS';
374 my (@symfiles, $drvrname);
375 if ($isvax) {
376   $drvrname = "Compile_shrmars.tmp_".time;
377   open (DRVR,'>', $drvrname) or die "$0: Can't write to $drvrname: $!\n";
378   print DRVR "\$ Set NoOn\n";  
379   print DRVR "\$ Delete/NoLog/NoConfirm $drvrname;\n";
380   print DRVR "\$ old_proc_vfy = F\$Environment(\"VERIFY_PROCEDURE\")\n";
381   print DRVR "\$ old_img_vfy = F\$Environment(\"VERIFY_IMAGE\")\n";
382   print DRVR "\$ MCR $^X -e \"\$ENV{'LIBPERL_RDT'} = (stat('$libperl'))[9]\"\n";
383   print DRVR "\$ Set Verify\n";
384   print DRVR "\$ If F\$Search(\"$libperl\").eqs.\"\" Then Library/Object/Create $libperl\n";
385   do {
386     push(@symfiles,"perlshr_gbl$marord");
387     print DRVR "\$ Macro/NoDebug/Object=PerlShr_Gbl${marord}$objsuffix PerlShr_Gbl$marord.Mar\n";
388     print DRVR "\$ Library/Object/Replace/Log $libperl PerlShr_Gbl${marord}$objsuffix\n";
389   } while (--$marord); 
390   # We had to have a working miniperl to run this program; it's probably the
391   # one we just built.  It depended on LibPerl, which will be changed when
392   # the PerlShr_Gbl* modules get inserted, so miniperl will be out of date,
393   # and so, therefore, will all of its dependents . . .
394   # We touch LibPerl here so it'll be back 'in date', and we won't rebuild
395   # miniperl etc., and therefore LibPerl, the next time we invoke MM[KS].
396   print DRVR "\$ old_proc_vfy = F\$Verify(old_proc_vfy,old_img_vfy)\n";
397   print DRVR "\$ MCR $^X -e \"utime 0, \$ENV{'LIBPERL_RDT'}, '$libperl'\"\n";
398   close DRVR;
399 }
400
401 # Initial hack to permit building of compatible shareable images for a
402 # given version of Perl.
403 if ($ENV{PERLSHR_USE_GSMATCH}) {
404   if ($ENV{PERLSHR_USE_GSMATCH} eq 'INCLUDE_COMPILE_OPTIONS') {
405     # Build up a major ID. Since it can only be 8 bits, we encode the version
406     # number in the top four bits and use the bottom four for build options
407     # that'll cause incompatibilities
408     my ($ver, $sub) = $] =~ /\.(\d\d\d)(\d\d)/;
409     $ver += 0; $sub += 0;
410     my $gsmatch = ($sub >= 50) ? "equal" : "lequal"; # Force an equal match for
411                                                   # dev, but be more forgiving
412                                                   # for releases
413
414     $ver *=16;
415     $ver += 8 if $debugging_enabled;    # If DEBUGGING is set
416     $ver += 4 if $use_threads;          # if we're threaded
417     $ver += 2 if $use_mymalloc;         # if we're using perl's malloc
418     print OPTBLD "GSMATCH=$gsmatch,$ver,$sub\n";
419   }
420   else {
421     my $major = int($] * 1000)                        & 0xFF;  # range 0..255
422     my $minor = int(($] * 1000 - $major) * 100 + 0.5) & 0xFF;  # range 0..255
423     print OPTBLD "GSMATCH=LEQUAL,$major,$minor\n";
424   }
425   print OPTBLD 'CLUSTER=$$TRANSFER_VECTOR,,',
426                map(",$_$objsuffix",@symfiles), "\n";
427 }
428 elsif (@symfiles) { $incstr .= ',' . join(',',@symfiles); }
429 # Include object modules and RTLs in options file
430 # Linker wants /Include and /Library on different lines
431 print OPTBLD "$libperl/Include=($incstr)\n";
432 print OPTBLD "$libperl/Library\n";
433 open(RTLOPT,$rtlopt) or die "$0: Can't read options file $rtlopt: $!\n";
434 while (<RTLOPT>) { print OPTBLD; }
435 close RTLOPT;
436 close OPTBLD;
437
438 exec "\$ \@$drvrname" if $isvax;
439
440
441 __END__
442
443 # Oddball cases, so we can keep the perl.h scan above simple
444 #Foo=vars    # uncommented becomes PL_Foo
445 #Bar=funcs   # uncommented becomes Perl_Bar