Newer
Older
NetAddr-IP / Lite / Util / Makefile.PL
@Michael Robinton Michael Robinton on 21 Oct 2014 4 KB Import of MIKER/NetAddr-IP-4.027 from CPAN.
  1. use ExtUtils::MakeMaker qw(
  2. WriteMakefile
  3. prompt
  4. );
  5. use Config;
  6. use Cwd;
  7. use Getopt::Long qw(
  8. GetOptions
  9. );
  10.  
  11. my $pwd = getcwd();
  12.  
  13. unlink 'Makefile'; # remove Makefile to stabilize CC test
  14.  
  15. #
  16. # get any command line arguments
  17. #
  18. my($useXS);
  19. GetOptions(
  20. 'xs!' => \$useXS,
  21. 'pm' => sub {
  22. warn "\n\t".'WARNING: Use of "--pm" is deprecated, use "-noxs" instead'."\n\n";
  23. $useXS = 0;
  24. },
  25. );
  26.  
  27. if ($^O eq 'MSWin32') {
  28. $useXS = 0; # force NOXS mode for windows
  29. }
  30.  
  31. my $pkg = 'NetAddr::IP::Util';
  32. $pkg =~ /[^:]+$/;
  33. my $module = $& .'.pm';
  34. my $cfile = $& .'.c';
  35.  
  36. my %makeparms = (
  37. NAME => $pkg,
  38. VERSION_FROM => $module, # finds $VERSION
  39. depend => {$cfile => q[xs_include/miniSocket.inc localconf.h config.h localperl.h],
  40. },
  41. # PREREQ_PM => {Test::More => 0,
  42. # },
  43. LIBS => [],
  44. XS => {},
  45. C => [],
  46. clean => { FILES => "*.bs *.o *.c *~ tmp* Util_IS.pm localperl.h auto*"},
  47. realclean => { FILES => "config.h config.log config.status"},
  48. dist => {COMPRESS=>'gzip', SUFFIX=>'gz'}
  49.  
  50. );
  51.  
  52. #
  53. # Check if we have a C compiler
  54.  
  55. unless (defined $useXS) {
  56. if (test_cc()) {
  57. print "You have a working compiler.\n";
  58. $useXS = 1;
  59. # $makeparms{'MYEXTLIB'} = 'netdns$(LIB_EXT)',
  60.  
  61. } else {
  62. $useXS = 0;
  63. print <<END;
  64.  
  65. I cannot determine if you have a C compiler. I will install the
  66. perl-only implementation.
  67.  
  68. You can force installation of the XS version with:
  69.  
  70. perl Makefile.PL --xs
  71. END
  72.  
  73. # $makeparms{'MYEXTLIB'} = '',
  74. }
  75. }
  76.  
  77. my $begin = '';
  78.  
  79. if ($useXS) {
  80. # turn the XS bits on.
  81. delete $makeparms{'XS'};
  82. delete $makeparms{'C'};
  83.  
  84. unless (-e './config.h') {
  85. system $Config{sh}, 'configure';
  86. }
  87. my @LIBS;
  88. open(F,'config.h') or die "could not open config.h\n";
  89. foreach(<F>) {
  90. if ($_ =~ /^#define LIBS([ a-zA-Z-]+)/) {
  91. @LIBS = ($1 =~ /[a-zA-Z0-9-]+/g);
  92.  
  93. $makeparms{LIBS} = [$1];
  94. last;
  95. }
  96. }
  97. close F;
  98. my $link = '';
  99. foreach(@libs) {
  100. if ($Config{libs} =~ /$_\b/) {
  101. $link .= $_ .' ';
  102. }
  103. }
  104. chop $link;
  105. $makeparms{LIBS} = [$link];
  106.  
  107. open(F,'>localperl.h') or die "could not open localperl.h for write\n";
  108. print F q|
  109. /* Written by Makefile.PL
  110. *
  111. * Do not modify this file, modify Makefile.PL instead
  112. *
  113. */
  114. |;
  115. close F;
  116.  
  117. $begin = q|
  118. config :: config.h
  119. @$(NOOP)
  120.  
  121. config.h :
  122. $(SHELL) configure
  123. |;
  124. }
  125.  
  126. open(F,'>Util_IS.pm');
  127. print F q|#!/usr/bin/perl
  128. #
  129. # DO NOT ALTER THIS FILE
  130. # IT IS WRITTEN BY Makefile.PL
  131. # EDIT THAT INSTEAD
  132. #
  133. package NetAddr::IP::Util_IS;
  134. use vars qw($VERSION);
  135. $VERSION = 1.00;
  136.  
  137.  
  138. sub pure {
  139. return |, (($useXS) ? 0 : 1), q|;
  140. }
  141. sub not_pure {
  142. return |, (($useXS) ? 1 : 0), q|;
  143. }
  144. 1;
  145. __END__
  146.  
  147. =head1 NAME
  148.  
  149. NetAddr::IP::Util_IS - Tell about Pure Perl
  150.  
  151. =head1 SYNOPSIS
  152.  
  153. use NetAddr::IP::Util_IS;
  154.  
  155. $rv = NetAddr::IP::Util_IS->pure;
  156. $rv = NetAddr::IP::Util_IS->not_pure;
  157.  
  158. =head1 DESCRIPTION
  159.  
  160. Util_IS indicates whether or not B<NetAddr::IP::Util> was compiled in Pure
  161. Perl mode.
  162.  
  163. =over 4
  164.  
  165. =item * $rv = NetAddr::IP::Util_IS->pure;
  166.  
  167. Returns true if PurePerl mode, else false.
  168.  
  169. =item * $rv = NetAddr::IP::Util_IS->not_pure;
  170.  
  171. Returns true if NOT PurePerl mode, else false
  172.  
  173. =back
  174.  
  175. =cut
  176.  
  177. 1;
  178. |;
  179.  
  180. sub test_cc {
  181. #
  182. # The perl/C check borrowed and modified from
  183. # Graham Barr's Scalar-List-Utils distribution.
  184. #
  185. print "Testing if you have a C compiler and the needed header files....\n";
  186.  
  187. unless (open(F, ">compile.c")) {
  188. warn "Cannot write compile.c, skipping test compilation and installing pure Perl version.\n";
  189. return;
  190. }
  191.  
  192. my $CC = $ENV{CC} || $Config{ccname};
  193. my $command = qq|$CC compile.c -o compile.output|;
  194.  
  195. print F <<'EOF';
  196. int main() { return 0; }
  197. EOF
  198.  
  199. close(F) or return;
  200.  
  201. print STDERR $command,"\n";
  202.  
  203. my $rv = system($command);
  204.  
  205. foreach my $file (glob('compile*')) {
  206. unlink($file) || warn "Could not delete $file: $!\n";
  207. }
  208.  
  209. return ($rv == 0);
  210. }
  211.  
  212. sub MY::top_targets {
  213. package MY;
  214. my $inherited = shift->SUPER::top_targets(@_);
  215. $inherited =~ s/(pure_all\s+::.+)/$1 README/;
  216. $begin . $inherited;
  217. }
  218.  
  219. sub MY::post_constants {
  220. my $post_constants = q|
  221. MY_POD2TEXT = |. $Config{scriptdirexp} .'/pod2text' .q|
  222. |;
  223. }
  224.  
  225. sub MY::postamble {
  226. package MY;
  227. my $postamble = q|
  228. README : |. $module .q|
  229. @$(MY_POD2TEXT) |. $module .q| > README
  230.  
  231. |;
  232. }
  233.  
  234. WriteMakefile(%makeparms);