| 1 |
# Copyright 2022 Jeffrey Kegler |
| 2 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 3 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 4 |
# General Public License as published by the Free Software Foundation, |
| 5 |
# either version 3 of the License, or (at your option) any later version. |
| 6 |
# |
| 7 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 10 |
# Lesser General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU Lesser |
| 13 |
# General Public License along with Marpa::R2. If not, see |
| 14 |
# http://www.gnu.org/licenses/. |
| 15 |
|
| 16 |
package Marpa::R2::Build_Me; |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
@Marpa::R2::Build_Me::ISA = ('Module::Build'); |
| 23 |
|
| 24 |
use Config; |
| 25 |
use ExtUtils::Manifest; |
| 26 |
use File::Copy; |
| 27 |
use Cwd; |
| 28 |
use IPC::Cmd; |
| 29 |
use Module::Build; |
| 30 |
use Fatal qw(open close chdir chmod utime); |
| 31 |
use English qw( -no_match_vars ); |
| 32 |
use Time::Piece; |
| 33 |
|
| 34 |
use Marpa::R2::Config; |
| 35 |
|
| 36 |
BEGIN { |
| 37 |
if ($Marpa::R2::USE_PERL_AUTOCONF) { |
| 38 |
say "Using Config::AutoConf"; |
| 39 |
for my $package (qw( ExtUtils::MakeMaker Config::AutoConf )) |
| 40 |
{ |
| 41 |
if ( not eval "require $package" ) { |
| 42 |
die "$package is not installed: $EVAL_ERROR\n", |
| 43 |
" Module $package is required for Windows and for USE_PERL_AUTOCONF mode\n"; |
| 44 |
} |
| 45 |
my $version = $Marpa::R2::VERSION_FOR_CONFIG{$package}; |
| 46 |
if ( not $package->VERSION($version) ) { |
| 47 |
die "Version $version of $package is not installed\n", |
| 48 |
" Version $version of $package is required for Windows and for USE_PERL_AUTOCONF mode\n"; |
| 49 |
} |
| 50 |
} ## end for my $package (...) |
| 51 |
} ## end if ($Marpa::R2::USE_PERL_AUTOCONF) |
| 52 |
} ## end BEGIN |
| 53 |
|
| 54 |
my $preamble = <<'END_OF_STRING'; |
| 55 |
# This file is written by Build.PL |
| 56 |
# It is not intended to be modified directly |
| 57 |
|
| 58 |
END_OF_STRING |
| 59 |
|
| 60 |
sub installed_contents { |
| 61 |
my ( $self, $package ) = @_; |
| 62 |
my $marpa_version = $self->dist_version(); |
| 63 |
my $text = $preamble; |
| 64 |
$text .= "package $package;\n"; |
| 65 |
|
| 66 |
##no critic(ValuesAndExpressions::RequireInterpolationOfMetachars) |
| 67 |
$text .= q{use vars qw($VERSION $STRING_VERSION)} . qq{;\n}; |
| 68 |
$text .= q{$VERSION = '} . $marpa_version . qq{';\n}; |
| 69 |
$text .= q{$STRING_VERSION = $VERSION} . qq{;\n}; |
| 70 |
$text .= q{$VERSION = eval $VERSION} . qq{;\n}; |
| 71 |
##use critic |
| 72 |
|
| 73 |
$text .= "1;\n"; |
| 74 |
return $text; |
| 75 |
} ## end sub installed_contents |
| 76 |
|
| 77 |
sub xs_version_contents { |
| 78 |
my ( $self, $package ) = @_; |
| 79 |
my @use_packages = |
| 80 |
qw( Scalar::Util List::Util Carp Data::Dumper ); |
| 81 |
my $text = $preamble; |
| 82 |
$text .= "package $package;\n"; |
| 83 |
|
| 84 |
##no critic(ValuesAndExpressions::RequireInterpolationOfMetachars) |
| 85 |
$text .= q{use vars qw($TIMESTAMP)} . qq{;\n}; |
| 86 |
$text .= q{$TIMESTAMP='} . localtime()->datetime . qq{';\n}; |
| 87 |
##use critic |
| 88 |
|
| 89 |
for my $package (@use_packages) { |
| 90 |
my $version = $Marpa::R2::VERSION_FOR_CONFIG{$package}; |
| 91 |
die "No version defined for $package" if not defined $version; |
| 92 |
$text .= "use $package $version ();\n"; |
| 93 |
} |
| 94 |
$text .= "1;\n"; |
| 95 |
return $text; |
| 96 |
} ## end sub xs_version_contents |
| 97 |
|
| 98 |
sub perl_version_contents { |
| 99 |
my ( $self, $package, ) = @_; |
| 100 |
my @use_packages = qw( Scalar::Util Carp Data::Dumper PPI Marpa::R2 ); |
| 101 |
my $text = $preamble; |
| 102 |
my $marpa_version = $self->dist_version(); |
| 103 |
$text .= "package $package;\n"; |
| 104 |
|
| 105 |
##no critic(ValuesAndExpressions::RequireInterpolationOfMetachars) |
| 106 |
$text .= q{use vars qw($TIMESTAMP)} . qq{;\n}; |
| 107 |
$text .= q{$TIMESTAMP='} . localtime()->datetime . qq{';\n}; |
| 108 |
##use critic |
| 109 |
|
| 110 |
for my $package (@use_packages) { |
| 111 |
my $version = |
| 112 |
$package eq 'Marpa::R2' |
| 113 |
? $marpa_version |
| 114 |
: $Marpa::R2::VERSION_FOR_CONFIG{$package}; |
| 115 |
die "No version defined for $package" if not defined $version; |
| 116 |
$text .= "use $package $version ();\n"; |
| 117 |
} ## end for my $package (@use_packages) |
| 118 |
$text .= "1;\n"; |
| 119 |
return $text; |
| 120 |
} ## end sub perl_version_contents |
| 121 |
|
| 122 |
sub file_write { |
| 123 |
my ( $self, $contents, @name_components ) = @_; |
| 124 |
my $base_dir = $self->base_dir(); |
| 125 |
my $file_name = pop @name_components; |
| 126 |
my $dir_name = File::Spec->catdir( $base_dir, @name_components ); |
| 127 |
-d $dir_name or mkdir $dir_name; |
| 128 |
my $path_name = File::Spec->catfile( $dir_name, $file_name ); |
| 129 |
open my $fh, q{>}, $path_name; |
| 130 |
print {$fh} $contents or die "print failed: $ERRNO"; |
| 131 |
close $fh; |
| 132 |
return 1; |
| 133 |
} ## end sub file_write |
| 134 |
|
| 135 |
sub file_slurp { |
| 136 |
my ( $self, @name_components ) = @_; |
| 137 |
my $path_name = File::Spec->catfile( @name_components ); |
| 138 |
open my $fh, q{<}, $path_name; |
| 139 |
my $contents = do { local $RS = undef; <$fh> }; |
| 140 |
close $fh; |
| 141 |
return $contents; |
| 142 |
} |
| 143 |
|
| 144 |
# This is based on _infer_xs_spec() from Module::Build. It was |
| 145 |
# copied here in order to be customized |
| 146 |
sub marpa_infer_xs_spec { |
| 147 |
my $self = shift; |
| 148 |
my $file = shift; |
| 149 |
|
| 150 |
my $cf = $self->{config}; |
| 151 |
|
| 152 |
my %spec; |
| 153 |
|
| 154 |
my ( $v, $d, $f ) = File::Spec->splitpath($file); |
| 155 |
my @d = File::Spec->splitdir($d); |
| 156 |
( my $file_base = $f ) =~ s/\. [^.]+ \z//xmsi; |
| 157 |
|
| 158 |
$spec{base_name} = $file_base; |
| 159 |
|
| 160 |
$spec{src_dir} = File::Spec->catpath( $v, $d, q{} ); |
| 161 |
|
| 162 |
# the module name |
| 163 |
shift @d while @d && ( $d[0] eq 'lib' || $d[0] eq q{} ); |
| 164 |
pop @d while @d && $d[-1] eq q{}; |
| 165 |
$spec{module_name} = join q{::}, @d, $file_base; |
| 166 |
|
| 167 |
$spec{archdir} = |
| 168 |
File::Spec->catdir( $self->blib, 'arch', 'auto', @d, $file_base ); |
| 169 |
|
| 170 |
|
| 171 |
require DynaLoader; |
| 172 |
my $modfname = defined &DynaLoader::mod2fname |
| 173 |
? DynaLoader::mod2fname([@d, $file_base]) |
| 174 |
: $file_base; |
| 175 |
|
| 176 |
$spec{bs_file} = File::Spec->catfile( $spec{archdir}, "${modfname}.bs" ); |
| 177 |
|
| 178 |
$spec{lib_file} = |
| 179 |
File::Spec->catfile( $spec{archdir}, |
| 180 |
"${modfname}." . $cf->get('dlext') ); |
| 181 |
|
| 182 |
$spec{c_file} = File::Spec->catfile( $spec{src_dir}, "${file_base}.c" ); |
| 183 |
|
| 184 |
$spec{obj_file} = |
| 185 |
File::Spec->catfile( $spec{src_dir}, |
| 186 |
"${file_base}" . $cf->get('obj_ext') ); |
| 187 |
|
| 188 |
return \%spec; |
| 189 |
} ## end sub marpa_infer_xs_spec |
| 190 |
|
| 191 |
sub gcc_is_at_least { |
| 192 |
my ($required) = @_; |
| 193 |
state $gcc_version = $Config{gccversion}; |
| 194 |
return if not $gcc_version; |
| 195 |
my @actual = ($gcc_version =~ m/ \A (\d+) [.] (\d+) [.] (\d+) \z /xms); |
| 196 |
return if @actual != 3; |
| 197 |
my @required = ($required =~ m/ \A (\d+) [.] (\d+) [.] (\d+) \z /xms); |
| 198 |
die if scalar @required != 3; |
| 199 |
my $cmp = $actual[0] <=> $required[0] || |
| 200 |
$actual[1] <=> $required[1] || |
| 201 |
$actual[2] <=> $required[2] ; |
| 202 |
return $cmp >= 0 ? 1 : 0; |
| 203 |
} |
| 204 |
|
| 205 |
# The following initially copied from Module::Build, to be customized for |
| 206 |
# Marpa. |
| 207 |
sub process_xs { |
| 208 |
my ( $self, $xs_file ) = @_; |
| 209 |
|
| 210 |
my $development_mode = $self->args('Dev'); |
| 211 |
|
| 212 |
my $spec = marpa_infer_xs_spec( $self, $xs_file ); |
| 213 |
|
| 214 |
my $xs_dir = File::Spec->catdir(qw(xs)); |
| 215 |
my $gp_xsh = File::Spec->catfile( $xs_dir, 'general_pattern.xsh' ); |
| 216 |
if ($development_mode) { |
| 217 |
my $gp_generate_pl = File::Spec->catfile( $xs_dir, 'gp_generate.pl' ); |
| 218 |
if ( not $self->up_to_date( [$gp_generate_pl], $gp_xsh ) ) { |
| 219 |
if (not IPC::Cmd::run( |
| 220 |
command => [ $EXECUTABLE_NAME, $gp_generate_pl, $gp_xsh ], |
| 221 |
verbose => 1 |
| 222 |
) |
| 223 |
) |
| 224 |
{ |
| 225 |
die "Could not generate $gp_xsh"; |
| 226 |
} ## end if ( not IPC::Cmd::run( command => [ $EXECUTABLE_NAME...])) |
| 227 |
} ## end if ( not $self->up_to_date( [$gp_generate_pl], $gp_xsh...)) |
| 228 |
} ## end if ($development_mode) |
| 229 |
|
| 230 |
my $dest_gp_xsh = |
| 231 |
File::Spec->catfile( $spec->{src_dir}, 'general_pattern.xsh' ); |
| 232 |
$self->copy_if_modified( from => $gp_xsh, to => $dest_gp_xsh, ); |
| 233 |
|
| 234 |
# .xs -> .c |
| 235 |
$self->add_to_cleanup( $spec->{c_file} ); |
| 236 |
|
| 237 |
my @libmarpa_build_dir = File::Spec->splitdir( $self->base_dir ); |
| 238 |
push @libmarpa_build_dir, 'libmarpa_build' ; |
| 239 |
my $libmarpa_build_dir = File::Spec->catdir(@libmarpa_build_dir); |
| 240 |
|
| 241 |
my @xs_dependencies = ( 'typemap', 'Build', $xs_file, $dest_gp_xsh ); |
| 242 |
push @xs_dependencies, |
| 243 |
map { File::Spec->catfile( @libmarpa_build_dir, $_ ) } |
| 244 |
qw(config.h marpa.h marpa_codes.c ); |
| 245 |
|
| 246 |
if ( not $self->up_to_date( \@xs_dependencies, $spec->{c_file} ) ) { |
| 247 |
$self->verbose() and say "compiling $xs_file"; |
| 248 |
$self->compile_xs( $xs_file, outfile => $spec->{c_file} ); |
| 249 |
} |
| 250 |
|
| 251 |
# .c -> .o |
| 252 |
my $v = $self->dist_version; |
| 253 |
$self->verbose() and say "compiling $spec->{c_file}"; |
| 254 |
my @new_ccflags = ( '-I', '"' . $libmarpa_build_dir . '"', '-I', 'xs' ); |
| 255 |
|
| 256 |
if ( $self->config('ccname') eq 'gcc' ) { |
| 257 |
## -W instead of -Wextra is case the GCC is pre 3.0.0 |
| 258 |
## -Winline omitted because too noisy |
| 259 |
push @new_ccflags, qw( -Wall -W |
| 260 |
-Wpointer-arith -Wstrict-prototypes -Wwrite-strings |
| 261 |
-Wmissing-declarations ); |
| 262 |
push @new_ccflags, '-std=c99'; |
| 263 |
} ## end if ( $self->config('cc') eq 'gcc' ) |
| 264 |
if ( defined $self->args('XS-debug') ) { |
| 265 |
say 'XS-debug flag is on'; |
| 266 |
if ( $self->config('ccname') eq 'gcc' ) { |
| 267 |
push @new_ccflags, qw( -Wall -std=c99 -pedantic -Wundef -Wendif-labels ); |
| 268 |
} ## end if ( $self->config('cc') eq 'gcc' ) |
| 269 |
elsif ( $self->config('ccname') eq 'cl' ){ |
| 270 |
## gcc's -Wundef is msvc's C4668 that is on with -Wall |
| 271 |
## gcc's -ansi -pedantic is msvc's /Za, but windows.h can't be compiled with it |
| 272 |
## msvc's equivalent for gcc's -Wendif-labels can't be found :) |
| 273 |
push @new_ccflags, qw( -W4 -Wp64 ); |
| 274 |
} ## end elsif ( $self->config('ccname') eq 'cl' ){ |
| 275 |
} |
| 276 |
my $ccflags = $self->config('ccflags'); |
| 277 |
$self->config( ccflags => ( $ccflags . q{ } . join q{ }, @new_ccflags ) ); |
| 278 |
$self->compile_c( $spec->{c_file}, |
| 279 |
defines => { VERSION => qq{"$v"}, XS_VERSION => qq{"$v"} } ); |
| 280 |
|
| 281 |
# archdir |
| 282 |
# Legacy mkpath(), for compatibility with Perl 5.10.0 |
| 283 |
File::Path::mkpath( $spec->{archdir}, 0, ( oct 777 ) ) |
| 284 |
if not -d $spec->{archdir}; |
| 285 |
|
| 286 |
my @extra_linker_flags = (); |
| 287 |
my $libmarpa_build_directory = |
| 288 |
File::Spec->catdir( $self->base_dir(), 'libmarpa_build' ); |
| 289 |
|
| 290 |
my $switched_to_file = |
| 291 |
File::Spec->catdir( $libmarpa_build_directory, 'SWITCHED_TO' ); |
| 292 |
my $libmarpa_mode_file = |
| 293 |
File::Spec->catdir( $self->base_dir(), 'engine', 'cf', 'LIBMARPA_MODE' ); |
| 294 |
if ( defined $self->args('libmarpa-external') ) { |
| 295 |
my $libmarpa_external_flags = $self->args('libmarpa-external'); |
| 296 |
push @extra_linker_flags, split q{ }, $libmarpa_external_flags; |
| 297 |
$self->log_info("Using external Libmarpa\n"); |
| 298 |
if ( -e $switched_to_file ) { |
| 299 |
$self->log_info("Deleting $switched_to_file\n"); |
| 300 |
File::Path::rmtree( $switched_to_file, 0, 0 ); |
| 301 |
die "Couldn't remove '$switched_to_file': $!\n" |
| 302 |
if -e $switched_to_file; |
| 303 |
} ## end if ( -e $switched_to_file ) |
| 304 |
$switched_to_file = undef; |
| 305 |
|
| 306 |
# Is there a Libmarpa mode file? |
| 307 |
# If not create it. |
| 308 |
if ( not -e $libmarpa_mode_file ) { |
| 309 |
open my $fh, q{>}, $libmarpa_mode_file; |
| 310 |
print {$fh} |
| 311 |
"DO NOT EDIT -- automatically generated by $PROGRAM_NAME\n", |
| 312 |
"Switched to external Libmarpa: " |
| 313 |
. localtime()->datetime . qq{\n} |
| 314 |
or die "print failed: $ERRNO"; |
| 315 |
close $fh; |
| 316 |
} |
| 317 |
} ## end if ( defined $self->args('libmarpa-external') ) |
| 318 |
else { |
| 319 |
# Is there a switched to file? |
| 320 |
# If not create it. |
| 321 |
$self->log_info("Using built-in Libmarpa\n"); |
| 322 |
|
| 323 |
if ( -e $libmarpa_mode_file ) { |
| 324 |
$self->log_info("Deleting $libmarpa_mode_file\n"); |
| 325 |
File::Path::rmtree( $libmarpa_mode_file, 0, 0 ); |
| 326 |
die "Couldn't remove '$libmarpa_mode_file': $!\n" |
| 327 |
if -e $libmarpa_mode_file; |
| 328 |
} ## end if ( -e $libmarpa_mode_file ) |
| 329 |
|
| 330 |
if ( not -e $switched_to_file ) { |
| 331 |
open my $fh, q{>}, $switched_to_file; |
| 332 |
print {$fh} |
| 333 |
"DO NOT EDIT -- automatically generated by $PROGRAM_NAME\n", |
| 334 |
"Last switch to built-in Libmarpa: " |
| 335 |
. localtime()->datetime . qq{\n} |
| 336 |
or die "print failed: $ERRNO"; |
| 337 |
close $fh; |
| 338 |
} ## end if ( not -e $switched_to_file ) |
| 339 |
|
| 340 |
my $libmarpa_archive; |
| 341 |
FIND_LIBRARY: { |
| 342 |
if ($Marpa::R2::USE_PERL_AUTOCONF) { |
| 343 |
my $libmarpa_libs_dir = |
| 344 |
File::Spec->catdir( $libmarpa_build_directory, 'blib', |
| 345 |
'arch', 'auto', 'libmarpa' ); |
| 346 |
$libmarpa_archive = File::Spec->catfile( $libmarpa_libs_dir, |
| 347 |
"libmarpa$Config{lib_ext}" ); |
| 348 |
last FIND_LIBRARY; |
| 349 |
} ## end if ($Marpa::R2::USE_PERL_AUTOCONF) |
| 350 |
my $libmarpa_libs_dir = |
| 351 |
File::Spec->catdir( $libmarpa_build_directory, '.libs' ); |
| 352 |
$libmarpa_archive = |
| 353 |
File::Spec->catfile( $libmarpa_libs_dir, 'libmarpa.a' ); |
| 354 |
} ## end FIND_LIBRARY: |
| 355 |
push @{ $self->{properties}->{objects} }, $libmarpa_archive; |
| 356 |
} ## end else [ if ( defined $self->args('libmarpa-external') ) ] |
| 357 |
|
| 358 |
# .xs -> .bs |
| 359 |
$self->add_to_cleanup( $spec->{bs_file} ); |
| 360 |
unless ( $self->up_to_date( $xs_file, $spec->{bs_file} ) ) { |
| 361 |
require ExtUtils::Mkbootstrap; |
| 362 |
$self->log_info( |
| 363 |
"ExtUtils::Mkbootstrap::Mkbootstrap('$spec->{bs_file}')\n"); |
| 364 |
ExtUtils::Mkbootstrap::Mkbootstrap( $spec->{bs_file} ) |
| 365 |
; # Original had $BSLOADLIBS - what's that? |
| 366 |
{ my $fh = IO::File->new(">> $spec->{bs_file}") } # create |
| 367 |
my $time = time; |
| 368 |
utime $time, $time, $spec->{bs_file}; # touch |
| 369 |
} ## end unless ( $self->up_to_date( $xs_file, $spec->{bs_file} ) ) |
| 370 |
|
| 371 |
# .o -> .(a|bundle) |
| 372 |
return marpa_link_c( $self, $spec, \@extra_linker_flags, $switched_to_file ); |
| 373 |
} ## end sub process_xs |
| 374 |
|
| 375 |
# The following was initially copied from Module::Build, and has |
| 376 |
# been customized for Marpa. |
| 377 |
sub marpa_link_c { |
| 378 |
my ( $self, $spec, $extra_linker_flags, $switched_to_file ) = @_; |
| 379 |
my $p = $self->{properties}; # For convenience |
| 380 |
|
| 381 |
$self->add_to_cleanup( $spec->{lib_file} ); |
| 382 |
|
| 383 |
my $objects = $p->{objects} || []; |
| 384 |
|
| 385 |
my @dependencies = ($spec->{obj_file}); |
| 386 |
push @dependencies, @{$objects}; |
| 387 |
push @dependencies, $switched_to_file if defined $switched_to_file; |
| 388 |
# Always relink if libmarpa-external is specified |
| 389 |
if ( |
| 390 |
not defined $self->args('libmarpa-external') and |
| 391 |
$self->up_to_date( \@dependencies, |
| 392 |
$spec->{lib_file} |
| 393 |
) |
| 394 |
) |
| 395 |
{ |
| 396 |
return $spec->{lib_file}; |
| 397 |
} ## end if ( $self->up_to_date( [ $spec->{obj_file}, @{$objects...}])) |
| 398 |
|
| 399 |
my $module_name = $spec->{module_name} || $self->module_name; |
| 400 |
|
| 401 |
my @extra_linker_flags = @{$p->{extra_linker_flags}}; |
| 402 |
push @extra_linker_flags, @{$extra_linker_flags}; |
| 403 |
$self->cbuilder->link( |
| 404 |
module_name => $module_name, |
| 405 |
objects => [ $spec->{obj_file}, @{$objects} ], |
| 406 |
lib_file => $spec->{lib_file}, |
| 407 |
extra_linker_flags => \@extra_linker_flags |
| 408 |
); |
| 409 |
|
| 410 |
return $spec->{lib_file}; |
| 411 |
} ## end sub marpa_link_c |
| 412 |
|
| 413 |
sub do_libmarpa { |
| 414 |
my $self = shift; |
| 415 |
my $cwd = $self->cwd(); |
| 416 |
my $base_dir = $self->base_dir(); |
| 417 |
|
| 418 |
my $dist_dir = File::Spec->catdir( $base_dir, 'engine/read_only' ); |
| 419 |
my $build_dir = File::Spec->catdir( $base_dir, 'libmarpa_build'); |
| 420 |
|
| 421 |
my $build_stamp_file = File::Spec->catfile( $build_dir, 'stamp-h1' ); |
| 422 |
my $build_makefile = File::Spec->catfile( $build_dir, 'Makefile' ); |
| 423 |
my $dist_stamp_file = File::Spec->catfile( $dist_dir, 'stamp-h1' ); |
| 424 |
|
| 425 |
# If build directory exists and contains a stamp file more recent than the |
| 426 |
# tar file, we are done. |
| 427 |
if (-e $dist_stamp_file && -e $build_stamp_file) { |
| 428 |
return if $self->up_to_date( [$dist_stamp_file], [$build_stamp_file, $build_makefile] ) ; |
| 429 |
} |
| 430 |
|
| 431 |
# Otherwise, rebuild from scratch |
| 432 |
File::Path::rmtree($build_dir); |
| 433 |
|
| 434 |
if ( $self->verbose() ) { |
| 435 |
say join q{ }, "Copying files from $dist_dir to $build_dir" |
| 436 |
or die "print failed: $ERRNO"; |
| 437 |
} |
| 438 |
|
| 439 |
## Make sure build dir structure exists, even if empty |
| 440 |
my $m4_dir = File::Spec->catdir( $build_dir, 'm4' ); |
| 441 |
|
| 442 |
# Legacy mkpath(), for compatibility with Perl 5.10.0 |
| 443 |
File::Path::mkpath($m4_dir); |
| 444 |
|
| 445 |
my @copy_work_list = (); |
| 446 |
{ |
| 447 |
my $from_m4_dir = File::Spec->catdir( $dist_dir, 'm4' ); |
| 448 |
my $to_m4_dir = File::Spec->catdir( $build_dir, 'm4' ); |
| 449 |
chdir $from_m4_dir; |
| 450 |
for my $file (<*>) { |
| 451 |
my $from_file = File::Spec->catfile($from_m4_dir, $file); |
| 452 |
my $to_file = File::Spec->catfile($to_m4_dir, $file); |
| 453 |
push @copy_work_list, [$from_file, $to_file]; |
| 454 |
} |
| 455 |
chdir $cwd; |
| 456 |
} |
| 457 |
{ |
| 458 |
chdir $dist_dir; |
| 459 |
FILE: for my $file (<*>) { |
| 460 |
next FILE if -d $file; |
| 461 |
next FILE if $file eq 'stamp-h1'; |
| 462 |
my $from_file = File::Spec->catfile($dist_dir, $file); |
| 463 |
my $to_file = File::Spec->catfile($build_dir, $file); |
| 464 |
push @copy_work_list, [$from_file, $to_file]; |
| 465 |
} |
| 466 |
chdir $cwd; |
| 467 |
} |
| 468 |
for my $file (@copy_work_list) { |
| 469 |
File::Copy::copy(@{$file}); |
| 470 |
} |
| 471 |
|
| 472 |
chdir $build_dir; |
| 473 |
|
| 474 |
if (! $Marpa::R2::USE_PERL_AUTOCONF) { |
| 475 |
|
| 476 |
if ( $self->verbose() ) { |
| 477 |
print "Configuring libmarpa\n" |
| 478 |
or die "print failed: $ERRNO"; |
| 479 |
} |
| 480 |
|
| 481 |
my $shell = $Config{sh}; |
| 482 |
|
| 483 |
##no critic(ValuesAndExpressions::RequireInterpolationOfMetachars) |
| 484 |
$shell or die q{No Bourne shell available says $Config{sh}}; |
| 485 |
##use critic |
| 486 |
} |
| 487 |
|
| 488 |
local $ENV{$_} = $ENV{$_} for qw(TMPDIR PATH_SEPARATOR CONFIG_SHELL); |
| 489 |
|
| 490 |
if ( $^O eq 'android' ) { |
| 491 |
# TMPDIR must be set due to a bug in /system/bin/sh |
| 492 |
# which breaks shell heredocs; this is for both |
| 493 |
# configure and libtool |
| 494 |
$ENV{TMPDIR} ||= File::Spec->tmpdir(); |
| 495 |
# 'configure' hardcodes /bin/sh in several spots; |
| 496 |
# setting CONFIG_SHELL gets all but one, the test |
| 497 |
# for PATH_SEPARATOR. |
| 498 |
$ENV{CONFIG_SHELL} ||= $Config{sh}; |
| 499 |
$ENV{PATH_SEPARATOR} ||= $Config{path_sep}; |
| 500 |
} |
| 501 |
|
| 502 |
my $original_cflags = $ENV{CFLAGS}; |
| 503 |
local $ENV{CFLAGS}; |
| 504 |
$ENV{CFLAGS} = $original_cflags if defined $original_cflags; |
| 505 |
|
| 506 |
my @configure_command_args = (); |
| 507 |
# We need PIC, but do not want the overhead of building the shared library |
| 508 |
push @configure_command_args, qw(--with-pic --disable-shared --disable-maintainer-mode); |
| 509 |
|
| 510 |
# Dependency tracking is buggy on many platforms, causing installations to |
| 511 |
# fail. We turn it off here. As a side effect, this makes the build |
| 512 |
# faster. It has no effect on builds from scratch -- builds where there |
| 513 |
# are no pre-existing object files. |
| 514 |
# |
| 515 |
# The downside is that, if a user uses the Perl distribution for development |
| 516 |
# (which to our knowledge has never happened). Their dependencies may get |
| 517 |
# out of sync, causing objects that should be re-built not to be re-built. |
| 518 |
# A developer who really wants to use the Perl distribution for development, |
| 519 |
# instead of the git repo, can either avoid changing the dependencies, |
| 520 |
# or remove the all the object files every time they build. |
| 521 |
push @configure_command_args, qw(--disable-dependency-tracking); |
| 522 |
|
| 523 |
my @debug_flags = (); |
| 524 |
if ( defined $self->args('Marpa-debug') ) { |
| 525 |
if ( defined $ENV{LIBMARPA_CFLAGS} ) { |
| 526 |
$ENV{CFLAGS} = $ENV{LIBMARPA_CFLAGS}; |
| 527 |
} |
| 528 |
push @debug_flags, '-DMARPA_DEBUG=1'; |
| 529 |
push @debug_flags, '-fno-inline', '-Wno-inline' if ($self->config('cc') eq 'gcc'); |
| 530 |
push @configure_command_args, |
| 531 |
'MARPA_DEBUG_FLAG=' . ( join q{ }, @debug_flags ); |
| 532 |
} ## end if ( defined $self->args('Marpa-debug') ) |
| 533 |
|
| 534 |
# As of this writing, only used by Config::AutoConf logic, |
| 535 |
# but that may change. |
| 536 |
my $libmarpa_version = $self->file_slurp('LIB_VERSION'); |
| 537 |
chomp $libmarpa_version; |
| 538 |
my @libmarpa_version = split /[.]/xms, $libmarpa_version; |
| 539 |
|
| 540 |
if ($Marpa::R2::USE_PERL_AUTOCONF) { |
| 541 |
|
| 542 |
# |
| 543 |
## C.f. http://fr.slideshare.net/hashashin/building-c-and-c-libraries-with-perl |
| 544 |
# |
| 545 |
my @c = qw/marpa_ami.c marpa_avl.c marpa.c |
| 546 |
marpa_codes.c marpa_obs.c marpa_tavl.c/; |
| 547 |
if (! -r 'config.h') { |
| 548 |
# |
| 549 |
## Because Config::AutoConf can only generate #define/#undef |
| 550 |
## stubs, we write our config.h with these stubs, our config.h |
| 551 |
## will then include a generated config_from_autoconf.h |
| 552 |
# |
| 553 |
if ( $self->verbose() ) { |
| 554 |
say join q{ }, "Doing config.h" |
| 555 |
or die "print failed: $ERRNO"; |
| 556 |
} |
| 557 |
open my $config_fh, '>>', 'config.h' || die "Cannot open config.h, $!\n"; |
| 558 |
my $ac = Config::AutoConf->new(); |
| 559 |
my $inline_ok = 0; |
| 560 |
{ |
| 561 |
$ac->msg_checking('inline'); |
| 562 |
my $program = $ac->lang_build_program("static inline int testinline() {return 1;}\n", 'testinline'); |
| 563 |
$inline_ok = $ac->compile_if_else($program); |
| 564 |
$ac->msg_result($inline_ok ? 'yes' : 'no'); |
| 565 |
} |
| 566 |
my $inline = ''; |
| 567 |
if (! $inline_ok) { |
| 568 |
foreach (qw/__inline__ __inline/) { |
| 569 |
my $candidate = $_; |
| 570 |
$ac->msg_checking($candidate); |
| 571 |
my $program = $ac->lang_build_program("static $candidate int testinline() {return 1;}\n", 'testinline'); |
| 572 |
my $rc = $ac->compile_if_else($program); |
| 573 |
$ac->msg_result($rc ? 'yes' : 'no'); |
| 574 |
if ($rc) { |
| 575 |
$inline = $candidate; |
| 576 |
last; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
if ($inline) { |
| 581 |
print {$config_fh} <<INLINEHOOK; |
| 582 |
#ifndef __CONFIG_WITH_STUBS_H |
| 583 |
#ifndef __cplusplus |
| 584 |
#define inline $inline |
| 585 |
#endif |
| 586 |
#include "config_from_autoconf.h" |
| 587 |
#endif /* __CONFIG_WITH_STUBS_H */ |
| 588 |
INLINEHOOK |
| 589 |
} else { |
| 590 |
print {$config_fh} <<INLINEHOOK; |
| 591 |
#ifndef __CONFIG_WITH_STUBS_H |
| 592 |
#ifndef __cplusplus |
| 593 |
/* #undef inline */ |
| 594 |
#endif |
| 595 |
#include "config_from_autoconf.h" |
| 596 |
#endif /* __CONFIG_WITH_STUBS_H */ |
| 597 |
INLINEHOOK |
| 598 |
} |
| 599 |
|
| 600 |
# Config::Autoconf mistakes 0 for undef, so these must be done explicitly |
| 601 |
say {$config_fh} join q{ }, '#define MARPA_LIB_MAJOR_VERSION', $libmarpa_version[0]; |
| 602 |
say {$config_fh} join q{ }, '#define MARPA_LIB_MINOR_VERSION', $libmarpa_version[1]; |
| 603 |
say {$config_fh} join q{ }, '#define MARPA_LIB_MICRO_VERSION', $libmarpa_version[2]; |
| 604 |
|
| 605 |
close($config_fh); |
| 606 |
$ac = Config::AutoConf->new(); |
| 607 |
my $sizeof_int = $ac->check_sizeof_type('int'); |
| 608 |
if ($sizeof_int < 4) { |
| 609 |
die "Marpa requires that int be at least 32 bits -- on this system that is not the case"; |
| 610 |
} |
| 611 |
|
| 612 |
$ac->check_stdc_headers; |
| 613 |
$ac->check_default_headers(); |
| 614 |
|
| 615 |
# This check was to prepare for Perl's 64-bit pseudo-UTF8 characters inside the SLIF. |
| 616 |
# It needs to be rethought. |
| 617 |
# |
| 618 |
# if (!$ac->check_type('unsigned long long int')) { |
| 619 |
# die "Marpa requires that unsigned long long int is supported by your compiler"; |
| 620 |
# } |
| 621 |
|
| 622 |
$ac->define_var('PACKAGE', "\"libmarpa\""); |
| 623 |
$ac->define_var('PACKAGE_BUGREPORT', "\"http://rt.cpan.org/NoAuth/Bugs.html?Dist=Marpa\""); |
| 624 |
$ac->define_var('PACKAGE_NAME', "\"libmarpa\""); |
| 625 |
$ac->define_var('PACKAGE_STRING', "\"libmarpa $libmarpa_version[0].$libmarpa_version[1].$libmarpa_version[2]\""); |
| 626 |
$ac->define_var('PACKAGE_TARNAME', "\"libmarpa\""); |
| 627 |
$ac->define_var('PACKAGE_URL', "\"\""); |
| 628 |
$ac->define_var('PACKAGE_VERSION', "\"$libmarpa_version\""); |
| 629 |
$ac->define_var('PACKAGE_STRING', "\"$libmarpa_version\""); |
| 630 |
$ac->write_config_h('config_from_autoconf.h'); |
| 631 |
} |
| 632 |
|
| 633 |
my @o = map {s/\.c$/$Config{obj_ext}/; $_} @c; |
| 634 |
if (! -r 'Makefile.PL') { |
| 635 |
open my $makefile_pl_fh, '>', 'Makefile.PL'; |
| 636 |
my $CCFLAGS = @debug_flags ? "$Config{ccflags} @debug_flags" : ''; |
| 637 |
my $linktype = 'static'; |
| 638 |
my $blib = 'blib'; |
| 639 |
my $name = 'libmarpa'; |
| 640 |
print {$makefile_pl_fh} " |
| 641 |
use ExtUtils::MakeMaker; |
| 642 |
WriteMakefile(VERSION => \"$libmarpa_version\", |
| 643 |
XS_VERSION => \"$libmarpa_version\", |
| 644 |
NAME => \"$name\", |
| 645 |
OBJECT => '@o', |
| 646 |
CCFLAGS => '$CCFLAGS', |
| 647 |
INST_LIB => \"./$blib/lib\", |
| 648 |
INST_ARCHLIB => \"./$blib/arch\", |
| 649 |
INST_SCRIPT => \"./$blib/script\", |
| 650 |
INST_BIN => \"./$blib/bin\", |
| 651 |
LINKTYPE => $linktype); |
| 652 |
"; |
| 653 |
close $makefile_pl_fh; |
| 654 |
die 'Making Makefile: perl Failure' |
| 655 |
if not IPC::Cmd::run( command => [$^X, 'Makefile.PL'], verbose => 1 ); |
| 656 |
} |
| 657 |
} else { |
| 658 |
my $shell = $Config{sh}; |
| 659 |
my $configure_script = 'configure'; |
| 660 |
if ( $self->verbose() ) { |
| 661 |
say join q{ }, "Running command:", $shell, $configure_script, |
| 662 |
@configure_command_args |
| 663 |
or die "print failed: $ERRNO"; |
| 664 |
} |
| 665 |
if (not IPC::Cmd::run( |
| 666 |
command => [ $shell, $configure_script, @configure_command_args ], |
| 667 |
verbose => 1 |
| 668 |
) |
| 669 |
) |
| 670 |
{ |
| 671 |
say {*STDERR} "Failed: $configure_script" |
| 672 |
or die "say failed: $ERRNO"; |
| 673 |
say {*STDERR} "Current directory: $build_dir" |
| 674 |
or die "say failed: $ERRNO"; |
| 675 |
die 'Cannot run libmarpa configure'; |
| 676 |
} ## end if ( not IPC::Cmd::run( command => [ $shell, $configure_script...])) |
| 677 |
|
| 678 |
} |
| 679 |
if ( $self->verbose() ) { |
| 680 |
print "Making libmarpa: Start\n" or die "Cannot print: $ERRNO"; |
| 681 |
} |
| 682 |
die 'Making libmarpa: make Failure' |
| 683 |
if not IPC::Cmd::run( command => [$Config{make}], verbose => 1 ); |
| 684 |
|
| 685 |
## stamp-h1 is a by-product of the GNU autotools, but for Config::AutoConf, |
| 686 |
## I need to dummy one up, so do it for both |
| 687 |
{ |
| 688 |
open my $time_stamp_fh, q{>}, 'stamp-h1'; |
| 689 |
say {$time_stamp_fh} scalar localtime(); |
| 690 |
close $time_stamp_fh; |
| 691 |
} |
| 692 |
|
| 693 |
chdir $cwd; |
| 694 |
|
| 695 |
return 1; |
| 696 |
|
| 697 |
} ## end sub do_libmarpa |
| 698 |
|
| 699 |
sub ACTION_manifest { |
| 700 |
die qq{Automatic generation of the MANIFEST file is disabled\n} |
| 701 |
. qq{The Marpa MANIFEST file is handwritten\n}; |
| 702 |
} |
| 703 |
|
| 704 |
sub make_writeable { |
| 705 |
my $file = shift; |
| 706 |
die qq{"$file" in "}, getcwd(), qq{" does not exist} if ! -e $file; |
| 707 |
my $current_mode = (stat $file)[2]; |
| 708 |
die qq{mode not defined for $file} if not defined $current_mode; |
| 709 |
chmod $current_mode | (oct 200), $file; |
| 710 |
} |
| 711 |
|
| 712 |
sub ACTION_metacheck { |
| 713 |
my $self = shift; |
| 714 |
|
| 715 |
# does not check CPAN::Meta |
| 716 |
# version -- assumes updated with Module::Build |
| 717 |
# this should only be run when making distributions |
| 718 |
# not on install, so we don't have to be too paranoid |
| 719 |
require CPAN::Meta; |
| 720 |
|
| 721 |
my $marpa_version = $self->dist_version(); |
| 722 |
my $meta = CPAN::Meta->load_file($self->metafile()); |
| 723 |
my $provides = $meta->{provides}; |
| 724 |
my @metacheck_problems = (); |
| 725 |
PROVIDED: for my $provided_name (keys %{$provides}) { |
| 726 |
my $provided_version = $provides->{$provided_name}->{version}; |
| 727 |
if (not defined $provided_version) { |
| 728 |
push @metacheck_problems, "No version for $provided_name\n"; |
| 729 |
next PROVIDED; |
| 730 |
} |
| 731 |
if ($provided_version ne $marpa_version) { |
| 732 |
push @metacheck_problems, |
| 733 |
"Version of $provided_name is $provided_version, but Marpa version is $marpa_version\n"; |
| 734 |
} |
| 735 |
} |
| 736 |
if (@metacheck_problems) { |
| 737 |
print {*STDERR} join q{}, @metacheck_problems |
| 738 |
or die "Cannot print: $ERRNO"; |
| 739 |
die 'Fatal error due to META file issues'; |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
sub ACTION_distcheck { |
| 744 |
my $self = shift; |
| 745 |
$self->ACTION_metacheck(); |
| 746 |
return $self->SUPER::ACTION_distcheck; |
| 747 |
} ## end sub ACTION_distcheck |
| 748 |
|
| 749 |
sub ACTION_distmeta { |
| 750 |
my $self = shift; |
| 751 |
my $return_value = $self->SUPER::ACTION_distmeta; |
| 752 |
|
| 753 |
# does not check CPAN::Meta |
| 754 |
# version -- assumes updated with Module::Build |
| 755 |
# this should only be run when making distributions |
| 756 |
# not on install, so we don't have to be too paranoid |
| 757 |
require CPAN::Meta; |
| 758 |
|
| 759 |
my $meta = CPAN::Meta->load_file( $self->metafile() ); |
| 760 |
my @delete = ('DynaLoader'); |
| 761 |
for my $provided ( keys %{ $meta->{provides} } ) { |
| 762 |
push @delete, $provided if $provided =~ m/\AMarpa::R2::Inner::/xms; |
| 763 |
push @delete, $provided if $provided =~ m/\AMarpa::R2::Internal::/xms; |
| 764 |
push @delete, $provided if $provided =~ m/\AMarpa::R2::HTML::Internal::/xms; |
| 765 |
push @delete, $provided if $provided =~ m/::Internal\z/xms; |
| 766 |
} |
| 767 |
if (@delete) { |
| 768 |
for my $deletion (@delete) { |
| 769 |
delete $meta->{provides}->{$deletion}; |
| 770 |
} |
| 771 |
if (defined $meta->{dynamic_config}) { |
| 772 |
# Make sure this stays numeric |
| 773 |
$meta->{dynamic_config} = $meta->{dynamic_config}+0; |
| 774 |
} |
| 775 |
$meta->save( 'META.yml', { version => '1.4' } ); |
| 776 |
$meta->save('META.json'); |
| 777 |
$self->log_info("Revised META.yml and META.json\n"); |
| 778 |
} ## end if (@delete) |
| 779 |
|
| 780 |
return $return_value; |
| 781 |
} ## end sub ACTION_distmeta |
| 782 |
|
| 783 |
sub ACTION_dist { |
| 784 |
my $self = shift; |
| 785 |
open my $fh, q{<}, 'Changes'; |
| 786 |
my $changes = do { |
| 787 |
local $RS = undef; |
| 788 |
<$fh>; |
| 789 |
}; |
| 790 |
close $fh; |
| 791 |
my $marpa_version = $self->dist_version(); |
| 792 |
die qq{"$marpa_version" not in Changes file} |
| 793 |
if 0 > index $changes, $marpa_version; |
| 794 |
return $self->SUPER::ACTION_dist; |
| 795 |
} ## end sub ACTION_dist |
| 796 |
|
| 797 |
sub write_installed_pm { |
| 798 |
my ( $self, @components ) = @_; |
| 799 |
my $filename = 'Installed'; |
| 800 |
my @package_components = @components[ 1 .. $#components ]; |
| 801 |
my $contents = installed_contents( $self, join q{::}, @package_components, |
| 802 |
$filename ); |
| 803 |
$filename .= q{.pm}; |
| 804 |
return $self->file_write( $contents, @components, $filename ); |
| 805 |
} ## end sub write_installed_pm |
| 806 |
|
| 807 |
sub ACTION_code { |
| 808 |
my $self = shift; |
| 809 |
my @r2_perl_components = qw(pperl Marpa R2 Perl); |
| 810 |
my @r2_components = qw(lib Marpa R2); |
| 811 |
my $config_pm_filename = File::Spec->catfile(qw(inc Marpa R2 Config.pm )); |
| 812 |
my $build_filename = 'Build'; |
| 813 |
my @derived_files = ( |
| 814 |
File::Spec->catfile( @r2_components, 'Version.pm' ), |
| 815 |
File::Spec->catfile( @r2_components, 'Installed.pm' ), |
| 816 |
File::Spec->catfile( @r2_perl_components, 'Version.pm' ), |
| 817 |
File::Spec->catfile( @r2_perl_components, 'Installed.pm' ), |
| 818 |
); |
| 819 |
if (not $self->up_to_date( |
| 820 |
[ $config_pm_filename, $build_filename ], |
| 821 |
\@derived_files |
| 822 |
) |
| 823 |
) |
| 824 |
{ |
| 825 |
say {*STDERR} 'Writing version files' or die "say failed: $ERRNO"; |
| 826 |
write_installed_pm( $self, qw(lib Marpa R2 ) ); |
| 827 |
write_installed_pm( $self, qw(pperl Marpa R2 Perl ) ); |
| 828 |
my $perl_version_pm = |
| 829 |
perl_version_contents( $self, 'Marpa::R2::Perl' ); |
| 830 |
my $version_pm = xs_version_contents( $self, 'Marpa::R2' ); |
| 831 |
$self->file_write( $version_pm, qw(lib Marpa R2 Version.pm) ); |
| 832 |
$self->file_write( $perl_version_pm, |
| 833 |
qw(pperl Marpa R2 Perl Version.pm) ); |
| 834 |
} ## end if ( not $self->up_to_date( [ $config_pm_filename, ...])) |
| 835 |
|
| 836 |
# If it's a shared library, we don't build it here. |
| 837 |
if ( not defined $self->args('libmarpa-external') ) { |
| 838 |
|
| 839 |
# If the mode file exists, any switch to the internal Libmarpa |
| 840 |
# must be made explicitly |
| 841 |
if ( not defined $self->args('libmarpa-internal') ) { |
| 842 |
my $libmarpa_mode_file = |
| 843 |
File::Spec->catdir( $self->base_dir(), 'engine', 'cf', |
| 844 |
'LIBMARPA_MODE' ); |
| 845 |
die |
| 846 |
"Libmarpa is external, but you did not explicity specify that -- you need to\n", |
| 847 |
' If you want to go back to the built-in Libmarpa, ', |
| 848 |
qq{use Build's "--libmarpa-internal=on" flag\n} |
| 849 |
if -e $libmarpa_mode_file; |
| 850 |
} ## end if ( not defined $self->args('libmarpa-internal') ) |
| 851 |
$self->do_libmarpa(); |
| 852 |
} ## end if ( not defined $self->args('libmarpa-external') ) |
| 853 |
|
| 854 |
return $self->SUPER::ACTION_code; |
| 855 |
} ## end sub ACTION_code |
| 856 |
|
| 857 |
sub ACTION_clean { |
| 858 |
my $self = shift; |
| 859 |
|
| 860 |
my $curdir = File::Spec->rel2abs( File::Spec->curdir() ); |
| 861 |
if ( $self->verbose ) { |
| 862 |
print "Cleaning libmarpa\n" or die "print failed: $ERRNO"; |
| 863 |
} |
| 864 |
|
| 865 |
return $self->SUPER::ACTION_clean; |
| 866 |
} ## end sub ACTION_clean |
| 867 |
|
| 868 |
sub ACTION_test { |
| 869 |
my $self = shift; |
| 870 |
local $ENV{PERL_DL_NONLAZY} = 1; |
| 871 |
return $self->SUPER::ACTION_test; |
| 872 |
} |
| 873 |
|
| 874 |
sub ACTION_install { |
| 875 |
my $self = shift; |
| 876 |
die qq{"libmarpa-shared" not supported for "install" target\n}, |
| 877 |
qq{ Marpa::R2 supports testing of shared libraries, but is not an installer for them\n} |
| 878 |
if defined $self->args('libmarpa-shared'); |
| 879 |
return $self->SUPER::ACTION_install; |
| 880 |
} |
| 881 |
|
| 882 |
1; |
| 883 |
|
| 884 |
# vim: expandtab shiftwidth=4: |