| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | |||
| 5 | package FindBin::libs v5.0.3; | ||
| 6 | use v5.40; | ||
| 7 | |||
| 8 | use File::Basename; | ||
| 9 | |||
| 10 | use Carp qw( croak ); | ||
| 11 | use List::Util qw( uniq first ); | ||
| 12 | use Symbol qw( qualify qualify_to_ref ); | ||
| 13 | |||
| 14 | use File::Spec::Functions | ||
| 15 | qw | ||
| 16 | ( | ||
| 17 | splitpath | ||
| 18 | splitdir | ||
| 19 | catpath | ||
| 20 | catdir | ||
| 21 | rel2abs | ||
| 22 | ); | ||
| 23 | |||
| 24 | use FindBin::Bin qw( $Bin_l $Bin_p ); | ||
| 25 | use FindBin::Parents qw( dir_paths ); | ||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | # package variables | ||
| 29 | ######################################################################## | ||
| 30 | |||
| 31 | my %defaultz = | ||
| 32 | ( | ||
| 33 | # note the lack of 'exports' due to post-assignment | ||
| 34 | # via base after defaults are assigned. | ||
| 35 | |||
| 36 | base => 'lib' | ||
| 37 | , use => 1 | ||
| 38 | , subdir => '' # add this subdir also if found. | ||
| 39 | , abspath => 1 # default to old behavior | ||
| 40 | |||
| 41 | , ignore => q{,usr} # dir's to skip looking for ./lib | ||
| 42 | ); | ||
| 43 | |||
| 44 | my %allow_undef = map { $_ => 1 } | ||
| 45 | qw | ||
| 46 | ( | ||
| 47 | ignore | ||
| 48 | export | ||
| 49 | ); | ||
| 50 | |||
| 51 | ######################################################################## | ||
| 52 | # utility subs | ||
| 53 | ######################################################################## | ||
| 54 | |||
| 55 | # HAK ALERT: $Bin is an absolute path, there are cases | ||
| 56 | # where splitdir does not add the leading '' onto the | ||
| 57 | # directory path for it on VMS. Fix is to unshift a leading | ||
| 58 | # '' into @dirpath where the leading entry is true. | ||
| 59 | |||
| 60 | my $append_dir | ||
| 61 | = sub | ||
| 62 | { | ||
| 63 | my $dir = shift; | ||
| 64 | my ( $vol, $dirs ) = splitpath $dir, 1; | ||
| 65 | my $path = catpath $vol, catdir $dirs, @_; | ||
| 66 | |||
| 67 | $path | ||
| 68 | }; | ||
| 69 | |||
| 70 | my $find_libs | ||
| 71 | = sub | ||
| 72 | { | ||
| 73 | my $argz = shift; | ||
| 74 | my $base = basename $argz->{ base }; | ||
| 75 | my $subdir = $argz->{ subdir } // ''; | ||
| 76 | my $subonly = defined $argz->{ subonly } || defined $argz->{ scalar }; | ||
| 77 | my @found = (); | ||
| 78 | my $ignorz = $argz->{ ignore }; | ||
| 79 | |||
| 80 | my $bin | ||
| 81 | = $argz->{ abspath } | ||
| 82 | ? $Bin_p | ||
| 83 | : $Bin_l | ||
| 84 | ; | ||
| 85 | |||
| 86 | say STDERR "\nSearching $bin for '$base' ($subdir)..." | ||
| 87 | if $argz->{ verbose }; | ||
| 88 | |||
| 89 | my @search | ||
| 90 | = do | ||
| 91 | { | ||
| 92 | my @dirz = dir_paths $bin, 1; | ||
| 93 | |||
| 94 | say STDERR join "\n\t" => 'Parent Dirs:', @dirz | ||
| 95 | if $argz->{ verbose }; | ||
| 96 | |||
| 97 | my ( $vol ) = splitpath $dirz[0]; | ||
| 98 | my %skipz | ||
| 99 | = map | ||
| 100 | { | ||
| 101 | ( | ||
| 102 | catpath | ||
| 103 | ( | ||
| 104 | $vol => catdir $_, $base | ||
| 105 | ) => 1 | ||
| 106 | ) | ||
| 107 | } | ||
| 108 | $ignorz->@*; | ||
| 109 | |||
| 110 | my @subz | ||
| 111 | = grep | ||
| 112 | { | ||
| 113 | ! exists $skipz{ $_ } | ||
| 114 | } | ||
| 115 | map | ||
| 116 | { | ||
| 117 | $_->$append_dir( $base ) | ||
| 118 | } | ||
| 119 | @dirz; | ||
| 120 | |||
| 121 | say STDERR join "\n\t" => 'Search Subs:', @subz | ||
| 122 | if $argz->{ verbose }; | ||
| 123 | |||
| 124 | if( $subdir ) | ||
| 125 | { | ||
| 126 | map | ||
| 127 | { | ||
| 128 | my $sub = $_->$append_dir( $subdir ); | ||
| 129 | |||
| 130 | $subonly | ||
| 131 | ? $sub | ||
| 132 | : [ $sub => $_ ] | ||
| 133 | } | ||
| 134 | @subz | ||
| 135 | } | ||
| 136 | else | ||
| 137 | { | ||
| 138 | $subonly = 1; | ||
| 139 | @subz | ||
| 140 | } | ||
| 141 | }; | ||
| 142 | |||
| 143 | if( $argz->{ scalar } ) | ||
| 144 | { | ||
| 145 | # return at most one item. | ||
| 146 | |||
| 147 | first | ||
| 148 | { | ||
| 149 | -e | ||
| 150 | } | ||
| 151 | @search | ||
| 152 | } | ||
| 153 | elsif( $subonly ) | ||
| 154 | { | ||
| 155 | # grab all the extant items on the list. | ||
| 156 | |||
| 157 | grep | ||
| 158 | { | ||
| 159 | -e | ||
| 160 | } | ||
| 161 | @search | ||
| 162 | } | ||
| 163 | else | ||
| 164 | { | ||
| 165 | # if the subdir exists then return it and the subdir. | ||
| 166 | |||
| 167 | map | ||
| 168 | { | ||
| 169 | -e $_->[0] | ||
| 170 | ? $_->@* | ||
| 171 | : () | ||
| 172 | } | ||
| 173 | @search | ||
| 174 | } | ||
| 175 | }; | ||
| 176 | |||
| 177 | my $handle_args | ||
| 178 | = sub | ||
| 179 | { | ||
| 180 | # discard the module, rest are arguments. | ||
| 181 | |||
| 182 | shift; | ||
| 183 | |||
| 184 | # default from the environment. for now assume that | ||
| 185 | # multiple modules whon't hack the environment during | ||
| 186 | # compilation to change this after we first see it. | ||
| 187 | |||
| 188 | state $verbose = $ENV{ VERBOSE_FINDBIN_LIBS } // $ENV{ VERBOSE } // ''; | ||
| 189 | |||
| 190 | my %argz = | ||
| 191 | ( | ||
| 192 | # default verbosity from the environment. | ||
| 193 | |||
| 194 | verbose => $verbose | ||
| 195 | , print => $verbose | ||
| 196 | , map | ||
| 197 | { | ||
| 198 | my ( $k, $v ) = split '=', $_, 2; | ||
| 199 | |||
| 200 | $k =~ s{^no}{} | ||
| 201 | and $v = ''; | ||
| 202 | |||
| 203 | $allow_undef{ $k } | ||
| 204 | or $v //= 1; | ||
| 205 | |||
| 206 | ( $k => $v ) | ||
| 207 | } | ||
| 208 | @_ | ||
| 209 | ); | ||
| 210 | |||
| 211 | # exporting a subdir as a scalar requires stopping at | ||
| 212 | # the subdir, ignoring its parent. | ||
| 213 | |||
| 214 | $argz{ subonly } | ||
| 215 | ||= $argz{ export } && $argz{ subdir } && $argz{ scalar }; | ||
| 216 | |||
| 217 | # stuff "debug=1" into your arguments and perl -d stops here. | ||
| 218 | |||
| 219 | $DB::single = 1 if defined $argz{ debug }; | ||
| 220 | |||
| 221 | |||
| 222 | if( exists $argz{ use } ) | ||
| 223 | { | ||
| 224 | # nothing further to do | ||
| 225 | } | ||
| 226 | elsif | ||
| 227 | ( | ||
| 228 | # default if nothing is supplied is use; | ||
| 229 | # without an explicit export option default | ||
| 230 | # it to off with an undef, otherwise it's true. | ||
| 231 | |||
| 232 | defined $argz{ export } | ||
| 233 | or | ||
| 234 | defined $argz{ p5lib } | ||
| 235 | or | ||
| 236 | defined $argz{ ld_lib } | ||
| 237 | ) | ||
| 238 | { | ||
| 239 | $argz{ use } = ''; | ||
| 240 | } | ||
| 241 | else | ||
| 242 | { | ||
| 243 | $argz{ use } = 1; | ||
| 244 | } | ||
| 245 | |||
| 246 | # now apply the defaults, then sanity check the result. | ||
| 247 | # base is a special case since it always has to exist. | ||
| 248 | # | ||
| 249 | # if $argz{ export } exists but is false then it takes | ||
| 250 | # its default from $argz{ base }. | ||
| 251 | |||
| 252 | while( my($k,$v) = each %defaultz ) | ||
| 253 | { | ||
| 254 | $argz{ $k } //= $v; | ||
| 255 | } | ||
| 256 | |||
| 257 | exists $argz{ base } && $argz{ base } | ||
| 258 | or croak "Bogus FindBin::libs: missing/false base argument, should be 'base=NAME'"; | ||
| 259 | |||
| 260 | # at this point the default for export can | ||
| 261 | # be assigned IFF it exists at all; otherwise | ||
| 262 | # the '' avoids issues with undef warnings. | ||
| 263 | |||
| 264 | if( exists $argz{ export } ) | ||
| 265 | { | ||
| 266 | $argz{ export } //= $argz{ base }; | ||
| 267 | } | ||
| 268 | else | ||
| 269 | { | ||
| 270 | $argz{ export } = ''; | ||
| 271 | } | ||
| 272 | |||
| 273 | $argz{ ignore } = | ||
| 274 | [ | ||
| 275 | uniq split /\s*,\s*/, ( $argz{ ignore } // '' ) | ||
| 276 | ]; | ||
| 277 | |||
| 278 | \%argz | ||
| 279 | }; | ||
| 280 | |||
| 281 | ######################################################################## | ||
| 282 | # interface | ||
| 283 | ######################################################################## | ||
| 284 | |||
| 285 | sub import | ||
| 286 | { | ||
| 287 | my $argz = &$handle_args; | ||
| 288 | |||
| 289 | # HAK ALERT: the regex does nothing for security, | ||
| 290 | # just dodges -T. putting this down here instead | ||
| 291 | # of inside find_libs allows people to use saner | ||
| 292 | # untainting plans via find_libs. | ||
| 293 | |||
| 294 | my @libz | ||
| 295 | = map | ||
| 296 | { | ||
| 297 | m{ (.+) }xs | ||
| 298 | } | ||
| 299 | $argz->$find_libs; | ||
| 300 | |||
| 301 | if( $argz->{ print } ) | ||
| 302 | { | ||
| 303 | local $\ = "\n"; | ||
| 304 | local $, = "\n\t"; | ||
| 305 | |||
| 306 | say STDERR "Found */$argz->{ base }/$argz->{ subdir }:", @libz; | ||
| 307 | } | ||
| 308 | |||
| 309 | if( $argz->{ export } ) | ||
| 310 | { | ||
| 311 | my $caller = caller; | ||
| 312 | |||
| 313 | # this has to run in order to install variables that | ||
| 314 | # the caller is expecting to exist at runtime -- even | ||
| 315 | # if they are empty/undef at the end of it. | ||
| 316 | |||
| 317 | my $ref = qualify_to_ref $argz->{ export }, $caller; | ||
| 318 | |||
| 319 | if( $argz->{ verbose } ) | ||
| 320 | { | ||
| 321 | my $dest = qualify $argz->{ export }, $caller; | ||
| 322 | |||
| 323 | $argz->{ scalar } | ||
| 324 | ? say STDERR "\nExporting: \$$dest" | ||
| 325 | : say STDERR "\nExporting: \@$dest" | ||
| 326 | ; | ||
| 327 | } | ||
| 328 | |||
| 329 | if( $argz->{ scalar } ) | ||
| 330 | { | ||
| 331 | *$ref | ||
| 332 | = @libz | ||
| 333 | ? \$libz[0] | ||
| 334 | : \( my $a = '' ) | ||
| 335 | ; | ||
| 336 | } | ||
| 337 | elsif | ||
| 338 | ( | ||
| 339 | $argz->{ append } | ||
| 340 | and | ||
| 341 | my $ary = *{ $ref }{ ARRAY } | ||
| 342 | ) | ||
| 343 | { | ||
| 344 | push $ary->@*, @libz; | ||
| 345 | } | ||
| 346 | else | ||
| 347 | { | ||
| 348 | *$ref = \@libz | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | # no 'else', these are not exclusive! | ||
| 353 | |||
| 354 | if( @libz ) | ||
| 355 | { | ||
| 356 | my $lib_str = join ':', @libz, ''; | ||
| 357 | |||
| 358 | for | ||
| 359 | ( | ||
| 360 | [ qw( p5lib PERL5LIB ) ] | ||
| 361 | , [ qw( ld_lib LD_LIBRARY_PATH ) ] | ||
| 362 | ) | ||
| 363 | { | ||
| 364 | my ( $arg, $name ) = $_->@*; | ||
| 365 | |||
| 366 | defined $argz->{ $arg } | ||
| 367 | or next; | ||
| 368 | |||
| 369 | # stuff the lib's found at the front of $ENV{ PERL5LIB } | ||
| 370 | # yes, virginia, substr is an lvalue -- and saner than | ||
| 371 | # dealing with \Q and a regex on arbitrary paths. | ||
| 372 | |||
| 373 | $ENV{ $name } //= ''; | ||
| 374 | ( substr $ENV{ $name }, 0, 0 ) = $lib_str; | ||
| 375 | |||
| 376 | say STDERR "\nUpdated $name:\t$ENV{ $name }" | ||
| 377 | if $argz->{ verbose }; | ||
| 378 | } | ||
| 379 | |||
| 380 | if( $argz->{ use } ) | ||
| 381 | { | ||
| 382 | # this obviously won't work if lib ever depends | ||
| 383 | # on the caller's package. | ||
| 384 | # | ||
| 385 | # it does avoids issues with -T blowing up on the | ||
| 386 | # old eval technique. | ||
| 387 | |||
| 388 | require lib; | ||
| 389 | |||
| 390 | lib->import( @libz ); | ||
| 391 | } | ||
| 392 | } | ||
| 393 | else | ||
| 394 | { | ||
| 395 | warn "Empty lib directory list from '$0'"; | ||
| 396 | } | ||
| 397 | |||
| 398 | 0 | ||
| 399 | } | ||
| 400 | |||
| 401 | # keep require happy | ||
| 402 | |||
| 403 | 1 | ||
| 404 | |||
| 405 | __END__ |