Use fork if available.
[p5sagit/p5-mst-13.2.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 speficies 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@genetics.upenn.edu
37 # Revised: 28-May-1995
38
39 require 5.000;
40
41 $debug = $ENV{'GEN_SHRFLS_DEBUG'};
42 $cc_cmd = shift @ARGV;
43
44 # Someday, we'll have $GetSyI built into perl . . .
45 $isvax = `\$ Write Sys\$Output F\$GetSyI(\"HW_MODEL\")` <= 1024;
46 print "\$isvax: \\$isvax\\\n" if $debug;
47
48 print "Input \$cc_cmd: \\$cc_cmd\\\n" if $debug;
49 $docc = ($cc_cmd !~ /^~~/);
50 print "\$docc = $docc\n" if $debug;
51
52 if ($docc) {
53   # put quotes back onto defines - they were removed by DCL on the way in
54   if (($prefix,$defines,$suffix) =
55          ($cc_cmd =~ m#(.*)/Define=(.*?)([/\s].*)#i)) {
56     $defines =~ s/^\((.*)\)$/$1/;
57     @defines = split(/,/,$defines);
58     $cc_cmd = "$prefix/Define=(" . join(',',grep($_ = "\"$_\"",@defines)) 
59               . ')' . $suffix;
60   }
61   print "Filtered \$cc_cmd: \\$cc_cmd\\\n" if $debug;
62
63   # check for gcc - if present, we'll need to use MACRO hack to
64   # define global symbols for shared variables
65   $isvaxc = 0;
66   $isgcc = `$cc_cmd _nla0:/Version` =~ /GNU/
67            or 0; # make debug output nice
68   $isvaxc = (!$isgcc && $isvax && `$cc_cmd /ansi_alias _nla0:` =~ /IVQUAL/)
69             or 0; # again, make debug output nice
70   print "\$isgcc: $isgcc\n" if $debug;
71   print "\$isvaxc: $isvaxc\n" if $debug;
72
73   if (-f 'perl.h') { $dir = '[]'; }
74   elsif (-f '[-]perl.h') { $dir = '[-]'; }
75   else { die "$0: Can't find perl.h\n"; }
76 }
77 else { 
78   ($ccvers,$cpp_file) = ($cc_cmd =~ /^~~(\w+)~~(.*)/);
79   $isgcc = $ccvers =~ /GCC/
80            or 0;  # for nice debug output
81   $isvaxc = (!$isgcc && $ccvers =~ /VAXC/)
82             or 0;  # again, for nice debug output
83   print "\$isgcc: \\$isgcc\\\n" if $debug;
84   print "\$isvaxc: \\$isvaxc\\\n" if $debug;
85   print "Not running cc, preprocesor output in \\$cpp_file\\\n" if $debug;
86 }
87
88 $objsuffix = shift @ARGV;
89 print "\$objsuffix: \\$objsuffix\\\n" if $debug;
90 $dbgprefix = shift @ARGV;
91 print "\$dbgprefix: \\$dbgprefix\\\n" if $debug;
92 $olbsuffix = shift @ARGV;
93 print "\$olbsuffix: \\$olbsuffix\\\n" if $debug;
94 $libperl = "${dbgprefix}libperl$olbsuffix";
95 $extnames = shift @ARGV;
96 print "\$extnames: \\$extnames\\\n" if $debug;
97 $rtlopt = shift @ARGV;
98 print "\$rtlopt: \\$rtlopt\\\n" if $debug;
99
100 # This part gets tricky.  VAXC creates creating global symbols for the
101 # constants in an enum if that enum is ever used as the data type of a
102 # global[dr]ef.  We have to detect enums which are used in this way, so we
103 # can set up the constants as universal symbols, since anything which
104 # #includes perl.h will want to resolve these global symbols.
105 # We're using a weak test here - we basically know that the only enums
106 # we need to handle now are the big one in opcode.h, and the
107 # "typedef enum { ... } expectation" in perl.h, so we hard code
108 # appropriate tests below. Since we can't know in general whether a given
109 # enum will be used elsewhere in a globaldef, it's hard to decide a
110 # priori whether its constants need to be treated as global symbols.
111 sub scan_enum {
112   my($line) = @_;
113
114   return unless $isvaxc;
115
116   return unless /^\s+(OP|X)/;  # we only want opcode and expectation enums
117   print "\tchecking for enum constant\n" if $debug > 1;
118   $line =~ s#/\*.+##;
119   $line =~ s/,?\s*\n?$//;
120   print "\tfiltered to \\$line\\\n" if $debug > 1;
121   if ($line =~ /(\w+)$/) {
122     print "\tvar name is \\$1\\\n" if $debug > 1;
123     $vars{$1}++;
124   }
125 }
126
127 sub scan_var {
128   my($line) = @_;
129
130   print "\tchecking for global variable\n" if $debug > 1;
131   $line =~ s/INIT\(.*\)//;
132   $line =~ s/\[.*//;
133   $line =~ s/=.*//;
134   $line =~ s/\W*;?\s*$//;
135   print "\tfiltered to \\$line\\\n" if $debug > 1;
136   if ($line =~ /(\w+)$/) {
137     print "\tvar name is \\$1\\\n" if $debug > 1;
138    $vars{$1}++;
139   }
140 }
141
142 sub scan_func {
143   my($line) = @_;
144
145   print "\tchecking for global routine\n" if $debug > 1;
146   if ( /(\w+)\s+\(/ ) {
147     print "\troutine name is \\$1\\\n" if $debug > 1;
148     if ($1 eq 'main' || $1 eq 'perl_init_ext') {
149       print "\tskipped\n" if $debug > 1;
150     }
151     else { $fcns{$1}++ }
152   }
153 }
154
155 if ($docc) {
156   open(CPP,"${cc_cmd}/NoObj/PreProc=Sys\$Output ${dir}perl.h|")
157     or die "$0: Can't preprocess ${dir}perl.h: $!\n";
158 }
159 else {
160   open(CPP,"$cpp_file") or die "$0: Can't read $cpp_file: $!\n";
161 }
162 LINE: while (<CPP>) {
163   while (/^#.*vmsish\.h/i .. /^#.*perl\.h/i) {
164     while (/__VMS_PROTOTYPES__/i .. /__VMS_SEPYTOTORP__/i) {
165       print "vms_proto>> $_" if $debug > 2;
166       &scan_func($_);
167       if (/^EXT/) { &scan_var($_); }
168       last LINE unless $_ = <CPP>;
169     }
170     print "vmsish.h>> $_" if $debug > 2;
171     if (/^EXT/) { &scan_var($_); }
172     last LINE unless $_ = <CPP>;
173   }    
174   while (/^#.*opcode\.h/i .. /^#.*perl\.h/i) {
175     print "opcode.h>> $_" if $debug > 2;
176     if (/^OP \*\s/) { &scan_func($_); }
177     if (/^EXT/) { &scan_var($_); }
178     if (/^\s+OP_/) { &scan_enum($_); }
179     last LINE unless $_ = <CPP>;
180   }
181   while (/^typedef enum/ .. /^\}/) {
182     print "global enum>> $_" if $debug > 2;
183     &scan_enum($_);
184     last LINE unless $_ = <CPP>;
185   }
186   while (/^#.*proto\.h/i .. /^#.*perl\.h/i) {
187     print "proto.h>> $_" if $debug > 2;
188     &scan_func($_);
189     if (/^EXT/) { &scan_var($_); }
190     last LINE unless $_ = <CPP>;
191   }
192   print $_ if $debug > 3;
193   if (/^EXT/) { &scan_var($_); }
194 }
195 close CPP;
196 while (<DATA>) {
197   next if /^#/;
198   s/\s+#.*\n//;
199   next if /^\s*$/;
200   ($key,$array) = split('=',$_);
201   print "Adding $key to \%$array list\n" if $debug > 1;
202   ${$array}{$key}++;
203 }
204 foreach (split /\s+/, $extnames) {
205   my($pkgname) = $_;
206   $pkgname =~ s/::/__/g;
207   $fcns{"boot_$pkgname"}++;
208   print "Adding boot_$pkgname to \%fcns (for extension $_)\n" if $debug;
209 }
210
211 # Eventually, we'll check against existing copies here, so we can add new
212 # symbols to an existing options file in an upwardly-compatible manner.
213
214 $marord++;
215 open(OPTBLD,">${dir}${dbgprefix}perlshr_bld.opt")
216   or die "$0: Can't write to ${dir}${dbgprefix}perlshr_bld.opt: $!\n";
217 if ($isvax) {
218   open(MAR,">${dir}perlshr_gbl${marord}.mar")
219     or die "$0: Can't write to ${dir}perlshr_gbl${marord}.mar: $!\n";
220   print MAR "\t.title perlshr_gbl$marord\n";
221 }
222 foreach $var (sort keys %vars) {
223   if ($isvax) { print OPTBLD "UNIVERSAL=$var\n"; }
224   else { print OPTBLD "SYMBOL_VECTOR=($var=DATA)\n"; }
225   # This hack brought to you by the lack of a globaldef in gcc.
226   if ($isgcc) {
227     if ($count++ > 200) {  # max 254 psects/file
228       print MAR "\t.end\n";
229       close MAR;
230       $marord++;
231       open(MAR,">${dir}perlshr_gbl${marord}.mar")
232         or die "$0: Can't write to ${dir}perlshr_gbl${marord}.mar: $!\n";
233       print MAR "\t.title perlshr_gbl$marord\n";
234       $count = 0;
235     }
236     print MAR "\t.psect ${var},long,pic,ovr,rd,wrt,noexe,noshr\n";
237     print MAR "\t${var}::       .blkl 1\n";
238   }
239 }
240
241 print MAR "\t.psect \$transfer_vec,pic,rd,nowrt,exe,shr\n" if ($isvax);
242 foreach $func (sort keys %fcns) {
243   if ($isvax) {
244     print MAR "\t.transfer $func\n";
245     print MAR "\t.mask $func\n";
246     print MAR "\tjmp G\^${func}+2\n";
247   }
248   else { print OPTBLD "SYMBOL_VECTOR=($func=PROCEDURE)\n"; }
249 }
250 if ($isvax) {
251   print MAR "\t.end\n";
252   close MAR;
253 }
254
255 open(OPTATTR,">${dir}perlshr_attr.opt")
256   or die "$0: Can't write to ${dir}perlshr_attr.opt: $!\n";
257 print OPTATTR "PSECT_ATTR=\$CHAR_STRING_CONSTANTS,PIC,SHR,NOEXE,RD,NOWRT\n";
258 foreach $var (sort keys %vars) {
259   print OPTATTR "PSECT_ATTR=${var},PIC,OVR,RD,NOEXE,WRT,NOSHR\n";
260 }
261 close OPTATTR;
262
263 $incstr = 'perl,globals';
264 if ($isvax) {
265   $drvrname = "Compile_shrmars.tmp_".time;
266   open (DRVR,">$drvrname") or die "$0: Can't write to $drvrname: $!\n";
267   print DRVR "\$ Set NoOn\n";  
268   print DRVR "\$ Delete/NoLog/NoConfirm $drvrname;\n";
269   print DRVR "\$ old_proc_vfy = F\$Environment(\"VERIFY_PROCEDURE\")\n";
270   print DRVR "\$ old_img_vfy = F\$Environment(\"VERIFY_IMAGE\")\n";
271   print DRVR "\$ MCR $^X -e \"\$ENV{'LIBPERL_RDT'} = (stat('$libperl'))[9]\"\n";
272   print DRVR "\$ Set Verify\n";
273   print DRVR "\$ If F\$Search(\"$libperl\").eqs.\"\" Then Library/Object/Create $libperl\n";
274   do {
275     $incstr .= ",perlshr_gbl$marord";
276     print DRVR "\$ Macro/NoDebug/Object=PerlShr_Gbl${marord}$objsuffix PerlShr_Gbl$marord.Mar\n";
277     print DRVR "\$ Library/Object/Replace/Log $libperl PerlShr_Gbl${marord}$objsuffix\n";
278   } while (--$marord); 
279   # We had to have a working miniperl to run this program; it's probably the
280   # one we just built.  It depended on LibPerl, which will be changed when
281   # the PerlShr_Gbl* modules get inserted, so miniperl will be out of date,
282   # and so, therefore, will all of its dependents . . .
283   # We touch LibPerl here so it'll be back 'in date', and we won't rebuild
284   # miniperl etc., and therefore LibPerl, the next time we invoke MM[KS].
285   print DRVR "\$ old_proc_vfy = F\$Verify(old_proc_vfy,old_img_vfy)\n";
286   print DRVR "\$ MCR $^X -e \"utime 0, \$ENV{'LIBPERL_RDT'}, '$libperl'\"\n";
287   close DRVR;
288 }
289
290 # Include object modules and RTLs in options file
291 # Linker wants /Include and /Library on different lines
292 print OPTBLD "$libperl/Include=($incstr)\n";
293 print OPTBLD "$libperl/Library\n";
294 open(RTLOPT,$rtlopt) or die "$0: Can't read $rtlopt: $!\n";
295 while (<RTLOPT>) { print OPTBLD; }
296 close RTLOPT;
297 close OPTBLD;
298
299 exec "\$ \@$drvrname" if $isvax;
300
301
302 __END__
303
304 # Oddball cases, so we can keep the perl.h scan above simple
305 error=vars      # declared in perl.h only when DOINIT defined by INTERN.h
306 rcsid=vars      # declared in perl.c
307 regarglen=vars  # declared in regcomp.h
308 regdummy=vars   # declared in regcomp.h
309 regkind=vars    # declared in regcomp.h
310 simple=vars     # declared in regcomp.h
311 varies=vars     # declared in regcomp.h
312 watchaddr=vars  # declared in run.c
313 watchok=vars    # declared in run.c
314 yychar=vars     # generated by byacc in perly.c
315 yycheck=vars    # generated by byacc in perly.c
316 yydebug=vars    # generated by byacc in perly.c
317 yydefred=vars   # generated by byacc in perly.c
318 yydgoto=vars    # generated by byacc in perly.c
319 yyerrflag=vars  # generated by byacc in perly.c
320 yygindex=vars   # generated by byacc in perly.c
321 yylen=vars      # generated by byacc in perly.c
322 yylhs=vars      # generated by byacc in perly.c
323 yylval=vars     # generated by byacc in perly.c
324 yyname=vars     # generated by byacc in perly.c
325 yynerrs=vars    # generated by byacc in perly.c
326 yyrindex=vars   # generated by byacc in perly.c
327 yyrule=vars     # generated by byacc in perly.c
328 yysindex=vars   # generated by byacc in perly.c
329 yytable=vars    # generated by byacc in perly.c
330 yyval=vars      # generated by byacc in perly.c