]> git.vpit.fr Git - perl/modules/Scalar-Vec-Util.git/blob - Makefile.PL
Clean up unit discovery
[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 my ($pp, $skip_arg);
76 for my $i (0 .. $#ARGV) {
77  my $arg = $ARGV[$i];
78  if ($arg =~ /^PP=(.*)/) {
79   my $val = $1;
80   if (do { no warnings 'numeric'; int $val } or $val =~ /^(?:y|yes)$/i) {
81    print "Forcing the pure-Perl implementation from the arguments passed to Makefile.PL.\n";
82    $pp       = 1;
83    $skip_arg = $i;
84    last;
85   }
86  }
87 }
88 if (defined $skip_arg) {
89  splice @ARGV, $skip_arg, 1;
90 }
91
92 my ($cc, $ld);
93 unless ($pp) {
94  $cc = check_exe('C compiler', 'CC', 'cc', 'CC');
95  if (defined $cc) {
96   my ($vol, $dir, $file) = File::Spec->splitpath($cc);
97   my $preferred_path = File::Spec->catpath($vol, $dir, '');
98   $ld = check_exe('linker', 'LD', 'ld', 'LD', $preferred_path);
99  }
100 }
101
102 sub is_little_endian {
103  my $order = $Config{byteorder};
104  return 0 unless $order;
105  my $len = length $order;
106  if ($len > 8) {
107   $order = substr $order, 0, 8;
108   $len   = 8;
109  }
110  return $order eq (join '', 1 .. $len);
111 }
112
113 my %PARAMS;
114 my @DEFINES;
115
116 if (defined $cc and defined $ld) {
117  $PARAMS{C}  = [ 'Util.c' ];
118  $PARAMS{XS} = { 'Util.xs' => 'Util.c' };
119  $PARAMS{CC} = $cc;
120  $PARAMS{LD} = $ld;
121
122  my $bits = 8;
123  if (not is_little_endian()) {
124   print "Forcing unit size of 8 on non-little-endian systems.\n";
125  } else {
126   print "Checking unit size in bits... ";
127   my $size  = 1;
128   my $align = $Config{alignbytes} || 1;
129   my @units = (8, 16, 32, 64);
130   for my $unit (@units) {
131    my $unit_size = $Config{"u${unit}size"};
132    if ($unit_size and $unit_size <= $align) {
133     $bits = $unit;
134     $size = $unit_size;
135    }
136   }
137   print "$bits (actually $size bytes for $align bytes alignment).\n";
138  }
139  push @DEFINES, '-DBV_UNIT="' . ($Config{"u${bits}type"} || "U$bits") . '"';
140  push @DEFINES, "-DSVU_SIZE=$bits";
141 } else {
142  $PARAMS{C}      = [ ];
143  $PARAMS{XS}     = { };
144  $PARAMS{OBJECT} = '';
145  print "Falling back to the pure-Perl implementation.\n";
146 }
147
148 $PARAMS{DEFINE} = join ' ', @DEFINES if @DEFINES;
149
150 my $dist = 'Scalar-Vec-Util';
151
152 (my $name = $dist) =~ s{-}{::}g;
153
154 (my $file = $dist) =~ s{-}{/}g;
155 $file = "lib/$file.pm";
156
157 my %PREREQ_PM = (
158  'Exporter' => 0,
159  'Carp'     => 0,
160  'XSLoader' => 0,
161  'base'     => 0,
162 );
163
164 my %BUILD_REQUIRES = (
165  'Config'              => 0,
166  'ExtUtils::MakeMaker' => 0,
167  'File::Spec'          => 0,
168  'Test::More'          => 0,
169  %PREREQ_PM,
170 );
171
172 my %META = (
173  configure_requires => {
174   'Config'              => 0,
175   'ExtUtils::MakeMaker' => 0,
176   'File::Spec'          => 0,
177  },
178  build_requires => {
179   %BUILD_REQUIRES,
180  },
181  dynamic_config => 1,
182  resources => {
183   bugtracker => "http://rt.cpan.org/Dist/Display.html?Name=$dist",
184   homepage   => "http://search.cpan.org/dist/$dist/",
185   license    => 'http://dev.perl.org/licenses/',
186   repository => "http://git.profvince.com/?p=perl%2Fmodules%2F$dist.git",
187  },
188 );
189
190 WriteMakefile(
191  NAME             => $name,
192  AUTHOR           => 'Vincent Pit <perl@profvince.com>',
193  LICENSE          => 'perl',
194  VERSION_FROM     => $file,
195  ABSTRACT_FROM    => $file,
196  PL_FILES         => {},
197  BUILD_REQUIRES   => \%BUILD_REQUIRES,
198  PREREQ_PM        => \%PREREQ_PM,
199  MIN_PERL_VERSION => '5.006',
200  META_MERGE       => \%META,
201  dist             => {
202   PREOP    => "pod2text -u $file > \$(DISTVNAME)/README",
203   COMPRESS => 'gzip -9f', SUFFIX => 'gz'
204  },
205  clean            => {
206   FILES => "$dist-* *.gcov *.gcda *.gcno cover_db Debian_CPANTS.txt*"
207  },
208  %PARAMS,
209 );
210
211 1;
212
213 package MY;
214
215 sub postamble {
216  my $cv = join ' -coverage ', 'cover',
217                             qw<statement branch condition path subroutine time>;
218  <<POSTAMBLE;
219 cover test_cover:
220         $cv -test
221 POSTAMBLE
222 }