]> git.vpit.fr Git - perl/modules/Scalar-Vec-Util.git/blob - Makefile.PL
Allow forcing the unit by passing UNIT=type to Makefile.PL
[perl/modules/Scalar-Vec-Util.git] / Makefile.PL
1 use 5.006;
2
3 use strict;
4 use warnings;
5 use ExtUtils::MakeMaker;
6
7 use Config;
8 use File::Spec;
9
10 sub validate_exe {
11  my ($name, $preferred_path) = @_;
12
13  my (@candidates, @args);
14  if (File::Spec->file_name_is_absolute($name)) {
15   # No need to look for args if the name is absolute.
16   @candidates = $name;
17  } else {
18   my @path;
19   if (defined $preferred_path) {
20    @path = ($preferred_path, grep { $_ ne $preferred_path } File::Spec->path);
21   } else {
22    @path = File::Spec->path;
23   }
24
25   (my $base, @args) = split ' ', $name;
26   for my $path_entry (@path) {
27    my ($vol, $dir, $file) = File::Spec->splitpath($path_entry, 1);
28    next if defined $file and length $file;
29    push @candidates, File::Spec->catpath($vol, $dir, $base);
30   }
31  }
32
33  for my $path (@candidates) {
34   my $command = MM->maybe_command($path);
35   if (defined $command) {
36    $command .= " @args" if @args;
37    return $command;
38   }
39  }
40
41  return;
42 }
43
44 sub check_exe {
45  my ($desc, $arg_var, $config_var, $env_var, $preferred_path) = @_;
46
47  my $exe;
48
49  for (@ARGV) {
50   if (/^\Q$arg_var\E=(.*)/) {
51    $exe = validate_exe($1, $preferred_path);
52    last if defined $exe;
53   }
54  }
55
56  if (defined $exe) {
57   print "Forcing the use of $exe as the $desc.\n";
58  } else {
59   print "Checking for a valid $desc in the PATH... ";
60   $exe = validate_exe($Config{$config_var}, $preferred_path);
61   # Only fall back to env if we cannot find the one used to build perl.
62   if (not defined $exe and defined $ENV{$env_var}) {
63    $exe = validate_exe($ENV{$env_var}, $preferred_path);
64   }
65   if (defined $exe) {
66    print "$exe\n";
67   } else {
68    print "none\n";
69   }
70  }
71
72  return $exe;
73 }
74
75 sub filter_argv {
76  my ($var, $code) = @_;
77
78  my $ret;
79
80  for my $i (0 .. $#ARGV) {
81   my $arg = $ARGV[$i];
82   if ($arg =~ /^\Q$var\E=(.*)/) {
83    my $val = $1;
84    $ret = $code->($val);
85    $ARGV[$i] = undef;
86    last if $ret;
87   }
88  }
89
90  @ARGV = grep defined, @ARGV;
91
92  return $ret;
93 }
94
95 my ($cc, $ld);
96 my $pp = filter_argv PP => sub {
97  my ($val) = @_;
98  return (do { no warnings 'numeric'; int $val } or $val =~ /^(?:y|yes)$/i)
99         ? 1 : 0;
100 };
101 if ($pp) {
102  print "Forcing the pure-Perl implementation from the arguments passed to Makefile.PL.\n";
103 } else {
104  $cc = check_exe('C compiler', 'CC', 'cc', 'CC');
105  if (defined $cc) {
106   my ($vol, $dir, $file) = File::Spec->splitpath($cc);
107   my $preferred_path = File::Spec->catpath($vol, $dir, '');
108   $ld = check_exe('linker', 'LD', 'ld', 'LD', $preferred_path);
109  }
110 }
111
112 sub is_little_endian {
113  my $order = $Config{byteorder};
114  return 0 unless $order;
115  my $len = length $order;
116  if ($len > 8) {
117   $order = substr $order, 0, 8;
118   $len   = 8;
119  }
120  return $order eq (join '', 1 .. $len);
121 }
122
123 my %PARAMS;
124 my @DEFINES;
125
126 if (defined $cc and defined $ld) {
127  $PARAMS{C}  = [ 'Util.c' ];
128  $PARAMS{XS} = { 'Util.xs' => 'Util.c' };
129  $PARAMS{CC} = $cc;
130  $PARAMS{LD} = $ld;
131
132  my $type = filter_argv UNIT => sub { return $_[0] };
133  if (defined $type) {
134   print "Forcing '$type' as the unit.\n";
135   push @DEFINES, '-DBV_UNIT="' . $type . '"';
136  } else {
137   my $bits = 8;
138   if (not is_little_endian()) {
139    print "Forcing unit size of 8 on non-little-endian systems.\n";
140   } else {
141    print "Checking unit size in bits... ";
142    my $size  = 1;
143    my $align = $Config{alignbytes} || 1;
144    my @units = (8, 16, 32, 64);
145    for my $unit (@units) {
146     my $unit_size = $Config{"u${unit}size"};
147     if ($unit_size and $unit_size <= $align) {
148      $bits = $unit;
149      $size = $unit_size;
150     }
151    }
152    print "$bits (actually $size bytes for $align bytes alignment).\n";
153   }
154   push @DEFINES, '-DBV_UNIT="' . ($Config{"u${bits}type"} || "U$bits") . '"';
155   push @DEFINES, "-DSVU_SIZE=$bits";
156  }
157 } else {
158  $PARAMS{C}      = [ ];
159  $PARAMS{XS}     = { };
160  $PARAMS{OBJECT} = '';
161  print "Falling back to the pure-Perl implementation.\n";
162 }
163
164 $PARAMS{DEFINE} = join ' ', @DEFINES if @DEFINES;
165
166 my $dist = 'Scalar-Vec-Util';
167
168 (my $name = $dist) =~ s{-}{::}g;
169
170 (my $file = $dist) =~ s{-}{/}g;
171 $file = "lib/$file.pm";
172
173 my %PREREQ_PM = (
174  'Exporter' => 0,
175  'Carp'     => 0,
176  'XSLoader' => 0,
177  'base'     => 0,
178 );
179
180 my %BUILD_REQUIRES = (
181  'Config'              => 0,
182  'ExtUtils::MakeMaker' => 0,
183  'File::Spec'          => 0,
184  'Test::More'          => 0,
185  %PREREQ_PM,
186 );
187
188 my %META = (
189  configure_requires => {
190   'Config'              => 0,
191   'ExtUtils::MakeMaker' => 0,
192   'File::Spec'          => 0,
193  },
194  build_requires => {
195   %BUILD_REQUIRES,
196  },
197  dynamic_config => 1,
198  resources => {
199   bugtracker => "http://rt.cpan.org/Dist/Display.html?Name=$dist",
200   homepage   => "http://search.cpan.org/dist/$dist/",
201   license    => 'http://dev.perl.org/licenses/',
202   repository => "http://git.profvince.com/?p=perl%2Fmodules%2F$dist.git",
203  },
204 );
205
206 WriteMakefile(
207  NAME             => $name,
208  AUTHOR           => 'Vincent Pit <perl@profvince.com>',
209  LICENSE          => 'perl',
210  VERSION_FROM     => $file,
211  ABSTRACT_FROM    => $file,
212  PL_FILES         => {},
213  BUILD_REQUIRES   => \%BUILD_REQUIRES,
214  PREREQ_PM        => \%PREREQ_PM,
215  MIN_PERL_VERSION => '5.006',
216  META_MERGE       => \%META,
217  dist             => {
218   PREOP    => "pod2text -u $file > \$(DISTVNAME)/README",
219   COMPRESS => 'gzip -9f', SUFFIX => 'gz'
220  },
221  clean            => {
222   FILES => "$dist-* *.gcov *.gcda *.gcno cover_db Debian_CPANTS.txt*"
223  },
224  %PARAMS,
225 );
226
227 1;
228
229 package MY;
230
231 sub postamble {
232  my $cv = join ' -coverage ', 'cover',
233                             qw<statement branch condition path subroutine time>;
234  <<POSTAMBLE;
235 cover test_cover:
236         $cv -test
237 POSTAMBLE
238 }