only check for archname as last component of %INC path
[scpubgit/Object-Remote.git] / lib / Object / Remote / FatNode.pm
CommitLineData
eee9a548 1package Object::Remote::FatNode;
2
3use strictures 1;
4use Config;
5use B qw(perlstring);
6
f6ac5927 7my @exclude_mods = qw(XSLoader.pm DynaLoader.pm);
1bf55307 8#used by t/watchdog_fatnode
f1d70835 9our $INHIBIT_RUN_NODE = 0;
f6ac5927 10
eee9a548 11sub stripspace {
12 my ($text) = @_;
13 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
14 $text;
15}
16
c5699dec 17chomp(my @base_libs = `"$^X" -le"print for grep defined and !ref, \@INC"`);
18my %base_libs = map +($_ => 1), @base_libs;
19
20my @extra_libs = grep not(ref($_) or $base_libs{$_}), @INC;
21my $extra_libs = join ' ', map {
22 my $lib = $_;
23 $lib =~ s{'}{'\\''}g;
24 "-I'$lib'\n";
668343cd 25} @extra_libs;
eee9a548 26
27my $command = qq(
28 $^X
29 $extra_libs
30 -mObject::Remote
31 -mObject::Remote::Connector::STDIO
783105c4 32 -mFuture
8c81a0e5 33 -mMRO::Compat
eee9a548 34 -mClass::C3
8c81a0e5 35 -mClass::C3::next
36 -mAlgorithm::C3
eee9a548 37 -mObject::Remote::ModuleLoader
38 -mObject::Remote::Node
4c8c83d7 39 -mMethod::Generate::BuildAll
40 -mMethod::Generate::DemolishAll
dd6e1327 41 -mMoo::HandleMoose::_TypeMap
eee9a548 42 -mJSON::PP
df8e0ca6 43 -e 'print join "\\n", \%INC'
eee9a548 44);
45
46$command =~ s/\n/ /g;
47
df8e0ca6 48chomp(my @inc = qx($command));
49
1bf55307 50my %exclude = map { $_ => 1 } @exclude_mods;
e454581e 51
5e850e3e 52my %file_names = @inc;
eee9a548 53
e454581e 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
57my %mods =
58 map { $file_names{$_} => $_ }
59 grep { $file_names{$_} =~ /\Q$_\E$/ } keys %file_names;
60
f6ac5927 61foreach(keys(%mods)) {
62 if ($exclude{ $mods{$_} }) {
55c0d020 63 delete($mods{$_});
f6ac5927 64 }
65}
66
06330047 67my %mod_libdir = map {
68 (my $lib_dir = $_) =~ s/[\\\/]$mods{$_}\z//;
69 $_ => $lib_dir;
70} keys %mods;
71
5e850e3e 72my @non_core_non_arch = ( $file_names{'Devel/GlobalDestruction.pm'} );
06330047 73
74push @non_core_non_arch, grep {
75 local $_ = $mod_libdir{$_};
61c6bba7 76 not (
06330047 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}/
15b847bb 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 )
06330047 85} keys %mods;
15b847bb 86
06330047 87my @core_non_arch = grep {
88 local $_ = $mod_libdir{$_};
15b847bb 89 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
90 and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
06330047 91 or /[\\\/]\Q$Config{archname}\E\z/ or /[\\\/]\Q$Config{myarchname}\E\z/)
92} keys %mods;
15b847bb 93
eee9a548 94my $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 {
1ef3d225 99 my (%fatpacked,%fatpacked_extra);
eee9a548 100END_START
f6ac5927 101
cc62b744 102$start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
f6ac5927 103
eee9a548 104my $end = stripspace <<'END_END';
1ef3d225 105 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 106
1ef3d225 107 sub load_from_hash {
108 if (my $fat = $_[0]->{$_[1]}) {
f6ac5927 109 if ($exclude{$_[1]}) {
110 warn "Will not pre-load '$_[1]'";
55c0d020 111 return undef;
f6ac5927 112 }
55c0d020 113
cc62b744 114 #warn "Handling $_[1]";
eee9a548 115 open my $fh, '<', \$fat;
116 return $fh;
117 }
55c0d020 118
8c81a0e5 119 #Uncomment this to find brokenness
120 #warn "Missing $_[1]";
f6ac5927 121 return;
1ef3d225 122 }
123
124 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
125 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 126
127 } # END OF FATPACK CODE
128
129 use strictures 1;
130 use Object::Remote::Node;
55c0d020 131
f129bfaf 132 unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
55c0d020 133 Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
f129bfaf 134 }
55c0d020 135
eee9a548 136END_END
137
138my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
15b847bb 139 @non_core_non_arch, @core_non_arch;
eee9a548 140
1ef3d225 141sub generate_fatpack_hash {
142 my ($hash_name, $orig) = @_;
143 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 144 my $name = uc join '_', split '/', $stub;
1ef3d225 145 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
3ab6f7e2 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;
1ef3d225 151}
152
153my @segments = (
15b847bb 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),
1ef3d225 156);
eee9a548 157
90f5193d 158#print STDERR Dumper(\@segments);
159our $DATA = join "\n", $start, @segments, $end;
eee9a548 160
1611;