This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Mark the common case with LIKELY branch predictor hint
[perl5.git] / win32 / FindExt.pm
1 package FindExt;
2
3 our $VERSION = '1.02';
4
5 use strict;
6 use warnings;
7
8 my $no = join('|',qw(GDBM_File ODBM_File NDBM_File DB_File
9                      VMS VMS-DCLsym VMS-Stdio Sys-Syslog IPC-SysV I18N-Langinfo));
10 $no = qr/^(?:$no)$/i;
11
12 sub apply_config {
13     my ($config) = @_;
14     my @no;
15
16     push @no, 'Sys-Syslog' if $^O eq 'MSWin32';
17
18     # duplicates logic from Configure (mostly)
19     push @no, "DB_File" unless $config->{i_db};
20     push @no, "GDBM_File" unless $config->{i_gdbm};
21     push @no, "I18N-Langinfo" unless $config->{i_langinfo} && $config->{i_nl_langinfo};
22     push @no, "IPC-SysV" unless $config->{d_msg} || $config->{d_sem} || $config->{d_shm};
23     push @no, "NDBM_File" unless $config->{d_ndbm};
24     push @no, "ODBM_File"
25       unless ($config->{i_dbm} || $config->{i_rpcsvcdbm}) && !$config->{d_cplusplus};
26     push @no, "VMS.*" unless $^O eq "VMS";
27     push @no, "Win32.*" unless $^O eq "MSWin32" || $^O eq "cygwin";
28
29     $no = join('|', @no);
30     $no = qr/^(?:$no)$/i;
31 }
32
33 my %ext;
34 my %static;
35
36 sub set_static_extensions {
37     # adjust results of scan_ext, and also save
38     # statics in case scan_ext hasn't been called yet.
39     # if '*' is passed then all XS extensions are static
40     # (with possible exclusions)
41     %static = ();
42     my @list = @_;
43     if (@_ and $_[0] eq '*') {
44         my %excl = map {$_=>1} map {m/^!(.*)$/} @_[1 .. $#_];
45         @list = grep {!exists $excl{$_}} keys %ext;
46     }
47     for (@list) {
48         $static{$_} = 1;
49         $ext{$_} = 'static' if $ext{$_} && $ext{$_} eq 'dynamic';
50     }
51
52     # Encode is a special case.  If we are building Encode as a static
53     # extension, we need to explicitly list its subextensions as well.
54     # For other nested extensions, this is handled automatically by
55     # the appropriate Makefile.PL.
56     if ($ext{Encode} && $ext{Encode} eq 'static') {
57         foreach my $file (`dir /s /b ..\\cpan\\Encode\\Makefile.PL`) {
58             if ($file =~ /\b(Encode\\.+)\\Makefile\.PL/) {
59                 (my $xxx = $1) =~ s|\\|/|g;
60                 $static{$xxx} = 1;
61                 $ext{$xxx} = 'static';
62             }
63         }
64     }
65 }
66
67 sub scan_ext
68 {
69     my $dir  = shift;
70     find_ext("$dir/");
71     extensions();
72 }
73
74 sub _ext_eq {
75     my $key = shift;
76     sub {
77         sort grep $ext{$_} eq $key, keys %ext;
78     }
79 }
80
81 *dynamic_ext = _ext_eq('dynamic');
82 *static_ext = _ext_eq('static');
83 *nonxs_ext = _ext_eq('nonxs');
84
85 sub _ext_ne {
86     my $key = shift;
87     sub {
88         sort grep $ext{$_} ne $key, keys %ext;
89     }
90 }
91
92 *extensions = _ext_ne('known');
93 # faithfully copy Configure in not including nonxs extensions for the nonce
94 *known_extensions = _ext_ne('nonxs');
95
96 sub is_static
97 {
98  return $ext{$_[0]} eq 'static'
99 }
100
101 sub has_xs_or_c {
102     my $dir = shift;
103     opendir my $dh, $dir or die "opendir $dir: $!";
104     while (defined (my $item = readdir $dh)) {
105         return 1 if $item =~ /\.xs$/;
106         return 1 if $item =~ /\.c$/;
107     }
108     return 0;
109 }
110
111 # Function to find available extensions, ignoring DynaLoader
112 sub find_ext
113 {
114     my $ext_dir = shift;
115     opendir my $dh, "$ext_dir";
116     while (defined (my $item = readdir $dh)) {
117         next if $item =~ /^\.\.?$/;
118         next if $item eq "DynaLoader";
119         next unless -d "$ext_dir$item";
120         my $this_ext = $item;
121         my $leaf = $item;
122
123         $this_ext =~ s!-!/!g;
124         $leaf =~ s/.*-//;
125
126         # Temporary hack to cope with smokers that are not clearing directories:
127         next if $ext{$this_ext};
128
129         if (has_xs_or_c("$ext_dir$item")) {
130             $ext{$this_ext} = $static{$this_ext} ? 'static' : 'dynamic';
131         } else {
132             $ext{$this_ext} = 'nonxs';
133         }
134         $ext{$this_ext} = 'known' if $ext{$this_ext} && $item =~ $no;
135     }
136 }
137
138 1;
139 # Local variables:
140 # cperl-indent-level: 4
141 # indent-tabs-mode: nil
142 # End:
143 #
144 # ex: set ts=8 sts=4 sw=4 et: