Allow packages to be cleaned multiple times
[p5sagit/namespace-clean.git] / Makefile.PL
1 use strict;
2 use warnings;
3 use 5.008001;
4
5 my %META = (
6   name => 'namespace-clean',
7   license => 'perl_5',
8
9   abstract => 'Keep imports and functions out of your namespace',
10   author => [
11     'Robert \'phaylon\' Sedlacek <rs@474.at>',
12     'Florian Ragwitz <rafl@debian.org>',
13     'Jesse Luehrs <doy@tozt.net>',
14     'Peter Rabbitson <ribasushi@cpan.org>',
15     'Father Chrysostomos <sprout@cpan.org>',
16   ],
17
18   prereqs => {
19     configure => {
20       requires => {
21         'ExtUtils::MakeMaker'   => 0,
22         'ExtUtils::CBuilder'    => 0.27,
23       }
24     },
25     runtime => {
26       requires => {
27         'Package::Stash' => '0.23',
28         'B::Hooks::EndOfScope' => '0.12',
29         'perl' => '5.008001',
30       },
31     },
32     test => {
33       requires => {
34         'Test::More' => '0.47',
35       }
36     },
37   },
38
39   resources => {
40     x_IRC => 'irc://irc.perl.org/#toolchain',
41     homepage => 'http://search.cpan.org/dist/namespace-clean',
42     repository => {
43       type => 'git',
44       url => 'git://git.shadowcat.co.uk/p5sagit/namespace-clean.git',
45       web => 'https://github.com/p5sagit/namespace-clean',
46     },
47     bugtracker => {
48       mailto => 'bug-namespace-clean@rt.cpan.org',
49       web => 'http://rt.cpan.org/Public/Dist/Display.html?Name=namespace-clean',
50     },
51   },
52 );
53
54 my %MM_ARGS = ( (
55   # a sub-namer is needed if using the debugger on some perls
56   require 'lib/namespace/clean/_Util.pm'
57     and
58   namespace::clean::_Util::DEBUGGER_NEEDS_CV_RENAME()
59     and
60   namespace::clean::_Util::_namer_load_error()
61     and
62   can_xs()
63 )
64   # when changing version, also change $sn_ver in namespace/clean/_Util.pm
65   ? ( PREREQ_PM => { 'Sub::Name' => '0.04' } )
66   : ()
67 );
68
69
70 ## BOILERPLATE ###############################################################
71 require ExtUtils::MakeMaker;
72
73 # have to do this since old EUMM dev releases miss the eval $VERSION line
74 my $eumm_version  = eval $ExtUtils::MakeMaker::VERSION;
75 my $mymeta        = $eumm_version >= 6.57_02;
76 my $mymeta_broken = $mymeta && $eumm_version < 6.57_07;
77
78 ($MM_ARGS{NAME} = $META{name}) =~ s/-/::/g;
79 ($MM_ARGS{VERSION_FROM} = "lib/$MM_ARGS{NAME}.pm") =~ s{::}{/}g;
80 $META{license} = [ $META{license} ]
81   if $META{license} && !ref $META{license};
82 $MM_ARGS{LICENSE} = $META{license}[0]
83   if $META{license} && $eumm_version >= 6.30;
84 $MM_ARGS{NO_MYMETA} = 1
85   if $mymeta_broken;
86 $MM_ARGS{META_ADD} = { 'meta-spec' => { version => 2 }, %META }
87   unless -f 'META.yml';
88
89 for (qw(configure build test runtime)) {
90   my $key = $_ eq 'runtime' ? 'PREREQ_PM' : uc $_.'_REQUIRES';
91   my $r = $MM_ARGS{$key} = {
92     %{$META{prereqs}{$_}{requires} || {}},
93     %{delete $MM_ARGS{$key} || {}},
94   };
95   defined $r->{$_} or delete $r->{$_} for keys %$r;
96 }
97
98 $MM_ARGS{MIN_PERL_VERSION} = delete $MM_ARGS{PREREQ_PM}{perl} || 0;
99
100 delete $MM_ARGS{MIN_PERL_VERSION}
101   if $eumm_version < 6.47_01;
102 $MM_ARGS{BUILD_REQUIRES} = {%{$MM_ARGS{BUILD_REQUIRES}}, %{delete $MM_ARGS{TEST_REQUIRES}}}
103   if $eumm_version < 6.63_03;
104 $MM_ARGS{PREREQ_PM} = {%{$MM_ARGS{PREREQ_PM}}, %{delete $MM_ARGS{BUILD_REQUIRES}}}
105   if $eumm_version < 6.55_01;
106 delete $MM_ARGS{CONFIGURE_REQUIRES}
107   if $eumm_version < 6.51_03;
108
109 ExtUtils::MakeMaker::WriteMakefile(%MM_ARGS);
110 ## END BOILERPLATE ###########################################################
111
112
113 # FIXME
114 # Need to replace with EU::HC, but too many changes for this release already
115 ###########################################
116 # can we locate a (the) C compiler
117 sub can_cc {
118   my @chunks = split(/ /, $Config::Config{cc}) or return;
119
120   # $Config{cc} may contain args; try to find out the program part
121   while (@chunks) {
122     return can_run("@chunks") || (pop(@chunks), next);
123   }
124
125   return;
126 }
127
128 # check if we can run some command
129 sub can_run {
130   my ($cmd) = @_;
131
132   return $cmd if -x $cmd;
133   if (my $found_cmd = MM->maybe_command($cmd)) {
134     return $found_cmd;
135   }
136
137   require File::Spec;
138   for my $dir ((split /$Config::Config{path_sep}/, $ENV{PATH}), '.') {
139     next if $dir eq '';
140     my $abs = File::Spec->catfile($dir, $cmd);
141     return $abs if (-x $abs or $abs = MM->maybe_command($abs));
142   }
143
144   return;
145 }
146
147 # Can our C compiler environment build XS files
148 sub can_xs {
149   # Do we have the configure_requires checker?
150   local $@;
151   eval "require ExtUtils::CBuilder; ExtUtils::CBuilder->VERSION(0.27)";
152   if ( $@ ) {
153     # They don't obey configure_requires, so it is
154     # someone old and delicate. Try to avoid hurting
155     # them by falling back to an older simpler test.
156     return can_cc();
157   }
158
159   # Do we have a working C compiler
160   my $builder = ExtUtils::CBuilder->new(
161     quiet => 1,
162   );
163   unless ( $builder->have_compiler ) {
164     # No working C compiler
165     return 0;
166   }
167
168   # Write a C file representative of what XS becomes
169   require File::Temp;
170   my ( $FH, $tmpfile ) = File::Temp::tempfile(
171     "compilexs-XXXXX",
172     SUFFIX => '.c',
173   );
174   binmode $FH;
175   print $FH <<'END_C';
176 #include "EXTERN.h"
177 #include "perl.h"
178 #include "XSUB.h"
179
180 int main(int argc, char **argv) {
181     return 0;
182 }
183
184 int boot_sanexs() {
185     return 1;
186 }
187
188 END_C
189   close $FH;
190
191   # Can the C compiler access the same headers XS does
192   my @libs   = ();
193   my $object = undef;
194   eval {
195     local $^W = 0;
196     $object = $builder->compile(
197       source => $tmpfile,
198     );
199     @libs = $builder->link(
200       objects     => $object,
201       module_name => 'sanexs',
202     );
203   };
204   my $result = $@ ? 0 : 1;
205
206   # Clean up all the build files
207   foreach ( $tmpfile, $object, @libs ) {
208     next unless defined $_;
209     1 while unlink;
210   }
211
212   return $result;
213 }