slightly modified version of suggested patch
[p5sagit/p5-mst-13.2.git] / makedef.pl
1 #
2 # Create the export list for perl.
3 #
4 # Needed by WIN32 and OS/2 for creating perl.dll
5 # and by AIX for creating libperl.a when -Dusershrplib is in effect.
6 #
7 # reads global.sym, pp.sym, perlvars.h, intrpvar.h, thrdvar.h, config.h
8 # On OS/2 reads miniperl.map as well
9
10 my $PLATFORM;
11 my $CCTYPE;
12
13 while (@ARGV)
14  {
15   my $flag = shift;
16   $define{$1} = 1 if ($flag =~ /^-D(\w+)$/);
17   $define{$1} = $2 if ($flag =~ /^-D(\w+)=(.+)$/);
18   $CCTYPE   = $1 if ($flag =~ /^CCTYPE=(\w+)$/);
19   $PLATFORM = $1 if ($flag =~ /^PLATFORM=(\w+)$/);
20  } 
21
22 my @PLATFORM = qw(aix win32 os2);
23 my %PLATFORM;
24 @PLATFORM{@PLATFORM} = ();
25
26 defined $PLATFORM || die "PLATFORM undefined, must be one of: @PLATFORM\n";
27 exists $PLATFORM{$PLATFORM} || die "PLATFORM must be one of: @PLATFORM\n"; 
28
29 my $config_sh   = "config.sh";
30 my $config_h    = "config.h";
31 my $thrdvar_h   = "thrdvar.h";
32 my $intrpvar_h  = "intrpvar.h";
33 my $perlvars_h  = "perlvars.h";
34 my $global_sym  = "global.sym";
35 my $pp_sym      = "pp.sym";
36 my $globvar_sym = "globvar.sym";
37 my $perlio_sym  = "perlio.sym";
38
39 if ($PLATFORM eq 'aix') { 
40     # Nothing for now.
41 } elsif ($PLATFORM eq 'win32') {
42     $CCTYPE = "MSVC" unless defined $CCTYPE;
43     foreach ($thrdvar_h, $intrpvar_h, $perlvars_h, $global_sym, $pp_sym, $globvar_sym) {
44         s!^!..\\!;
45     }
46 }
47
48 unless ($PLATFORM eq 'win32') {
49     open(CFG,$config_sh) || die "Cannot open $config_sh: $!\n";
50     while (<CFG>)
51     {
52         if (/^(?:ccflags|optimize)='(.+)'$/) {
53             $_ = $1;
54             $define{$1} = 1 while /-D(\w+)/g;
55         }
56         if ($PLATFORM eq 'os2') {
57             $CONFIG_ARGS = $1 if /^(?:config_args)='(.+)'$/;
58             $ARCHNAME = $1 if /^(?:archname)='(.+)'$/;
59         }
60     }
61     close(CFG);
62 }
63
64 open(CFG,$config_h) || die "Cannot open $config_h: $!\n";
65 while (<CFG>)
66  {
67   $define{$1} = 1 if /^\s*#\s*define\s+(MYMALLOC)\b/;
68   $define{$1} = 1 if /^\s*#\s*define\s+(USE_THREADS)\b/;
69   $define{$1} = 1 if /^\s*#\s*define\s+(USE_PERLIO)\b/;
70   $define{$1} = 1 if /^\s*#\s*define\s+(MULTIPLICITY)\b/;
71  }
72 close(CFG);
73
74 if ($PLATFORM eq 'win32') {
75     warn join(' ',keys %define)."\n";
76     if ($define{PERL_OBJECT}) {
77         print "LIBRARY Perl56\n";
78         print "DESCRIPTION 'Perl interpreter'\n";
79         print "EXPORTS\n";
80 #    output_symbol("perl_alloc");
81         output_symbol("perl_get_host_info");
82         output_symbol("perl_alloc_using");
83 #    output_symbol("perl_construct");
84 #    output_symbol("perl_destruct");
85 #    output_symbol("perl_free");
86 #    output_symbol("perl_parse");
87 #    output_symbol("perl_run");
88 #    output_symbol("RunPerl");
89 #    exit(0);
90     }
91     else {
92         if ($CCTYPE ne 'GCC') {
93             print "LIBRARY Perl\n";
94             print "DESCRIPTION 'Perl interpreter, export autogenerated'\n";
95         }
96         else {
97             $define{'PERL_GLOBAL_STRUCT'} = 1;
98             $define{'MULTIPLICITY'} = 1;
99         }
100         print "EXPORTS\n";
101     } 
102 } elsif ($PLATFORM eq 'os2') {
103     ($v = $]) =~ s/(\d\.\d\d\d)(\d\d)$/$1_$2/;
104     $v .= '-thread' if $ARCHNAME =~ /-thread/;
105     #$sum = 0;
106     #for (split //, $v) {
107     #   $sum = ($sum * 33) + ord;
108     #   $sum &= 0xffffff;
109     #}
110     #$sum += $sum >> 5;
111     #$sum &= 0xffff;
112     #$sum = printf '%X', $sum;
113     ($dll = $define{PERL_DLL}) =~ s/\.dll$//i;
114     # print STDERR "'$dll' <= '$define{PERL_DLL}'\n";
115     print <<"---EOP---";
116 LIBRARY '$dll' INITINSTANCE TERMINSTANCE
117 DESCRIPTION '\@#perl5-porters\@perl.org:$v#\@ Perl interpreter, configured as $CONFIG_ARGS'
118 STACKSIZE 32768
119 CODE LOADONCALL
120 DATA LOADONCALL NONSHARED MULTIPLE
121 EXPORTS
122 ---EOP---
123 } elsif ($PLATFORM eq 'aix') {
124     print "#!\n";
125 }
126
127 my %skip;
128 my %export;
129
130 sub skip_symbols {
131     my $list = shift;
132     foreach my $symbol (@$list) {
133         $skip{$symbol} = 1;
134     }
135 }
136
137 sub emit_symbols {
138     my $list = shift;
139     foreach my $symbol (@$list) {
140         my $skipsym = $symbol;
141         # XXX hack
142         if ($define{PERL_OBJECT}) {
143             $skipsym =~ s/^Perl_[GIT](\w+)_ptr$/PL_$1/;
144         }
145         emit_symbol($symbol) unless exists $skip{$skipsym};
146     }
147 }
148
149 if ($PLATFORM eq 'win32') {
150 skip_symbols [qw(
151 PL_statusvalue_vms
152 PL_archpat_auto
153 PL_cryptseen
154 PL_DBcv
155 PL_generation
156 PL_lastgotoprobe
157 PL_linestart
158 PL_modcount
159 PL_pending_ident
160 PL_sortcxix
161 PL_sublex_info
162 PL_timesbuf
163 main
164 Perl_ErrorNo
165 Perl_GetVars
166 Perl_do_exec3
167 Perl_do_ipcctl
168 Perl_do_ipcget
169 Perl_do_msgrcv
170 Perl_do_msgsnd
171 Perl_do_semop
172 Perl_do_shmio
173 Perl_dump_fds
174 Perl_init_thread_intern
175 Perl_my_bzero
176 Perl_my_htonl
177 Perl_my_ntohl
178 Perl_my_swap
179 Perl_my_chsize
180 Perl_same_dirent
181 Perl_setenv_getix
182 Perl_unlnk
183 Perl_watch
184 Perl_safexcalloc
185 Perl_safexmalloc
186 Perl_safexfree
187 Perl_safexrealloc
188 Perl_my_memcmp
189 Perl_my_memset
190 PL_cshlen
191 PL_cshname
192 PL_opsave
193 )];
194 } elsif ($PLATFORM eq 'aix') {
195     skip_symbols([qw(
196 Perl_dump_fds
197 Perl_ErrorNo
198 Perl_GetVars
199 Perl_my_bcopy
200 Perl_my_bzero
201 Perl_my_chsize
202 Perl_my_htonl
203 Perl_my_memcmp
204 Perl_my_memset
205 Perl_my_ntohl
206 Perl_my_swap
207 Perl_safexcalloc
208 Perl_safexfree
209 Perl_safexmalloc
210 Perl_safexrealloc
211 Perl_same_dirent
212 Perl_unlnk
213 PL_cryptseen
214 PL_opsave
215 PL_statusvalue_vms
216 PL_sys_intern
217 )]);
218 }
219
220 if ($PLATFORM eq 'os2') {
221     emit_symbols([qw(
222 ctermid
223 get_sysinfo
224 Perl_OS2_init
225 OS2_Perl_data
226 dlopen
227 dlsym
228 dlerror
229 my_tmpfile
230 my_tmpnam
231 my_flock
232 malloc_mutex
233 threads_mutex
234 nthreads
235 nthreads_cond
236 os2_cond_wait
237 pthread_join
238 pthread_create
239 pthread_detach
240 XS_Cwd_change_drive
241 XS_Cwd_current_drive
242 XS_Cwd_extLibpath
243 XS_Cwd_extLibpath_set
244 XS_Cwd_sys_abspath
245 XS_Cwd_sys_chdir
246 XS_Cwd_sys_cwd
247 XS_Cwd_sys_is_absolute
248 XS_Cwd_sys_is_relative
249 XS_Cwd_sys_is_rooted
250 XS_DynaLoader_mod2fname
251 XS_File__Copy_syscopy
252 Perl_Register_MQ
253 Perl_Deregister_MQ
254 Perl_Serve_Messages
255 Perl_Process_Messages
256 init_PMWIN_entries
257 PMWIN_entries
258 Perl_hab_GET
259 )]);
260 }
261
262 if ($define{'PERL_OBJECT'}) {
263   skip_symbols [qw(
264     Perl_getenv_len
265     Perl_my_popen
266     Perl_my_pclose
267     )];
268 }
269 else {
270   skip_symbols [qw(
271     PL_Dir
272     PL_Env
273     PL_LIO
274     PL_Mem
275     PL_Proc
276     PL_Sock
277     PL_StdIO
278     )];
279 }
280
281 if ($define{'MYMALLOC'})
282  {
283   emit_symbols [qw(
284     Perl_dump_mstats
285     Perl_malloc
286     Perl_mfree
287     Perl_realloc
288     Perl_calloc)];
289  }
290 else
291  {
292   skip_symbols [qw(
293     Perl_dump_mstats
294     Perl_malloc
295     Perl_mfree
296     Perl_realloc
297     Perl_calloc
298     Perl_malloced_size)];
299  }
300
301 unless ($define{'USE_THREADS'})
302  {
303   skip_symbols [qw(
304 PL_thr_key
305 PL_sv_mutex
306 PL_strtab_mutex
307 PL_svref_mutex
308 PL_malloc_mutex
309 PL_cred_mutex
310 PL_eval_mutex
311 PL_eval_cond
312 PL_eval_owner
313 PL_threads_mutex
314 PL_nthreads
315 PL_nthreads_cond
316 PL_threadnum
317 PL_threadsv_names
318 PL_thrsv
319 PL_vtbl_mutex
320 Perl_getTHR
321 Perl_setTHR
322 Perl_condpair_magic
323 Perl_new_struct_thread
324 Perl_per_thread_magicals
325 Perl_thread_create
326 Perl_find_threadsv
327 Perl_unlock_condpair
328 Perl_magic_mutexfree
329 )];
330  }
331 unless ($define{'USE_THREADS'} or $define{'PERL_IMPLICIT_CONTEXT'}
332         or $define{'PERL_OBJECT'})
333 {
334   skip_symbols [qw(
335                    Perl_croak_nocontext
336                    Perl_die_nocontext
337                    Perl_deb_nocontext
338                    Perl_form_nocontext
339                    Perl_warn_nocontext
340                    Perl_warner_nocontext
341                    Perl_newSVpvf_nocontext
342                    Perl_sv_catpvf_nocontext
343                    Perl_sv_setpvf_nocontext
344                    Perl_sv_catpvf_mg_nocontext
345                    Perl_sv_setpvf_mg_nocontext
346                    )];
347  }
348
349 unless ($define{'FAKE_THREADS'})
350  {
351   skip_symbols [qw(PL_curthr)];
352  }
353
354 sub readvar
355 {
356  my $file = shift;
357  my $proc = shift || sub { "PL_$_[2]" };
358  open(VARS,$file) || die "Cannot open $file: $!\n";
359  my @syms;
360  while (<VARS>)
361   {
362    # All symbols have a Perl_ prefix because that's what embed.h
363    # sticks in front of them.
364    push(@syms, &$proc($1,$2,$3)) if (/\bPERLVAR(A?I?C?)\(([IGT])(\w+)/);
365   } 
366  close(VARS); 
367  return \@syms;
368 }
369
370 if ($define{'USE_THREADS'} || $define{'MULTIPLICITY'})
371  {
372   my $thrd = readvar($thrdvar_h);
373   skip_symbols $thrd;
374  } 
375
376 if ($define{'MULTIPLICITY'})
377  {
378   my $interp = readvar($intrpvar_h);
379   skip_symbols $interp;
380  } 
381
382 if ($define{'PERL_GLOBAL_STRUCT'})
383  {
384   my $global = readvar($perlvars_h);
385   skip_symbols $global;
386   emit_symbols [qw(Perl_GetVars)];
387   emit_symbols [qw(PL_Vars PL_VarsPtr)] unless $CCTYPE eq 'GCC';
388  } 
389
390 unless ($define{'DEBUGGING'})
391  {
392   skip_symbols [qw(
393     Perl_deb
394     Perl_deb_growlevel
395     Perl_debop
396     Perl_debprofdump
397     Perl_debstack
398     Perl_debstackptrs
399     Perl_runops_debug
400     Perl_sv_peek
401     PL_block_type
402     PL_watchaddr
403     PL_watchok)];
404  }
405
406 if ($PLATFORM eq 'win32' && $define{'HAVE_DES_FCRYPT'})
407  {
408   emit_symbols [qw(win32_crypt)];
409  }
410
411 # functions from *.sym files
412
413 my @syms = ($global_sym, $pp_sym, $globvar_sym);
414
415 if ($define{'USE_PERLIO'})
416  {
417      push @syms, $perlio_sym;
418  }
419
420 for my $syms (@syms)
421  {
422   open (GLOBAL, "<$syms") || die "failed to open $syms: $!\n";
423   while (<GLOBAL>) 
424    {
425     next if (!/^[A-Za-z]/);
426     # Functions have a Perl_ prefix
427     # Variables have a PL_ prefix
428     chomp($_);
429     my $symbol = ($syms =~ /var\.sym$/i ? "PL_" : "");
430     $symbol .= $_;
431     emit_symbol($symbol) unless exists $skip{$symbol};
432    }
433   close(GLOBAL);
434  }
435
436 # variables
437
438 if ($define{'PERL_OBJECT'}) {
439     for my $f ($perlvars_h, $intrpvar_h, $thrdvar_h) {
440         my $glob = readvar($f, sub { "Perl_" . $_[1] . $_[2] . "_ptr" });
441         emit_symbols $glob;
442     }
443 }
444 else {
445     unless ($define{'PERL_GLOBAL_STRUCT'}) {
446         my $glob = readvar($perlvars_h);
447         emit_symbols $glob;
448     } 
449     unless ($define{'MULTIPLICITY'}) {
450         my $glob = readvar($intrpvar_h);
451         emit_symbols $glob;
452     } 
453
454     unless ($define{'MULTIPLICITY'} || $define{'USE_THREADS'}) {
455         my $glob = readvar($thrdvar_h);
456         emit_symbols $glob;
457     } 
458 }
459
460 sub try_symbol {
461     my $symbol = shift;
462
463     return if $symbol !~ /^[A-Za-z]/;
464     return if $symbol =~ /^\#/;
465     $symbol =~s/\r//g;
466     chomp($symbol);
467     return if exists $skip{$symbol};
468     emit_symbol($symbol);
469 }
470
471 while (<DATA>) {
472     try_symbol($_);
473 }
474
475 if ($PLATFORM eq 'win32') {
476     foreach my $symbol (qw(
477 boot_DynaLoader
478 Perl_getTHR
479 Perl_init_os_extras
480 Perl_setTHR
481 Perl_thread_create
482 Perl_win32_init
483 RunPerl
484 GetPerlInterpreter
485 SetPerlInterpreter
486 win32_errno
487 win32_environ
488 win32_stdin
489 win32_stdout
490 win32_stderr
491 win32_ferror
492 win32_feof
493 win32_strerror
494 win32_fprintf
495 win32_printf
496 win32_vfprintf
497 win32_vprintf
498 win32_fread
499 win32_fwrite
500 win32_fopen
501 win32_fdopen
502 win32_freopen
503 win32_fclose
504 win32_fputs
505 win32_fputc
506 win32_ungetc
507 win32_getc
508 win32_fileno
509 win32_clearerr
510 win32_fflush
511 win32_ftell
512 win32_fseek
513 win32_fgetpos
514 win32_fsetpos
515 win32_rewind
516 win32_tmpfile
517 win32_abort
518 win32_fstat
519 win32_stat
520 win32_pipe
521 win32_popen
522 win32_pclose
523 win32_rename
524 win32_setmode
525 win32_lseek
526 win32_tell
527 win32_dup
528 win32_dup2
529 win32_open
530 win32_close
531 win32_eof
532 win32_read
533 win32_write
534 win32_spawnvp
535 win32_mkdir
536 win32_rmdir
537 win32_chdir
538 win32_flock
539 win32_execv
540 win32_execvp
541 win32_htons
542 win32_ntohs
543 win32_htonl
544 win32_ntohl
545 win32_inet_addr
546 win32_inet_ntoa
547 win32_socket
548 win32_bind
549 win32_listen
550 win32_accept
551 win32_connect
552 win32_send
553 win32_sendto
554 win32_recv
555 win32_recvfrom
556 win32_shutdown
557 win32_closesocket
558 win32_ioctlsocket
559 win32_setsockopt
560 win32_getsockopt
561 win32_getpeername
562 win32_getsockname
563 win32_gethostname
564 win32_gethostbyname
565 win32_gethostbyaddr
566 win32_getprotobyname
567 win32_getprotobynumber
568 win32_getservbyname
569 win32_getservbyport
570 win32_select
571 win32_endhostent
572 win32_endnetent
573 win32_endprotoent
574 win32_endservent
575 win32_getnetent
576 win32_getnetbyname
577 win32_getnetbyaddr
578 win32_getprotoent
579 win32_getservent
580 win32_sethostent
581 win32_setnetent
582 win32_setprotoent
583 win32_setservent
584 win32_getenv
585 win32_putenv
586 win32_perror
587 win32_setbuf
588 win32_setvbuf
589 win32_flushall
590 win32_fcloseall
591 win32_fgets
592 win32_gets
593 win32_fgetc
594 win32_putc
595 win32_puts
596 win32_getchar
597 win32_putchar
598 win32_malloc
599 win32_calloc
600 win32_realloc
601 win32_free
602 win32_sleep
603 win32_times
604 win32_alarm
605 win32_open_osfhandle
606 win32_get_osfhandle
607 win32_ioctl
608 win32_utime
609 win32_uname
610 win32_wait
611 win32_waitpid
612 win32_kill
613 win32_str_os_error
614 win32_opendir
615 win32_readdir
616 win32_telldir
617 win32_seekdir
618 win32_rewinddir
619 win32_closedir
620 win32_longpath
621 win32_os_id
622                            )) {
623         try_symbol($symbol);
624     }
625 }
626 elsif ($PLATFORM eq 'os2') {
627   open MAP, 'miniperl.map' or die 'Cannot read miniperl.map';
628   /^\s*[\da-f:]+\s+(\w+)/i and $mapped{$1}++ foreach <MAP>;
629   close MAP or die 'Cannot close miniperl.map';
630   
631   @missing = grep { !exists $mapped{$_} } keys %export;
632   delete $export{$_} foreach @missing;
633 }
634
635 # Now all symbols should be defined because
636 # next we are going to output them.
637
638 foreach my $symbol (sort keys %export)
639  {
640    output_symbol($symbol);
641  }
642
643 sub emit_symbol {
644         my $symbol = shift;
645         chomp($symbol); 
646         $export{$symbol} = 1;
647 }
648
649 sub output_symbol {
650     my $symbol = shift;
651     if ($PLATFORM eq 'win32') {
652         $symbol = "_$symbol" if $CCTYPE eq 'BORLAND';
653         print "\t$symbol\n";
654 # XXX: binary compatibility between compilers is an exercise
655 # in frustration :-(
656 #        if ($CCTYPE eq "BORLAND") {
657 #           # workaround Borland quirk by exporting both the straight
658 #           # name and a name with leading underscore.  Note the
659 #           # alias *must* come after the symbol itself, if both
660 #           # are to be exported. (Linker bug?)
661 #           print "\t_$symbol\n";
662 #           print "\t$symbol = _$symbol\n";
663 #       }
664 #       elsif ($CCTYPE eq 'GCC') {
665 #           # Symbols have leading _ whole process is $%@"% slow
666 #           # so skip aliases for now
667 #           nprint "\t$symbol\n";
668 #       }
669 #       else {
670 #           # for binary coexistence, export both the symbol and
671 #           # alias with leading underscore
672 #           print "\t$symbol\n";
673 #           print "\t_$symbol = $symbol\n";
674 #       }
675     } elsif ($PLATFORM eq 'os2') {
676         print qq(    "$symbol"\n);
677     } elsif ($PLATFORM eq 'aix') {
678         print "$symbol\n";
679     }
680 }
681
682 1;
683 __DATA__
684 # extra globals not included above.
685 perl_alloc
686 perl_construct
687 perl_destruct
688 perl_free
689 perl_parse
690 perl_run