830619e80075db0318b0f4c3b2e272686a4b3d00
[p5sagit/p5-mst-13.2.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 Syslog SysV Langinfo));
10 $no = qr/^(?:$no)$/i;
11
12 my %ext;
13 my %static;
14
15 sub set_static_extensions {
16     # adjust results of scan_ext, and also save
17     # statics in case scan_ext hasn't been called yet.
18     # if '*' is passed then all XS extensions are static
19     # (with possible exclusions)
20     %static = ();
21     my @list = @_;
22     if ($_[0] eq '*') {
23         my %excl = map {$_=>1} map {m/^!(.*)$/} @_[1 .. $#_];
24         @list = grep {!exists $excl{$_}} keys %ext;
25     }
26     for (@list) {
27         $static{$_} = 1;
28         $ext{$_} = 'static' if $ext{$_} && $ext{$_} eq 'dynamic';
29     }
30 }
31
32 sub scan_ext
33 {
34     my $dir  = shift;
35     find_ext("$dir/", '');
36     extensions();
37 }
38
39 sub dynamic_ext
40 {
41  return sort grep $ext{$_} eq 'dynamic',keys %ext;
42 }
43
44 sub static_ext
45 {
46  return sort grep $ext{$_} eq 'static',keys %ext;
47 }
48
49 sub nonxs_ext
50 {
51  return sort grep $ext{$_} eq 'nonxs',keys %ext;
52 }
53
54 sub extensions
55 {
56  return sort grep $ext{$_} ne 'known',keys %ext;
57 }
58
59 sub known_extensions
60 {
61  # faithfully copy Configure in not including nonxs extensions for the nonce
62  return sort grep $ext{$_} ne 'nonxs',keys %ext;
63 }
64
65 sub is_static
66 {
67  return $ext{$_[0]} eq 'static'
68 }
69
70 # Function to recursively find available extensions, ignoring DynaLoader
71 # NOTE: recursion limit of 10 to prevent runaway in case of symlink madness
72 sub find_ext
73 {
74     my $ext_dir = shift;
75     my $prefix = shift;
76     opendir my $dh, "$ext_dir$prefix";
77     while (defined (my $item = readdir $dh)) {
78         next if $item =~ /^\.\.?$/;
79         next if $item eq "DynaLoader";
80         my $this_ext = my $this_ext_dir = "$prefix$item";
81         my $leaf = $item;
82
83         $this_ext =~ s!-!/!g;
84         $leaf =~ s/.*-//;
85
86         if (-f "$ext_dir$this_ext_dir/$leaf.xs" || -f "$ext_dir$this_ext_dir/$leaf.c" ) {
87             $ext{$this_ext} = $static{$this_ext} ? 'static' : 'dynamic';
88         } elsif (-f "$ext_dir$this_ext_dir/Makefile.PL") {
89             $ext{$this_ext} = 'nonxs';
90         } else {
91             # It's not actually an extension. So recurse into it.
92             if (-d "$ext_dir$this_ext_dir" && $prefix =~ tr#/## < 10) {
93                 find_ext($ext_dir, "$this_ext_dir/");
94             }
95         }
96         $ext{$this_ext} = 'known' if $ext{$this_ext} && $item =~ $no;
97     }
98
99 # Special case:  Add in modules that nest beyond the first level.
100 # Currently threads/shared and Hash/Util/FieldHash, since they are
101 # not picked up by the recursive find above (and adding in general
102 # recursive finding breaks SDBM_File/sdbm).
103 # A.D. 20011025 (SDBM), ajgough 20071008 (FieldHash)
104
105     if (!$prefix && -d "${ext_dir}threads/shared") {
106         $ext{"threads/shared"} = 'dynamic';
107     }
108     if (!$prefix && -d "${ext_dir}Hash/Util/FieldHash") {
109         $ext{"Hash/Util/FieldHash"} = 'dynamic';
110     }
111 }
112
113 1;
114 # Local variables:
115 # cperl-indent-level: 4
116 # indent-tabs-mode: nil
117 # End:
118 #
119 # ex: set ts=8 sts=4 sw=4 et: