Skip to content

Commit

Permalink
C 1.55_11: WIP --cross=path/config.sh support
Browse files Browse the repository at this point in the history
See GH #428
  • Loading branch information
rurban committed Nov 18, 2018
1 parent 34fe573 commit 43fde10
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 20 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -5,6 +5,7 @@

1.55_11 2018-11-?? rurban
* CC 1.16_02: Fixup illegal goto lab_0; (#425)
* C: Support -cross=<path/config.sh> (#428)

1.55_10 2018-11-06 rurban
* C: Add missing optimize flags
Expand Down
22 changes: 13 additions & 9 deletions Makefile.PL
Expand Up @@ -65,7 +65,7 @@ sub b_c_deps {
my $blib = "-I".File::Spec->catfile(qw(.. .. lib auto))
." -I".File::Spec->catfile(qw(.. .. lib));
my $runperl = $X . ($CORE ? ' '.$blib : "");
return "AnyDBM_File AutoLoader B B::AV B::Asmdata B::BINOP B::BM B::C B::C::Config B::C::InitSection B::C::Section B::CC B::COP B::CV B::FAKEOP B::FM B::GV B::HE B::HV B::INVLIST B::IO B::IV B::LEXWARN B::LISTOP B::LOGOP B::LOOP B::MAGIC B::METHOP B::NULL B::NV B::OBJECT B::OP B::PADLIST B::PADNAME B::PADNAMELIST B::PADOP B::PMOP B::PV B::PVIV B::PVLV B::PVMG B::PVNV B::PVOP B::REGEXP B::RHE B::RV B::SPECIAL B::STASHGV B::SV B::SVOP B::Section B::UNOP B::UNOP_AUX B::UV CORE CORE::GLOBAL Carp Config DB DynaLoader EV Encode Errno Exporter Exporter::Heavy ExtUtils ExtUtils::Constant ExtUtils::Constant::ProxySubs Fcntl FileHandle IO IO::File IO::Handle IO::Poll IO::Seekable IO::Socket IO::Socket::SSL Int Internals Net Net::DNS Num O POSIX PerlIO PerlIO::Layer PerlIO::scalar Regexp SelectSaver Str Symbol UInt UNIVERSAL XSConfig XSLoader __ANON__ arybase arybase::mg attributes constant coretypes int main mro num re str strict threads uint utf8 vars version warnings warnings::register Socket" if $CORE and $] > 5.020;
return "AnyDBM_File AutoLoader B B::AV B::Asmdata B::BINOP B::BM B::C B::C::Config B::C::InitSection B::C::Section B::CC B::COP B::CV B::FAKEOP B::FM B::GV B::HE B::HV B::INVLIST B::IO B::IV B::LEXWARN B::LISTOP B::LOGOP B::LOOP B::MAGIC B::METHOP B::NULL B::NV B::OBJECT B::OP B::PADLIST B::PADNAME B::PADNAMELIST B::PADOP B::PMOP B::PV B::PVIV B::PVLV B::PVMG B::PVNV B::PVOP B::REGEXP B::RHE B::RV B::SPECIAL B::STASHGV B::SV B::SVOP B::Section B::UNOP B::UNOP_AUX B::UV CORE CORE::GLOBAL Carp Config DB DynaLoader EV Encode Errno Exporter Exporter::Heavy ExtUtils ExtUtils::Constant ExtUtils::Constant::ProxySubs Fcntl FileHandle IO IO::File IO::Handle IO::Poll IO::Seekable IO::Socket IO::Socket::SSL Int Internals Net Net::DNS Num O POSIX PerlIO PerlIO::Layer PerlIO::scalar Regexp SelectSaver Str Symbol UInt UNIVERSAL XSConfig XSLoader __ANON__ arybase arybase::mg attributes constant coretypes int main mro num re str strict threads uint utf8 vars version warnings warnings::register" if $CORE and $] > 5.020;
return `$runperl -Ilib -mB::C -mO -MB -e"B::C::collect_deps()"`;
}

Expand Down Expand Up @@ -97,7 +97,7 @@ sub write_b_c_config {
print PH "# broken or patched upstream 5.22 ByteLoader. undef for yet undecided\n";
$have_byteloader = 0 if $Config{usecperl}; # have it, but still broken
print PH "\$have_byteloader = $have_byteloader;\n";
print PH "# cperl 5.22.2:\n";
print PH "# since cperl 5.22.2:\n";
print PH "\$have_op_rettype = $have_op_rettype;\n";
print PH "\$have_HEK_STATIC = $have_HEK_STATIC;\n";
print PH "# use extra compiler flags, after ccopts, resp. ldopts\n";
Expand All @@ -113,12 +113,15 @@ sub write_b_c_config {
my %cfg;
my $conf = "(\n";
# Ensure to add all used Config keys in B::C here, otherwise they will be silently empty.
# easier hash key/value slices only came with 5.22
for my $s (qw(archname cc ccflags
# easier hash key/value slices only came with 5.22.
# Some keys are only needed with cross
for my $s (qw(archname archlib sitearch sitelib byteorder cc ccflags
d_c99_variadic_macros d_dlopen d_isinf d_isnan d_longdbl dlext
i_dlfcn ivdformat ivsize longsize mad nvgformat ptrsize sizesize
static_ext usecperl usedl useithreads uselongdouble usemultiplicity
usemymalloc uvuformat))
i_dlfcn ivdformat ivsize ld ldflags libs longsize mad nvgformat
osname osvers perlpath privlib ptrsize sizesize static_ext
usecperl usedl useithreads uselongdouble usemultiplicity
usemymalloc uvuformat version))
# maybe: use64bitall use64bitint
{
if ($CORE) {
my $v = $Config::Config{$s};
Expand Down Expand Up @@ -490,7 +493,8 @@ subdirs ::
sub test {
shift->SUPER::test(@_) .
"
PERL_VER = v5.".substr($],3,2) .
# PERL_VAR: major only
PERL_VER = v5.".substr("$]",3,2) .
q(
testmod :: pure_all
PERL_DL_NONLAZY=1 $(FULLPERLRUN) -Iblib/arch -Iblib/lib t/modules.t
Expand All @@ -506,7 +510,7 @@ testbc :: pure_all
testfast :: pure_all
NO_AUTHOR=1 $(FULLPERLRUN) -S prove -b -j10 -f
testfast-log :: pure_all
NO_AUTHOR=1 $(FULLPERLRUN) -S prove -b -j10 -f | tee log.test-).$].q(-`git describe --tags`
NO_AUTHOR=1 $(FULLPERLRUN) -S prove -b -j10 -f | tee log.test-)."$]".q(-`git describe --tags`
testcritical :: pure_all
t/critical.sh
teststatus :: pure_all
Expand Down
14 changes: 14 additions & 0 deletions README
Expand Up @@ -215,6 +215,20 @@ to ensure that linked-against .so files are found at runtime too.

Read about perlcc --staticxs

CROSS COMPILATION

Small devices often do not have enough memory or not even an installed
compiler to compile to a compiled perl executable. In this case you
need the target config.sh on your filesystem, and pass it as
-cross=pathto/config.sh option to the compiler. Several host config
values will then be replaced by its target values, so that it can be
run on the target system with the target configuration. Esp. for the
target specific @INC, which must find run-time modules in its path.
Note that the basic architecture must still match, i.e. the perl
version and the settings for usemultiplicity and useithreads must be
the same as on the host. It's is strongly recommended to also have
the same byteorder, ivsize and nvsize settings on the native host.

DIFFERENCES

The result of running a CC compiled Perl program can sometimes be different
Expand Down
91 changes: 80 additions & 11 deletions lib/B/C.pm
Expand Up @@ -4,6 +4,7 @@
# Copyright (c) 2008, 2009, 2010, 2011 Reini Urban
# Copyright (c) 2010 Nick Koston
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 cPanel Inc
# Copyright (c) 2017, 2018 Reini Urban
#
# You may distribute under the terms of either the GNU General Public
# License or the Artistic License, as specified in the README file.
Expand All @@ -12,12 +13,12 @@
package B::C;
use strict;

our $VERSION = '1.55_10';
our (%debug, $check, %Config);
our $VERSION = '1.55_11';
our (%debug, $check, %Config, %Cross, %OriConfig, $cross);
BEGIN {
require B::C::Config;
*Config = \%B::C::Config::Config;
if (!keys %Config or !exists $Config{usecperl}) {
if (!keys %Config) { #or !exists $Config{usecperl}
warn "Empty \%B::C::Config::Config";
require Config;
Config->import;
Expand Down Expand Up @@ -563,6 +564,35 @@ BEGIN {
eval 'sub DynaLoader::croak {die @_}' unless $CPERL51;
}

sub cross_config { # overrides %B::C::Config::Config
my ($file) = @_;
-e $file or die("-cross \"$file\" not found");
open my $fh, "<", $file or
die("Could not open -cross \"$file\": $!");
while (<$fh>) {
my ($k,$v) = /^(\w+)=(.+)$/; # startperl for $^X, osname for $^O
next unless $k;
$OriConfig{$k} = $Config{$k} if exists $Config{$k};
if (exists $Config{$k}) {
if ($v =~ /^'(.*)'$/) {
$v = $1;
} elsif ($v =~ /^"(.*)"$/) {
$v = $1;
}
$v = '' if $v eq 'undef';
if ($Config{$k} ne $v) {
if ($k =~ /^(version|usemultiplicity|useithreads)$/) {
die "Invalid cross $k $v. Require $Config{$k}";
}
warn "\$Config{$k}: $Config{$k} => $v\n" if $verbose;
$Cross{$k} = $v;
$Config{$k} = $v;
}
}
}
close $fh;
}

# needed for init2 remap and Dynamic annotation
sub dl_module_to_sofile {
my $module = shift
Expand Down Expand Up @@ -6598,7 +6628,7 @@ EOT
if (exists $xsub{$pkg}) {
if ($HAVE_DLFCN_DLOPEN) {
my $ldopt = 'RTLD_NOW|RTLD_NOLOAD';
$ldopt = 'RTLD_NOW' if $^O =~ /bsd/i; # 351 (only on solaris and linux, not any bsd)
$ldopt = 'RTLD_NOW' if $Config{osname} =~ /bsd/i; # 351 (only on solaris and linux, not any bsd)
$init2->add( "", sprintf(" handle = dlopen(%s, %s);", cstring($init2_remap{$pkg}{FILE}), $ldopt));
}
else {
Expand Down Expand Up @@ -6807,9 +6837,10 @@ EOT0
}

sub output_boilerplate {
my $creator = "created at ".scalar localtime()." with B::C $B::C::VERSION ";
my $creator = "created at ".scalar localtime()." with B::C $B::C::VERSION";
$creator .= $B::C::REVISION if $B::C::REVISION;
$creator .= " for $^X";
$creator .= " for $Config{perlpath}";
$creator .= " for cross target $Config{archname}" if $cross;
print "/* $creator */\n";
# Store the sv_list index in sv_debug_file when debugging
print "#define DEBUG_LEAKING_SCALARS 1\n" if $debug{flags} and $DEBUG_LEAKING_SCALARS;
Expand Down Expand Up @@ -7884,8 +7915,9 @@ _EOT15
];
}

print sprintf(qq{ sv_setpv_mg(get_svs("\030", GV_ADD|GV_NOTQUAL), %s); /* \$^X */\n}, cstring($^X));
print <<"EOT";
print sprintf(qq{ sv_setpv_mg(get_svs("\030", GV_ADD|GV_NOTQUAL), %s); /* \$^X */\n},
cstring($Config{perlpath}));
print <<'EOT';
TAINT_NOT;
#if PERL_VERSION < 10 || ((PERL_VERSION == 10) && (PERL_SUBVERSION < 1))
Expand Down Expand Up @@ -8695,8 +8727,30 @@ sub save_context {
inc_cleanup(0);
my $inc_gv = svref_2object( \*main::INC );
$inc_hv = $inc_gv->HV->save('main::INC');
$init->add('/* @INC */');
$inc_av = $inc_gv->AV->save('main::INC');
if ($cross) {
$init->add('/* cross @INC */');
my @crossinc = ($Config{archlib});
if ($Config{archlib} ne $Config{privlib}) {
push @crossinc, $Config{privlib};
}
if (exists $Config{sitearch} and $Config{sitearch}) {
unshift @crossinc, $Config{sitearch};
unshift @crossinc, $Config{sitelib}
if $Config{sitearch} ne $Config{sitelib};
}
if (exists $Config{vendorarch} and $Config{vendorarch}) {
push @crossinc, $Config{vendorarch};
push @crossinc, $Config{vendorlib}
if $Config{vendorarch} ne $Config{vendorlib};
}
if ($] < 5.026 and !$Config{usecperl}) {
push @crossinc, '.';
}
$inc_av = svref_2object(\@crossinc)->save('main::INC');
} else {
$init->add('/* @INC */');
$inc_av = $inc_gv->AV->save('main::INC');
}
}
# ensure all included @ISA's are stored (#308), and also assign c3 (#325)
my @saved_isa;
Expand Down Expand Up @@ -9062,7 +9116,11 @@ sub compile {

OPTION:
while ( $option = shift @options ) {
if ( $option =~ /^-(.)(.*)/ ) {
if ( $option =~ /^-(cross)=(.*)/ ) {
$opt = $1;
$arg = $2;
}
elsif ( $option =~ /^-(.)(.*)/ ) {
$opt = $1;
$arg = $2;
}
Expand Down Expand Up @@ -9183,6 +9241,10 @@ OPTION:
elsif ( $opt eq "l" ) {
$max_string_len = $arg;
}
elsif ( $opt eq "cross" ) {
$cross = $arg;
cross_config($cross); # overrides %B::C::Config::Config
}
}
if (!$B::C::Config::have_independent_comalloc) {
if ($B::C::av_init2) {
Expand Down Expand Up @@ -9614,6 +9676,13 @@ exceeding that limit.
Evaluate ARG at startup
=item B<-cross=pathto/config.sh>
Use a different C<%Config> from another F<config.sh> for
cross-compilation.
C<%INC> will still have the host paths, but C<@INC> and C<$^X>
the target paths. See L<B::C::Config>.
=back
=head1 EXAMPLES
Expand Down

0 comments on commit 43fde10

Please sign in to comment.