Debian Codegraph @

inc/Marpa/R2/Build_Me.pm

raw ◀ ▶
2 Debian patches touch this file
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='} . (gmtime($ENV{SOURCE_DATE_EPOCH} || time()))->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='} . (gmtime($ENV{SOURCE_DATE_EPOCH} || time()))->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->compile_xs( $xs_file, outfile => $spec->{c_file} );
248 }
249
250 # .c -> .o
251 my $v = $self->dist_version;
252 $self->verbose() and say "compiling $spec->{c_file}";
253 my @new_ccflags = ( '-I', '"' . $libmarpa_build_dir . '"', '-I', 'xs' );
254
255 if ( $self->config('ccname') eq 'gcc' ) {
256 ## -W instead of -Wextra is case the GCC is pre 3.0.0
257 ## -Winline omitted because too noisy
258 push @new_ccflags, qw( -Wall -W
259 -Wpointer-arith -Wstrict-prototypes -Wwrite-strings
260 -Wmissing-declarations );
261 push @new_ccflags, '-std=c99';
262 } ## end if ( $self->config('cc') eq 'gcc' )
263 if ( defined $self->args('XS-debug') ) {
264 say 'XS-debug flag is on';
265 if ( $self->config('ccname') eq 'gcc' ) {
266 push @new_ccflags, qw( -Wall -std=c99 -pedantic -Wundef -Wendif-labels );
267 } ## end if ( $self->config('cc') eq 'gcc' )
268 elsif ( $self->config('ccname') eq 'cl' ){
269 ## gcc's -Wundef is msvc's C4668 that is on with -Wall
270 ## gcc's -ansi -pedantic is msvc's /Za, but windows.h can't be compiled with it
271 ## msvc's equivalent for gcc's -Wendif-labels can't be found :)
272 push @new_ccflags, qw( -W4 -Wp64 );
273 } ## end elsif ( $self->config('ccname') eq 'cl' ){
274 }
275 my $ccflags = $self->config('ccflags');
276 $self->config( ccflags => ( $ccflags . q{ } . join q{ }, @new_ccflags ) );
277 $self->compile_c( $spec->{c_file},
278 defines => { VERSION => qq{"$v"}, XS_VERSION => qq{"$v"} } );
279
280 # archdir
281 # Legacy mkpath(), for compatibility with Perl 5.10.0
282 File::Path::mkpath( $spec->{archdir}, 0, ( oct 777 ) )
283 if not -d $spec->{archdir};
284
285 my @extra_linker_flags = ();
286 my $libmarpa_build_directory =
287 File::Spec->catdir( $self->base_dir(), 'libmarpa_build' );
288
289 my $switched_to_file =
290 File::Spec->catdir( $libmarpa_build_directory, 'SWITCHED_TO' );
291 my $libmarpa_mode_file =
292 File::Spec->catdir( $self->base_dir(), 'engine', 'cf', 'LIBMARPA_MODE' );
293 if ( defined $self->args('libmarpa-external') ) {
294 my $libmarpa_external_flags = $self->args('libmarpa-external');
295 push @extra_linker_flags, split q{ }, $libmarpa_external_flags;
296 $self->log_info("Using external Libmarpa\n");
297 if ( -e $switched_to_file ) {
298 $self->log_info("Deleting $switched_to_file\n");
299 File::Path::rmtree( $switched_to_file, 0, 0 );
300 die "Couldn't remove '$switched_to_file': $!\n"
301 if -e $switched_to_file;
302 } ## end if ( -e $switched_to_file )
303 $switched_to_file = undef;
304
305 # Is there a Libmarpa mode file?
306 # If not create it.
307 if ( not -e $libmarpa_mode_file ) {
308 open my $fh, q{>}, $libmarpa_mode_file;
309 print {$fh}
310 "DO NOT EDIT -- automatically generated by $PROGRAM_NAME\n",
311 "Switched to external Libmarpa: "
312 . localtime()->datetime . qq{\n}
313 or die "print failed: $ERRNO";
314 close $fh;
315 }
316 } ## end if ( defined $self->args('libmarpa-external') )
317 else {
318 # Is there a switched to file?
319 # If not create it.
320 $self->log_info("Using built-in Libmarpa\n");
321
322 if ( -e $libmarpa_mode_file ) {
323 $self->log_info("Deleting $libmarpa_mode_file\n");
324 File::Path::rmtree( $libmarpa_mode_file, 0, 0 );
325 die "Couldn't remove '$libmarpa_mode_file': $!\n"
326 if -e $libmarpa_mode_file;
327 } ## end if ( -e $libmarpa_mode_file )
328
329 if ( not -e $switched_to_file ) {
330 open my $fh, q{>}, $switched_to_file;
331 print {$fh}
332 "DO NOT EDIT -- automatically generated by $PROGRAM_NAME\n",
333 "Last switch to built-in Libmarpa: "
334 . localtime()->datetime . qq{\n}
335 or die "print failed: $ERRNO";
336 close $fh;
337 } ## end if ( not -e $switched_to_file )
338
339 my $libmarpa_archive;
340 FIND_LIBRARY: {
341 if ($Marpa::R2::USE_PERL_AUTOCONF) {
342 my $libmarpa_libs_dir =
343 File::Spec->catdir( $libmarpa_build_directory, 'blib',
344 'arch', 'auto', 'libmarpa' );
345 $libmarpa_archive = File::Spec->catfile( $libmarpa_libs_dir,
346 "libmarpa$Config{lib_ext}" );
347 last FIND_LIBRARY;
348 } ## end if ($Marpa::R2::USE_PERL_AUTOCONF)
349 my $libmarpa_libs_dir =
350 File::Spec->catdir( $libmarpa_build_directory, '.libs' );
351 $libmarpa_archive =
352 File::Spec->catfile( $libmarpa_libs_dir, 'libmarpa.a' );
353 } ## end FIND_LIBRARY:
354 push @{ $self->{properties}->{objects} }, $libmarpa_archive;
355 } ## end else [ if ( defined $self->args('libmarpa-external') ) ]
356
357 # .xs -> .bs
358 $self->add_to_cleanup( $spec->{bs_file} );
359 unless ( $self->up_to_date( $xs_file, $spec->{bs_file} ) ) {
360 require ExtUtils::Mkbootstrap;
361 $self->log_info(
362 "ExtUtils::Mkbootstrap::Mkbootstrap('$spec->{bs_file}')\n");
363 ExtUtils::Mkbootstrap::Mkbootstrap( $spec->{bs_file} )
364 ; # Original had $BSLOADLIBS - what's that?
365 { my $fh = IO::File->new(">> $spec->{bs_file}") } # create
366 my $time = time;
367 utime $time, $time, $spec->{bs_file}; # touch
368 } ## end unless ( $self->up_to_date( $xs_file, $spec->{bs_file} ) )
369
370 # .o -> .(a|bundle)
371 return marpa_link_c( $self, $spec, \@extra_linker_flags, $switched_to_file );
372 } ## end sub process_xs
373
374 # The following was initially copied from Module::Build, and has
375 # been customized for Marpa.
376 sub marpa_link_c {
377 my ( $self, $spec, $extra_linker_flags, $switched_to_file ) = @_;
378 my $p = $self->{properties}; # For convenience
379
380 $self->add_to_cleanup( $spec->{lib_file} );
381
382 my $objects = $p->{objects} || [];
383
384 my @dependencies = ($spec->{obj_file});
385 push @dependencies, @{$objects};
386 push @dependencies, $switched_to_file if defined $switched_to_file;
387 # Always relink if libmarpa-external is specified
388 if (
389 not defined $self->args('libmarpa-external') and
390 $self->up_to_date( \@dependencies,
391 $spec->{lib_file}
392 )
393 )
394 {
395 return $spec->{lib_file};
396 } ## end if ( $self->up_to_date( [ $spec->{obj_file}, @{$objects...}]))
397
398 my $module_name = $spec->{module_name} || $self->module_name;
399
400 my @extra_linker_flags = @{$p->{extra_linker_flags}};
401 push @extra_linker_flags, @{$extra_linker_flags};
402 $self->cbuilder->link(
403 module_name => $module_name,
404 objects => [ $spec->{obj_file}, @{$objects} ],
405 lib_file => $spec->{lib_file},
406 extra_linker_flags => \@extra_linker_flags
407 );
408
409 return $spec->{lib_file};
410 } ## end sub marpa_link_c
411
412 sub do_libmarpa {
413 my $self = shift;
414 my $cwd = $self->cwd();
415 my $base_dir = $self->base_dir();
416
417 my $dist_dir = File::Spec->catdir( $base_dir, 'engine/read_only' );
418 my $build_dir = File::Spec->catdir( $base_dir, 'libmarpa_build');
419
420 my $build_stamp_file = File::Spec->catfile( $build_dir, 'stamp-h1' );
421 my $build_makefile = File::Spec->catfile( $build_dir, 'Makefile' );
422 my $dist_stamp_file = File::Spec->catfile( $dist_dir, 'stamp-h1' );
423
424 # If build directory exists and contains a stamp file more recent than the
425 # tar file, we are done.
426 if (-e $dist_stamp_file && -e $build_stamp_file) {
427 return if $self->up_to_date( [$dist_stamp_file], [$build_stamp_file, $build_makefile] ) ;
428 }
429
430 # Otherwise, rebuild from scratch
431 File::Path::rmtree($build_dir);
432
433 if ( $self->verbose() ) {
434 say join q{ }, "Copying files from $dist_dir to $build_dir"
435 or die "print failed: $ERRNO";
436 }
437
438 ## Make sure build dir structure exists, even if empty
439 my $m4_dir = File::Spec->catdir( $build_dir, 'm4' );
440
441 # Legacy mkpath(), for compatibility with Perl 5.10.0
442 File::Path::mkpath($m4_dir);
443
444 my @copy_work_list = ();
445 {
446 my $from_m4_dir = File::Spec->catdir( $dist_dir, 'm4' );
447 my $to_m4_dir = File::Spec->catdir( $build_dir, 'm4' );
448 chdir $from_m4_dir;
449 for my $file (<*>) {
450 my $from_file = File::Spec->catfile($from_m4_dir, $file);
451 my $to_file = File::Spec->catfile($to_m4_dir, $file);
452 push @copy_work_list, [$from_file, $to_file];
453 }
454 chdir $cwd;
455 }
456 {
457 chdir $dist_dir;
458 FILE: for my $file (<*>) {
459 next FILE if -d $file;
460 next FILE if $file eq 'stamp-h1';
461 my $from_file = File::Spec->catfile($dist_dir, $file);
462 my $to_file = File::Spec->catfile($build_dir, $file);
463 push @copy_work_list, [$from_file, $to_file];
464 }
465 chdir $cwd;
466 }
467 for my $file (@copy_work_list) {
468 File::Copy::copy(@{$file});
469 }
470
471 chdir $build_dir;
472
473 if (! $Marpa::R2::USE_PERL_AUTOCONF) {
474
475 if ( $self->verbose() ) {
476 print "Configuring libmarpa\n"
477 or die "print failed: $ERRNO";
478 }
479
480 my $shell = $Config{sh};
481
482 ##no critic(ValuesAndExpressions::RequireInterpolationOfMetachars)
483 $shell or die q{No Bourne shell available says $Config{sh}};
484 ##use critic
485 }
486
487 local $ENV{$_} = $ENV{$_} for qw(TMPDIR PATH_SEPARATOR CONFIG_SHELL);
488
489 if ( $^O eq 'android' ) {
490 # TMPDIR must be set due to a bug in /system/bin/sh
491 # which breaks shell heredocs; this is for both
492 # configure and libtool
493 $ENV{TMPDIR} ||= File::Spec->tmpdir();
494 # 'configure' hardcodes /bin/sh in several spots;
495 # setting CONFIG_SHELL gets all but one, the test
496 # for PATH_SEPARATOR.
497 $ENV{CONFIG_SHELL} ||= $Config{sh};
498 $ENV{PATH_SEPARATOR} ||= $Config{path_sep};
499 }
500
501 my $original_cflags = $ENV{CFLAGS};
502 local $ENV{CFLAGS};
503 $ENV{CFLAGS} = $original_cflags if defined $original_cflags;
504
505 my @configure_command_args = ();
506 # We need PIC, but do not want the overhead of building the shared library
507 push @configure_command_args, qw(--with-pic --disable-shared --disable-maintainer-mode);
508
509 # Dependency tracking is buggy on many platforms, causing installations to
510 # fail. We turn it off here. As a side effect, this makes the build
511 # faster. It has no effect on builds from scratch -- builds where there
512 # are no pre-existing object files.
513 #
514 # The downside is that, if a user uses the Perl distribution for development
515 # (which to our knowledge has never happened). Their dependencies may get
516 # out of sync, causing objects that should be re-built not to be re-built.
517 # A developer who really wants to use the Perl distribution for development,
518 # instead of the git repo, can either avoid changing the dependencies,
519 # or remove the all the object files every time they build.
520 push @configure_command_args, qw(--disable-dependency-tracking);
521
522 my @debug_flags = ();
523 if ( defined $self->args('Marpa-debug') ) {
524 if ( defined $ENV{LIBMARPA_CFLAGS} ) {
525 $ENV{CFLAGS} = $ENV{LIBMARPA_CFLAGS};
526 }
527 push @debug_flags, '-DMARPA_DEBUG=1';
528 push @debug_flags, '-fno-inline', '-Wno-inline' if ($self->config('cc') eq 'gcc');
529 push @configure_command_args,
530 'MARPA_DEBUG_FLAG=' . ( join q{ }, @debug_flags );
531 } ## end if ( defined $self->args('Marpa-debug') )
532
533 # As of this writing, only used by Config::AutoConf logic,
534 # but that may change.
535 my $libmarpa_version = $self->file_slurp('LIB_VERSION');
536 chomp $libmarpa_version;
537 my @libmarpa_version = split /[.]/xms, $libmarpa_version;
538
539 if ($Marpa::R2::USE_PERL_AUTOCONF) {
540
541 #
542 ## C.f. http://fr.slideshare.net/hashashin/building-c-and-c-libraries-with-perl
543 #
544 my @c = qw/marpa_ami.c marpa_avl.c marpa.c
545 marpa_codes.c marpa_obs.c marpa_tavl.c/;
546 if (! -r 'config.h') {
547 #
548 ## Because Config::AutoConf can only generate #define/#undef
549 ## stubs, we write our config.h with these stubs, our config.h
550 ## will then include a generated config_from_autoconf.h
551 #
552 if ( $self->verbose() ) {
553 say join q{ }, "Doing config.h"
554 or die "print failed: $ERRNO";
555 }
556 open my $config_fh, '>>', 'config.h' || die "Cannot open config.h, $!\n";
557 my $ac = Config::AutoConf->new();
558 my $inline_ok = 0;
559 {
560 $ac->msg_checking('inline');
561 my $program = $ac->lang_build_program("static inline int testinline() {return 1;}\n", 'testinline');
562 $inline_ok = $ac->compile_if_else($program);
563 $ac->msg_result($inline_ok ? 'yes' : 'no');
564 }
565 my $inline = '';
566 if (! $inline_ok) {
567 foreach (qw/__inline__ __inline/) {
568 my $candidate = $_;
569 $ac->msg_checking($candidate);
570 my $program = $ac->lang_build_program("static $candidate int testinline() {return 1;}\n", 'testinline');
571 my $rc = $ac->compile_if_else($program);
572 $ac->msg_result($rc ? 'yes' : 'no');
573 if ($rc) {
574 $inline = $candidate;
575 last;
576 }
577 }
578 }
579 if ($inline) {
580 print {$config_fh} <<INLINEHOOK;
581 #ifndef __CONFIG_WITH_STUBS_H
582 #ifndef __cplusplus
583 #define inline $inline
584 #endif
585 #include "config_from_autoconf.h"
586 #endif /* __CONFIG_WITH_STUBS_H */
587 INLINEHOOK
588 } else {
589 print {$config_fh} <<INLINEHOOK;
590 #ifndef __CONFIG_WITH_STUBS_H
591 #ifndef __cplusplus
592 /* #undef inline */
593 #endif
594 #include "config_from_autoconf.h"
595 #endif /* __CONFIG_WITH_STUBS_H */
596 INLINEHOOK
597 }
598
599 # Config::Autoconf mistakes 0 for undef, so these must be done explicitly
600 say {$config_fh} join q{ }, '#define MARPA_LIB_MAJOR_VERSION', $libmarpa_version[0];
601 say {$config_fh} join q{ }, '#define MARPA_LIB_MINOR_VERSION', $libmarpa_version[1];
602 say {$config_fh} join q{ }, '#define MARPA_LIB_MICRO_VERSION', $libmarpa_version[2];
603
604 close($config_fh);
605 $ac = Config::AutoConf->new();
606 my $sizeof_int = $ac->check_sizeof_type('int');
607 if ($sizeof_int < 4) {
608 die "Marpa requires that int be at least 32 bits -- on this system that is not the case";
609 }
610
611 $ac->check_stdc_headers;
612 $ac->check_default_headers();
613
614 # This check was to prepare for Perl's 64-bit pseudo-UTF8 characters inside the SLIF.
615 # It needs to be rethought.
616 #
617 # if (!$ac->check_type('unsigned long long int')) {
618 # die "Marpa requires that unsigned long long int is supported by your compiler";
619 # }
620
621 $ac->define_var('PACKAGE', "\"libmarpa\"");
622 $ac->define_var('PACKAGE_BUGREPORT', "\"http://rt.cpan.org/NoAuth/Bugs.html?Dist=Marpa\"");
623 $ac->define_var('PACKAGE_NAME', "\"libmarpa\"");
624 $ac->define_var('PACKAGE_STRING', "\"libmarpa $libmarpa_version[0].$libmarpa_version[1].$libmarpa_version[2]\"");
625 $ac->define_var('PACKAGE_TARNAME', "\"libmarpa\"");
626 $ac->define_var('PACKAGE_URL', "\"\"");
627 $ac->define_var('PACKAGE_VERSION', "\"$libmarpa_version\"");
628 $ac->define_var('PACKAGE_STRING', "\"$libmarpa_version\"");
629 $ac->write_config_h('config_from_autoconf.h');
630 }
631
632 my @o = map {s/\.c$/$Config{obj_ext}/; $_} @c;
633 if (! -r 'Makefile.PL') {
634 open my $makefile_pl_fh, '>', 'Makefile.PL';
635 my $CCFLAGS = @debug_flags ? "$Config{ccflags} @debug_flags" : '';
636 my $linktype = 'static';
637 my $blib = 'blib';
638 my $name = 'libmarpa';
639 print {$makefile_pl_fh} "
640 use ExtUtils::MakeMaker;
641 WriteMakefile(VERSION => \"$libmarpa_version\",
642 XS_VERSION => \"$libmarpa_version\",
643 NAME => \"$name\",
644 OBJECT => '@o',
645 CCFLAGS => '$CCFLAGS',
646 INST_LIB => \"./$blib/lib\",
647 INST_ARCHLIB => \"./$blib/arch\",
648 INST_SCRIPT => \"./$blib/script\",
649 INST_BIN => \"./$blib/bin\",
650 LINKTYPE => $linktype);
651 ";
652 close $makefile_pl_fh;
653 die 'Making Makefile: perl Failure'
654 if not IPC::Cmd::run( command => [$^X, 'Makefile.PL'], verbose => 1 );
655 }
656 } else {
657 my $shell = $Config{sh};
658 my $configure_script = 'configure';
659 if ( $self->verbose() ) {
660 say join q{ }, "Running command:", $shell, $configure_script,
661 @configure_command_args
662 or die "print failed: $ERRNO";
663 }
664 if (not IPC::Cmd::run(
665 command => [ $shell, $configure_script, @configure_command_args ],
666 verbose => 1
667 )
668 )
669 {
670 say {*STDERR} "Failed: $configure_script"
671 or die "say failed: $ERRNO";
672 say {*STDERR} "Current directory: $build_dir"
673 or die "say failed: $ERRNO";
674 die 'Cannot run libmarpa configure';
675 } ## end if ( not IPC::Cmd::run( command => [ $shell, $configure_script...]))
676
677 }
678 if ( $self->verbose() ) {
679 print "Making libmarpa: Start\n" or die "Cannot print: $ERRNO";
680 }
681 die 'Making libmarpa: make Failure'
682 if not IPC::Cmd::run( command => [$Config{make}], verbose => 1 );
683
684 ## stamp-h1 is a by-product of the GNU autotools, but for Config::AutoConf,
685 ## I need to dummy one up, so do it for both
686 {
687 open my $time_stamp_fh, q{>}, 'stamp-h1';
688 say {$time_stamp_fh} scalar localtime();
689 close $time_stamp_fh;
690 }
691
692 chdir $cwd;
693
694 return 1;
695
696 } ## end sub do_libmarpa
697
698 sub ACTION_manifest {
699 die qq{Automatic generation of the MANIFEST file is disabled\n}
700 . qq{The Marpa MANIFEST file is handwritten\n};
701 }
702
703 sub make_writeable {
704 my $file = shift;
705 die qq{"$file" in "}, getcwd(), qq{" does not exist} if ! -e $file;
706 my $current_mode = (stat $file)[2];
707 die qq{mode not defined for $file} if not defined $current_mode;
708 chmod $current_mode | (oct 200), $file;
709 }
710
711 sub ACTION_metacheck {
712 my $self = shift;
713
714 # does not check CPAN::Meta
715 # version -- assumes updated with Module::Build
716 # this should only be run when making distributions
717 # not on install, so we don't have to be too paranoid
718 require CPAN::Meta;
719
720 my $marpa_version = $self->dist_version();
721 my $meta = CPAN::Meta->load_file($self->metafile());
722 my $provides = $meta->{provides};
723 my @metacheck_problems = ();
724 PROVIDED: for my $provided_name (keys %{$provides}) {
725 my $provided_version = $provides->{$provided_name}->{version};
726 if (not defined $provided_version) {
727 push @metacheck_problems, "No version for $provided_name\n";
728 next PROVIDED;
729 }
730 if ($provided_version ne $marpa_version) {
731 push @metacheck_problems,
732 "Version of $provided_name is $provided_version, but Marpa version is $marpa_version\n";
733 }
734 }
735 if (@metacheck_problems) {
736 print {*STDERR} join q{}, @metacheck_problems
737 or die "Cannot print: $ERRNO";
738 die 'Fatal error due to META file issues';
739 }
740 }
741
742 sub ACTION_distcheck {
743 my $self = shift;
744 $self->ACTION_metacheck();
745 return $self->SUPER::ACTION_distcheck;
746 } ## end sub ACTION_distcheck
747
748 sub ACTION_distmeta {
749 my $self = shift;
750 my $return_value = $self->SUPER::ACTION_distmeta;
751
752 # does not check CPAN::Meta
753 # version -- assumes updated with Module::Build
754 # this should only be run when making distributions
755 # not on install, so we don't have to be too paranoid
756 require CPAN::Meta;
757
758 my $meta = CPAN::Meta->load_file( $self->metafile() );
759 my @delete = ('DynaLoader');
760 for my $provided ( keys %{ $meta->{provides} } ) {
761 push @delete, $provided if $provided =~ m/\AMarpa::R2::Inner::/xms;
762 push @delete, $provided if $provided =~ m/\AMarpa::R2::Internal::/xms;
763 push @delete, $provided if $provided =~ m/\AMarpa::R2::HTML::Internal::/xms;
764 push @delete, $provided if $provided =~ m/::Internal\z/xms;
765 }
766 if (@delete) {
767 for my $deletion (@delete) {
768 delete $meta->{provides}->{$deletion};
769 }
770 if (defined $meta->{dynamic_config}) {
771 # Make sure this stays numeric
772 $meta->{dynamic_config} = $meta->{dynamic_config}+0;
773 }
774 $meta->save( 'META.yml', { version => '1.4' } );
775 $meta->save('META.json');
776 $self->log_info("Revised META.yml and META.json\n");
777 } ## end if (@delete)
778
779 return $return_value;
780 } ## end sub ACTION_distmeta
781
782 sub ACTION_dist {
783 my $self = shift;
784 open my $fh, q{<}, 'Changes';
785 my $changes = do {
786 local $RS = undef;
787 <$fh>;
788 };
789 close $fh;
790 my $marpa_version = $self->dist_version();
791 die qq{"$marpa_version" not in Changes file}
792 if 0 > index $changes, $marpa_version;
793 return $self->SUPER::ACTION_dist;
794 } ## end sub ACTION_dist
795
796 sub write_installed_pm {
797 my ( $self, @components ) = @_;
798 my $filename = 'Installed';
799 my @package_components = @components[ 1 .. $#components ];
800 my $contents = installed_contents( $self, join q{::}, @package_components,
801 $filename );
802 $filename .= q{.pm};
803 return $self->file_write( $contents, @components, $filename );
804 } ## end sub write_installed_pm
805
806 sub ACTION_code {
807 my $self = shift;
808 my @r2_perl_components = qw(pperl Marpa R2 Perl);
809 my @r2_components = qw(lib Marpa R2);
810 my $config_pm_filename = File::Spec->catfile(qw(inc Marpa R2 Config.pm ));
811 my $build_filename = 'Build';
812 my @derived_files = (
813 File::Spec->catfile( @r2_components, 'Version.pm' ),
814 File::Spec->catfile( @r2_components, 'Installed.pm' ),
815 File::Spec->catfile( @r2_perl_components, 'Version.pm' ),
816 File::Spec->catfile( @r2_perl_components, 'Installed.pm' ),
817 );
818 if (not $self->up_to_date(
819 [ $config_pm_filename, $build_filename ],
820 \@derived_files
821 )
822 )
823 {
824 say {*STDERR} 'Writing version files' or die "say failed: $ERRNO";
825 write_installed_pm( $self, qw(lib Marpa R2 ) );
826 write_installed_pm( $self, qw(pperl Marpa R2 Perl ) );
827 my $perl_version_pm =
828 perl_version_contents( $self, 'Marpa::R2::Perl' );
829 my $version_pm = xs_version_contents( $self, 'Marpa::R2' );
830 $self->file_write( $version_pm, qw(lib Marpa R2 Version.pm) );
831 $self->file_write( $perl_version_pm,
832 qw(pperl Marpa R2 Perl Version.pm) );
833 } ## end if ( not $self->up_to_date( [ $config_pm_filename, ...]))
834
835 # If it's a shared library, we don't build it here.
836 if ( not defined $self->args('libmarpa-external') ) {
837
838 # If the mode file exists, any switch to the internal Libmarpa
839 # must be made explicitly
840 if ( not defined $self->args('libmarpa-internal') ) {
841 my $libmarpa_mode_file =
842 File::Spec->catdir( $self->base_dir(), 'engine', 'cf',
843 'LIBMARPA_MODE' );
844 die
845 "Libmarpa is external, but you did not explicity specify that -- you need to\n",
846 ' If you want to go back to the built-in Libmarpa, ',
847 qq{use Build's "--libmarpa-internal=on" flag\n}
848 if -e $libmarpa_mode_file;
849 } ## end if ( not defined $self->args('libmarpa-internal') )
850 $self->do_libmarpa();
851 } ## end if ( not defined $self->args('libmarpa-external') )
852
853 return $self->SUPER::ACTION_code;
854 } ## end sub ACTION_code
855
856 sub ACTION_clean {
857 my $self = shift;
858
859 my $curdir = File::Spec->rel2abs( File::Spec->curdir() );
860 if ( $self->verbose ) {
861 print "Cleaning libmarpa\n" or die "print failed: $ERRNO";
862 }
863
864 return $self->SUPER::ACTION_clean;
865 } ## end sub ACTION_clean
866
867 sub ACTION_test {
868 my $self = shift;
869 local $ENV{PERL_DL_NONLAZY} = 1;
870 return $self->SUPER::ACTION_test;
871 }
872
873 sub ACTION_install {
874 my $self = shift;
875 die qq{"libmarpa-shared" not supported for "install" target\n},
876 qq{ Marpa::R2 supports testing of shared libraries, but is not an installer for them\n}
877 if defined $self->args('libmarpa-shared');
878 return $self->SUPER::ACTION_install;
879 }
880
881 1;
882
883 # vim: expandtab shiftwidth=4: