Move env var forwarding from fatnode to perl interpreter role
[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);
f129bfaf 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
17my %maybe_libs = map +($_ => 1), grep defined, (values %Config, '.');
18
19my @extra_libs = grep not(ref($_) or $maybe_libs{$_}), @INC;
20
21my $extra_libs = join '', map " -I$_\n", @extra_libs;
22
23my $command = qq(
24 $^X
25 $extra_libs
26 -mObject::Remote
27 -mObject::Remote::Connector::STDIO
28 -mCPS::Future
8c81a0e5 29 -mMRO::Compat
eee9a548 30 -mClass::C3
8c81a0e5 31 -mClass::C3::next
32 -mAlgorithm::C3
eee9a548 33 -mObject::Remote::ModuleLoader
34 -mObject::Remote::Node
4c8c83d7 35 -mMethod::Generate::BuildAll
36 -mMethod::Generate::DemolishAll
eee9a548 37 -mJSON::PP
df8e0ca6 38 -e 'print join "\\n", \%INC'
eee9a548 39);
40
41$command =~ s/\n/ /g;
42
df8e0ca6 43chomp(my @inc = qx($command));
44
f6ac5927 45my %exclude = map { $_ => 1 } @exclude_mods;
df8e0ca6 46my %mods = reverse @inc;
eee9a548 47
f6ac5927 48foreach(keys(%mods)) {
49 if ($exclude{ $mods{$_} }) {
50 delete($mods{$_});
51 }
52}
53
15b847bb 54my @non_core_non_arch = ( $mods{'Devel/GlobalDestruction.pm'} );
55push @non_core_non_arch, grep +(
61c6bba7 56 not (
15b847bb 57 #some of the config variables can be empty which will eval as a matching regex
58 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
59 or $Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
60 or $Config{vendorarchexp} ne '' && /^\Q$Config{vendorarchexp}/
61 or $Config{sitearchexp} ne '' && /^\Q$Config{sitearchexp}/
62 )
63), grep !/\Q$Config{archname}/, grep !/\Q$Config{myarchname}/, keys %mods;
64
65my @core_non_arch = grep +(
66 $Config{privlibexp} ne '' && /^\Q$Config{privlibexp}/
67 and not($Config{archlibexp} ne '' && /^\Q$Config{archlibexp}/
68 or /\Q$Config{archname}/ or /\Q$Config{myarchname}/)
69), keys %mods;
70
eee9a548 71my $start = stripspace <<'END_START';
72 # This chunk of stuff was generated by Object::Remote::FatNode. To find
73 # the original file's code, look for the end of this BEGIN block or the
74 # string 'FATPACK'
75 BEGIN {
1ef3d225 76 my (%fatpacked,%fatpacked_extra);
eee9a548 77END_START
f6ac5927 78
cc62b744 79$start .= 'my %exclude = map { $_ => 1 } (\'' . join("','", @exclude_mods) . "');\n";
f6ac5927 80
eee9a548 81my $end = stripspace <<'END_END';
1ef3d225 82 s/^ //mg for values %fatpacked, values %fatpacked_extra;
eee9a548 83
1ef3d225 84 sub load_from_hash {
85 if (my $fat = $_[0]->{$_[1]}) {
f6ac5927 86 if ($exclude{$_[1]}) {
87 warn "Will not pre-load '$_[1]'";
88 return undef;
89 }
90
cc62b744 91 #warn "Handling $_[1]";
eee9a548 92 open my $fh, '<', \$fat;
93 return $fh;
94 }
1900601d 95
8c81a0e5 96 #Uncomment this to find brokenness
97 #warn "Missing $_[1]";
f6ac5927 98 return;
1ef3d225 99 }
100
101 unshift @INC, sub { load_from_hash(\%fatpacked, $_[1]) };
102 push @INC, sub { load_from_hash(\%fatpacked_extra, $_[1]) };
eee9a548 103
104 } # END OF FATPACK CODE
105
106 use strictures 1;
107 use Object::Remote::Node;
f129bfaf 108
109 unless ($Object::Remote::FatNode::INHIBIT_RUN_NODE) {
110 Object::Remote::Node->run(watchdog_timeout => $WATCHDOG_TIMEOUT);
111 }
112
eee9a548 113END_END
114
115my %files = map +($mods{$_} => scalar do { local (@ARGV, $/) = ($_); <> }),
15b847bb 116 @non_core_non_arch, @core_non_arch;
eee9a548 117
1ef3d225 118sub generate_fatpack_hash {
119 my ($hash_name, $orig) = @_;
120 (my $stub = $orig) =~ s/\.pm$//;
eee9a548 121 my $name = uc join '_', split '/', $stub;
1ef3d225 122 my $data = $files{$orig} or die $orig; $data =~ s/^/ /mg;
3ab6f7e2 123 $data .= "\n" unless $data =~ m/\n$/;
124 my $ret = '$'.$hash_name.'{'.perlstring($orig).qq!} = <<'${name}';\n!
125 .qq!${data}${name}\n!;
126# warn $ret;
127 return $ret;
1ef3d225 128}
129
130my @segments = (
15b847bb 131 map(generate_fatpack_hash('fatpacked', $_), sort map $mods{$_}, @non_core_non_arch),
132 map(generate_fatpack_hash('fatpacked_extra', $_), sort map $mods{$_}, @core_non_arch),
1ef3d225 133);
eee9a548 134
90f5193d 135#print STDERR Dumper(\@segments);
136our $DATA = join "\n", $start, @segments, $end;
eee9a548 137
1381;