Move bulk of POSIX::setlocale to locale.c
[perl.git] / makedef.pl
1 #./perl -w
2 #
3 # Create the export list for perl.
4 #
5 # Needed by WIN32 and OS/2 for creating perl.dll,
6 # and by AIX for creating libperl.a when -Duseshrplib is in effect,
7 # and by VMS for creating perlshr.exe.
8 #
9 # Reads from information stored in
10 #
11 #    %Config::Config (ie config.sh)
12 #    config.h
13 #    embed.fnc
14 #    globvar.sym
15 #    intrpvar.h
16 #    miniperl.map (on OS/2)
17 #    perl5.def    (on OS/2; this is the old version of the file being made)
18 #    perlio.sym
19 #    perlvars.h
20 #    regen/opcodes
21 #
22 # plus long lists of function names hard-coded directly in this script.
23 #
24 # Writes the result to STDOUT.
25 #
26 # Normally this script is invoked from a makefile (e.g. win32/Makefile),
27 # which redirects STDOUT to a suitable file, such as:
28 #
29 #    perl5.def   OS/2
30 #    perldll.def Windows
31 #    perl.exp    AIX
32 #    perl.imp    NetWare
33 #    makedef.lis VMS
34
35 my $fold;
36 my %ARGS;
37 my %define;
38
39 BEGIN {
40     BEGIN { unshift @INC, "lib" }
41     use Config;
42     use strict;
43
44     %ARGS = (CCTYPE => 'MSVC', TARG_DIR => '');
45
46     sub process_cc_flags {
47         foreach (map {split /\s+/, $_} @_) {
48             $define{$1} = $2 // 1 if /^-D(\w+)(?:=(.+))?/;
49         }
50     }
51
52     while (@ARGV) {
53         my $flag = shift;
54         if ($flag =~ /^(?:CC_FLAGS=)?(-D\w.*)/) {
55             process_cc_flags($1);
56         } elsif ($flag =~ /^(CCTYPE|FILETYPE|PLATFORM|TARG_DIR)=(.+)$/) {
57             $ARGS{$1} = $2;
58         } elsif ($flag eq '--sort-fold') {
59             ++$fold;
60         }
61     }
62     my @PLATFORM = qw(aix win32 wince os2 netware vms test);
63     my %PLATFORM;
64     @PLATFORM{@PLATFORM} = ();
65
66     die "PLATFORM undefined, must be one of: @PLATFORM\n"
67         unless defined $ARGS{PLATFORM};
68     die "PLATFORM must be one of: @PLATFORM\n"
69         unless exists $PLATFORM{$ARGS{PLATFORM}};
70 }
71 use constant PLATFORM => $ARGS{PLATFORM};
72
73 require "./$ARGS{TARG_DIR}regen/embed_lib.pl";
74
75 # Is the following guard strictly necessary? Added during refactoring
76 # to keep the same behaviour when merging other code into here.
77 process_cc_flags(@Config{qw(ccflags optimize)})
78     if $ARGS{PLATFORM} ne 'win32' && $ARGS{PLATFORM} ne 'wince'
79     && $ARGS{PLATFORM} ne 'netware';
80
81 # Add the compile-time options that miniperl was built with to %define.
82 # On Win32 these are not the same options as perl itself will be built
83 # with since miniperl is built with a canned config (one of the win32/
84 # config_H.*) and none of the BUILDOPT's that are set in the makefiles,
85 # but they do include some #define's that are hard-coded in various
86 # source files and header files and don't include any BUILDOPT's that
87 # the user might have chosen to disable because the canned configs are
88 # minimal configs that don't include any of those options.
89
90 #don't use the host Perl's -V defines for the WinCE Perl
91 if($ARGS{PLATFORM} ne 'wince') {
92     my @options = sort(Config::bincompat_options(), Config::non_bincompat_options());
93     print STDERR "Options: (@options)\n" unless $ARGS{PLATFORM} eq 'test';
94     $define{$_} = 1 foreach @options;
95 }
96
97 my %exportperlmalloc =
98     (
99        Perl_malloc              =>      "malloc",
100        Perl_mfree               =>      "free",
101        Perl_realloc             =>      "realloc",
102        Perl_calloc              =>      "calloc",
103     );
104
105 my $exportperlmalloc = $ARGS{PLATFORM} eq 'os2';
106
107 my $config_h = 'config.h';
108 open(CFG, '<', $config_h) || die "Cannot open $config_h: $!\n";
109 while (<CFG>) {
110     $define{$1} = 1 if /^\s*\#\s*define\s+(MYMALLOC|MULTIPLICITY
111                                            |SPRINTF_RETURNS_STRLEN
112                                            |KILL_BY_SIGPRC
113                                            |(?:PERL|USE|HAS)_\w+)\b/x;
114 }
115 close(CFG);
116
117 # perl.h logic duplication begins
118
119 if ($define{USE_ITHREADS}) {
120     if (!$define{MULTIPLICITY}) {
121         $define{MULTIPLICITY} = 1;
122     }
123 }
124
125 $define{PERL_IMPLICIT_CONTEXT} ||=
126     $define{USE_ITHREADS} ||
127     $define{MULTIPLICITY} ;
128
129 if ($define{USE_ITHREADS} && $ARGS{PLATFORM} ne 'win32' && $^O ne 'darwin') {
130     $define{USE_REENTRANT_API} = 1;
131 }
132
133 # perl.h logic duplication ends
134
135 print STDERR "Defines: (" . join(' ', sort keys %define) . ")\n"
136      unless $ARGS{PLATFORM} eq 'test';
137
138 my $sym_ord = 0;
139 my %ordinal;
140
141 if ($ARGS{PLATFORM} eq 'os2') {
142     if (open my $fh, '<', 'perl5.def') {
143       while (<$fh>) {
144         last if /^\s*EXPORTS\b/;
145       }
146       while (<$fh>) {
147         $ordinal{$1} = $2 if /^\s*"(\w+)"\s*(?:=\s*"\w+"\s*)?\@(\d+)\s*$/;
148         # This allows skipping ordinals which were used in older versions
149         $sym_ord = $1 if /^\s*;\s*LAST_ORDINAL\s*=\s*(\d+)\s*$/;
150       }
151       $sym_ord < $_ and $sym_ord = $_ for values %ordinal; # Take the max
152     }
153 }
154
155 my %skip;
156 # All platforms export boot_DynaLoader unconditionally.
157 my %export = ( boot_DynaLoader => 1 );
158
159 sub try_symbols {
160     foreach my $symbol (@_) {
161         ++$export{$symbol} unless exists $skip{$symbol};
162     }
163 }
164
165 sub readvar {
166     # $hash is the hash that we're adding to. For one of our callers, it will
167     # actually be the skip hash but that doesn't affect the intent of what
168     # we're doing, as in that case we skip adding something to the skip hash
169     # for the second time.
170
171     my $file = $ARGS{TARG_DIR} . shift;
172     my $hash = shift;
173     my $proc = shift;
174     open my $vars, '<', $file or die die "Cannot open $file: $!\n";
175
176     while (<$vars>) {
177         # All symbols have a Perl_ prefix because that's what embed.h sticks
178         # in front of them.  The A?I?S?C? is strictly speaking wrong.
179         next unless /\bPERLVAR(A?I?S?C?)\(([IGT]),\s*(\w+)/;
180
181         my $var = "PL_$3";
182         my $symbol = $proc ? &$proc($1,$2,$3) : $var;
183         ++$hash->{$symbol} unless exists $skip{$var};
184     }
185 }
186
187 if ($ARGS{PLATFORM} ne 'os2') {
188     ++$skip{$_} foreach qw(
189                      PL_cryptseen
190                      PL_opsave
191                      Perl_GetVars
192                      Perl_dump_fds
193                      Perl_my_bcopy
194                      Perl_my_bzero
195                      Perl_my_chsize
196                      Perl_my_htonl
197                      Perl_my_memcmp
198                      Perl_my_memset
199                      Perl_my_ntohl
200                      Perl_my_swap
201                          );
202     if ($ARGS{PLATFORM} eq 'vms') {
203         ++$skip{PL_statusvalue_posix};
204         # This is a wrapper if we have symlink, not a replacement
205         # if we don't.
206         ++$skip{Perl_my_symlink} unless $Config{d_symlink};
207     } else {
208         ++$skip{PL_statusvalue_vms};
209         ++$skip{PL_perllib_sep};
210         if ($ARGS{PLATFORM} ne 'aix') {
211             ++$skip{$_} foreach qw(
212                                 PL_DBcv
213                                 PL_generation
214                                 PL_lastgotoprobe
215                                 PL_modcount
216                                 main
217                                  );
218         }
219     }
220 }
221
222 if ($ARGS{PLATFORM} ne 'vms') {
223     # VMS does its own thing for these symbols.
224     ++$skip{$_} foreach qw(
225                         PL_sig_handlers_initted
226                         PL_sig_ignoring
227                         PL_sig_defaulting
228                          );
229     if ($ARGS{PLATFORM} ne 'win32') {
230         ++$skip{$_} foreach qw(
231                             Perl_do_spawn
232                             Perl_do_spawn_nowait
233                             Perl_do_aspawn
234                              );
235     }
236 }
237
238 if ($ARGS{PLATFORM} ne 'win32') {
239     ++$skip{$_} foreach qw(
240                     Perl_my_setlocale
241                          );
242 }
243
244 unless ($define{UNLINK_ALL_VERSIONS}) {
245     ++$skip{Perl_unlnk};
246 }
247
248 unless ($define{'DEBUGGING'}) {
249     ++$skip{$_} foreach qw(
250                     Perl_debop
251                     Perl_debprofdump
252                     Perl_debstack
253                     Perl_debstackptrs
254                     Perl_pad_sv
255                     Perl_pad_setsv
256                     Perl__setlocale_debug_string
257                     Perl_set_padlist
258                     Perl_hv_assert
259                     PL_watchaddr
260                     PL_watchok
261                     PL_watch_pvx
262                          );
263 }
264
265 if ($define{'PERL_IMPLICIT_SYS'}) {
266     ++$skip{$_} foreach qw(
267                     Perl_my_popen
268                     Perl_my_pclose
269                          );
270     ++$export{$_} foreach qw(perl_get_host_info perl_alloc_override);
271     ++$export{perl_clone_host} if $define{USE_ITHREADS};
272 }
273 else {
274     ++$skip{$_} foreach qw(
275                     PL_Mem
276                     PL_MemShared
277                     PL_MemParse
278                     PL_Env
279                     PL_StdIO
280                     PL_LIO
281                     PL_Dir
282                     PL_Sock
283                     PL_Proc
284                     perl_alloc_using
285                     perl_clone_using
286                          );
287 }
288
289 if (!$define{'PERL_COPY_ON_WRITE'} || $define{'PERL_NO_COW'}) {
290     ++$skip{Perl_sv_setsv_cow};
291 }
292
293 unless ($define{PERL_SAWAMPERSAND}) {
294     ++$skip{PL_sawampersand};
295 }
296
297 unless ($define{'USE_REENTRANT_API'}) {
298     ++$skip{PL_reentrant_buffer};
299 }
300
301 if ($define{'MYMALLOC'}) {
302     try_symbols(qw(
303                     Perl_dump_mstats
304                     Perl_get_mstats
305                     Perl_strdup
306                     Perl_putenv
307                     MallocCfg_ptr
308                     MallocCfgP_ptr
309                     ));
310     unless ($define{USE_ITHREADS}) {
311         ++$skip{PL_malloc_mutex}
312     }
313 }
314 else {
315     ++$skip{$_} foreach qw(
316                     PL_malloc_mutex
317                     Perl_dump_mstats
318                     Perl_get_mstats
319                     MallocCfg_ptr
320                     MallocCfgP_ptr
321                          );
322 }
323
324 if ($define{'PERL_USE_SAFE_PUTENV'}) {
325     ++$skip{PL_use_safe_putenv};
326 }
327
328 unless ($define{'USE_ITHREADS'}) {
329     ++$skip{PL_thr_key};
330 }
331
332 # USE_5005THREADS symbols. Kept as reference for easier removal
333 ++$skip{$_} foreach qw(
334                     PL_sv_mutex
335                     PL_strtab_mutex
336                     PL_svref_mutex
337                     PL_cred_mutex
338                     PL_eval_mutex
339                     PL_fdpid_mutex
340                     PL_sv_lock_mutex
341                     PL_eval_cond
342                     PL_eval_owner
343                     PL_threads_mutex
344                     PL_nthreads
345                     PL_nthreads_cond
346                     PL_threadnum
347                     PL_threadsv_names
348                     PL_thrsv
349                     PL_vtbl_mutex
350                     Perl_condpair_magic
351                     Perl_new_struct_thread
352                     Perl_per_thread_magicals
353                     Perl_thread_create
354                     Perl_find_threadsv
355                     Perl_unlock_condpair
356                     Perl_magic_mutexfree
357                     Perl_sv_lock
358                      );
359
360 unless ($define{'USE_ITHREADS'}) {
361     ++$skip{$_} foreach qw(
362                     PL_check_mutex
363                     PL_op_mutex
364                     PL_regex_pad
365                     PL_regex_padav
366                     PL_dollarzero_mutex
367                     PL_hints_mutex
368                     PL_locale_mutex
369                     PL_my_ctx_mutex
370                     PL_perlio_mutex
371                     PL_stashpad
372                     PL_stashpadix
373                     PL_stashpadmax
374                     Perl_alloccopstash
375                     Perl_allocfilegv
376                     Perl_clone_params_del
377                     Perl_clone_params_new
378                     Perl_parser_dup
379                     Perl_dirp_dup
380                     Perl_cx_dup
381                     Perl_si_dup
382                     Perl_any_dup
383                     Perl_ss_dup
384                     Perl_fp_dup
385                     Perl_gp_dup
386                     Perl_he_dup
387                     Perl_mg_dup
388                     Perl_re_dup_guts
389                     Perl_sv_dup
390                     Perl_sv_dup_inc
391                     Perl_rvpv_dup
392                     Perl_hek_dup
393                     Perl_sys_intern_dup
394                     perl_clone
395                     perl_clone_using
396                     Perl_stashpv_hvname_match
397                     Perl_regdupe_internal
398                     Perl_newPADOP
399                          );
400 }
401
402 unless (   $define{'USE_ITHREADS'}
403         && $define{'HAS_NEWLOCALE'})
404 {
405     ++$skip{$_} foreach qw(
406         PL_C_locale_obj
407     );
408 }
409
410 unless ($define{'PERL_IMPLICIT_CONTEXT'}) {
411     ++$skip{$_} foreach qw(
412                     PL_my_cxt_index
413                     PL_my_cxt_list
414                     PL_my_cxt_size
415                     PL_my_cxt_keys
416                     Perl_croak_nocontext
417                     Perl_die_nocontext
418                     Perl_deb_nocontext
419                     Perl_form_nocontext
420                     Perl_load_module_nocontext
421                     Perl_mess_nocontext
422                     Perl_warn_nocontext
423                     Perl_warner_nocontext
424                     Perl_newSVpvf_nocontext
425                     Perl_sv_catpvf_nocontext
426                     Perl_sv_setpvf_nocontext
427                     Perl_sv_catpvf_mg_nocontext
428                     Perl_sv_setpvf_mg_nocontext
429                     Perl_my_cxt_init
430                     Perl_my_cxt_index
431                          );
432 }
433
434 unless ($define{'PERL_OP_PARENT'}) {
435     ++$skip{$_} foreach qw(
436                     Perl_op_parent
437                 );
438 }
439
440 unless ($define{'USE_DTRACE'}) {
441     ++$skip{$_} foreach qw(
442                     Perl_dtrace_probe_call
443                     Perl_dtrace_probe_load
444                     Perl_dtrace_probe_op
445                     Perl_dtrace_probe_phase
446                 );
447 }
448
449 unless ($define{'PERL_NEED_APPCTX'}) {
450     ++$skip{PL_appctx};
451 }
452
453 unless ($define{'PERL_NEED_TIMESBASE'}) {
454     ++$skip{PL_timesbase};
455 }
456
457 unless ($define{'DEBUG_LEAKING_SCALARS'}) {
458     ++$skip{PL_sv_serial};
459 }
460
461 unless ($define{'DEBUG_LEAKING_SCALARS_FORK_DUMP'}) {
462     ++$skip{PL_dumper_fd};
463 }
464
465 unless ($define{'PERL_DONT_CREATE_GVSV'}) {
466     ++$skip{Perl_gv_SVadd};
467 }
468
469 if ($define{'SPRINTF_RETURNS_STRLEN'}) {
470     ++$skip{Perl_my_sprintf};
471 }
472
473 unless ($define{'PERL_USES_PL_PIDSTATUS'}) {
474     ++$skip{PL_pidstatus};
475 }
476
477 unless ($define{'PERL_TRACK_MEMPOOL'}) {
478     ++$skip{PL_memory_debug_header};
479 }
480
481 unless ($define{'MULTIPLICITY'}) {
482     ++$skip{$_} foreach qw(
483                     PL_interp_size
484                     PL_interp_size_5_18_0
485                          );
486 }
487
488 unless ($define{'PERL_GLOBAL_STRUCT'}) {
489     ++$skip{PL_global_struct_size};
490 }
491
492 unless ($define{'PERL_GLOBAL_STRUCT_PRIVATE'}) {
493     ++$skip{$_} foreach qw(
494                     PL_my_cxt_keys
495                     Perl_my_cxt_index
496                          );
497 }
498
499 unless ($define{HAS_MMAP}) {
500     ++$skip{PL_mmap_page_size};
501 }
502
503 if ($define{HAS_SIGACTION}) {
504     ++$skip{PL_sig_trapped};
505
506     if ($ARGS{PLATFORM} eq 'vms') {
507         # FAKE_PERSISTENT_SIGNAL_HANDLERS defined as !defined(HAS_SIGACTION)
508         ++$skip{PL_sig_ignoring};
509         ++$skip{PL_sig_handlers_initted} unless $define{KILL_BY_SIGPRC};
510     }
511 }
512
513 if ($ARGS{PLATFORM} eq 'vms' && !$define{KILL_BY_SIGPRC}) {
514     # FAKE_DEFAULT_SIGNAL_HANDLERS defined as KILL_BY_SIGPRC
515     ++$skip{Perl_csighandler_init};
516     ++$skip{Perl_my_kill};
517     ++$skip{Perl_sig_to_vmscondition};
518     ++$skip{PL_sig_defaulting};
519     ++$skip{PL_sig_handlers_initted} unless !$define{HAS_SIGACTION};
520 }
521
522 unless ($define{USE_LOCALE_COLLATE}) {
523     ++$skip{$_} foreach qw(
524                     PL_collation_ix
525                     PL_collation_name
526                     PL_collation_standard
527                     PL_collxfrm_base
528                     PL_collxfrm_mult
529                     Perl_sv_collxfrm
530                     Perl_sv_collxfrm_flags
531                          );
532 }
533
534 unless ($define{USE_LOCALE_NUMERIC}) {
535     ++$skip{$_} foreach qw(
536                     PL_numeric_local
537                     PL_numeric_name
538                     PL_numeric_radix_sv
539                     PL_numeric_standard
540                          );
541 }
542
543 unless ($define{'USE_C_BACKTRACE'}) {
544     ++$skip{Perl_get_c_backtrace_dump};
545     ++$skip{Perl_dump_c_backtrace};
546 }
547
548 unless ($define{HAVE_INTERP_INTERN}) {
549     ++$skip{$_} foreach qw(
550                     Perl_sys_intern_clear
551                     Perl_sys_intern_dup
552                     Perl_sys_intern_init
553                     PL_sys_intern
554                          );
555 }
556
557 if ($define{HAS_SIGNBIT}) {
558     ++$skip{Perl_signbit};
559 }
560
561 if ($define{'PERL_GLOBAL_STRUCT'}) {
562     readvar('perlvars.h', \%skip);
563     # This seems like the least ugly way to cope with the fact that PL_sh_path
564     # is mentioned in perlvar.h and globvar.sym, and always exported.
565     delete $skip{PL_sh_path};
566     ++$export{Perl_GetVars};
567     try_symbols(qw(PL_Vars PL_VarsPtr))
568       unless $ARGS{CCTYPE} eq 'GCC' || $define{PERL_GLOBAL_STRUCT_PRIVATE};
569 } else {
570     ++$skip{$_} foreach qw(Perl_init_global_struct Perl_free_global_struct);
571 }
572
573 ++$skip{PL_op_exec_cnt}
574     unless $define{PERL_TRACE_OPS};
575
576 ++$skip{PL_hash_chars}
577     unless $define{PERL_USE_SINGLE_CHAR_HASH_CACHE};
578
579 # functions from *.sym files
580
581 my @syms = qw(globvar.sym);
582
583 # Symbols that are the public face of the PerlIO layers implementation
584 # These are in _addition to_ the public face of the abstraction
585 # and need to be exported to allow XS modules to implement layers
586 my @layer_syms = qw(
587                     PerlIOBase_binmode
588                     PerlIOBase_clearerr
589                     PerlIOBase_close
590                     PerlIOBase_dup
591                     PerlIOBase_eof
592                     PerlIOBase_error
593                     PerlIOBase_fileno
594                     PerlIOBase_open
595                     PerlIOBase_noop_fail
596                     PerlIOBase_noop_ok
597                     PerlIOBase_popped
598                     PerlIOBase_pushed
599                     PerlIOBase_read
600                     PerlIOBase_setlinebuf
601                     PerlIOBase_unread
602                     PerlIOBuf_bufsiz
603                     PerlIOBuf_close
604                     PerlIOBuf_dup
605                     PerlIOBuf_fill
606                     PerlIOBuf_flush
607                     PerlIOBuf_get_base
608                     PerlIOBuf_get_cnt
609                     PerlIOBuf_get_ptr
610                     PerlIOBuf_open
611                     PerlIOBuf_popped
612                     PerlIOBuf_pushed
613                     PerlIOBuf_read
614                     PerlIOBuf_seek
615                     PerlIOBuf_set_ptrcnt
616                     PerlIOBuf_tell
617                     PerlIOBuf_unread
618                     PerlIOBuf_write
619                     PerlIO_allocate
620                     PerlIO_apply_layera
621                     PerlIO_apply_layers
622                     PerlIO_arg_fetch
623                     PerlIO_debug
624                     PerlIO_define_layer
625                     PerlIO_find_layer
626                     PerlIO_isutf8
627                     PerlIO_layer_fetch
628                     PerlIO_list_alloc
629                     PerlIO_list_free
630                     PerlIO_modestr
631                     PerlIO_parse_layers
632                     PerlIO_pending
633                     PerlIO_perlio
634                     PerlIO_pop
635                     PerlIO_push
636                     PerlIO_sv_dup
637                     Perl_PerlIO_clearerr
638                     Perl_PerlIO_close
639                     Perl_PerlIO_context_layers
640                     Perl_PerlIO_eof
641                     Perl_PerlIO_error
642                     Perl_PerlIO_fileno
643                     Perl_PerlIO_fill
644                     Perl_PerlIO_flush
645                     Perl_PerlIO_get_base
646                     Perl_PerlIO_get_bufsiz
647                     Perl_PerlIO_get_cnt
648                     Perl_PerlIO_get_ptr
649                     Perl_PerlIO_read
650                     Perl_PerlIO_restore_errno
651                     Perl_PerlIO_save_errno
652                     Perl_PerlIO_seek
653                     Perl_PerlIO_set_cnt
654                     Perl_PerlIO_set_ptrcnt
655                     Perl_PerlIO_setlinebuf
656                     Perl_PerlIO_stderr
657                     Perl_PerlIO_stdin
658                     Perl_PerlIO_stdout
659                     Perl_PerlIO_tell
660                     Perl_PerlIO_unread
661                     Perl_PerlIO_write
662 );
663 if ($ARGS{PLATFORM} eq 'netware') {
664     push(@layer_syms,'PL_def_layerlist','PL_known_layers','PL_perlio');
665 }
666
667 # Export the symbols that make up the PerlIO abstraction, regardless
668 # of its implementation - read from a file
669 push @syms, 'perlio.sym';
670
671 # PerlIO with layers - export implementation
672 try_symbols(@layer_syms, 'perlsio_binmode');
673
674
675 unless ($define{'USE_QUADMATH'}) {
676   ++$skip{Perl_quadmath_format_needed};
677   ++$skip{Perl_quadmath_format_single};
678 }
679
680 ###############################################################################
681
682 # At this point all skip lists should be completed, as we are about to test
683 # many symbols against them.
684
685 {
686     my %seen;
687     my ($embed) = setup_embed($ARGS{TARG_DIR});
688
689     foreach (@$embed) {
690         my ($flags, $retval, $func, @args) = @$_;
691         next unless $func;
692         if (   ($flags =~ /[AX]/ && $flags !~ /[xmi]/)
693             || ($flags =~ /b/ && ! $define{'NO_MATHOMS'}))
694         {
695             # public API, so export
696
697             # If a function is defined twice, for example before and after
698             # an #else, only export its name once. Important to do this test
699             # within the block, as the *first* definition may have flags which
700             # mean "don't export"
701             next if $seen{$func}++;
702             # Should we also skip adding the Perl_ prefix if $flags =~ /o/ ?
703             $func = "Perl_$func" if ($flags =~ /[pX]/ && $func !~ /^Perl_/);
704             ++$export{$func} unless exists $skip{$func};
705         }
706     }
707 }
708
709 foreach (@syms) {
710     my $syms = $ARGS{TARG_DIR} . $_;
711     open my $global, '<', $syms or die "failed to open $syms: $!\n";
712     while (<$global>) {
713         next unless /^([A-Za-z].*)/;
714         my $symbol = "$1";
715         ++$export{$symbol} unless exists $skip{$symbol};
716     }
717 }
718
719 # variables
720
721 if ($define{'MULTIPLICITY'} && $define{PERL_GLOBAL_STRUCT}) {
722     readvar('perlvars.h', \%export, sub { "Perl_" . $_[1] . $_[2] . "_ptr" });
723     # XXX AIX seems to want the perlvars.h symbols, for some reason
724     if ($ARGS{PLATFORM} eq 'aix' or $ARGS{PLATFORM} eq 'os2') { # OS/2 needs PL_thr_key
725         readvar('perlvars.h', \%export);
726     }
727 }
728 else {
729     unless ($define{'PERL_GLOBAL_STRUCT'}) {
730         readvar('perlvars.h', \%export);
731     }
732     unless ($define{MULTIPLICITY}) {
733         readvar('intrpvar.h', \%export);
734     }
735 }
736
737 # Oddities from PerlIO
738 # All have alternate implementations in perlio.c, so always exist.
739 # Should they be considered to be part of the API?
740 try_symbols(qw(
741                     PerlIO_binmode
742                     PerlIO_getpos
743                     PerlIO_init
744                     PerlIO_setpos
745                     PerlIO_tmpfile
746              ));
747
748 if ($ARGS{PLATFORM} eq 'win32') {
749     try_symbols(qw(
750                                  win32_free_childdir
751                                  win32_free_childenv
752                                  win32_get_childdir
753                                  win32_get_childenv
754                                  win32_spawnvp
755                  ));
756 }
757
758 if ($ARGS{PLATFORM} eq 'wince') {
759     ++$skip{'win32_isatty'}; # commit 4342f4d6df is win32-only
760 }
761
762 if ($ARGS{PLATFORM} =~ /^win(?:32|ce)$/) {
763     try_symbols(qw(
764                             Perl_init_os_extras
765                             Perl_thread_create
766                             Perl_win32_init
767                             Perl_win32_term
768                             RunPerl
769                             win32_async_check
770                             win32_errno
771                             win32_environ
772                             win32_abort
773                             win32_fstat
774                             win32_stat
775                             win32_pipe
776                             win32_popen
777                             win32_pclose
778                             win32_rename
779                             win32_setmode
780                             win32_chsize
781                             win32_lseek
782                             win32_tell
783                             win32_dup
784                             win32_dup2
785                             win32_open
786                             win32_close
787                             win32_eof
788                             win32_isatty
789                             win32_read
790                             win32_write
791                             win32_mkdir
792                             win32_rmdir
793                             win32_chdir
794                             win32_flock
795                             win32_execv
796                             win32_execvp
797                             win32_htons
798                             win32_ntohs
799                             win32_htonl
800                             win32_ntohl
801                             win32_inet_addr
802                             win32_inet_ntoa
803                             win32_socket
804                             win32_bind
805                             win32_listen
806                             win32_accept
807                             win32_connect
808                             win32_send
809                             win32_sendto
810                             win32_recv
811                             win32_recvfrom
812                             win32_shutdown
813                             win32_closesocket
814                             win32_ioctlsocket
815                             win32_setsockopt
816                             win32_getsockopt
817                             win32_getpeername
818                             win32_getsockname
819                             win32_gethostname
820                             win32_gethostbyname
821                             win32_gethostbyaddr
822                             win32_getprotobyname
823                             win32_getprotobynumber
824                             win32_getservbyname
825                             win32_getservbyport
826                             win32_select
827                             win32_endhostent
828                             win32_endnetent
829                             win32_endprotoent
830                             win32_endservent
831                             win32_getnetent
832                             win32_getnetbyname
833                             win32_getnetbyaddr
834                             win32_getprotoent
835                             win32_getservent
836                             win32_sethostent
837                             win32_setnetent
838                             win32_setprotoent
839                             win32_setservent
840                             win32_getenv
841                             win32_putenv
842                             win32_perror
843                             win32_malloc
844                             win32_calloc
845                             win32_realloc
846                             win32_free
847                             win32_sleep
848                             win32_times
849                             win32_access
850                             win32_alarm
851                             win32_chmod
852                             win32_open_osfhandle
853                             win32_get_osfhandle
854                             win32_ioctl
855                             win32_link
856                             win32_unlink
857                             win32_utime
858                             win32_gettimeofday
859                             win32_uname
860                             win32_wait
861                             win32_waitpid
862                             win32_kill
863                             win32_str_os_error
864                             win32_opendir
865                             win32_readdir
866                             win32_telldir
867                             win32_seekdir
868                             win32_rewinddir
869                             win32_closedir
870                             win32_longpath
871                             win32_ansipath
872                             win32_os_id
873                             win32_getpid
874                             win32_crypt
875                             win32_dynaload
876                             win32_clearenv
877                             win32_stdin
878                             win32_stdout
879                             win32_stderr
880                             win32_ferror
881                             win32_feof
882                             win32_strerror
883                             win32_fprintf
884                             win32_printf
885                             win32_vfprintf
886                             win32_vprintf
887                             win32_fread
888                             win32_fwrite
889                             win32_fopen
890                             win32_fdopen
891                             win32_freopen
892                             win32_fclose
893                             win32_fputs
894                             win32_fputc
895                             win32_ungetc
896                             win32_getc
897                             win32_fileno
898                             win32_clearerr
899                             win32_fflush
900                             win32_ftell
901                             win32_fseek
902                             win32_fgetpos
903                             win32_fsetpos
904                             win32_rewind
905                             win32_tmpfile
906                             win32_setbuf
907                             win32_setvbuf
908                             win32_flushall
909                             win32_fcloseall
910                             win32_fgets
911                             win32_gets
912                             win32_fgetc
913                             win32_putc
914                             win32_puts
915                             win32_getchar
916                             win32_putchar
917                  ));
918 }
919 elsif ($ARGS{PLATFORM} eq 'vms') {
920     try_symbols(qw(
921                       Perl_cando
922                       Perl_cando_by_name
923                       Perl_closedir
924                       Perl_csighandler_init
925                       Perl_do_rmdir
926                       Perl_fileify_dirspec
927                       Perl_fileify_dirspec_ts
928                       Perl_fileify_dirspec_utf8
929                       Perl_fileify_dirspec_utf8_ts
930                       Perl_flex_fstat
931                       Perl_flex_lstat
932                       Perl_flex_stat
933                       Perl_kill_file
934                       Perl_my_chdir
935                       Perl_my_chmod
936                       Perl_my_crypt
937                       Perl_my_endpwent
938                       Perl_my_fclose
939                       Perl_my_fdopen
940                       Perl_my_fgetname
941                       Perl_my_flush
942                       Perl_my_fwrite
943                       Perl_my_gconvert
944                       Perl_my_getenv
945                       Perl_my_getenv_len
946                       Perl_my_getpwnam
947                       Perl_my_getpwuid
948                       Perl_my_gmtime
949                       Perl_my_kill
950                       Perl_my_killpg
951                       Perl_my_localtime
952                       Perl_my_mkdir
953                       Perl_my_sigaction
954                       Perl_my_symlink
955                       Perl_my_time
956                       Perl_my_tmpfile
957                       Perl_my_trnlnm
958                       Perl_my_utime
959                       Perl_my_waitpid
960                       Perl_opendir
961                       Perl_pathify_dirspec
962                       Perl_pathify_dirspec_ts
963                       Perl_pathify_dirspec_utf8
964                       Perl_pathify_dirspec_utf8_ts
965                       Perl_readdir
966                       Perl_readdir_r
967                       Perl_rename
968                       Perl_rmscopy
969                       Perl_rmsexpand
970                       Perl_rmsexpand_ts
971                       Perl_rmsexpand_utf8
972                       Perl_rmsexpand_utf8_ts
973                       Perl_seekdir
974                       Perl_sig_to_vmscondition
975                       Perl_telldir
976                       Perl_tounixpath
977                       Perl_tounixpath_ts
978                       Perl_tounixpath_utf8
979                       Perl_tounixpath_utf8_ts
980                       Perl_tounixspec
981                       Perl_tounixspec_ts
982                       Perl_tounixspec_utf8
983                       Perl_tounixspec_utf8_ts
984                       Perl_tovmspath
985                       Perl_tovmspath_ts
986                       Perl_tovmspath_utf8
987                       Perl_tovmspath_utf8_ts
988                       Perl_tovmsspec
989                       Perl_tovmsspec_ts
990                       Perl_tovmsspec_utf8
991                       Perl_tovmsspec_utf8_ts
992                       Perl_trim_unixpath
993                       Perl_vms_case_tolerant
994                       Perl_vms_do_aexec
995                       Perl_vms_do_exec
996                       Perl_vms_image_init
997                       Perl_vms_realpath
998                       Perl_vmssetenv
999                       Perl_vmssetuserlnm
1000                       Perl_vmstrnenv
1001                       PerlIO_openn
1002                  ));
1003 }
1004 elsif ($ARGS{PLATFORM} eq 'os2') {
1005     try_symbols(qw(
1006                       ctermid
1007                       get_sysinfo
1008                       Perl_OS2_init
1009                       Perl_OS2_init3
1010                       Perl_OS2_term
1011                       OS2_Perl_data
1012                       dlopen
1013                       dlsym
1014                       dlerror
1015                       dlclose
1016                       dup2
1017                       dup
1018                       my_tmpfile
1019                       my_tmpnam
1020                       my_flock
1021                       my_rmdir
1022                       my_mkdir
1023                       my_getpwuid
1024                       my_getpwnam
1025                       my_getpwent
1026                       my_setpwent
1027                       my_endpwent
1028                       fork_with_resources
1029                       croak_with_os2error
1030                       setgrent
1031                       endgrent
1032                       getgrent
1033                       malloc_mutex
1034                       threads_mutex
1035                       nthreads
1036                       nthreads_cond
1037                       os2_cond_wait
1038                       os2_stat
1039                       os2_execname
1040                       async_mssleep
1041                       msCounter
1042                       InfoTable
1043                       pthread_join
1044                       pthread_create
1045                       pthread_detach
1046                       XS_Cwd_change_drive
1047                       XS_Cwd_current_drive
1048                       XS_Cwd_extLibpath
1049                       XS_Cwd_extLibpath_set
1050                       XS_Cwd_sys_abspath
1051                       XS_Cwd_sys_chdir
1052                       XS_Cwd_sys_cwd
1053                       XS_Cwd_sys_is_absolute
1054                       XS_Cwd_sys_is_relative
1055                       XS_Cwd_sys_is_rooted
1056                       XS_DynaLoader_mod2fname
1057                       XS_File__Copy_syscopy
1058                       Perl_Register_MQ
1059                       Perl_Deregister_MQ
1060                       Perl_Serve_Messages
1061                       Perl_Process_Messages
1062                       init_PMWIN_entries
1063                       PMWIN_entries
1064                       Perl_hab_GET
1065                       loadByOrdinal
1066                       pExtFCN
1067                       os2error
1068                       ResetWinError
1069                       CroakWinError
1070                       PL_do_undump
1071                  ));
1072 }
1073 elsif ($ARGS{PLATFORM} eq 'netware') {
1074     try_symbols(qw(
1075                         Perl_init_os_extras
1076                         Perl_thread_create
1077                         Perl_nw5_init
1078                         RunPerl
1079                         AllocStdPerl
1080                         FreeStdPerl
1081                         do_spawn2
1082                         do_aspawn
1083                         nw_uname
1084                         nw_stdin
1085                         nw_stdout
1086                         nw_stderr
1087                         nw_feof
1088                         nw_ferror
1089                         nw_fopen
1090                         nw_fclose
1091                         nw_clearerr
1092                         nw_getc
1093                         nw_fgets
1094                         nw_fputc
1095                         nw_fputs
1096                         nw_fflush
1097                         nw_ungetc
1098                         nw_fileno
1099                         nw_fdopen
1100                         nw_freopen
1101                         nw_fread
1102                         nw_fwrite
1103                         nw_setbuf
1104                         nw_setvbuf
1105                         nw_vfprintf
1106                         nw_ftell
1107                         nw_fseek
1108                         nw_rewind
1109                         nw_tmpfile
1110                         nw_fgetpos
1111                         nw_fsetpos
1112                         nw_dup
1113                         nw_access
1114                         nw_chmod
1115                         nw_chsize
1116                         nw_close
1117                         nw_dup2
1118                         nw_flock
1119                         nw_isatty
1120                         nw_link
1121                         nw_lseek
1122                         nw_stat
1123                         nw_mktemp
1124                         nw_open
1125                         nw_read
1126                         nw_rename
1127                         nw_setmode
1128                         nw_unlink
1129                         nw_utime
1130                         nw_write
1131                         nw_chdir
1132                         nw_rmdir
1133                         nw_closedir
1134                         nw_opendir
1135                         nw_readdir
1136                         nw_rewinddir
1137                         nw_seekdir
1138                         nw_telldir
1139                         nw_htonl
1140                         nw_htons
1141                         nw_ntohl
1142                         nw_ntohs
1143                         nw_accept
1144                         nw_bind
1145                         nw_connect
1146                         nw_endhostent
1147                         nw_endnetent
1148                         nw_endprotoent
1149                         nw_endservent
1150                         nw_gethostbyaddr
1151                         nw_gethostbyname
1152                         nw_gethostent
1153                         nw_gethostname
1154                         nw_getnetbyaddr
1155                         nw_getnetbyname
1156                         nw_getnetent
1157                         nw_getpeername
1158                         nw_getprotobyname
1159                         nw_getprotobynumber
1160                         nw_getprotoent
1161                         nw_getservbyname
1162                         nw_getservbyport
1163                         nw_getservent
1164                         nw_getsockname
1165                         nw_getsockopt
1166                         nw_inet_addr
1167                         nw_listen
1168                         nw_socket
1169                         nw_recv
1170                         nw_recvfrom
1171                         nw_select
1172                         nw_send
1173                         nw_sendto
1174                         nw_sethostent
1175                         nw_setnetent
1176                         nw_setprotoent
1177                         nw_setservent
1178                         nw_setsockopt
1179                         nw_inet_ntoa
1180                         nw_shutdown
1181                         nw_crypt
1182                         nw_execvp
1183                         nw_kill
1184                         nw_Popen
1185                         nw_Pclose
1186                         nw_Pipe
1187                         nw_times
1188                         nw_waitpid
1189                         nw_getpid
1190                         nw_spawnvp
1191                         nw_os_id
1192                         nw_open_osfhandle
1193                         nw_get_osfhandle
1194                         nw_abort
1195                         nw_sleep
1196                         nw_wait
1197                         nw_dynaload
1198                         nw_strerror
1199                         fnFpSetMode
1200                         fnInsertHashListAddrs
1201                         fnGetHashListAddrs
1202                         Perl_deb
1203                         Perl_sv_setsv
1204                         Perl_sv_catsv
1205                         Perl_sv_catpvn
1206                         Perl_sv_2pv
1207                         nw_freeenviron
1208                         Remove_Thread_Ctx
1209                  ));
1210 }
1211
1212 # When added this code was only run for Win32 and WinCE
1213 # Currently only Win32 links static extensions into the shared library.
1214 # The WinCE makefile doesn't appear to support static extensions, so this code
1215 # can't have any effect there.
1216 # The NetWare Makefile doesn't support static extensions (and hardcodes the
1217 # list of dynamic extensions, and the rules to build them)
1218 # For *nix (and presumably OS/2) with a shared libperl, Makefile.SH compiles
1219 # static extensions with -fPIC, but links them to perl, not libperl.so
1220 # The VMS build scripts don't yet implement static extensions at all.
1221
1222 if ($ARGS{PLATFORM} =~ /^win(?:32|ce)$/) {
1223     # records of type boot_module for statically linked modules (except Dynaloader)
1224     my $static_ext = $Config{static_ext} // "";
1225     $static_ext =~ s/\//__/g;
1226     $static_ext =~ s/\bDynaLoader\b//;
1227     try_symbols(map {"boot_$_"} grep {/\S/} split /\s+/, $static_ext);
1228     try_symbols("init_Win32CORE") if $static_ext =~ /\bWin32CORE\b/;
1229 }
1230
1231 if ($ARGS{PLATFORM} eq 'os2') {
1232     my (%mapped, @missing);
1233     open MAP, '<', 'miniperl.map' or die 'Cannot read miniperl.map';
1234     /^\s*[\da-f:]+\s+(\w+)/i and $mapped{$1}++ foreach <MAP>;
1235     close MAP or die 'Cannot close miniperl.map';
1236
1237     @missing = grep { !exists $mapped{$_} }
1238                     keys %export;
1239     @missing = grep { !exists $exportperlmalloc{$_} } @missing;
1240     delete $export{$_} foreach @missing;
1241 }
1242
1243 ###############################################################################
1244
1245 # Now all symbols should be defined because next we are going to output them.
1246
1247 # Start with platform specific headers:
1248
1249 if ($ARGS{PLATFORM} =~ /^win(?:32|ce)$/) {
1250     my $dll = $define{PERL_DLL} ? $define{PERL_DLL} =~ s/\.dll$//ir
1251         : "perl$Config{api_revision}$Config{api_version}";
1252     print "LIBRARY $dll\n";
1253     # The DESCRIPTION module definition file statement is not supported
1254     # by VC7 onwards.
1255     if ($ARGS{CCTYPE} =~ /^(?:MSVC60|GCC)$/) {
1256         print "DESCRIPTION 'Perl interpreter'\n";
1257     }
1258     print "EXPORTS\n";
1259 }
1260 elsif ($ARGS{PLATFORM} eq 'os2') {
1261     (my $v = $]) =~ s/(\d\.\d\d\d)(\d\d)$/$1_$2/;
1262     $v .= '-thread' if $Config{archname} =~ /-thread/;
1263     (my $dll = $define{PERL_DLL}) =~ s/\.dll$//i;
1264     $v .= "\@$Config{perl_patchlevel}" if $Config{perl_patchlevel};
1265     my $d = "DESCRIPTION '\@#perl5-porters\@perl.org:$v#\@ Perl interpreter, configured as $Config{config_args}'";
1266     $d = substr($d, 0, 249) . "...'" if length $d > 253;
1267     print <<"---EOP---";
1268 LIBRARY '$dll' INITINSTANCE TERMINSTANCE
1269 $d
1270 STACKSIZE 32768
1271 CODE LOADONCALL
1272 DATA LOADONCALL NONSHARED MULTIPLE
1273 EXPORTS
1274 ---EOP---
1275 }
1276 elsif ($ARGS{PLATFORM} eq 'aix') {
1277     my $OSVER = `uname -v`;
1278     chop $OSVER;
1279     my $OSREL = `uname -r`;
1280     chop $OSREL;
1281     if ($OSVER > 4 || ($OSVER == 4 && $OSREL >= 3)) {
1282         print "#! ..\n";
1283     } else {
1284         print "#!\n";
1285     }
1286 }
1287 elsif ($ARGS{PLATFORM} eq 'netware') {
1288         if ($ARGS{FILETYPE} eq 'def') {
1289         print "LIBRARY perl$Config{api_revision}$Config{api_version}\n";
1290         print "DESCRIPTION 'Perl interpreter for NetWare'\n";
1291         print "EXPORTS\n";
1292         }
1293 }
1294
1295 # Then the symbols
1296
1297 my @symbols = $fold ? sort {lc $a cmp lc $b} keys %export : sort keys %export;
1298 foreach my $symbol (@symbols) {
1299     if (PLATFORM eq 'win32' || PLATFORM eq 'wince') {
1300         # Remembering the origin file of each symbol is an alternative to PL_ matching
1301         if (substr($symbol, 0, 3) eq 'PL_') {
1302             print "\t$symbol DATA\n";
1303         }
1304         else {
1305             print "\t$symbol\n";
1306         }
1307     }
1308     elsif (PLATFORM eq 'os2') {
1309         printf qq(    %-31s \@%s\n),
1310           qq("$symbol"), $ordinal{$symbol} || ++$sym_ord;
1311         printf qq(    %-31s \@%s\n),
1312           qq("$exportperlmalloc{$symbol}" = "$symbol"),
1313           $ordinal{$exportperlmalloc{$symbol}} || ++$sym_ord
1314           if $exportperlmalloc and exists $exportperlmalloc{$symbol};
1315     }
1316     elsif (PLATFORM eq 'netware') {
1317         print "\t$symbol,\n";
1318     } else {
1319         print "$symbol\n";
1320     }
1321 }
1322
1323 # Then platform specific footers.
1324
1325 if ($ARGS{PLATFORM} eq 'os2') {
1326     print <<EOP;
1327     dll_perlmain=main
1328     fill_extLibpath
1329     dir_subst
1330     Perl_OS2_handler_install
1331
1332 ; LAST_ORDINAL=$sym_ord
1333 EOP
1334 }
1335
1336 1;