2
0

fallback.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License 2.0 (the "License"). You may not use
  4. # this file except in compliance with the License. You can obtain a copy
  5. # in the file LICENSE in the source distribution or at
  6. # https://www.openssl.org/source/license.html
  7. =head1 NAME
  8. OpenSSL::fallback - push directories to the end of @INC at compile time
  9. =cut
  10. package OpenSSL::fallback;
  11. use strict;
  12. use warnings;
  13. use Carp;
  14. our $VERSION = '0.01';
  15. =head1 SYNOPSIS
  16. use OpenSSL::fallback LIST;
  17. =head1 DESCRIPTION
  18. This small simple module simplifies the addition of fallback directories
  19. in @INC at compile time.
  20. It is used to add extra directories at the end of perl's search path so
  21. that later "use" or "require" statements will find modules which are not
  22. located on perl's default search path.
  23. This is similar to L<lib>, except the paths are I<appended> to @INC rather
  24. than prepended, thus allowing the use of a newer module on perl's default
  25. search path if there is one.
  26. =head1 CAVEAT
  27. Just like with B<lib>, this only works with Unix filepaths.
  28. Just like with L<lib>, this doesn't mean that it only works on Unix, but that
  29. non-Unix users must first translate their file paths to Unix conventions.
  30. # VMS users wanting to put [.my.stuff] into their @INC should write:
  31. use fallback 'my/stuff';
  32. =head1 NOTES
  33. If you try to add a file to @INC as follows, you will be warned, and the file
  34. will be ignored:
  35. use fallback 'file.txt';
  36. The sole exception is the file F<MODULES.txt>, which must contain a list of
  37. sub-directories relative to the location of that F<MODULES.txt> file.
  38. All these sub-directories will be appended to @INC.
  39. =cut
  40. # Forward declare
  41. sub glob;
  42. use constant DEBUG => 0;
  43. sub import {
  44. shift; # Skip module name
  45. foreach (@_) {
  46. my $path = $_;
  47. if ($path eq '') {
  48. carp "Empty compile time value given to use fallback";
  49. next;
  50. }
  51. print STDERR "DEBUG: $path\n" if DEBUG;
  52. unless (-e $path
  53. && ($path =~ m/(?:^|\/)MODULES.txt/ || -d $path)) {
  54. croak "Parameter to use fallback must be a directory, not a file";
  55. next;
  56. }
  57. my @dirs = ();
  58. if (-f $path) { # It's a MODULES.txt file
  59. (my $dir = $path) =~ s|/[^/]*$||; # quick dirname
  60. open my $fh, $path or die "Could not open $path: $!\n";
  61. while (my $l = <$fh>) {
  62. $l =~ s|\R$||; # Better chomp
  63. my $d = "$dir/$l";
  64. my $checked = $d;
  65. if ($^O eq 'VMS') {
  66. # Some VMS unpackers replace periods with underscores
  67. # We must be real careful not to convert the directories
  68. # '.' and '..', though.
  69. $checked =
  70. join('/',
  71. map { my $x = $_;
  72. $x =~ s|\.|_|g
  73. if ($x ne '..' && $x ne '.');
  74. $x }
  75. split(m|/|, $checked))
  76. unless -e $checked && -d $checked;
  77. }
  78. croak "All lines in $path must be a directory, not a file: $l"
  79. unless -e $checked && -d $checked;
  80. push @INC, $checked;
  81. }
  82. } else { # It's a directory
  83. push @INC, $path;
  84. }
  85. }
  86. }
  87. =head1 SEE ALSO
  88. L<FindBin> - optional module which deals with paths relative to the source
  89. file.
  90. =head1 AUTHOR
  91. Richard Levitte, 2019
  92. =cut