builder/Affix/Builder.pm

filter: Statementstmt 25.7% 92 / 357 Branchbran 8.0% 15 / 186 Conditioncond 10.0% 6 / 60 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 52.7% 19 / 36 Podpod 0.0% 0 / 17 totaltotal 19.1% 132 / 688 CC 125 SCAR 58.4
CC
125
Cov
19%
step_fuzz
756.0
step_infix
756.0
step_affix
462.0
CRAP 343.6
↑ Summary blib/lib/Affix/Build.pm »
1 # Based on Module::Build::Tiny which is copyright (c) 2011 by Leon Timmermans, David Golden.
2 # Module::Build::Tiny is free software; you can redistribute it and/or modify it under
3 # the same terms as the Perl 5 programming language system itself.
4 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 class #
8 1 Affix::Builder {
9 1 ▶ use CPAN::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use ExtUtils::Install qw[pm_to_blib install];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶ use ExtUtils::InstallPaths 0.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶ use File::Basename qw[basename dirname];
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶ use File::Path qw[make_path remove_tree];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶ use File::Spec::Functions qw[catfile catdir rel2abs abs2rel splitdir curdir];
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶ use JSON::PP 2 qw[encode_json decode_json];
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use File::Temp qw[tempfile];
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Not in CORE
19 1 ▶ use Path::Tiny qw[path cwd];
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use ExtUtils::Helpers 0.028 qw[make_executable split_like_shell detildefy];
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # infix and Affix stuff
23 1 ▶ use Config qw[%Config];
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 field $force : param //= 0;
25 1 field $debug : param //= 0;
26 1 field $libver;
27 1 field $cflags;
28 1 field $ldflags;
29 1 field $cppver = 'c++17'; # https://en.wikipedia.org/wiki/C%2B%2B20#Compiler_support
30 1 field $cver = 'c17'; # https://en.wikipedia.org/wiki/C17_(C_standard_revision)
31 1 field $make : param //= $Config{make} || 'dmake';
32 #
33 1 field $action : param //= 'build';
34 1 field $meta : reader = CPAN::Meta->load_file('META.json');
35  
36 # Params to Build script
37 1 field $install_base : param //= '';
38 1 field $installdirs : param //= '';
39 1 field $uninst : param //= 0; # Make more sense to have a ./Build uninstall command but...
40 1 field $install_paths : param //= ExtUtils::InstallPaths->new( dist_name => $meta->name );
41 1 field $verbose : param(v) //= 0;
42 1 field $dry_run : param //= 0;
43 1 field $pureperl : param //= 0;
44 1 field $jobs : param //= 1;
45 1 field $destdir : param //= '';
46 1 field $prefix : param //= '';
47 #
48 1 ▶ ADJUST {
Subroutine __ANON__: called CC=15 SCAR=34.4
49 1 ▶ die "No META information provided\n" unless -e 'META.json';
Branch: 1 branch
BranchTrueFalseTotal
unless -e "META.json" 0 1 1
50 1 ▶ die "OS unsupported: Affix requires a 64-bit architecture\n" if $Config{ptrsize} < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $Affix::Builder::Config{"ptrsize"} < 8 0 1 1
51  
52 # Configure Flags
53 1 my $is_bsd = $^O =~ /bsd/i;
54 1 my $is_win = $^O =~ /MSWin32/i;
55 1 my $is_sun = $^O =~ /(solaris|sunos)/i;
56 1 ▶ $cflags = ( $is_bsd || $is_sun ) ? '' : '-fPIC ';
Branch: 1 branch
BranchTrueFalseTotal
$is_bsd || $is_sun ? : 0 1 1
Condition33%1/3
$is_bsd or $is_sun
l!l&&r!l&&!r
001
MC/DC0%0/2
$is_bsd or $is_sun
$is_bsd $is_sun
Truth table33%1/3
A or B
A= $is_bsdB= $is_sun
ABresult
1X1
011
000
57 1 ▶ $ldflags = ( $is_bsd || $is_sun ) ? '' : ' -flto=auto ';
Branch: 1 branch
BranchTrueFalseTotal
$is_bsd || $is_sun ? : 0 1 1
Condition33%1/3
$is_bsd or $is_sun
l!l&&r!l&&!r
001
MC/DC0%0/2
$is_bsd or $is_sun
$is_bsd $is_sun
Truth table33%1/3
A or B
A= $is_bsdB= $is_sun
ABresult
1X1
011
000
58 1 ▶ $cflags .= ' -D__EXTENSIONS__ -D_LARGEFILE64_SOURCE' if $is_sun;
Branch: 1 branch
BranchTrueFalseTotal
if $is_sun 0 1 1
59 1 ▶ if ( $debug > 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($debug > 0) { } 0 1 1
elsif (not $is_win) { } 1 0 1
60 0 $cflags
61 .= '-DDEBUG=' .
62 $debug .
63 ' -g3 -gdwarf-4 ' .
64 ' -Wno-deprecated -pipe ' .
65 ' -Wall -Wextra -Wpedantic -Wvla -Wnull-dereference ' .
66 ' -Wswitch-enum -Wduplicated-cond ' .
67 ' -Wduplicated-branches';
68 0 ▶ $cflags .= ' -fvar-tracking-assignments' unless $Config{osname} eq 'darwin';
Branch: 1 branch
BranchTrueFalseTotal
unless $Affix::Builder::Config{"osname"} eq "darwin" 0 0 0
69 }
70 elsif ( !$is_win ) {
71 1 $cflags .= ' -DNDEBUG -DBOOST_DISABLE_ASSERTS -O3 -ftree-vectorize -ffast-math -fno-align-functions -fno-omit-frame-pointer';
72 1 ▶ $cflags .= ' -flto' unless ( $is_bsd || $is_sun );
Branch: 1 branch
BranchTrueFalseTotal
unless $is_bsd or $is_sun 1 0 1
Condition33%1/3
$is_bsd or $is_sun
l!l&&r!l&&!r
001
MC/DC0%0/2
$is_bsd or $is_sun
$is_bsd $is_sun
Truth table33%1/3
A or B
A= $is_bsdB= $is_sun
ABresult
1X1
011
000
73 }
74  
75 # Threading support (Critical for shm_open/librt on Linux)
76 1 ▶ if ( !$is_win ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_win) 1 0 1
77 1 $cflags .= ' -pthread';
78 1 $ldflags .= ' -pthread';
79 1 ▶ $ldflags .= ' -Wl,-z,nobtcfi' if $^O eq 'openbsd'; # XXX - Temp BTI/IBT workaround https://www.openbsd.org/innovations.html
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "openbsd" 0 1 1
80 }
81 }
82 0 ▶ method write_file( $filename, $content ) { path($filename)->spew_raw($content) or die "Could not open $filename: $!\n" }
Subroutine write_file: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
path($filename)->spew_raw($content) or die "Could not open ${filename}: $!\n" 0 0 0
83 0 ▶ method read_file ($filename) { path($filename)->slurp_utf8 or die "Could not open $filename: $!\n" }
Subroutine read_file: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
path($filename)->slurp_utf8 or die "Could not open ${filename}: $!\n" 0 0 0
84  
85 0 ▶ method step_build() {
Subroutine step_build: not called CC=2 SCAR=17.9
Pod undocumented
86 0 $self->step_affix;
87 0 my %modules = map { $_ => catfile( 'blib', $_ ) } find( qr/\.pm$/, 'lib' );
88 0 my %docs = map { $_ => catfile( 'blib', $_ ) } find( qr/\.pod$/, 'lib' );
89 0 my %scripts = map { $_ => catfile( 'blib', $_ ) } find( qr/(?:)/, 'script' );
90 0 my %sdocs = map { $_ => delete $scripts{$_} } grep {/.pod$/} keys %scripts;
91 0 my %dist_shared = map { $_ => catfile( qw[blib lib auto share dist], $meta->name, abs2rel( $_, 'share' ) ) } find( qr/(?:)/, 'share' );
92 0 my %module_shared = map { $_ => catfile( qw[blib lib auto share module], abs2rel( $_, 'module-share' ) ) } find( qr/(?:)/, 'module-share' );
93 0 pm_to_blib( { %modules, %docs, %scripts, %dist_shared, %module_shared }, catdir(qw[blib lib auto]) );
94 0 make_executable($_) for values %scripts;
95 0 make_path( catdir(qw[blib arch]), { chmod => 0755, verbose => $verbose } );
96 0 0;
97 }
98  
99 0 ▶ method _clean_objects() {
Subroutine _clean_objects: not called CC=2 SCAR=17.9
100 0 my $lib_dir = path('lib');
101 0 my @objs;
102 $lib_dir->visit(
103 0 ▶ sub ( $path, $state ) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
104 0 ▶ push @objs, $path if $path->is_file && $path =~ /\.(?:o|obj)\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $path->is_file and $path =~ /\.(?:o|obj)\z/iu 0 0 0
Condition0%0/3
$path->is_file and $path =~ /\.(?:o|obj)\z/iu
!ll&&!rl&&r
000
MC/DC0%0/2
$path->is_file and $path =~ /\.(?:o|obj)\z/iu
$path->is_file $path =~ /\.(?:o|obj)\z/iu
Truth table0%0/3
A and B
A= $path->is_fileB= $path =~ /\.(?:o|obj)\z/iu
ABresult
0X0
100
111
105 },
106 0 { recurse => 1 }
107 );
108 0 remove_tree( $_, { verbose => $verbose } ) for @objs;
109 0 0;
110 }
111  
112 0 ▶ method step_clean() {
Subroutine step_clean: not called CC=2 SCAR=17.9
Pod undocumented
113 0 remove_tree( $_, { verbose => $verbose } ) for qw[blib temp];
114 0 $self->_clean_objects;
115 0 0;
116 }
117  
118 0 ▶ method step_install() {
Subroutine step_install: not called CC=2 SCAR=17.9
Pod undocumented
119 0 ▶ $self->step_build() unless -d 'blib';
Branch: 1 branch
BranchTrueFalseTotal
unless -d "blib" 0 0 0
120 0 my %res;
121 0 install(
122 [ from_to => $install_paths->install_map,
123 verbose => $verbose,
124 always_copy => 1,
125 dry_run => $dry_run,
126 uninst => $uninst,
127 result => \%res
128 ]
129 );
130  
131 # In the future, I might check the values of %res according to https://metacpan.org/pod/ExtUtils::Install#install
132 0 0;
133 }
134  
135 0 ▶ method step_realclean () {
Subroutine step_realclean: not called CC=2 SCAR=17.9
Pod undocumented
136 0 remove_tree( $_, { verbose => $verbose } ) for qw[blib temp Build _build_params MYMETA.yml MYMETA.json];
137 0 remove_tree( 'infix/build_lib', { verbose => $verbose } );
138 0 $self->_clean_objects;
139 0 0;
140 }
141  
142 1 ▶ method step_test(%args) {
Subroutine step_test: called CC=5 SCAR=18.1
Pod undocumented
143 1 ▶ $jobs = $args{j} // $args{jobs} // $jobs;
Condition33%2/6
($args{"j"} // $args{"jobs"}) // $jobs
l!l&&r!l&&!r
010
$args{"j"} // $args{"jobs"}
l!l&&r!l&&!r
001
MC/DC0%0/3
($args{"j"} // $args{"jobs"}) // $jobs
$args{"j"} $args{"jobs"} $jobs
Truth table25%1/4
A // B // C
A= $args{"j"}B= $args{"jobs"}C= $jobs
ABCresult
1XX1
01X1
0011
0000
144 1 ▶ $self->step_build() unless -d 'blib';
Branch: 1 branch
BranchTrueFalseTotal
unless -d "blib" 0 1 1
145 1 require TAP::Harness::Env;
146 my %test_args = (
147 ( verbosity => $verbose ),
148 ( $jobs ? ( jobs => $jobs ) : () ),
149 ( color => -t STDOUT ),
150 1 ▶ lib => [ map { rel2abs( catdir( 'blib', $_ ) ) } qw[arch lib] ]
Branch: 1 branch
BranchTrueFalseTotal
$jobs ? : 1 0 1
151 );
152 1 TAP::Harness::Env->create( \%test_args )->runtests( sort map { $_->stringify } find( qr/\.t$/, 't' ) )->has_errors;
153 }
154  
155 0 ▶ method step_fuzz(@args) {
Subroutine step_fuzz: not called CC=27 SCAR=66.3
Pod undocumented
156 0 my %args = @args;
157 0 ▶ $self->step_build() unless -d 'blib';
Branch: 1 branch
BranchTrueFalseTotal
unless -d "blib" 0 0 0
158  
159 # Fuzz config via env vars (or caller can pass %args)
160 0 ▶ my $max_iter = $ENV{FUZZ_MAX_ITER} // $args{iter} // 10000;
Condition0%0/5
($ENV{"FUZZ_MAX_ITER"} // $args{"iter"}) // 10000
l!l
00
$ENV{"FUZZ_MAX_ITER"} // $args{"iter"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"FUZZ_MAX_ITER"} // $args{"iter"}) // 10000
$ENV{"FUZZ_MAX_ITER"} $args{"iter"}
Truth table0%0/3
A // B
A= $ENV{"FUZZ_MAX_ITER"}B= $args{"iter"}
ABresult
1X1
011
000
161 0 ▶ my $timeout = $ENV{FUZZ_TIMEOUT} // $args{timeout} // 5;
Condition0%0/5
($ENV{"FUZZ_TIMEOUT"} // $args{"timeout"}) // 5
l!l
00
$ENV{"FUZZ_TIMEOUT"} // $args{"timeout"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"FUZZ_TIMEOUT"} // $args{"timeout"}) // 5
$ENV{"FUZZ_TIMEOUT"} $args{"timeout"}
Truth table0%0/3
A // B
A= $ENV{"FUZZ_TIMEOUT"}B= $args{"timeout"}
ABresult
1X1
011
000
162 0 ▶ my $verbose_fuzz = $ENV{FUZZ_VERBOSE} // $args{verbose} // 0;
Condition0%0/5
($ENV{"FUZZ_VERBOSE"} // $args{"verbose"}) // 0
l!l
00
$ENV{"FUZZ_VERBOSE"} // $args{"verbose"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"FUZZ_VERBOSE"} // $args{"verbose"}) // 0
$ENV{"FUZZ_VERBOSE"} $args{"verbose"}
Truth table0%0/3
A // B
A= $ENV{"FUZZ_VERBOSE"}B= $args{"verbose"}
ABresult
1X1
011
000
163  
164 # Determine which Perl targets to run
165 0 my @targets;
166 0 ▶ if ( $args{all} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'all'}) { } 0 0 0
elsif ($args{'smoke'}) { } 0 0 0
elsif ($args{'target'}) { } 0 0 0
167 0 @targets = qw[wrap grammar register cross compile];
168 }
169 elsif ( $args{smoke} ) {
170 0 @targets = qw[wrap grammar register cross];
171 }
172 elsif ( $args{target} ) {
173 0 ▶ @targets = ref $args{target} ? @{ $args{target} } : ( $args{target} );
Branch: 1 branch
BranchTrueFalseTotal
ref $args{'target'} ? : 0 0 0
174 }
175 else {
176 0 @targets = qw[wrap grammar register cross];
177 }
178  
179 # Perl fuzz target metadata
180 0 my %perl_targets = (
181 wrap => { script => 'fuzz_wrap_type_sig.pl', desc => 'Affix::Wrap::Type->parse()', needs_lib => 1 },
182 grammar => { script => 'fuzz_grammar_mutate.pl', desc => 'Grammar-aware C sig mutations', needs_lib => 1, extra_inc => 1 },
183 register => { script => 'fuzz_register_types.pl', desc => 'Affix::_typedef() — C parser direct', needs_lib => 1 },
184 cross => { script => 'fuzz_cross_boundary.pl', desc => 'Cross-boundary Perl->C->JIT', needs_lib => 1 },
185 compile => { script => 'fuzz_compile_ok.pl', desc => 'compile_ok() C compilation', needs_lib => 1 },
186 shared => { script => 'fuzz_shared_lib.pl', desc => 'Compile→load→affix→call→verify ABI', needs_lib => 1 }
187 );
188  
189 # C fuzz targets (delegate to infix/build.pl)
190 0 my %c_targets = (
191 signature => 'Parser crashes + arena stress',
192 abi => 'ABI classification',
193 types => 'Type generator bugs',
194 roundtrip => 'Type->String->Type consistency',
195 trampoline => 'JIT trampoline creation',
196 direct => 'Direct marshalling JIT'
197 );
198 0 my $fuzz_dir = path('fuzz');
199 0 ▶ die "fuzz/ directory not found\n" unless -d $fuzz_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $fuzz_dir 0 0 0
200 0 my $failures = 0;
201 0 ▶ my $iters = $args{smoke} ? 100 : $max_iter;
Branch: 1 branch
BranchTrueFalseTotal
$args{'smoke'} ? : 0 0 0
202 0 ▶ my $to = $args{smoke} ? 3 : $timeout;
Branch: 1 branch
BranchTrueFalseTotal
$args{'smoke'} ? : 0 0 0
203  
204 # Run Perl fuzz targets via TAP::Harness for proper TAP output
205 0 require TAP::Harness::Env;
206 0 my @fuzz_scripts;
207 0 for my $name (@targets) {
208 0 ▶ my $target = $perl_targets{$name} // do { warn "Unknown Perl fuzz target: $name\n"; $failures++; next };
Condition0%0/3
$perl_targets{$name} // do { warn "Unknown Perl fuzz target: $name\n"; ++$failures; next }
l!l&&r!l&&!r
000
MC/DC0%0/2
$perl_targets{$name} // do { warn "Unknown Perl fuzz target: $name\n"; ++$failures; next }
$perl_targets{$name} do { warn "Unknown Perl fuzz target: $name\n"; ++$failures; next }
Truth table0%0/3
A // B
A= $perl_targets{$name}B= do { warn "Unknown Perl fuzz target: $name\n"; ++$failures; next }
ABresult
1X1
011
000
209 0 my $script = $fuzz_dir->child( $target->{script} );
210 0 ▶ unless ( -f $script ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $script) 0 0 0
211 0 warn "Fuzz script not found: $script\n";
212 0 $failures++;
213 0 next;
214 }
215 0 say "=" x 60;
216 0 say "Fuzzing: $target->{desc}";
217 0 say " Script: $target->{script}";
218 0 say " Iterations: $iters, Timeout: ${to}s";
219 0 say "=" x 60;
220 0 push @fuzz_scripts, $script->stringify;
221 }
222 0 ▶ if (@fuzz_scripts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fuzz_scripts) 0 0 0
223 0 local $ENV{FUZZ_MAX_ITER} = $iters;
224 0 local $ENV{FUZZ_TIMEOUT} = $to;
225 0 local $ENV{FUZZ_VERBOSE} = $verbose_fuzz;
226 0 my %harness_args = ( ( verbosity => $verbose ), ( color => -t STDOUT ), lib => [ map { rel2abs( catdir( 'blib', $_ ) ) } qw[arch lib] ] );
227 0 my $harness = TAP::Harness::Env->create( \%harness_args );
228 0 my $aggr = $harness->runtests( sort @fuzz_scripts );
229 0 ▶ $failures++ if $aggr->has_errors;
Branch: 1 branch
BranchTrueFalseTotal
if $aggr->has_errors 0 0 0
230 0 say "";
231 }
232  
233 # Smoke test: also quick-build C targets if available
234 0 ▶ if ( $args{smoke} || $args{all} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"smoke"} or $args{"all"}) 0 0 0
Condition0%0/3
$args{"smoke"} or $args{"all"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"smoke"} or $args{"all"}
$args{"smoke"} $args{"all"}
Truth table0%0/3
A or B
A= $args{"smoke"}B= $args{"all"}
ABresult
1X1
011
000
235 0 my $build_pl = path('infix/build.pl');
236 0 ▶ if ( -f $build_pl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $build_pl) { } 0 0 0
237 0 for my $name ( sort keys %c_targets ) {
238 0 say "=" x 60;
239 0 say "Building C fuzz target: fuzz:$name";
240 0 say " $c_targets{$name}";
241 0 say "=" x 60;
242 0 my $exit = system( $^X, $build_pl->stringify, "fuzz:$name" );
243 0 ▶ $failures++ if $exit != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exit != 0 0 0 0
244 0 say "";
245 }
246 }
247 else {
248 0 say "Skipping C fuzz targets (infix/build.pl not found)";
249 }
250 }
251 0 say "=" x 60;
252 0 ▶ if ($failures) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failures) { } 0 0 0
253 0 say "FAILED: $failures target(s) reported crashes or build errors";
254 }
255 else {
256 0 say "All fuzz targets clean.";
257 }
258 0 ▶ return $failures > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$failures > 0 ? : 0 0 0
259 }
260  
261 0 ▶ method get_arguments (@sources) {
Subroutine get_arguments: not called CC=2 SCAR=17.9
Pod undocumented
262 0 $_ = detildefy($_) for grep {defined} $install_base, $destdir, $prefix, values %{$install_paths};
263 0 $install_paths = ExtUtils::InstallPaths->new( dist_name => $meta->name );
264 0 return;
265 }
266  
267 1 ▶ method Build(@args) {
Subroutine Build: called CC=2 SCAR=7.1
Pod undocumented
268 1 my $method = $self->can( 'step_' . $action );
269 1 ▶ $method // die "No such action '$action'\n";
Condition50%1/2
$method // die("No such action '${action}'\n")
l!l
10
MC/DC0%0/1
$method // die("No such action '${action}'\n")
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
270 1 exit $method->( $self, @args );
271 }
272  
273 0 ▶ method Build_PL() {
Subroutine Build_PL: not called CC=5 SCAR=34.0
Pod undocumented
274 0 ▶ die "Pure perl Affix? Ha! You wish.\n" if $pureperl;
Branch: 1 branch
BranchTrueFalseTotal
if $pureperl 0 0 0
275 0 say sprintf 'Creating new Build script for %s %s', $meta->name, $meta->version;
276 0 $self->write_file( 'Build', sprintf <<'', $^X, __PACKAGE__, __PACKAGE__ );
277 #!%s
278 use lib 'builder';
279 use %s;
280 my $action = @ARGV && $ARGV[0] =~ /\A\w+\z/ ? shift @ARGV : 'build';
281 my $opts = {};
282 while ( @ARGV ) {
283 my $a = shift @ARGV;
284 if ( $a =~ /^-(\w+)$/ ) { $opts->{$1} = shift @ARGV // 1; }
285 elsif ( $a =~ /^-(\w+)=(.+)$/ ) { $opts->{$1} = $2; }
286 }
287 %s->new( action => $action )->Build( %%$opts );
288  
289 0 make_executable('Build');
290 0 ▶ my @env = defined $ENV{PERL_MB_OPT} ? split_like_shell( $ENV{PERL_MB_OPT} ) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'PERL_MB_OPT'} ? : 0 0 0
291 0 $self->write_file( '_build_params', encode_json( [ \@env, \@ARGV ] ) );
292 0 ▶ if ( my $dynamic = $meta->custom('x_dynamic_prereqs') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dynamic = $meta->custom("x_dynamic_prereqs")) 0 0 0
293 0 my %meta = ( %{ $meta->as_struct }, dynamic_config => 1 );
294 0 $self->get_arguments( \@env, \@ARGV );
295 0 require CPAN::Requirements::Dynamic;
296 0 my $dynamic_parser = CPAN::Requirements::Dynamic->new();
297 0 my $prereq = $dynamic_parser->evaluate($dynamic);
298 0 $meta{prereqs} = $meta->effective_prereqs->with_merged_prereqs($prereq)->as_string_hash;
299 0 $meta = CPAN::Meta->new( \%meta );
300 }
301 0 $meta->save(@$_) for ['MYMETA.json'];
302 }
303  
304 1 ▶ sub find ( $pattern, $base ) {
Subroutine find: called CC=2 SCAR=7.0
Pod undocumented
305 1 ▶ $base = path($base) unless builtin::blessed $base;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($base) 1 0 1
306 my $blah = $base->visit(
307 60 ▶ sub ( $path, $state ) {
Subroutine __ANON__: called CC=2 SCAR=6.9
308 60 ▶ $state->{$path} = $path if $path =~ $pattern;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$pattern/u 58 2 60
309 },
310 1 { recurse => 1 }
311 );
312 1 values %$blah;
313 }
314  
315 # infix builder
316 0 ▶ method step_clone_infix() {
Subroutine step_clone_infix: not called CC=3 SCAR=24.8
Pod undocumented
317 0 ▶ return if cwd->absolute->child('infix')->exists;
Branch: 1 branch
BranchTrueFalseTotal
if cwd()->absolute->child("infix")->exists 0 0 0
318 0 my $clone_dir = cwd->absolute->child('infix');
319 0 ▶ die 'Failed to clone infix' if system( 'git', 'clone', '--depth', '1', 'https://github.com/sanko/infix.git', $clone_dir->stringify );
Branch: 1 branch
BranchTrueFalseTotal
if system "git", "clone", "--depth", 1, "https://github.com/sanko/infix.git", $clone_dir->stringify 0 0 0
320 }
321  
322 0 ▶ method step_infix () {
Subroutine step_infix: not called CC=27 SCAR=66.3
Pod undocumented
323 0 $self->step_clone_infix();
324 0 my $cwd = cwd->absolute;
325 0 my $infix_dir = $cwd->child('infix');
326  
327 # Use architecture-specific directory to avoid collision on shared filesystems (WSL vs Windows)
328 0 my $build_lib = $infix_dir->child( 'build_lib', $Config{archname} );
329  
330 # If library already exists and is newer than source, we are good.
331 0 my $is_msvc = ( $Config{cc} =~ /cl(\.exe)?$/i );
332 0 ▶ my $lib_ext = $is_msvc ? '.lib' : '.a';
Branch: 1 branch
BranchTrueFalseTotal
$is_msvc ? : 0 0 0
333 0 ▶ my $lib_pre = $is_msvc ? '' : 'lib';
Branch: 1 branch
BranchTrueFalseTotal
$is_msvc ? : 0 0 0
334 0 my $lib_file = $build_lib->child( $lib_pre . 'infix' . $lib_ext );
335 0 my $src_file = $infix_dir->child( 'src', 'infix.c' );
336 0 ▶ if ( -e $lib_file && !$force ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $lib_file and not $force) 0 0 0
Condition0%0/3
-e $lib_file and not $force
!ll&&!rl&&r
000
MC/DC0%0/2
-e $lib_file and not $force
-e $lib_file not $force
Truth table0%0/3
A and not(B)
A= -e $lib_fileB= not $force
ABresult
0X0
100
111
337  
338 # Check timestamps to ensure we rebuild if source changed
339 0 ▶ if ( $src_file->stat->mtime <= $lib_file->stat->mtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src_file->stat->mtime <= $lib_file->stat->mtime) 0 0 0
340 0 return 0;
341 }
342 }
343 #
344 0 ▶ warn "Building infix static library for $Config{archname}..." if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
345 0 ▶ $build_lib->mkpath unless -d $build_lib;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $build_lib 0 0 0
346 0 my @include_dirs = ( $infix_dir->child('include'), $infix_dir->child('src') );
347  
348 # Detect Compiler settings using Perl's Config as base
349 0 ▶ my $cc_cmd = $Config{cc} || 'cc';
Condition0%0/2
$Affix::Builder::Config{'cc'} || 'cc'
l!l
00
MC/DC0%0/1
$Affix::Builder::Config{'cc'} || 'cc'
$Affix::Builder::Config{'cc'}
Truth table0%0/2
A
A= $Affix::Builder::Config{'cc'}
Aresult
11
00
350 0 my $cc_type = 'gcc'; # Default flavor
351 0 ▶ if ( $cc_cmd =~ /cl(\.exe)?$/i ) { $cc_type = 'msvc'; }
Branch: 4 branches
BranchTrueFalseTotal
if ($cc_cmd =~ /cl(\.exe)?$/iu) { } 0 0 0
elsif ($cc_cmd =~ /clang/iu) { } 0 0 0
elsif ($cc_cmd =~ /gcc/iu) { } 0 0 0
elsif ($cc_cmd =~ /egcc/iu) { } 0 0 0
352 0 elsif ( $cc_cmd =~ /clang/i ) { $cc_type = 'clang'; }
353 0 elsif ( $cc_cmd =~ /gcc/i ) { $cc_type = 'gcc'; }
354 0 elsif ( $cc_cmd =~ /egcc/i ) { $cc_type = 'gcc'; }
355  
356 # Setup Flags
357 0 my ( $ar_cmd, @cflags, @arflags, $out_flag_cc, $out_flag_ar );
358 0 ▶ my @includes = map { ( $cc_type eq 'msvc' ? '/I' : '-I' ) . $_ } @include_dirs;
Branch: 1 branch
BranchTrueFalseTotal
$cc_type eq 'msvc' ? : 0 0 0
359 0 ▶ if ( $cc_type eq 'msvc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc_type eq 'msvc') { } 0 0 0
360 0 $ar_cmd = 'lib';
361 0 @cflags = ( '/nologo', '/c', '/std:c11', '/W3', '/GS', '/MD', '/O2', @includes );
362 0 ▶ @cflags = ( @cflags, '/DINFIX_DEBUG_ENABLED=1' ) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
363 0 @arflags = ('/nologo');
364 0 $out_flag_cc = '/Fo';
365 0 $out_flag_ar = '/OUT:';
366 }
367 else {
368 # GCC / Clang
369 0 $ar_cmd = 'ar';
370 0 @cflags = ( '-std=c11', '-Wall', '-Wextra', '-O2', '-fPIC', @includes );
371 0 ▶ @cflags = ( @cflags, '-DINFIX_DEBUG_ENABLED=1' ) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
372 0 @arflags = ('rcs');
373 0 $out_flag_cc = '-o';
374 0 $out_flag_ar = '';
375  
376 # Pass pthread to compilation of static lib too (safe practice)
377 0 ▶ push @cflags, '-pthread' unless $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MSWin32" 0 0 0
378 #
379 0 ▶ push @cflags, '-D__EXTENSIONS__', '-D_LARGEFILE64_SOURCE' if $^O =~ /(solaris|sunos)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /(solaris|sunos)/iu 0 0 0
380 }
381  
382 # Compile infix.c -> infix.o
383 0 ▶ my $obj_ext = $cc_type eq 'msvc' ? '.obj' : '.o';
Branch: 1 branch
BranchTrueFalseTotal
$cc_type eq 'msvc' ? : 0 0 0
384 0 my $obj_file = $build_lib->child( 'infix' . $obj_ext );
385 0 my @compile_cmd;
386 0 ▶ if ( $cc_type eq 'msvc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc_type eq 'msvc') { } 0 0 0
387 0 @compile_cmd = ( $cc_cmd, @cflags, $out_flag_cc . $obj_file, $src_file );
388 }
389 else {
390 0 @compile_cmd = ( $cc_cmd, @cflags, '-c', $src_file, $out_flag_cc, $obj_file );
391 }
392 0 ▶ warn " Compiling: @compile_cmd\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
393 0 ▶ if ( system(@compile_cmd) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (system(@compile_cmd) != 0) 0 0 0
394 0 die "Failed to compile infix.c";
395 }
396  
397 # Archive infix.o -> libinfix.a
398 0 my @archive_cmd;
399 0 ▶ if ( $cc_type eq 'msvc' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc_type eq 'msvc') { } 0 0 0
400 0 @archive_cmd = ( $ar_cmd, @arflags, $out_flag_ar . $lib_file, $obj_file );
401 }
402 else {
403 0 @archive_cmd = ( $ar_cmd, @arflags, $lib_file, $obj_file );
404 }
405 0 ▶ warn " Archiving: @archive_cmd\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
406 0 ▶ if ( system(@archive_cmd) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (system(@archive_cmd) != 0) 0 0 0
407 0 die "Failed to create infix static library";
408 }
409 0 ▶ warn "Infix library built: $lib_file\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
410 0 return 0;
411 }
412  
413 # Detects if linking against librt is required (common on Linux/BSD/Solaris for shm_open)
414 0 ▶ method check_for_lrt() {
Subroutine check_for_lrt: not called CC=15 SCAR=54.8
Pod undocumented
415 0 ▶ return '' if $^O eq 'MSWin32' || $^O eq 'android';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" or $^O eq "android" 0 0 0
Condition0%0/3
$^O eq "MSWin32" or $^O eq "android"
l!l&&r!l&&!r
000
MC/DC0%0/2
$^O eq "MSWin32" or $^O eq "android"
$^O eq "MSWin32" $^O eq "android"
Truth table0%0/3
A or B
A= $^O eq "MSWin32"B= $^O eq "android"
ABresult
1X1
011
000
416 0 ▶ warn "Checking if -lrt is required...\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
417 0 ▶ my $cc = $Config{cc} || 'cc';
Condition0%0/2
$Affix::Builder::Config{'cc'} || 'cc'
l!l
00
MC/DC0%0/1
$Affix::Builder::Config{'cc'} || 'cc'
$Affix::Builder::Config{'cc'}
Truth table0%0/2
A
A= $Affix::Builder::Config{'cc'}
Aresult
11
00
418 0 my $test_code = <<'END_C';
419 #include <sys/mman.h>
420 #include <fcntl.h>
421 int main(void) { shm_open("/test", O_RDONLY, 0); return 0; }
422 END_C
423 0 my ( $fh, $src ) = tempfile( SUFFIX => '.c', UNLINK => 1 );
424 0 print $fh $test_code;
425 0 close $fh;
426 0 my ( $ofh, $out ) = tempfile( UNLINK => 1 );
427 0 close $ofh;
428  
429 # Try without -lrt (list-form system to avoid shell injection)
430 0 ▶ open( my $devnull, '>', '/dev/null' ) if $^O ne 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O ne "MSWin32" 0 0 0
431 0 ▶ my $old_stdout = select $devnull if $devnull;
Branch: 1 branch
BranchTrueFalseTotal
if $devnull 0 0 0
432 0 system( $cc, '-o', $out, $src );
433 0 ▶ select $old_stdout if $old_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $old_stdout 0 0 0
434 0 ▶ close $devnull if $devnull;
Branch: 1 branch
BranchTrueFalseTotal
if $devnull 0 0 0
435 0 ▶ return '' if $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 0 0 0
436  
437 # Try with -lrt
438 0 ▶ open( $devnull, '>', '/dev/null' ) if $^O ne 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O ne "MSWin32" 0 0 0
439 0 ▶ $old_stdout = select $devnull if $devnull;
Branch: 1 branch
BranchTrueFalseTotal
if $devnull 0 0 0
440 0 system( $cc, '-o', $out, $src, '-lrt' );
441 0 ▶ select $old_stdout if $old_stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $old_stdout 0 0 0
442 0 ▶ close $devnull if $devnull;
Branch: 1 branch
BranchTrueFalseTotal
if $devnull 0 0 0
443 0 ▶ return '-lrt' if $? == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? == 0 0 0 0
444 0 return '';
445 }
446  
447 0 ▶ sub command_exists {
Subroutine command_exists: not called CC=2 SCAR=17.9
Pod undocumented
448 0 my ($cmd) = @_;
449 0 ▶ if ( $Config{osname} eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Affix::Builder::Config{'osname'} eq 'MSWin32') { } 0 0 0
450 0 return system( 'where', $cmd ) == 0;
451 }
452 else {
453 0 return system( 'command', '-v', $cmd ) == 0;
454 }
455 }
456  
457 0 ▶ method step_affix {
Subroutine step_affix: not called CC=21 SCAR=61.4
Pod undocumented
458 0 $self->step_infix;
459 0 my $cwd = cwd->absolute;
460 0 my @objs;
461 0 require ExtUtils::CBuilder;
462 0 my %config = %Config;
463  
464 # ExtUtils::CBuilder only uses $Config{ccflags} and silently drops
465 # ccflags_uselargefiles, which Configure keeps separate on Solaris/
466 # illumos (and a few other platforms). Without those defines the system
467 # headers do not expose off64_t, sigjmp_buf, alloca or fileno, so the
468 # same source that perl itself builds fails to compile here. Merge them
469 # in (harmless duplication when ccflags already contains them).
470 0 $config{ccflags} = join ' ', grep {length} $config{ccflags}, $config{ccflags_uselargefiles};
471 0 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 0 0
472 0 $config{ldflags} =~ s/-s //g;
473 0 $config{ldflags} =~ s/ -s//g;
474 0 $config{lddlflags} =~ s/-s //g;
475 0 $config{lddlflags} =~ s/ -s//g;
476 }
477 0 my $builder = ExtUtils::CBuilder->new( quiet => !$verbose, config => \%config );
478 0 my $pre = $cwd->child(qw[blib arch auto])->absolute;
479 0 require DynaLoader;
480 0 ▶ my $mod2fname = defined &DynaLoader::mod2fname ? \&DynaLoader::mod2fname : sub { return $_[0][-1] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined &DynaLoader::mod2fname ? : 0 0 0
481 0 my @parts = ('Affix');
482 0 my $archdir = rel2abs catdir( curdir, qw[. blib arch auto], @parts );
483 0 my $err;
484 0 make_path( $archdir, { chmod => 0755, error => \$err, verbose => $verbose } );
485 0 my $lib_file = catfile( $archdir, $mod2fname->( \@parts ) . '.' . $Config{dlext} );
486 0 my @dirs;
487 0 push @dirs, '../';
488 0 my $has_cxx = !1;
489 0 my $recompiled = 0;
490 0 my @sources = $cwd->child('lib/Affix.c');
491  
492 #~ warn "Sources to process: @sources\n";
493 0 for my $source (@sources) {
494  
495 #~ warn "Processing source: $source\n";
496 0 my $cxx = $source =~ /cx+$/;
497 0 my $file_base = $source->basename(qr[.c$]);
498 0 my $tempdir = path('lib');
499 0 $tempdir->mkdir( { verbose => $verbose, mode => oct '755' } );
500 0 my $version = $meta->version;
501 0 my $obj = $builder->object_file($source);
502  
503 #~ warn "Checking obj: $obj\n";
504 # Check mtimes of all .c and .h files under lib/ (includes Affix.c, marshal.c, Affix.h)
505 0 my $newest_dep = $source->stat->mtime;
506 0 my $iter = path('lib')->iterator;
507 0 ▶ while ( my $entry = $iter->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = &$iter()) 0 0 0
508 0 ▶ next unless $entry->is_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry->is_file 0 0 0
509 0 ▶ next unless $entry =~ /\.[ch]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry =~ /\.[ch]$/u 0 0 0
510 0 my $dep_mtime = $entry->stat->mtime;
511 0 ▶ $newest_dep = $dep_mtime if $dep_mtime > $newest_dep;
Branch: 1 branch
BranchTrueFalseTotal
if $dep_mtime > $newest_dep 0 0 0
512 }
513 0 ▶ my $should_compile
Condition0%0/9
$force || !(-f $obj) || $newest_dep >= path($obj)->stat->mtime || path(__FILE__)->stat->mtime > path($obj)->stat->mtime
l!l&&r!l&&!r
000
$force or not -f $obj or $newest_dep >= path($obj)->stat->mtime
l!l&&r!l&&!r
000
$force or not -f $obj
l!l&&r!l&&!r
000
MC/DC0%0/4
$force || !(-f $obj) || $newest_dep >= path($obj)->stat->mtime || path(__FILE__)->stat->mtime > path($obj)->stat->mtime
$force not -f $obj $newest_dep >= path($obj)->stat->mtime path(__FILE__)->stat->mtime > path($obj)->stat->mtime
Truth table0%0/5
A or not(B) or C || D
A= $forceB= not -f $objC= $newest_dep >= path($obj)->stat->mtimeD= path(__FILE__)->stat->mtime > path($obj)->stat->mtime
ABCDresult
1XXX1
01XX1
001X1
00011
00000
514 = ( $force ||
515 ( !-f $obj ) ||
516 ( $newest_dep >= path($obj)->stat->mtime ) ||
517 ( path(__FILE__)->stat->mtime > path($obj)->stat->mtime ) );
518 0 ▶ if ($should_compile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_compile) 0 0 0
519 0 ▶ my $reason
Branch: 3 branches
BranchTrueFalseTotal
!(-f $obj) ? : 0 0 0
$newest_dep >= path($obj)->stat->mtime ? : 0 0 0
path(__FILE__)->stat->mtime > path($obj)->stat->mtime ? : 0 0 0
520 = !-f $obj ? 'object file missing' :
521 $newest_dep >= path($obj)->stat->mtime ? 'source newer than object' :
522 path(__FILE__)->stat->mtime > path($obj)->stat->mtime ? 'builder changed' :
523 'forced';
524 0 warn "Compiling $source ($reason)\n";
525 0 $recompiled = 1;
526 }
527 0 push @dirs, $source->dirname();
528 0 ▶ $has_cxx = 1 if $cxx;
Branch: 1 branch
BranchTrueFalseTotal
if $cxx 0 0 0
529 0 ▶ push @objs,
Branch: 3 branches
BranchTrueFalseTotal
$should_compile ? : 0 0 0
$cxx ? : 0 0 0
$debug ? : 0 0 0
530 $should_compile ?
531 $builder->compile(
532 quiet => 0,
533 'C++' => $cxx,
534 source => $source->stringify,
535 defines => { VERSION => qq/"$version"/, XS_VERSION => qq/"$version"/ },
536 include_dirs => [
537 cwd->stringify, cwd->child('infix')->realpath->stringify,
538 cwd->child('infix')->child('include')->realpath->stringify, cwd->child('infix')->child('src')->realpath->stringify,
539 $source->dirname, $pre->child( $meta->name, 'include' )->stringify
540 ],
541 extra_compiler_flags =>
542 ( '-fPIC -std=' . ( $cxx ? $cppver : $cver ) . ' ' . $cflags . ( $debug ? ' -ggdb3 -g -Wall -Wextra -pedantic' : '' ) )
543 ) :
544 $obj;
545 }
546  
547 # Point to the Architecture-specific build lib
548 0 my $infix_build_lib = cwd->absolute->child('infix')->child( 'build_lib', $Config{archname} )->stringify;
549  
550 # Check for -lrt requirement
551 0 my $lrt_flag = $self->check_for_lrt();
552 0 ▶ my $data = {
Branch: 1 branch
BranchTrueFalseTotal
$lrt_flag ? : 0 0 0
553  
554 # Removed incorrect -lstdc++ logic. Added -lm for math.
555 # -pthread is already in $ldflags via ADJUST
556 extra_linker_flags =>
557 [ split( ' ', $ldflags ), "-L$infix_build_lib", '-linfix', ( $lrt_flag ? ( split( ' ', $lrt_flag ) ) : () ), '-lm' ],
558 objects => [@objs],
559 lib_file => $lib_file,
560 module_name => join '::',
561 @parts
562 };
563 0 ▶ warn "Linking $lib_file\n" if $recompiled;
Branch: 1 branch
BranchTrueFalseTotal
if $recompiled 0 0 0
564 0 return $builder->link(%$data);
565 }
566 };
567 1 1;
↑ Summary blib/lib/Affix/Build.pm »