| 1 | # | ||
| 2 | # This file is part of ASPEER::MakeMaker. | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | # Full license text is available at: | ||
| 10 | # | ||
| 11 | # <http://dev.perl.org/licenses/> | ||
| 12 | # | ||
| 13 | package ASPEER::MakeMaker::MM::Util; | ||
| 14 | |||
| 15 | |||
| 16 | # Pragma | ||
| 17 | # | ||
| 18 | use strict; | ||
| 19 | use warnings; | ||
| 20 | use vars qw($VERSION $DEBUG $QUIET $VERBOSE @EXPORT); | ||
| 21 | |||
| 22 | |||
| 23 | # External modules | ||
| 24 | # | ||
| 25 | use FindBin qw($RealBin $Script); | ||
| 26 | FindBin::again(); | ||
| 27 | use Data::Dumper; | ||
| 28 | use File::Spec; | ||
| 29 | use IO::File; | ||
| 30 | local $Data::Dumper::Indent=1; | ||
| 31 | local $Data::Dumper::Terse=1; | ||
| 32 | local $Data::Dumper::Sortkeys=1; | ||
| 33 | |||
| 34 | |||
| 35 | # Export functions | ||
| 36 | # | ||
| 37 | use base 'Exporter'; | ||
| 38 | @EXPORT=qw(err msg verbose debug quiet_enable verbose_enable debug_enable Dumper slurp blurp touch arg perlrun); | ||
| 39 | |||
| 40 | |||
| 41 | # Version information in a format suitable for CPAN etc. Must be | ||
| 42 | # all on one line | ||
| 43 | # | ||
| 44 | $VERSION='1.011'; | ||
| 45 | |||
| 46 | |||
| 47 | # Debugging on ? | ||
| 48 | # | ||
| 49 | $Script=~s/\.pl$//; | ||
| 50 | ($Carp::Verbose=++$DEBUG) if $ENV{uc("${Script}_DEBUG")}; | ||
| 51 | |||
| 52 | |||
| 53 | # Done | ||
| 54 | # | ||
| 55 | 1; | ||
| 56 | |||
| 57 | #================================================================================================== | ||
| 58 | |||
| 59 | |||
| 60 | sub quiet_enable { | ||
| 61 | |||
| 62 | |||
| 63 | # Turn on quiet flag | ||
| 64 | # | ||
| 65 | $QUIET=shift() || 1; | ||
| 66 | |||
| 67 | |||
| 68 | } | ||
| 69 | |||
| 70 | |||
| 71 | sub verbose { | ||
| 72 | |||
| 73 | # Print verbose message | ||
| 74 | # | ||
| 75 | return if $QUIET || !$VERBOSE; | ||
| 76 | return CORE::print STDERR &fmt(@_), $/; | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub verbose_enable { | ||
| 82 | |||
| 83 | # Turn on verbose flag | ||
| 84 | # | ||
| 85 | $VERBOSE=shift() || 1; | ||
| 86 | |||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub debug_enable { | ||
| 91 | |||
| 92 | # Turn on debugging flag | ||
| 93 | # | ||
| 94 | $DEBUG=shift(); | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | sub debug { | ||
| 100 | |||
| 101 | # Debug | ||
| 102 | # | ||
| 103 | $DEBUG || return; | ||
| 104 | my $debug=sprintf(shift(), @_); | ||
| 105 | chomp($debug); | ||
| 106 | my ($package, undef, $line, $method) = caller(1); # '1' for caller of the function | ||
| 107 | print STDERR sprintf("[%s:%d] %s$/", | ||
| 108 | join('::', grep {$_} ($package, $method)), | ||
| 109 | $line, | ||
| 110 | $debug | ||
| 111 | ); | ||
| 112 | |||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | sub err { | ||
| 117 | |||
| 118 | # Quit on errors | ||
| 119 | # | ||
| 120 | my $msg=&fmt('error: %s', @_); | ||
| 121 | CORE::print STDERR $msg, "\n"; | ||
| 122 | eval {require Carp; 1}; | ||
| 123 | Carp::croak; | ||
| 124 | |||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub fmt { | ||
| 129 | |||
| 130 | |||
| 131 | # Format message nicely. Always called by err or msg so caller=2 | ||
| 132 | # | ||
| 133 | my $message=sprintf(shift(), @_); | ||
| 134 | chomp($message); | ||
| 135 | my @caller=(caller(2)); | ||
| 136 | my $caller=$caller[3] || $caller[0]; | ||
| 137 | my ($class, $method)=($caller=~/^(.*)::([^:]+)$/); | ||
| 138 | $method ||=$caller[0]; | ||
| 139 | $caller=~s/^_?!(_)//; | ||
| 140 | my $format=' @<<<<<<<<<<<<<<<<<< @*'; | ||
| 141 | local $^A=''; | ||
| 142 | formline $format, "[$method]", $message; | ||
| 143 | return $^A; | ||
| 144 | |||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | sub msg { | ||
| 149 | |||
| 150 | # Print message | ||
| 151 | # | ||
| 152 | return (CORE::print STDERR &fmt(@_), $/) unless $QUIET; | ||
| 153 | |||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | sub slurp { | ||
| 158 | |||
| 159 | # Slurp in file content | ||
| 160 | # | ||
| 161 | my ($fn)=@_; | ||
| 162 | my $fh=IO::File->new($fn, 'r') || | ||
| 163 | return err("unable to open file $fn, $!"); | ||
| 164 | local $/=undef; | ||
| 165 | my $text=<$fh>; | ||
| 166 | $fh->close(); | ||
| 167 | return $text || ''; | ||
| 168 | |||
| 169 | } | ||
| 170 | |||
| 171 | |||
| 172 | sub blurp { | ||
| 173 | |||
| 174 | # Save file content | ||
| 175 | # | ||
| 176 | my ($fn, $text)=@_; | ||
| 177 | my $fh=IO::File->new($fn, 'w') || | ||
| 178 | return err("unable to open $fn for write, $!"); | ||
| 179 | $fh->print($text) || | ||
| 180 | return err("unable to write file $fn, $!"); | ||
| 181 | $fh->close() || | ||
| 182 | return err("unable to close file $fn, $!"); | ||
| 183 | return 1; | ||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | sub touch { | ||
| 189 | |||
| 190 | my ($fn)=@_; | ||
| 191 | return 1 if -e $fn; | ||
| 192 | return blurp($fn, ''); | ||
| 193 | |||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | sub perlrun { | ||
| 198 | |||
| 199 | |||
| 200 | # Get self ref | ||
| 201 | # | ||
| 202 | my ($self, $mm_or)=@_; | ||
| 203 | |||
| 204 | |||
| 205 | # Construct PERL runtime | ||
| 206 | # | ||
| 207 | my $perl_inc_ar=&perl_inc; | ||
| 208 | |||
| 209 | |||
| 210 | # And modules | ||
| 211 | # | ||
| 212 | my $perl_mod_ar=&perl_mod; | ||
| 213 | my @import_class; | ||
| 214 | { | ||
| 215 | no warnings qw(once); | ||
| 216 | @import_class=@MY::ExtUtils_MM_Import_Order; | ||
| 217 | } | ||
| 218 | @import_class=(ref($self)) unless @import_class; | ||
| 219 | my %import_class=map {$_=>1} @import_class; | ||
| 220 | my @perl_mod_no_import=grep {!$import_class{$_}} @{$perl_mod_ar}; | ||
| 221 | my %seen; | ||
| 222 | my @perl_mod=grep {!$seen{$_}++} ( | ||
| 223 | @perl_mod_no_import, | ||
| 224 | @import_class | ||
| 225 | ); | ||
| 226 | |||
| 227 | |||
| 228 | # Now construct final PERLRUN string | ||
| 229 | # | ||
| 230 | my $perlrun; | ||
| 231 | my $quote_cr=$mm_or && $mm_or->can('quote_literal'); | ||
| 232 | my $perlrun_inc=join(' ', map { | ||
| 233 | my $arg="-I$_"; | ||
| 234 | $quote_cr ? $quote_cr->($mm_or, $arg) : $arg | ||
| 235 | } @{$perl_inc_ar}); | ||
| 236 | my $perlrun_mod=join(' ', map { | ||
| 237 | my $class=$_; | ||
| 238 | no warnings qw(once); | ||
| 239 | my $import_tag_ar=$MY::ExtUtils_MM_Import_Tag{$class}; | ||
| 240 | $import_tag_ar && @{$import_tag_ar} ? | ||
| 241 | sprintf('-M%s=%s', $class, join(',', @{$import_tag_ar})) : | ||
| 242 | "-M$class" | ||
| 243 | } @perl_mod); | ||
| 244 | $perlrun="\$(PERL) $perlrun_inc $perlrun_mod"; | ||
| 245 | |||
| 246 | |||
| 247 | # And return | ||
| 248 | # | ||
| 249 | return $perlrun | ||
| 250 | |||
| 251 | } | ||
| 252 | |||
| 253 | |||
| 254 | sub perl_mod { | ||
| 255 | |||
| 256 | my %seen=( | ||
| 257 | __PACKAGE__ => 1 | ||
| 258 | ); | ||
| 259 | my @m=sort | ||
| 260 | grep { !$seen{$_}++ } | ||
| 261 | map {(my $m = $_) =~ s{\.pm$}{}; $m =~ s{/}{::}g; $m;} | ||
| 262 | grep { m{^ExtUtils/} } | ||
| 263 | keys %INC; | ||
| 264 | return \@m; | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | sub perl_inc { | ||
| 269 | |||
| 270 | # Return array ref of any additional libraries specified via command line (-I) | ||
| 271 | # | ||
| 272 | my %default_inc=map { $_ => 1 } @{ perl_inc_default() || [] }; | ||
| 273 | my %seen; | ||
| 274 | my @lib=grep { | ||
| 275 | !$default_inc{$_} && !$seen{$_}++ | ||
| 276 | } map { | ||
| 277 | File::Spec->rel2abs($_) | ||
| 278 | } grep { | ||
| 279 | defined($_) && !ref($_) && length($_) && -d $_ | ||
| 280 | } @INC; | ||
| 281 | |||
| 282 | return \@lib; | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | sub perl_inc_default { | ||
| 287 | |||
| 288 | my @default_inc; | ||
| 289 | if (open(my $perl_fh, '-|', $^X, '-e', 'print join qq(\0), grep { defined && !ref && length && -d } @INC')) { | ||
| 290 | local $/; | ||
| 291 | my $inc=<$perl_fh>; | ||
| 292 | close($perl_fh); | ||
| 293 | @default_inc=map { | ||
| 294 | File::Spec->rel2abs($_) | ||
| 295 | } split(/\0/, ($inc || '')); | ||
| 296 | } | ||
| 297 | return \@default_inc; | ||
| 298 | } | ||
| 299 | |||
| 300 | |||
| 301 | sub arg { | ||
| 302 | |||
| 303 | # Convert MakeMaker target args to a named parameter hash. | ||
| 304 | # | ||
| 305 | my (%param, @argv); | ||
| 306 | (@param{qw( | ||
| 307 | NAME | ||
| 308 | NAME_SYM | ||
| 309 | DISTNAME | ||
| 310 | DISTVNAME | ||
| 311 | VERSION | ||
| 312 | VERSION_SYM | ||
| 313 | VERSION_FROM | ||
| 314 | LICENSE AUTHOR | ||
| 315 | TO_INST_PM | ||
| 316 | EXE_FILES | ||
| 317 | DIST_DEFAULT_TARGET | ||
| 318 | SUFFIX | ||
| 319 | ABSTRACT_FROM | ||
| 320 | )}, @argv)=@_; | ||
| 321 | $param{'TO_INST_PM_AR'}=[split /\s+/, $param{'TO_INST_PM'}]; | ||
| 322 | $param{'EXE_FILES_AR'}=[split /\s+/, $param{'EXE_FILES'}]; | ||
| 323 | $param{'ARGV_AR'}=\@argv; | ||
| 324 | return \%param | ||
| 325 | |||
| 326 | } | ||
| 327 | |||
| 328 | __END__ |