only check for archname as last component of %INC path
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
1 package Object::Remote::FatNode;
2
3 use strictures 1;
4 use Config;
5 use B qw(perlstring);
6
7 my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
8 #used by t/watchdog_fatnode
9 our $INHIBIT_RUN_NODE = 0;
10
11 sub stripspace {
12   my ($text) = @_;
13   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
14   $text;
15 }
16
17 chomp(my @base_libs = `"$^X" -le"print for grep defined and !ref, \@INC"`);
18 my %base_libs = map +($_ => 1), @base_libs;
19
20 my @extra_libs = grep not(ref($_) or $base_libs{$_}), @INC;
21 my $extra_libs = join ' ', map {
22   my $lib = $_;
23   $lib =~ s{'}{'\\''}g;
24   "-I'$lib'\n";
25 } @extra_libs;
26
27 my $command = qq(
28   $^X
29   $extra_libs
30   -mObject::Remote
31   -mObject::Remote::Connector::STDIO
32   -mFuture
33   -mMRO::Compat
34   -mClass::C3
35   -mClass::C3::next
36   -mAlgorithm::C3
37   -mObject::Remote::ModuleLoader
38   -mObject::Remote::Node
39   -mMethod::Generate::BuildAll
40   -mMethod::Generate::DemolishAll
41   -mMoo::HandleMoose::_TypeMap
42   -mJSON::PP
43   -e 'print join "\\n", \%INC'
44 );
45
46 $command =~ s/\n/ /g;
47
48 chomp(my @inc = qx($command));
49
50 my %exclude = map { $_ => 1 } @exclude_mods;
51
52 my %file_names = @inc;
53
54 # only include mods that match the filename,
55 # ie ones that will succeed with a require $module
56 # https://rt.cpan.org/Ticket/Display.html?id=100478
57 my %mods =
58   map { $file_names{$_} => $_ }
59   grep { $file_names{$_} =~ /\Q$_\E$/ } keys %file_names;
60
61 foreach(keys(%mods)) {
62   if ($exclude{ $mods{$_} }) {
63     delete($mods{$_});
64   }
65 }
66
67 my %mod_libdir = map {
68   (my $lib_dir = $_) =~ s/[\\\/]$mods{$_}\z//;
69   $_ => $lib_dir;
70 } keys %mods;
71
72 my @non_core_non_arch = ( $file_names{'Devel/GlobalDestruction.pm'} );
73
74 push @non_core_non_arch, grep {
75   local $_ = $mod_libdir{$_};
76   not (
77     /[\\\/]\Q$Config{archname}\E\z/
78       or /[\\\/]\Q$Config{myarchname}\E\z/
79       #some of the config variables can be empty which will eval as a matching regex
80       or $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
81       or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
82       or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
83       or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
84   )
85 } keys %mods;
86
87 my @core_non_arch = grep {
88   local $_ = $mod_libdir{$_};
89   $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
90   and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
91     or /[\\\/]\Q$Config{archname}\E\z/ or /[\\\/]\Q$Config{myarchname}\E\z/)
92 } keys %mods;
93
94 my $start = stripspace <<'END_START';
95   # This chunk of stuff was generated by Object::Remote::FatNode. To find
96   # the original file's code, look for the end of this BEGIN block or the
97   # string 'FATPACK'
98   BEGIN {
99   my (%fatpacked,%fatpacked_extra);
100 END_START
101
102 $start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
103
104 my $end = stripspace <<'END_END';
105   s/^  //mg for values %fatpacked, values %fatpacked_extra;
106
107   sub load_from_hash {
108     if (my $fat = $_[0]->{$_[1]}) {
109       if ($exclude{$_[1]}) {
110         warn "Will not pre-load '$_[1]'";
111         return undef;
112       }
113
114       #warn "Handling $_[1]";
115       open my $fh, '<', \$fat;
116       return $fh;
117     }
118
119     #Uncomment this to find brokenness
120     #warn "Missing $_[1]";
121     return;
122   }
123
124   unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
125   push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
126
127   } # END OF FATPACK CODE
128
129   use strictures 1;
130   use Object::Remote::Node;
131
132   unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
133     Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
134   }
135
136 END_END
137
138 my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
139               @non_core_non_arch, @core_non_arch;
140
141 sub generate_fatpack_hash {
142   my ($hash_name, $orig) = @_;
143   (my $stub = $orig) =~ s/\.pm$//;
144   my $name = uc join '_', split '/', $stub;
145   my $data = $files{$orig} or die $orig; $data =~ s/^/  /mg;
146   $data .= "\n" unless $data =~ m/\n$/;
147   my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
148     .qq!${data}${name}\n!;
149 #  warn $ret;
150   return $ret;
151 }
152
153 my @segments = (
154   map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
155   map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
156 );
157
158 #print STDERR Dumper(\@segments);
159 our $DATA = join "\n", $start, @segments, $end;
160
161 1;